首页 > 代码库 > boggle(未完)

boggle(未完)

/* * File: Boggle.cpp * ---------------- * Name: [TODO: enter name here] * Section: [TODO: enter section leader here] * This file is the main starter file for Assignment #4, Boggle. * [TODO: extend the documentation] */#include <iostream>#include "gboggle.h"#include "grid.h"#include "gwindow.h"#include "lexicon.h"#include "random.h"#include "simpio.h"#include "vector.h"#include "set.h"using namespace std;/* Constants */const int BOGGLE_WINDOW_WIDTH = 650;const int BOGGLE_WINDOW_HEIGHT = 350;const string STANDARD_CUBES[16]  = {    "AAEEGN", "ABBJOO", "ACHOPS", "AFFKPS",    "AOOTTW", "CIMOTU", "DEILRX", "DELRVY",    "DISTTY", "EEGHNW", "EEINSU", "EHRTVW",    "EIOSST", "ELRTTY", "HIMNQU", "HLNNRZ"}; const string BIG_BOGGLE_CUBES[25]  = {    "AAAFRS", "AAEEEE", "AAFIRS", "ADENNN", "AEEEEM",    "AEEGMU", "AEGMNN", "AFIRSY", "BJKQXZ", "CCNSTW",    "CEIILT", "CEILPT", "CEIPST", "DDLNOR", "DDHNOT",    "DHHLOR", "DHLNOR", "EIIITT", "EMOTTT", "ENSSSU",    "FIPRSY", "GORRVW", "HIPRRY", "NOOTUW", "OOOTTU"};const int MIN_WORD_LENGTH = 4;/* Function prototypes */void welcome();void giveInstructions();void initBoard();bool isOnTheBoard(string word);void playersTurn();/* Main program */int main() {    GWindow gw(BOGGLE_WINDOW_WIDTH, BOGGLE_WINDOW_HEIGHT);    initGBoggle(gw);    welcome();    giveInstructions();    initBoard();    playersTurn();    return 0;}/* * Function: welcome * Usage: welcome(); * ----------------- * Print out a cheery welcome message. */void welcome() {    cout << "Welcome!  You‘re about to play an intense game ";    cout << "of mind-numbing Boggle.  The good news is that ";    cout << "you might improve your vocabulary a bit.  The ";    cout << "bad news is that you‘re probably going to lose ";    cout << "miserably to this little dictionary-toting hunk ";    cout << "of silicon.  If only YOU had a gig of RAM..." << endl << endl;}/* * Function: giveInstructions * Usage: giveInstructions(); * -------------------------- * Print out the instructions for the user. */void giveInstructions() {    cout << endl;    cout << "The boggle board is a grid onto which I ";    cout << "I will randomly distribute cubes. These ";    cout << "6-sided cubes have letters rather than ";    cout << "numbers on the faces, creating a grid of ";    cout << "letters on which you try to form words. ";    cout << "You go first, entering all the words you can ";    cout << "find that are formed by tracing adjoining ";    cout << "letters. Two letters adjoin if they are next ";    cout << "to each other horizontally, vertically, or ";    cout << "diagonally. A letter can only be used once ";    cout << "in each word. Words must be at least four ";    cout << "letters long and can be counted only once. ";    cout << "You score points based on word length: a ";    cout << "4-letter word is worth 1 point, 5-letters ";    cout << "earn 2 points, and so on. After your puny ";    cout << "brain is exhausted, I, the supercomputer, ";    cout << "will find all the remaining words and double ";    cout << "or triple your paltry score." << endl << endl;    cout << "Hit return when you‘re ready...";    getLine();}void initBoard(){    Grid<char> board(4,4);    Vector<string> vec;    drawBoard(4,4);    while(true){        string userDecision = getLine("Do you want to use a custom board?");        if( userDecision == "YES" || userDecision == "yes"){            string customBoard = getLine("Enter the string to init the board.");                while(customBoard.size() < 16){                    customBoard += getLine("Not long enough.To generate the board,input more chars.");                }                cout << customBoard << endl;                for(int i = 0;i < 4;i++){                    for(int j = 0; j < 4;j++){                        board.set(i,j,customBoard[4*i+j]);                        labelCube(i,j,board.get(i,j));                    }                }                        break;        }else if(userDecision == "NO" || userDecision == "no"){                    for(int i = 0;i < 16;i ++){                        vec.add(STANDARD_CUBES[i]);                    }                    for(int i = 0; i < vec.size(); i++){                        int swapPosition = randomInteger(i,vec.size() - 1);                        string temp = vec[i];                        vec[i] = vec[swapPosition];                        vec[swapPosition] = temp;                    }                    for(int i = 0;i < 4;i++){                        for(int j = 0; j < 4;j ++){                            int r = randomInteger(0,5);                            board.set(i,j,vec[4*i+j][r]);                            labelCube(i,j,board.get(i,j));                        }                    }                    break;        }else{            cout << "Wrong input" << endl;        }    }                            }bool isOnTheBoard(string word){    return true;}void playersTurn(){    Set<string> usedWords;    Lexicon english("EnglishWords.dat");    while(true){        string word = getLine("enter a word.enter return when you are done.");        if(word == ""){            break;        }        cout << word << endl;        if(word.size() < MIN_WORD_LENGTH){            cout << "Too short." << endl;            continue;        }else if(!english.contains(word)){                cout << "It‘s not a word." << endl;                continue;        }else if(usedWords.contains(word)){                cout << "It‘s been used." << endl;                continue;        }else if(isOnTheBoard(word)){                recordWordForPlayer(word,HUMAN);                usedWords.add(word);        }            }}

 

boggle(未完)