Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Download tictactoe.cpp. The opponent (the \'O\'s) are not very smart (i.e. r

ID: 3676995 • Letter: C

Question

C++

Download tictactoe.cpp. The opponent (the 'O's) are not very smart (i.e. random). For this problem
you need to add functionality to load and save games. In addition to choosing 1-9 to play, you should
add an option 's', 'l' and 'q' to save the current game (for playing later!), load the game or quitting thecurrent game. You should assume the file to for save to and load from (i.e. write to and read from) is
called “TTT.save”. The format of the file should be the character mark followed by a list of numbers of
where the player currently played. For example, the game:
X|O|O
­­­­­
4|X|6
­­­­­
7|8|9
Should correspond to a TTT.save file:
X: 1 5
O: 2 3
You should assume only TTT.save is used. If the file does not exist, you should inform the user that the
file does not exist and act the same as if they tried to input a bad command (such as “-10” or “x”). If
you choose to save, you should override the existing file if it exists with the new saved data.
Note: in all the examples below, the character “l” is always the letter for loading and never the number
for playing in the top left.
Example 1 (no file exists) (user input is underlined):
1|2|3
­­­­­
4|5|6
­­­­­
7|8|9
What number do you with to play at? (or (l)oad, (s)ave or (q)uit) l
1|2|3
­­­­­
4|5|6
­­­­­
7|8|9
What number do you with to play at? (or (l)oad, (s)ave or (q)uit) q
Goodbye.
Example 2 (user input is underlined):
1|2|3
­­­­­
4|5|6
­­­­­
7|8|9
What number do you with to play at? (or (l)oad, (s)ave or (q)uit) 2O|X|3
­­­­­
4|5|6
­­­­­
7|8|9
What number do you with to play at? (or (l)oad, (s)ave or (q)uit) s
O|X|3
­­­­­
4|5|6
­­­­­
7|8|9
What number do you with to play at? (or (l)oad, (s)ave or (q)uit) q
Goodbye.
Example 3 (save file exists from example 2) (user input is underlined):
1|2|3
­­­­­
4|5|6
­­­­­
7|8|9
What number do you with to play at? (or (l)oad, (s)ave or (q)uit) l
O|X|3
­­­­­
4|5|6
­­­­­
7|8|9
What number do you with to play at? (or (l)oad, (s)ave or (q)uit) 5
O|X|O
­­­­­
4|X|6
­­­­­
7|8|9
What number do you with to play at? (or (l)oad, (s)ave or (q)uit) 8
O|X|O
­­­­­
4|X|6
­­­­­
7|X|9
Game over! You win!

Explanation / Answer

#include <iostream>
#include <string>
#include <fstream>

const int SIZE = 3;
const char PLAYER = 'X';
const char COMPUTER = 'O';
const char TIE = 'T';
const char STILL_PLAY = ' ';

using namespace std;

int findBest(char board[SIZE][SIZE], int player);
bool isOpen(char board[SIZE][SIZE], int row, int col);
void copyBoard(char original[SIZE][SIZE], char copy[SIZE][SIZE]);
void print(char board[SIZE][SIZE]);
char gameOver(char board[SIZE][SIZE]);

int main()
{
   char board[SIZE][SIZE];
   for(int i=0; i < SIZE; i++)
   {
       for(int j=0; j < SIZE; j++)
       {
           board[i][j] = '1' + 3*i+j;
       }
   }

   int turn = 1;
   bool noTurn = true;
   int OCount = 0, XCount = 0;
   while(gameOver(board) == STILL_PLAY)
   {
       if(turn%2 == 1)
       {
           int play;
           do {
               noTurn = true;
               print(board);
               cout << "What number do you with to play at? (or (l)oad, (s)ave or (q)uit) ";
               char cplay;
               cin >> cplay;
               cin.ignore(20000,' ');
               if(cplay == 'q' || cplay == 'Q')
               {
                   cout << "Goodbye. " << endl;
                   return 0;
               }
               else if(cplay == 'L' || cplay == 'l'){
                   ifstream in("TTT.save");
                   if(in.is_open()){
                       for(int i = 0; i < SIZE; ++i){
                           for(int j = 0; j < SIZE; ++j){
                               board[i][j] = ((i * SIZE) + (j + 1)) + '0';
                           }
                       }
                       string str;
                       int ind;
                       while(in >> str){
                           if(str == "X:"){
                               while(in >> str){
                                   if(str == "O:"){
                                       break;
                                   }
                                   ind = atoi(str.c_str());
                                   board[(ind-1) / 3][(ind-1)%3] = 'X';
                                   ++XCount;
                               }
                           }
                           while(in >> ind){
                               board[(ind-1) / 3][(ind-1)%3] = 'O';
                               ++OCount;
                           }
                       }
                       in.close();
                   }
                   else{
                       cout << "TTT.save does not exist" << endl;
                   }
                   continue;
               }
               else if(cplay == 'S' || cplay == 's'){
                   ofstream out("TTT.save");
                   out << "X: ";
                   for(int i = 0; i < SIZE; ++i){
                       for(int j = 0; j < SIZE; ++j){
                           if(board[i][j] == 'X'){
                               out << (i * SIZE) + (j+1) << " ";
                           }
                       }
                   }
                   out << endl << "O: ";
                   for(int i = 0; i < SIZE; ++i){
                       for(int j = 0; j < SIZE; ++j){
                           if(board[i][j] == 'O'){
                               out << (i * SIZE) + (j+1) << " ";
                           }
                       }
                   }
                   out << endl;
                   out.close();
                   continue;
               }
               else if(cplay > '9' || cplay < '0'){
                   cout << "Invalid choice. Try again" << endl;
                   continue;
               }
               play=cplay-'0';
               noTurn = false;
           } while(!noTurn && !isOpen(board, (play-1)/3, (play-1)%3));
           if(!noTurn){
               board[(play-1)/3][(play-1)%3] = PLAYER;
           }
           else{
               turn = (XCount + OCount) % 2;
           }
       }
       else
       {
           int bestI = -1;
           int bestJ = -1;
           int bestValue = -1;

           for(int i=0; i < SIZE; i++)
           {
               for(int j=0; j < SIZE; j++)
               {
                   if(isOpen(board,i,j))
                   {
                       int value = findBest(board, 1);
                       if(value > bestValue)
                       {
                           bestValue=value;
                           bestJ = j;
                           bestI = i;
                       }
                   }
               }
           }

           board[bestI][bestJ] = COMPUTER;

       }
       turn++;
   }

   print(board);
   cout << "Game over!";
   if(gameOver(board) == PLAYER)
   {
       cout << " You win! ";
   }
   else if(gameOver(board) == COMPUTER)
   {
       cout << " You lose! ";
   }
   else
   {
       cout << " Tie... ";
   }   

   return 0;
}

char gameOver(char board[SIZE][SIZE])
{

   for(int j=0; j<SIZE; j++)
   {
       bool win=true;
       for(int i=0; i<SIZE-1; i++)
       {
           if(board[i+1][j] != board[i][j])
           {
               win=false;
           }
       }
       if(win)
       {
           return board[0][j];
       }
   }   

   for(int j=0; j<SIZE; j++)
   {
       bool win=true;
       for(int i=0; i<SIZE-1; i++)
       {
           if(board[j][i] != board[j][i+1])
           {
               win=false;
           }
       }
       if(win)
       {
           return board[j][0];
       }
   }   

   bool win=true;
   for(int i=0; i < SIZE-1; i++)
   {
       if(board[i][i] != board[i+1][i+1])
       {
           win=false;
       }
   }
   if(win)
   {
       return board[0][0];
   }

   win=true;
   for(int i=0; i < SIZE-1; i++)
   {
       if(board[i][SIZE-1-i] != board[i+1][SIZE-1-i-1])
       {
           win=false;
       }
   }
   if(win)
   {
       return board[0][SIZE-1];
   }

   // above is someone wins

   for(int i=0; i < SIZE; i++)
   {
       for(int j=0; j < SIZE; j++)
       {
           if(isOpen(board,i,j))
           {
               return STILL_PLAY; // still playing
           }
       }
   }


   return TIE;
}   

int findBest(char board[SIZE][SIZE], int player)
{
   return 0;
}

bool isOpen(char board[SIZE][SIZE], int row, int col)
{
   return board[row][col] != 'X' && board[row][col] != 'O';
}

void copyBoard(char original[SIZE][SIZE], char copy[SIZE][SIZE])
{
   for(int i=0; i < SIZE; i++)
   {
       for(int j=0; j < SIZE; j++)
       {
           copy[i][j] = original[i][j];
       }
   }
}

void print(char board[SIZE][SIZE])
{
   cout << " ";
   string temp;
   for(int i=0; i < SIZE; i++)
   {
       temp = "";
       for(int j=0; j < SIZE; j++)
       {
           temp += board[i][j];
           temp += "|";
       }
       cout << temp.substr(0, temp.length()-1) << endl;;
       if(i != SIZE-1)
       {
           cout << "----- ";
       }
   }
}