1. Engineering
  2. Computer Science
  3. write a modular program no classes yet just from what...

Question: write a modular program no classes yet just from what...

Question details

Write a modular program (no classes yet, just from what you learned last year), that allows two players to play a game of tic-tac-toe. Use a two-dimensional char array with 3 rows and 3 columns as the game board. Each element of the array should be initialized with an asterisk (*). The program should display the initial board configuration and then start a loop that does the following: Allow player 1 to select a location on the board for an X by entering a row and column number. Then redisplay the board with an X replacing the * in the chosen location. If there is no winner yet and the board is not yet full, allow player 2 to select a location on the board for an O by entering a row and column number. Then redisplay the board with an O replacing the * in the chosen location. The loop should continue until a player has won or a tie has occurred, then display a message indicating who won, or reporting that a tie occurred. Player 1 wins when there are three Xs in a row, a column, or a diagonal on the game board. Player 2 wins when there are three Ox in a row, a column, or a diagonal on the game board. A tie occurs when all of the locations on the board are full, but there is no winner. Input Validation: Only allow legal moves to be entered. The row must be 1, 2, or 3. The column must be 1, 2 3. The (row, column) position entered must currently be empty (i.e., still have an asterisk in it).

THIS IS MY WORK I DID IT MANUALLY

#include <iostream>

using namespace std;

char matrix [3][3] = { '1', '2', '3', '4', '5', '6', '7', '8','9'};

int player;

void Draw()

{

cout << "Quick TIC TAC TOE!" << endl;

for (int i= 0; i < 3; i++)

{

for (int j = 0; j < 3; j++)

{

cout << matrix [i][j] << " ";

}

cout << endl;

}

}

void Input()

{

int a;

cout << "Press any # on the chart: ";

cin >> a;

if (a == 1)

matrix[0][0] = player;

else if (a == 2)

matrix[0][1] = player;

else if (a == 3)

matrix[0][2] = player;

else if (a == 4)

matrix[1][0] = player;

else if (a == 5)

matrix[1][1] = player;

else if (a == 6)

matrix[1][2] = player;

else if (a == 7)

matrix[2][0] = player;

else if (a == 8)

matrix[2][1] = player;

else if (a == 9)

matrix[2][2] = player;

}

void Toggleplayer()

{

if (player != 'X')

player = 'O';

else

player == 'X';

player = 'X';

}

char win()

{

// X player

if (matrix[0][0] == 'X' && matrix[0][1] == 'X' && matrix[0][0] == 'X')

return 'X';

if (matrix[1][0] == 'X' && matrix[1][1] == 'X' && matrix[1][0] == 'X')

return 'X';

if (matrix[2][0] == 'X' && matrix[2][1] == 'X' && matrix[2][0] == 'X')

return 'X';

if (matrix[0][0] == 'X' && matrix[1][0] == 'X' && matrix[2][0] == 'X')

return 'X';

if (matrix[0][0] == 'X' && matrix[1][0] == 'X' && matrix[2][0] == 'X')

return 'X';

if (matrix[0][1] == 'X' && matrix[1][1] == 'X' && matrix[2][1] == 'X')

return 'X';

if (matrix[0][2] == 'X' && matrix[1][2] == 'X' && matrix[2][2] == 'X')

return 'X';

if (matrix[0][0] == 'X' && matrix[1][1] == 'X' && matrix[2][2] == 'X')

return 'X';

if (matrix[2][0] == 'X' && matrix[1][1] == 'X' && matrix[0][2] == 'X')

return 'X';

//O player

if (matrix[0][0] == 'O' && matrix[0][1] == 'O' && matrix[0][0] == 'O')

return 'O';

if (matrix[1][0] == 'O' && matrix[1][1] == 'O' && matrix[1][0] == 'O')

return 'O';

if (matrix[2][0] == 'O' && matrix[2][1] == 'O' && matrix[2][0] == 'O')

return 'O';

if (matrix[0][0] == 'O' && matrix[1][0] == 'O' && matrix[2][0] == 'O')

return 'O';

if (matrix[0][0] == 'O' && matrix[1][0] == 'O' && matrix[2][0] == 'O')

return 'O';

if (matrix[0][1] == 'O' && matrix[1][1] == 'O' && matrix[2][1] == 'O')

return 'O';

if (matrix[0][2] == 'O' && matrix[1][2] == 'O' && matrix[2][2] == 'O')

return 'O';

if (matrix[0][0] == 'O' && matrix[1][1] == 'O' && matrix[2][2] == 'O')

return 'O';

if (matrix[2][0] == 'O' && matrix[1][1] == 'O' && matrix[0][2] == 'O')

return 'O';

return '/';

}

int main()

{

// 1 2 3

// 4 5 6

// 7 8 9

Draw();

while (1)

{

Input();

Draw();

if (win () == 'X')

{

cout << "X Player Wins!" << endl;

break;

}

else if (win() == 'O')

{

cout << "O Player Wins!" << endl;

break;

}

Toggleplayer();

}

system("pause");

return 0;

}

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution