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

Write a C++ program that repeatedly reads in tic-tac-toe boards and for each one

ID: 3809582 • Letter: W

Question

Write a C++ program that repeatedly reads in tic-tac-toe boards and for each one indicates if there is a winner and who won.

The board will be read in as 3x3 array of characters (x, o or . for a blank spot).

You are required to write the following functions to help solve the problem:

// input prompts the user for a board and inputs it

// into the given array

void input (char board [3][3]);

// print prints the board in nice format

void print (char board [3][3]);

// win returns true if the given player has won on the

// given board, else it returns false

bool win (char board [3][3], char player);

You are required to use nested loops to write the functions input and print.

The back of this page shows a sample run of the program (input in bold). Your output should look at least as nice as that shown. You may assume that the board will be legal and there will be at most one winner.

Enter your board as characters (x, o or .):

o.x

..o

.x.

The board is:

o |   | x

-----------

   |   | o

-----------

   | x |

No winner!

Would you like to do another (y or n)? y

Enter your board as characters (x, o or .):

oxo

ox.

.xo

The board is:

o | x | o

-----------

o | x |

-----------

   | x | o

X wins!

Would you like to do another (y or n)? y

Enter your board as characters (x, o or .):

xoo

xox

ox.

The board is:

x | o | o

-----------

x | o | x

-----------

o | x |

O wins!

Would you like to do another (y or n)? n

Thanks for playing!

Explanation / Answer

#include <iostream>

using namespace std;
char board[3][3];

void input(char board[3][3])
{
   for (int i = 0; i < 3; i++) {
       for (int j = 0; j < 3; j++)
           cin >> board[i][j]; // get input from user
   }
}

void print(char board[3][3])
{
   cout << "The board is:"<< endl;
   int i = 0, j;
   for (; i < 3; i++) {
       j = 0;
       for (; j < 2; j++)
           cout << board[i][j] << " " << "|";// print board
       cout << board[i][j] << endl;
       if (i < 2)
           cout << "--------------" << endl;
   }
}

bool win(char board[3][3], char player)
{
   // There are 8 cases in which a palyer can win, 3 horizontal, 3 vertical, 2 daigonal
   return (
       board[0][0] == player && board[0][1] == player && board[0][2] == player || // check for 1st row
       board[1][0] == player && board[1][1] == player && board[1][2] == player || // check for 2nd row
       board[2][0] == player && board[2][1] == player && board[2][2] == player || // check for 3rd row
       board[0][0] == player && board[1][0] == player && board[2][0] == player || // check for 1st col
       board[0][1] == player && board[1][1] == player && board[2][1] == player || // check for 2nd col
       board[0][2] == player && board[1][2] == player && board[2][2] == player || // check for 3rd col
       board[2][0] == player && board[1][1] == player && board[0][2] == player || // check for main diagonal
       board[0][0] == player && board[1][1] == player && board[2][2] == player    // check for other diagonal
       );  
}

int main()
{
   char choice = 'y';
   while(choice == 'y') {
       char board[3][3];
       cout << "Enter your board as characters (x, o or .):";
       input(board);
       print(board);
       if (win(board, 'x'))
           cout << "x Wins!" << endl;
       else if (win(board, 'o'))
           cout <<"o Wins!" << endl;
       else
           cout << "No Winner!"<< endl;
           cout << "Would you like to do another (y or n)?";
           cin >> choice;
   }
   return 0;
}

output:

sanjiv@sanjiv-VirtualBox:~$ g++ tic-tac.cpp -o tic
sanjiv@sanjiv-VirtualBox:~$
sanjiv@sanjiv-VirtualBox:~$ ./tic
Enter your board as characters (x, o or .):oxo

ox.

.xo
The board is:
o |x |o
----------
o |x |.
----------
. |x |o
x Wins!
Would you like to do another (y or n)?y
Enter your board as characters (x, o or .):xoo

xox

ox.
The board is:
x |o |o
----------
x |o |x
----------
o |x |.
o Wins!
Would you like to do another (y or n)?y
Enter your board as characters (x, o or .):o.x

..o

.x.
The board is:
o |. |x
-------
. |. |o
--------
. |x |.
No Winner!
Would you like to do another (y or n)?n

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