CS111 Lab 17 Review
The Game of Tic-Tac-Toe

In this lab you will code the game of Tic-Tac-Toe that allows the user to play against the computer.


  1. The board itself will be a vector of chars.
  2. There are nine squares on a Tic-Tac-Toe board, so the vector should have nine elements.
  3. Each square on the board will correspond to an element in the vector and will therefore be numbered 0-8.
  4. The user and computer will be represented by a char - either an 'X' or an 'O'. The 'X' always goes first
  5. At the start of the game, the Tic-Tac-Toe board will contain only spaces representing that the cells are empty.
  6. The psuedo code for the game is as follows.
          Create an empty Tic-Tac-Toe board
          Display the game instructions
          Determine who goes first
          Display the board
          While nobody's won and it's not a tie
            if it's the user's turn
              Get the user's move.
              Update the board with the user's move
            else
              Calculate the computer's move.
              Update the board with the computer's move.
            Display the board
            Switch turns
          Congratulate the winner or declare a tie.


  7. In the C++ program below, most of the simple code has been written.
    You will need to complete the Result() and the computerMove() functions.
    #include <iostream>
    #include <string>
    #include <vector>
    #include <algorithm>
    using namespace std;

    //GLOBAL CONSTANTS
    const char X = 'X';
    const char O = 'O';
    const char EMPTY = ' ';
    const char TIE = 'T';
    const char GAME_NOT_OVER = 'N';

    //FUNCTION PROTOTYPES
    void instructions();
    void DrawTheBoard(const vector& board);
    char Result(const vector& board);
    int userMove(const vector& board, char user);
    int computerMove(vector board, char computer);


    int main(){

      //CONSTRUCT A VECTOR BOARD AND FILL EACH CELL WITH A BLANK.
      vector board(9, ' ');

      instructions(); //DISPLAY BASIC INSTRUCTIONS

      //CONSTRUCT THE PLAYERS IN THE GAME
      char user;
      char computer;
      int move;

      //ASK USER IF THEY WANT TO GO FIRST. ASSIGN X TO THE PLAYER THAT GOES FIRST.
      cout << "X goes first. Would you like to go first? yY ";
      char response;
      cin >> response;
      if (response == 'y' || response == 'Y') {
        user = X;
        computer = O;
      }
      else {
        user = O;
        computer = 'X';
      }

      //THE FIRST TURN GOES TO THE X.
      char whoseTurn = X;
      DrawTheBoard(board);

      //PLAY THE GAME UNTIL SOMEONE WINS OR THE PLAYERS TIE.
      while (Result(board) == GAME_NOT_OVER ){
        if (whoseTurn == user){
          move = userMove(board, user);
          board[move] = user;
        }
        else{
          move = computerMove(board, computer);
          board[move] = computer;
        }
        DrawTheBoard(board);
        whoseTurn = (whoseTurn ==X ? O:X);
      }

      //DETERMINE WHO IS THE WINNER
      char Win = Result(board);
      if (Win == computer)
        cout <<"The computer won this game. Better luck next time." << endl;
      else if (Win== user)
        cout <<"Congratulations! You won this game." << endl;
      else
        cout << "No winner this time. It is a tie game." << endl;
      return 0;
    }


    void instructions(){
      cout << "This is the game of Tic-Tac-Toe." << endl;
      cout << "Make your move by entering a number, 0 - 8, to specify the board position shown below\n\n";

      cout << " 0 | 1 | 2"<< endl;
      cout << " ---------"<< endl;
      cout << " 3 | 4 | 5"<< endl;
      cout << " ---------"<< endl;
      cout << " 6 | 7 | 8\n"<< endl;
    }


    void DrawTheBoard(const vector& board){
      cout << "\n\t" << board[0] << " | " << board[1] << " | " << board[2];
      cout << "\n\t" << "---------";
      cout << "\n\t" << board[3] << " | " << board[4] << " | " << board[5];
      cout << "\n\t" << "---------";
      cout << "\n\t" << board[6] << " | " << board[7] << " | " << board[8];
      cout << "\n\n";
    }


    char Result(const vector& board){












    }


    int userMove(const vector& board, char user){
      int move;
      do {
        cout << "Select your move:(0 - 8): ";
        cin >> move;
        if (board[move] != EMPTY){
          cout << "That square is occupied." << endl;
          move = -1;
        }
      } while (move < 0 || move > 8);
      return move;
    }


    int computerMove(vector board, char computer){

















    }