Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Christian Ballard
- // COSC 1337 - 001
- // 9/10/15
- // TIC - TAC - TOE
- #include <iostream>
- #include <ctime>
- using namespace std;
- // Prototypes
- void displayBoard();
- void draw();
- void gameInstructions();
- int getHumanMove();
- int getComputerMove();
- bool winner();
- bool checkTie();
- const int BEST_MOVES[] = { 4, 0, 2, 6, 8, 1, 3, 5, 7 };
- enum gamePieces{ X_PIECE, O_PIECE, EMPTY_PIECE };
- enum gameState{ X_WINS, O_WINS, NOT_OVER, TIE };
- gamePieces board[9] = { EMPTY_PIECE, EMPTY_PIECE, EMPTY_PIECE, EMPTY_PIECE, EMPTY_PIECE, EMPTY_PIECE, EMPTY_PIECE, EMPTY_PIECE, EMPTY_PIECE };
- char gamePieceChr[3] = { 'X', 'O', ' ' };
- gamePieces humanPiece;
- gamePieces computerPiece;
- int const HUMAN = 1;
- int const COMPUTER = 0;
- int main()
- {
- char whoGoes1st;
- int playerTurn;
- gameInstructions();
- displayBoard();
- do
- {
- cout << "Would you like to go first?(y/n): ";
- cin >> whoGoes1st;
- whoGoes1st = toupper(whoGoes1st);
- if (whoGoes1st != 'Y' && whoGoes1st != 'N')
- cout << "Invalid Choice try again." << endl << endl;
- } while (whoGoes1st != 'N' && whoGoes1st != 'Y');
- switch (whoGoes1st)
- {
- case 'Y':
- playerTurn = HUMAN;
- humanPiece = X_PIECE;
- computerPiece = O_PIECE;
- break;
- case 'N':
- playerTurn = COMPUTER;
- humanPiece = O_PIECE;
- computerPiece = X_PIECE;
- break;
- }
- draw();
- do
- {
- if(playerTurn == HUMAN)
- {
- board[getHumanMove()] = humanPiece;
- draw();
- if (winner() == true)
- {
- cout << "Human wins!" << endl;
- }
- if(checkTie() == true)
- {
- cout << "It's Tie" << endl;
- }
- playerTurn = COMPUTER;
- }
- else if(playerTurn == COMPUTER)
- {
- board[getComputerMove()] = computerPiece;
- draw();
- if(winner() == true)
- {
- cout << "Computer wins, better luck next time !" << endl;
- }
- if(checkTie() == true)
- {
- cout << "It's a tie !" << endl;
- }
- playerTurn = HUMAN;
- }
- }while(winner() == false && checkTie() == false);
- system("pause");
- return 0;
- }
- void gameInstructions()
- {
- cout << "Tic - Tac - Toe" << endl << endl;
- cout << "The player who succeeds in placing three respective marks\n in a horizontal, vertical, or diagonal row wins the game." << endl << endl;
- }
- void displayBoard()
- {
- cout << " " << 0 << " | " << 1 << " | " << 2 << endl;
- cout << " -----+-----+-----" << endl;
- cout << " " << 3 << " | " << 4 << " | " << 5 << endl;
- cout << " -----+-----+-----" << endl;
- cout << " " << 6 << " | " << 7 << " | " << 8 << endl;
- }
- int getHumanMove()
- {
- int move;
- int hum = static_cast<int>(X_PIECE);
- do
- {
- cout << "What space would you like to take? (0-8): ";
- cin >> move;
- if (move < 0 || move > 8)
- {
- cout << "Invalid space, please try again." << endl;
- }
- if (board[move] == X_PIECE || board[move] == O_PIECE)
- {
- move = 72;
- cout << "Space is already taken please try again" << endl;
- }
- } while (move < 0 || move > 8 || move == 72);
- return move;
- }
- int getComputerMove()
- {
- int move = 0;
- cout << "Computers turn." << endl;
- //check if computer can win
- for (int i = 0; i <= 8; i++)
- {
- if (board[i] == EMPTY_PIECE)
- {
- board[i] = computerPiece;
- if (winner() == true)
- {
- return i;
- }
- else
- {
- board[i] = EMPTY_PIECE;
- }
- }
- }
- // check if human can win on next move
- for (int i = 0; i <= 8; i++)
- {
- if (board[i] == EMPTY_PIECE)
- {
- board[i] = humanPiece;
- if (winner() == true)
- {
- board[i] = computerPiece;
- return i;
- }
- else
- {
- board[i] = EMPTY_PIECE;
- }
- }
- }
- //computer can't on next move; human can't win
- for (int i = 0; i <= 8; i++)
- {
- move = BEST_MOVES[i];
- if (board[move] == EMPTY_PIECE)
- break;
- }
- return move;
- }
- void draw()
- {
- cout << endl;
- cout << " " << gamePieceChr[board[0]] << " | " << gamePieceChr[board[1]] << " | " << gamePieceChr[board[2]] << endl;
- cout << " -----+-----+-----" << endl;
- cout << " " << gamePieceChr[board[3]] << " | " << gamePieceChr[board[4]] << " | " << gamePieceChr[board[5]] << endl;
- cout << " -----+-----+-----" << endl;
- cout << " " << gamePieceChr[board[6]] << " | " << gamePieceChr[board[7]] << " | " << gamePieceChr[board[8]] << endl;
- }
- bool winner()
- {
- bool winneasdfr = false;
- if (board[0] == X_PIECE && board[1] == X_PIECE && board[2] == X_PIECE)
- winneasdfr = true;
- if (board[3] == X_PIECE && board[4] == X_PIECE && board[5] == X_PIECE)
- winneasdfr = true;
- if (board[6] == X_PIECE && board[7] == X_PIECE && board[8] == X_PIECE)
- winneasdfr = true;
- if (board[0] == X_PIECE && board[3] == X_PIECE && board[6] == X_PIECE)
- winneasdfr = true;
- if (board[1] == X_PIECE && board[4] == X_PIECE && board[7] == X_PIECE)
- winneasdfr = true;
- if (board[2] == X_PIECE && board[5] == X_PIECE && board[8] == X_PIECE)
- winneasdfr = true;
- if (board[0] == X_PIECE && board[4] == X_PIECE && board[8] == X_PIECE)
- winneasdfr = true;
- if (board[2] == X_PIECE && board[4] == X_PIECE && board[6] == X_PIECE)
- winneasdfr = true;
- //*************
- if (board[0] == O_PIECE && board[1] == O_PIECE && board[2] == O_PIECE)
- winneasdfr = true;
- if (board[3] == O_PIECE && board[4] == O_PIECE && board[5] == O_PIECE)
- winneasdfr = true;
- if (board[6] == O_PIECE && board[7] == O_PIECE && board[8] == O_PIECE)
- winneasdfr = true;
- if (board[0] == O_PIECE && board[3] == O_PIECE && board[6] == O_PIECE)
- winneasdfr = true;
- if (board[1] == O_PIECE && board[4] == O_PIECE && board[7] == O_PIECE)
- winneasdfr = true;
- if (board[2] == O_PIECE && board[5] == O_PIECE && board[8] == O_PIECE)
- winneasdfr = true;
- if (board[0] == O_PIECE && board[4] == O_PIECE && board[8] == O_PIECE)
- winneasdfr = true;
- if (board[2] == O_PIECE && board[4] == O_PIECE && board[6] == O_PIECE)
- winneasdfr = true;
- return winneasdfr;
- }
- bool checkTie()
- {
- bool tie = false;
- if (board[0, 1, 2, 3, 4, 5, 6, 7, 8] == X_PIECE || board[0, 1, 2, 3, 4, 5, 6, 7, 8] == O_PIECE)
- tie = true;
- return tie;
- }
Advertisement
Add Comment
Please, Sign In to add comment