The Code Is C Tic Tac Toe Game Include Using

Question

The Code is C++ // tic tac toe game #include <iostream> using namespace std; const int SIZE = 9; int check(char *); void displayBoard(char *); void initBoard(char *); int main() {    char board[…

The Code is C++

// tic tac toe game
#include <iostream>

using namespace std;

const int SIZE = 9;

int check(char *);
void displayBoard(char *);
void initBoard(char *);

int main() {
   char board[SIZE];
   int player, choice, win, count;
   char mark;

   count = 0; // number of boxes marked till now

   initBoard(board);
  
   // start the game
   player = 1; // default player
   mark = 'X'; // default mark
   do {
       displayBoard(board);
       cout << "Player " <<
player << "(" << mark << ")" << " Enter
number: ";
       do {
           cin >>
choice;
           if (choice <
1 || choice > 9 || board[choice – 1] != (choice + '0'))
          
    cout << "Number invalid. Enter again:
";
           else {
          
    board[choice – 1] = mark;
          
    count++;
          
    win = check(board);
          
    if (win == 1) {
          
        displayBoard(board);
          
        cout << "\n\n PLAYER "
<< player << " IS THE WINNER!\n\n";
          
        return 0;
          
    }
          
    break;
           }
       } while (1);

       if (count == 9) {
          
displayBoard(board);
           cout <<
"\n\n GAME ENDED IN DRAW\n\n";
           return 0;
       }

       else if (player == 1) {
           player =
2;
           mark =
'O';
       }

       else {
           player =
1;
           mark =
'X';
       }
   } while (1);
   return 0;
}

// function initializes the board
void initBoard(char *board) {
   int i;
   for (i = 0; i < SIZE; i++) {
       board[i] = '0' + i + 1;
   }
}

// function checks if game has been won
int check (char *board) {
   if (board[0] == board[1] && board[1] ==
board[2])
       return 1;

   else if (board[3] == board[4] && board[4]
== board[5])
       return 1;

   else if (board[6] == board[7] && board[7]
== board[8])
       return 1;

   else if (board[0] == board[3] && board[3]
== board[6])
       return 1;

   else if (board[1] == board[4] && board[4]
== board[7])
       return 1;

   else if (board[2] == board[5] && board[5]
== board[8])
       return 1;

   else if (board[0] == board[4] && board[4]
== board[8])
       return 1;

   else if (board[2] == board[4] && board[4]
== board[6])
       return 1;
  
   else
       return 0;
}

// displays the board
void displayBoard(char *board) {
   int i;

   cout << endl;
   for (i = 0; i < SIZE; i++) {
       cout << board[i] <<
"\t";
       if ((i+1) % 3 == 0) {
           cout <<
endl;
       }
   }
   cout << endl;
}

so how can this code be modified to not include pointers?
Pointers aren't allowed. And to include arrays with loops rather
than using a bunch of if-else statements? and instead of the value
count that the user must enter, give the user the option to enter
the array location, do this so that both players can enter using
array location.

Solutions

Expert Solution

c++ program :- Note :- pointers are removed and user need to
given array location now row(0-2) and column (0-2)

// tic tac toe game
#include <iostream>

using namespace
std;

const int SIZE =
9;

char board[SIZE];

int check();
void displayBoard();
void initBoard();

int main() {
int player, choicex,choicey,choice, win, count;
char mark;

count = 0; // number of boxes
marked till now

initBoard();
  
// start the game
player = 1; // default player
mark = 'X'; // default mark
do {
displayBoard();
cout << "Player " << player << "(" << mark
<< ")" << "Enter array location x(0-2) y(0-2) ";
do {
cin >> choicex>>choicey;
choice=(choicex*3)+choicey;
if ((choicex <0 && choicex >2) || (choicey <0
&& choicey >2) || board[choice – 1] != (choice +
'0'))
cout << "Location invalid. Enter again: ";
else {
board[choice – 1] = mark;
count++;
win = check();
if (win == 1) {
displayBoard();
cout << "\n\n PLAYER " << player << " IS THE
WINNER!\n\n";
return 0;
}
break;
}
} while (1);

if (count == 9) {
displayBoard();
cout << "\n\n GAME ENDED IN DRAW\n\n";
return 0;
}

else if (player == 1) {
player = 2;
mark = 'O';
}

else {
player = 1;
mark = 'X';
}
} while (1);
return 0;
}

// function initializes the
board
void initBoard() {
int i;
for (i = 0; i < SIZE; i++) {
board[i] = '0' + i + 1;
}
}

// function checks if game has
been won
int check () {
if (board[0] == board[1] && board[1] == board[2])
return 1;

else if (board[3] == board[4]
&& board[4] == board[5])
return 1;

else if (board[6] == board[7]
&& board[7] == board[8])
return 1;

else if (board[0] == board[3]
&& board[3] == board[6])
return 1;

else if (board[1] == board[4]
&& board[4] == board[7])
return 1;

else if (board[2] == board[5]
&& board[5] == board[8])
return 1;

else if (board[0] == board[4]
&& board[4] == board[8])
return 1;

else if (board[2] == board[4]
&& board[4] == board[6])
return 1;
  
else
return 0;
}

// displays the board
void displayBoard() {
int i;

cout << endl;
for (i = 0; i < SIZE; i++) {
cout << board[i] << "\t";
if ((i+1) % 3 == 0) {
cout << endl;
}
}
cout << endl;
}


Submit Your Answer

[contact-form-7 id=”5″ html_class=”cf7_custom_style_1″ title=”Answer Question”]