?? bag.java
字號:
import java.util.Random;
/**
* 口袋字母的簡單抽象
**/
class Bag {
private Random rand;
private int letter_counts[] = {
2, 9, 2, 2, 4, 12, 2, 3, 2, 9, 1, 1, 4, 2,
6, 8, 2, 1, 6, 4, 6, 4, 2, 2, 1, 2, 1
};
private int letter_points[] = {
0, 1, 3, 3, 2, 1, 4, 2, 4, 1, 8, 5, 1, 3,
1, 1, 3, 10, 1, 1, 1, 1, 4, 4, 8, 4, 10
};
private Letter letters[] = new Letter[100];
private int n = 0;
/** 構(gòu)造函數(shù)通過傳進(jìn)來的參數(shù)生成一個(gè)Random對象,然后遍歷
* 數(shù)組letter_counts,構(gòu)造合適數(shù)目的新的Letter對象 */
Bag(int seed) {
rand = new Random(seed);
for (int i = 0; i < letter_counts.length; i++) {
for (int j = 0; j < letter_counts[i]; j++) {
Letter l = new Letter(i == 0 ? '*' : (char)('A' + i - 1),
letter_points[i]);
putBack(l);
}
}
}
/** 從0 ~ n-1 之間挑選一個(gè)隨機(jī)數(shù),利用這個(gè)隨機(jī)數(shù)做偏移量從letters
* 數(shù)組中抽取字母
**/
synchronized Letter takeOut() {
if (n == 0)
return null;
int i = (int)(rand.nextDouble() * n);
Letter l = letters[i];
if (i != n - 1)
System.arraycopy(letters, i + 1, letters, i, n - i - 1);
n--;
return l;
}
/** 構(gòu)造函數(shù)調(diào)用該函數(shù)來講字母方塊放入原始口袋中 */
synchronized void putBack(Letter l) {
letters[n++] = l;
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -