Advertisement
Jaysds1

Tic-Tac-Toe Game

Sep 1st, 2012
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.23 KB | None | 0 0
  1.     #include <iostream>
  2.     #include <string>
  3.     using namespace std;
  4.  
  5.     // constants for rows and columns of tic-tac-toe board
  6.     const int ROWS = 3;
  7.     const int COLS = 3;
  8.  
  9.     enum eGameState { TIE = -1, PLAY, WON }; // make things easier to read
  10.  
  11.     // function headers
  12.     void outputBoard(char board[ROWS][COLS]);
  13.     bool processPlayerInput(char player, char board[ROWS][COLS], int* gameState, int numTurnsbool);
  14.     int hasPlayerWon(int userRow, int userColumn, int numTurns, char board[ROWS][COLS]);
  15.     void gameHasEnded(int gameState, char player);
  16.  
  17.  
  18.     int main()
  19.     {
  20.  
  21.             int gameState = PLAY; // flag for game has been won
  22.             int numTurns = 0; // initialize turn counter
  23.             char player = 'X'; // player X starts
  24.             char board[ROWS][COLS]; // initialize our tic-tac-toe board
  25.  
  26.             memset(board, '*', sizeof(board)); // assign * to every element in the array without an ugly array initialization list
  27.  
  28.             outputBoard(board); // output board initially
  29.  
  30.             do
  31.             {
  32.                     if ( processPlayerInput(player, board, &gameState, numTurns) ) // was the last turn valid?
  33.                     {
  34.                         numTurns++; // increment number of turns
  35.                         player = ( player == 'X' ) ? 'O' : 'X'; // swap player after the turn
  36.                     }
  37.                     system("cls"); // redraw screen
  38.                     outputBoard(board); // output updated board
  39.             } while (gameState == PLAY); // repeat while the game hasn't won
  40.  
  41.             gameHasEnded(gameState, player); // game is done! handle it
  42.             system("pause"); // wait for user exit (assuming the user did not launch this via "Start without debugging...", they can see the final result)
  43.             return 0; // obligatory return after the game has ended
  44.     }
  45.  
  46.     void outputBoard(char board[ROWS][COLS])
  47.     {
  48.                     cout << "\n\n\n\n\tCol: 1 2 3" << endl; // label columns
  49.                     cout << "\tRow:" << endl; //create row under it
  50.                     for(int row = 0; row < 3; row++) // output rows
  51.                     {
  52.                             cout << "\t" << (row+1) << "    "; // label rows
  53.                             for(int col = 0; col < 3; col++) // output columns
  54.                                     {
  55.                                             cout << board[row][col] << " "; // output board
  56.                                     }
  57.  
  58.                             cout << endl << endl; // new line for input line
  59.                     }
  60.     }
  61.  
  62.     bool processPlayerInput(char player, char board[ROWS][COLS], int* gameState, int numTurns)
  63.     {
  64.                     // initialize user row & column selection variables
  65.                     int userRow = 0;
  66.                     int userColumn = 0;
  67.  
  68.                     // ask user for input and store
  69.                     cout << "\tIt's " << player << "'s turn." << endl;
  70.                     cout << "\tEnter row and column:" << endl << endl;
  71.                     cout << "\tRow: ";
  72.                     cin >> userRow;
  73.                     cout << "\tColumn: ";
  74.                     cin >> userColumn;
  75.                     userRow--; // decrement by 1 for proper use
  76.                     userColumn--; // decrement by 1 for proper use
  77.  
  78.                     // check that input is valid (both row and column are between 0 and 2) and that the user isn't trying to overwrite an already played spot on the board
  79.                     if (userRow >= 0 && userRow <= 2 && userColumn >=0 && userColumn <=2 && board[userRow][userColumn] == '*')
  80.                     {
  81.                             board[userRow][userColumn] = player; // fill in user's choice
  82.                             *gameState = hasPlayerWon(userRow, userColumn, numTurns, board); // check if player has won
  83.                             return true; // turn is valid
  84.                     }
  85.  
  86.                     cout << "Invalid option.\n";
  87.                     system("pause"); // pause program and wait for user acknowledgement, since screen is redrawn after each turn and without this pause, the user wouldn't see the "Invalid option." text
  88.                     return false; // turn is invalid
  89.     }
  90.  
  91.     int hasPlayerWon(int userRow, int userColumn, int numTurns, char board[ROWS][COLS])
  92.     {
  93.              if (board [0][userColumn] == board [1][userColumn] && board [0][userColumn] == board [2][userColumn]) // check if one columns are equal (check if they are in a row)
  94.             {
  95.                     return WON;
  96.             }
  97.             else if (board [userRow][0] == board [userRow][1] && board[userRow][0] == board [userRow][2]) // check if one row is equal
  98.             {
  99.                     return WON;
  100.             }
  101.             else if (board [0][0] == board [1][1] && board [0][0] == board [2][2] && board [0][0] != '*') // check top left -> bottom right
  102.             {
  103.                     return WON;
  104.             }
  105.             else if (board [0][2] == board [1][1] && board [0][2] == board [2][0] && board [0][2] != '*') // check bottom left -> top right
  106.             {
  107.                     return WON;
  108.             }
  109.             else if (numTurns >= ROWS*COLS - 1) // turns start at 0, end at 8 - check if we have gone 9 turns; if so, there is a tie
  110.             {
  111.                     return TIE;
  112.             }
  113.             return PLAY; // return PLAY in case something breaks
  114.     }
  115.  
  116.     void gameHasEnded(int gameState, char player)
  117.     {
  118.             int asked;
  119.             player = ((player == 'X' ) ? 'O' : 'X'); // swap the player again since processPlayerInput always returns the next player
  120.             switch (gameState)
  121.             {
  122.                     case TIE:
  123.                             cout << "Tie! Thanks for playing, better luck next time." << endl;
  124.                             break;
  125.                     case WON:
  126.                             cout << player << " has won! Congratulations." << endl;
  127.                             break;
  128.             }
  129.             cout << "Would you like to play again?" << endl;
  130.             cout << "Enter 1 for yes and 0 for no" << endl;
  131.             cin >> asked;
  132.  
  133.             if (asked==1)
  134.             {
  135.                 main();
  136.             }
  137.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement