Question: Every time i try to run this, it runs for player X only. I dont want to play against the computer but i still want

Every time i try to run this, it runs for player X only. I dont want to play against the computer but i still want a player 2 (player O) to have a turn after player X ...

pleaseeee help

#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 < r; i++){ for(int j=0; j< c; j++){ cout<< grid[i][j]; } cout<< endl; } } // 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; bool end, hasWinner;

{ //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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!