I can't seem to get input validation for my tic tac toe game
Apr 13, 2018 at 1:39pm UTC
I have been trying for hours, but the game won't stop looping the player O. That and it won't keep one player from overwriting their mark on another player on the board. Please help. I'm very close to driving my head through my desk.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
void TicTacToe(){
int player = 1;
sBoard();
int row;
int col;
cout << "=== Player X ===" << endl;
cout << "Pick a row and column (1-3)" <<endl;
cout << "Row: " ;
cin >> row;
cout << "Column: " ;
cin >> col;
if ((row == 1 || row ==2 || row == 3) && (col == 1 || col == 2 || col == 3)){
board[--row][--col]=1;
}
else {
cout<< "Invalid Position" <<endl;
}
sBoard();
player = 2;
switchPlayer(player);
}
void playerI(int player){
int row;
int col;
if (player==1){
cout << "=== Player X ===" << endl;}
else {
cout << "=== Player O ===" << endl;}
cout << "Pick a row and column (1-3)" <<endl;
cout << "Row: " ;
cin >> row;
cout << "Column: " ;
cin >> col;
if ((row == 1 || row ==2 || row == 3 || col == 1 || col == 2 || col == 3) && board[row][col] ==0)
{if (player==1)
board[--row][--col]=1;
if (player==2)
board[--row][--col]=2;}
else {
cout<< "Invalid Position" <<endl;
playerI(player);
}
}
void sBoard(void ){
for (int r=0; r<=2; r++)
{
for (int c=0; c<=2; c++)
{
if ( board [r][c]==0)
cout << "- " ;
else if (board [r][c]==1)
cout << "X " ;
else
cout << "O " ;
}
cout<<endl;
}
}
void switchPlayer(int player)
{
playerI(player);
sBoard();
if (player==2)
player=1;
else
player=1;
switchPlayer(player);
}
Apr 13, 2018 at 1:58pm UTC
1 2 3 4 5 6 7 8 9 10 11 12
void switchPlayer(int player)
{
playerI(player);
sBoard();
if (player==2)
player=1;
else
player=1;
switchPlayer(player);
}
This makes no sense.
switchPlayer
function calls
switchPlayer(player);
forever and ever (with player
always set to 1, I note -
never with player set to 2, so it is always player 1 having a turn).
Last edited on Apr 13, 2018 at 2:00pm UTC
Topic archived. No new replies allowed.