#include using namespace std; #include #include enum YNAns {Yes, No}; enum MoveResult {Win, Done, Continue}; enum messages {YouWin, YouLose, TieGame, SeeYa}; char *endMessage[] = { "\n***** Congratulations!! You are a winner! *****\n\n", "\n***** Too bad. I win again. *****\n\n", "\n***** Nobody Wins -- It's a tie! *****\n\n", "\n***** Game Over. Thanks for playing! *****\n\n" }; char startBoard[] = {'0','1','2','3','4','5','6','7','8'}; char gameBoard[] = {' ',' ',' ',' ',' ',' ',' ',' ',' '}; YNAns DisplayIntro(); // ***** This routine has already been coded below ******* void DisplayBoard(char board[]); // ***** This routine has already been coded below ******* YNAns getYesNoAns(const char* question); void coinToss(char &comptr, char &humn); bool winner(char bd[], char moveVal); MoveResult ComputerMove(char bd[], char cMoveVal, char hMoveVal); MoveResult humanMove(char bd[], char humMoveVal); // ***** This routine has already been coded below ******* bool isLegal(int move, char bd[], bool supressOutput = true); // ***** This routine has already been coded below ******* void DisplayEndMsg(char **MsgArr, messages msgIdx); int main() { bool win = false; int counter = 1; int lastturn = 0; char comptr, humn; if (getYesNoAns("\nDo you wish to play? Enter 'y' or 'n': ") == Yes) cout << "The answer was Yes.\n"; else cout << "The answer was No.\n"; cout << "These are the keys that you need to press to play the game." << endl; DisplayBoard(startBoard); cout << "Now we are going to flip a coin to determine who goes first." << endl; coinToss(comptr, humn); cout << "Here is the current game board layout:" << endl; DisplayBoard(gameBoard); if (comptr == 'x') { cout << "The computer went first, here is the board." << endl; ComputerMove(gameBoard, comptr, humn); counter++; lastturn = 1; } else { humanMove(gameBoard, humn); counter++; lastturn = 2; } while(win != true && counter < 9) { //lastturn {computer = 1, player = 2 if(lastturn == 1) { humanMove(gameBoard, humn); counter++; lastturn = 2; } else { ComputerMove(gameBoard, comptr, humn); counter++; lastturn = 1; } } if(counter == 9) { if(win == true) { if(lastturn == 1) { cout << endMessage[1] << endl; cout << "Here is the final game board:" << endl; DisplayBoard(gameBoard); } else { cout << endMessage[0] << endl; cout << "Here is the final game board:" << endl; DisplayBoard(gameBoard); } } else { cout << endMessage[3] << endl; cout << "Here is the final board." << endl; DisplayBoard(gameBoard); } } cout << endMessage[4] << endl; return 0; } void coinToss(char &comptr, char &humn) { srand(time(0)); int random_integer = rand()%2; if (random_integer == 1) { cout << "Coin was flipped, heads landed." << endl; comptr = 'x'; humn = 'o'; } else { cout << "Coin was flipped, tails landed." << endl; comptr = 'o'; humn = 'x'; } cout << "Human is " << humn << " and Computer is " << comptr << "." << endl; } YNAns getYesNoAns(const char *question) { char resp; do { cout << question; cin >> resp; }while(resp != 'y' && resp != 'Y' && resp != 'n' && resp != 'N'); if (resp == 'y' || resp == 'Y') return Yes; return No; } void DisplayEndMsg(char **msgArr, messages msgIdx) { cout << msgArr[msgIdx]; } void DisplayBoard(char bd[]) { cout << endl; cout << fixed << setw(1); cout << "\t-------------\n"; for(int i=0; i<=2; i++) { cout << "\t"; cout << "| " << bd[i*3] << " | "; cout << bd[i*3+1] << " | "; cout << bd[i*3+2] << " |" << endl; cout << "\t"; cout << "-------------" << endl; } cout << endl; } bool isLegal(int move, char bd[], bool supressOutput) { if ((move < 0) || (move > 8) || (bd[move] != ' ')) { if (!supressOutput) cout << "Illegal Move\n"; return false; } return true; } bool winner(char bd[], char moveVal) { const int WINVALS[8][3] = { {0,1,2}, {3,4,5}, {6,7,8}, {0,3,6}, {1,4,7}, {2,5,8}, {0,4,8}, {2,4,6} }; return true; } MoveResult humanMove(char bd[], char humMoveVal) { int input = 0; bool okay = false; while(okay == false) { cout << "It is your turn to pick a spot to go." << endl; cin >> input; if(gameBoard[input] != ' ') { cout << "That spot is taken, please pick another spot." << endl; } else { okay = true; } } gameBoard[input] = humMoveVal; return Continue; } MoveResult ComputerMove(char bd[], char compMoveVal, char humMoveVal) { const int BEST_MOVES[] = {4,0,2,6,8,1,3,5,7}; /*Logic: 1. Check spaces to see if can win. 2. Check spaces to see if can block. 3. Pick BEST_MOVES spot. compMoveVal = computer's letter. humMoveVal = human's letter. const int WINVALS[8][3] = _______________ {0,1,2} | 0 | 1 | 2 | {3,4,5} |____|____|____| {6,7,8} | 3 | 4 | 5 | {0,3,6} |____|____|____| {1,4,7) | 6 | 7 | 8 | {2,5,8} |____|____|____| {0,4,8} {2,4,6} */ for(int i = 0; i < 9; i++) { if(isLegal(i, gameBoard) == true) { gameBoard[i] = compMoveVal; if(winner(gameBoard, compMoveVal) == true) { cout << "I shall take square number: " << i << endl; DisplayEndMsg(endMessage, YouLose); DisplayBoard(bd); return Win; } gameBoard[i] = ' '; } } for(int j = 0; j < 9; j++) { if(isLegal(j, gameBoard) == true) { gameBoard[j] = humMoveVal; if(winner(gameBoard, humMoveVal) == true) { cout << "I shall take square number: " << j << endl; gameBoard[j] = compMoveVal; DisplayBoard(bd); return Continue; } bd[j] = ' '; } } for(int k = 0; k < 9; k++) { if(isLegal(BEST_MOVES[k], gameBoard) == true) { cout << "I shall take square number: " << BEST_MOVES[k] << endl; gameBoard[BEST_MOVES[k]] = compMoveVal; DisplayBoard(gameBoard); return Continue; } } cout << "I shouldn't be here!" << endl; return Continue; }