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

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

ID: 3676193 • Letter: I

Question

It's 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 the

current 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.

tictactoe.cpp:

#include
#include

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;
   while(gameOver(board) == STILL_PLAY)
   {
       if(turn%2 == 1)
       {
           int play;
           do {
               print(board);
               cout << "What number do you with to play at? ";
               char cplay;
               cin >> cplay;
               cin.ignore(20000,' ');
               play=cplay-'0';
           } while(!isOpen(board, (play-1)/3, (play-1)%3));
           board[(play-1)/3][(play-1)%3] = PLAYER;
       }
       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    {
       bool win=true;
       for(int i=0; i        {
           if(board[i+1][j] != board[i][j])
           {
               win=false;
           }
       }
       if(win)
       {
           return board[0][j];
       }
   }      

   for(int j=0; j    {
       bool win=true;
       for(int i=0; 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 << "----- ";
       }
   }
}

For example, the game:

718 9 Should correspond to a TTT.save file X: 1 5 O: 23 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|213 415 |6 718 9 What number do you with toplay at? (or (1) oad, (s)ave or (a)uit) 1 1|213 415 |6 718 9 What number do you with toplay at? (or (1) oad, (s)ave or (a)uit) g Goodbye.

Explanation / Answer

// Save the Game in a File
void save(char board[SIZE][SIZE]){
   ofstream outfile;
   outfile.open("TTT.save");
   outfile << "X: ";
   // Save the Position of X
   for (int i = 0; i < SIZE; i++){
       for (int j = 0; j < SIZE; j++){
           if (board[i][j] == 'X' || board[i][j] == 'x')
               outfile << (3*i + j+1) << " ";
       }
   }
   outfile << endl;
   // Save the Position of O
   for (int i = 0; i < SIZE; i++){
       for (int j = 0; j < SIZE; j++){
           if (board[i][j] == 'O' || board[i][j] == 'o')
               outfile << (3*i + j+1) << " ";
       }
   }
}

// load the Board from the file
void load(char board[SIZE][SIZE]){
   ifstream infile;
   infile.exceptions ( std::ifstream::failbit | std::ifstream::badbit );
   string s;
   try {
       getline(infile,s);
       s = s.substr(2);
       for (int k = 0; k < s.length(); k += 2){
           int n = s[k] - '0';
           int j = n % 3;
           int i = n / 3;
           board[i][j] = 'X';
       }
       getline(infile,s);
       s = s.substr(2);
       for (int k = 0; k < s.length(); k += 2){
           int n = s[k] - '0';
           int j = n % 3;
           int i = n / 3;
           board[i][j] = 'O';
       }
       // print the board
       print(board);
   }
   catch (std::ifstream::failure e) {
       std::cerr << "The File ---- save.TTT ---- is not Here ";
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote