// 摇晃骰子并展示投掷结果 voidchooseLetterRandomly(){ Vector<string> cube = cubeType; for (int i = 0; i < cube.size(); i++) { // 洗牌 int r = randomInteger(i, cube.size()-1); string temp = cube[i]; cube[i] = cube[r]; cube[r] = temp; }
int d = 4; // dimension if (cube.size() % 5 == 0){ d = 5; }
// 绘制 int count = 0; for (int i = 0; i < cube.size(); i++) { if (d == 4 && i % 4 == 0 && i != 0) { count = 0; } elseif (d == 5 && i % 5 == 0 && i != 0) { count = 0; } else { count++; } for (int j = 0; j < d; j++) { int rr = randomInteger(0, 5); string letter = cube[count][rr]; labelCube(count, j, letter);
}
} }
// 找单词并高亮 staticvoidplayerFindWord(Player current_player){ // do something no matter who is current player
if (current_player == HUMAN) {
} else { // computer } }
/** * Function: playBoggle * -------------------- * Manages all details needed for the user to play one * or more games of Boggle. */ staticvoidplayBoggle() { int dimension = getPreferredBoardSize(); drawBoard(dimension, dimension); cout << "This is where you'd play the game of Boggle" << endl;
/** * seem should start at here * ------------------------- * 画版 * 打乱所有的字符串骰子 Shuffle * 递归的找出所有单词containWord(优化大小写 * 人类回合:输入并计分,空输入表示结束,单词高亮处理 * 计算机回合:输出所有未被人类发现的单词 */ if (dimension == 4) { chooseLetterRandomly(kStandardCubes);// kStand全局变量不用传参 } else { chooseLetterRandomly(kBigBoggleCubes); }
voidfindWordsHelper(Set<string> &allWords, Grid<char> &board, string word, int i, int j, int dimension, int flag, Lexicon &DIC){
// base case: 单词正确/访问出界 if (i + 1 >= dimension || j + 1 >= dimension) { flag++; // 改变下轮递归方向 return;
} elseif (!DIC.containsPrefix(word)) { // 剪枝 Lexion containPrefix() return; } else { // rec case if (DIC.contains(word)) { allWords.add(word); }
// exploer // [i][j] visited NO NEED // board[i][j]
if (flag == 0) { // 向右侧递归 findWordsHelper(allWords, board, word + board[i][j + 1], i, j + 1, dimension, flag, DIC); // i, j 作为单词的下一字母索引同步增加 } elseif (flag == 1) { // 向下 findWordsHelper(allWords, board, word + board[i + 1][j], i + 1, j, dimension, flag, DIC); } else { // 斜向右下 findWordsHelper(allWords, board, word + board[i + 1][j + 1], i + 1, j + 1, dimension, flag, DIC); }
// unchoose NO NEED } }
voidfindWords(Set<string> &allWords, Grid<char> &board, string word, int dimension,Lexicon &DIC){ for (int i = 0; i < dimension; i++) { for (int j = 0; j < dimension; j++) { word = board[i][j]; int flag = 0; findWordsHelper(allWords, board, word, i, j, dimension, flag, DIC); } } }
// 递归列出单词 Set<string> getAllWords(Grid<char> &board, int dimension, Lexicon &DIC){ Set<string> allWords; string word = ""; findWords(allWords, board, word, dimension, DIC); return allWords; }
staticvoidinitWordsHelper(Lexicon &dic, Set<string> &words, Grid<char> &board, string formed, int i, int j, Grid<int> &visited){ for (int ni = -1; ni < 2; ni++) { for (int nj = -1; nj < 2; nj++) { int x = i + ni, y = j + nj; if (x < 0 || x >= board.size() || y < 0 || y >= board.size()) { continue; } if (dic.contains(formed)) { words.add(formed); } elseif (dic.containsPrefix(formed)) { continue; }
if (visited[x][y] == 1) { continue; } visited[x][y] = 1; initWordsHelper(dic, words, board, formed + board[x][y], i, j, visited); visited[x][y] = 0;