Question: professor asked that each element of the array should be initialized with an * can someone please help me do that please? #include using namespace
professor asked that each element of the array should be initialized with an * can someone please help me do that please?
#include
using namespace std;
// Prints the grid
// Takes the 2D array with r row size and c column size
void printTheGrid(char grid[][3], int r, int c){
for(int i=0; i
for(int j=0; j< c; j++){
cout<
}
cout<
}
}
// Check if it's a winner row column and returns true
bool checkForWinner(char grid[3][3], int row, int column){
if (grid[row][0] == grid[row][1]
&& grid[row][0] == grid[row][2]) //Check row
return true;
if (grid[0][column] == grid[1][column]
&& grid[0][column] == grid[2][column]) //Check column
return true;
if (row == column && grid[0][0] == grid[1][1]
&& grid[0][0] == grid[2][2]) //top-left to bottom-right diagonal
return true;
if (row + column == 2 && grid[0][2] == grid[1][1]
&& grid[0][2] == grid[2][0]) //top-right to bottom-left diagonal
return true;
return false;
}
int main()
{
char grid[3][3];
int freeSlots, row, column;
char player, response = 'y';
bool end, hasWinner;
while (toupper(response) == 'Y')
{
//initializing the grid with dots
for (int r = 0; r < 3; r++)
for (int c = 0; c < 3; c++)
grid[r][c] = '.';
freeSlots = 9;
player = 'X';
hasWinner = end = false;
printTheGrid(grid, 3, 3);
while (!end) //while game is not over
{
row = column = -1; //get valid move
while (column > 2 || column < 0 || row > 2 || row < 0
|| grid[row][column] != '.')
{
cout << "Player "
<< player << ","
<<"row ";
cin >> row;
row--;
cout << "Player "
<< player << ", "
<<"column ";
cin >> column;
column--;
}
grid[row][column] = player; //place move
freeSlots--; //decrement free slots
printTheGrid(grid, 3, 3);
//check game status
if (freeSlots == 0) end = true;
hasWinner = end = checkForWinner(grid, row, column);
//display game result
if (hasWinner) cout <<"Player " << player << " wins ";
else cout << "The cat wins ";
}
return 0;
}}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
