#include "stdafx.h" #include <iostream> #include <string> using namespace std; class ticTacToe { public: void printBoard() const; void getMove(string playerList[], int number, char& playMove, int& playPosition); void setMove(char move, int position); void checkMove(char& move, int& position, int num); int numOfMoves() const; //keeps track of the number of moves made bool isWinner() const; ticTacToe(); private: char board[3][3]; int totalMoves; }; void setIntro(string playerList[]); void checkPlayer(int& number); void congratPlayer(string playerList[], int num); int main() { cout << "Welcome to TIC-TAC-TOE" << endl; ticTacToe myGame; //object to hold the board string player[2]; //variables to hold the names of the two players char move; //variable to hold wither X or O int position; //variable to hold the position on the board int index=0; //variable to hold the index of the player array setIntro(player); myGame.printBoard(); while(!myGame.isWinner() && myGame.numOfMoves()<9) //checks that there is no winner and totalMoves<9 { checkPlayer(index); //if index==0, its player[0] turn, otherwise player[1] turn. myGame.getMove(player,index,move,position); myGame.checkMove(move,position,index); myGame.setMove(move,position); if(myGame.isWinner()) //structure to end the program after someone wins { myGame.printBoard(); congratPlayer(player, index); return 0; } cout << endl; myGame.printBoard(); //print board after each move index++; }//end while cout << "Cats game!" << endl; //executes if myGame.isWinner() is false and totalMoves==8 return 0; }//end main void ticTacToe::printBoard() const { for(int row=0;row<3;row++) { for(int col=0;col<3;col++) cout << board[row][col] << " "; cout << endl << endl << endl; } } void ticTacToe::getMove(string playerList[], int number, char& playMove, int& playPosition) { if(number==0) { cout << playerList[number] << "Enter a move: "; cin >> playPosition >> playMove; } else { cout << playerList[number] << "Enter a move: "; cin >> playPosition >> playMove; } } void ticTacToe::checkMove(char& move, int& position, int num) { if(num==0) { while(move!='X' && move!='x') { cout << "You must enter X as your move. Please reenter your move: "; cin >> move; } } //end if else { while(move!='O' && move!='o') { cout << "You must enter O as your move. Please reenter your move: "; cin >> move; } } //end else while(position<=0 || position>=10) { cout << "Invalid position. Position must be between 1 and 9 inclusive. Reenter position: "; cin >> position; } //end while } void ticTacToe::setMove(char move, int position) { if(position<=3) board[0][position-1]=static_cast<char>(toupper(static_cast<int>(move))); else if(position<=6) { switch(position) { case 4: board[1][0]=static_cast<char>(toupper(static_cast<int>(move))); break; case 5: board[1][1]=static_cast<char>(toupper(static_cast<int>(move))); break; case 6: board[1][2]=static_cast<char>(toupper(static_cast<int>(move))); break; } } else if(position<=9) { switch(position) { case 7: board[2][0]=static_cast<char>(toupper(static_cast<int>(move))); break; case 8: board[2][1]=static_cast<char>(toupper(static_cast<int>(move))); break; case 9: board[2][2]=static_cast<char>(toupper(static_cast<int>(move))); break; } } totalMoves++; } int ticTacToe::numOfMoves() const { return totalMoves; } bool ticTacToe::isWinner() const { if(board[0][0]==board[1][1] && board [0][0]==board[2][2]) return true; else if(board[2][0]==board[1][1] && board [2][0]==board[0][2]) return true; else if(board[0][0]==board[0][1] && board [0][0]==board[0][2]) return true; else if(board[1][0]==board[1][1] && board [1][0]==board[1][2]) return true; else if(board[2][0]==board[2][1] && board [2][0]==board[2][2]) return true; else if(board[0][0]==board[1][0] && board [0][0]==board[2][0]) return true; else if(board[0][1]==board[1][1] && board [0][1]==board[2][1]) return true; else if(board[0][2]==board[1][2] && board [0][2]==board[2][2]) return true; return false; } ticTacToe::ticTacToe() { for(int row=0;row<3;row++) for(int col=0;col<3;col++) { board[row][col]='-'; // Closing instantly problem occurs here } totalMoves=0; } void setIntro(string playerList[]) { playerList[0]='X'; playerList[1]='O'; } void checkPlayer(int& number) { if(number>1) number=0; } void congratPlayer(string playerList[], int num) { cout << "Congratulations, " << playerList[num] << "! You have won!" << endl; system("pause"); } |