int main(int argc, char* argv[]) { // Initialization GameBoard *board; // the state of the game board GameView *v; // a View for outputting the board via operator<< string userInput; // a string to hold the user's command choice string gameSelection; vector<GameMove *> possMoves; // a holder for possible moves bool validMove = false; bool gameSelected = false; int undoAmount = 0; board = new TicTacToeBoard(); v = new TicTacToeView(board); while (!gameSelected){ cout << "Would you like to play othello or tictactoe"<<endl; getline(cin, gameSelection); if (gameSelection == "othello"){ board = new OthelloBoard(); v = new OthelloView(board); gameSelected = true; } if (gameSelection == "tictactoe"){ board = new TicTacToeBoard(); v = new TicTacToeView(board); gameSelected = true; } } // Main loop do { // Print the game board using the OthelloView object // Print all possible moves // Ask to input a command cout << *v; board->GetPossibleMoves(&possMoves); for(int i=0;i<possMoves.size();i++){ cout<<(string)*possMoves.at(i)<<" "; if(i==15) cout<<endl; } cout << endl; cout << "Please enter a command " << endl << "1. move (r,c)" << endl << "2. undo n" << endl << "3. showValue" << endl << "4. showHistory" << endl << "5. quit" << endl; getline(cin, userInput); if (userInput== "quit") break; else if (userInput.substr(0, 5) == "undo "){ undoAmount=userInput.at(5)-'0'; if(undoAmount>board->GetMoveCount()) cout<<"Undo amount is larger than moves made"<<endl; while(undoAmount && undoAmount<=board->GetMoveCount() ){ undoAmount--; board->UndoLastMove(); } } else if (userInput == "showValue") cout<<board->GetValue()<<endl; else if (userInput == "showHistory"){ const vector<GameMove *> *history=board->GetMoveHistory(); for(int i=0;i<board->GetMoveCount();i++) if (gameSelection == "othello"){ if (i % 2 == 1) cout << "White Move: " << (string)*history->at(i) << endl; else cout << "Black Move: " << (string)*history->at(i) << endl; } else{ if (i % 2 == 1) cout << "O Move: " << (string)*history->at(i) << endl; else cout << "X Move: " << (string)*history->at(i) << endl; } } else if (userInput.substr(0, 5) == "move ") { GameMove *currentMove = board->CreateMove(); try{ *currentMove = userInput; for (int i = 0; i < possMoves.size(); i++) if (*possMoves.at(i) == *currentMove) validMove = true; if(!validMove) throw OthelloException("Not a valid move"); board->ApplyMove(currentMove); } catch(OthelloException &err){ delete currentMove; cout<<err.GetMessage(); } } else cout << "Please enter a valid command"<<endl; // Command loop: // move (r,c) // undo n // showValue // showHistory // quit for (int i = 0; i < possMoves.size(); i++) delete possMoves.at(i); possMoves.clear(); cout<<endl; validMove = false; } while (!board->IsFinished()); if (gameSelection == "tictactoe"){ if (board->GetNextPlayer() == -1) cout << "X "; else cout << "O "; cout << "is the winner!!"; } }
int main(int argc, char* argv[]) { //---------Main loop---------------------- do { //Declaring Variable GameBoard *board = nullptr; GameView *v = nullptr; vector<GameMove *> possMoves; GameMove *m = nullptr; string gameType; cout << "What game do you want to play?" << endl << "1) Othello; 2) Tic Tac Toe; 3) Connect Four; 4) Exit" << endl; getline(cin, gameType); //Initialize Othello Game if (gameType == "1") { cout << "User want to play Othello" << endl; board = new OthelloBoard(); v = new OthelloView(board); } //Initialize Tic Tac Toe Game else if (gameType == "2") { cout << "User want to play Tic Tac Toe" << endl; board = new TicTacToeBoard(); v = new TicTacToeView(board); } //Initialize Connect Four else if (gameType == "3") { cout << "User want to play Connect Four" << endl; } //Quit Game else if (gameType == "4") { break; } //Game loop bool quit = false; do { //If the game hasn't been chosen then quit if (board == nullptr && v == nullptr) break; //Print out the board cout << *v << endl; //Get Possible Moves board->GetPossibleMoves(&possMoves); cout << "Board Possible Move: "; //cout << possMoves.size(); for (GameMove* i : possMoves) { cout << (string)(*i) << " "; } cout << endl; //-------Display Next Player--------- cout << board->GetPlayerString(board->GetNextPlayer()) + " Turn" << endl; //------Reading in input-------------------- string command; getline(std::cin, command); stringstream ss(command); ss >> command; //Reading in input if (command == "move") { if (!ss.eof()) { ss >> command; m = board->CreateMove(); try { *m = command; bool valid = false; //Check for possible move for (GameMove* i : possMoves) { if ((*m).Equals(*(i))) { valid = true; board->ApplyMove(m); } } if (!valid) { delete m; cout << "Invalid move" << endl; } } catch (GameException &e) { delete m; cout << e.what() << endl; } } else cout << "Invalid input" << endl; } //---------Undo move----------- if (command == "undo") { if (!ss.eof()) { ss >> command; istringstream buffer(command); int undoMove; buffer >> undoMove; for (int i = 0; i < undoMove; i++) { board->UndoLastMove(); } } }