Tic Tac Toe Problem

#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;

void GetMove(char [][3],int);

void DisplayBoard(char board[][3],int i,int j){
//char board[3][3]
cout << "|_" << board[0][0] << "_|_" << board[0][1] << "_|_" << board[0][2] << "_|" << endl;
cout << "|_" << board[1][0] << "_|_" << board[1][1] << "_|_" << board[1][2] << "_|" << endl;
cout << "|_" << board[2][0] << "_|_" << board[2][1] << "_|_" << board[2][2] << "_|" << endl;

}


void CheckMove(char board[][3],int i,int j,int player){
//if (player == 0){
while (board[i][j] == 'o' || board[i][j] == 'x'){
cout << "That spot is taken, please enter another move. \n";
GetMove(board,player);
}
//}
if (player == 0){
board[i][j] = 'x';
return;
}
else{
board[i][j] = 'o';
return;
}

}

void GetMove(char move[][3],int player){
int i,j;
cin >> i >> j;
while (i < 0 || i > 2 || j < 0 || j > 2)
cin >> i >> j;
CheckMove(move,i,j,player);
DisplayBoard(move,i,j);
return;

}


string PlayerMove(int turn){
string player1 = "Player1";
string player2 = "Player2";
int counter = turn;
if (counter % 2 == 0){
return player1;
}
else{
return player2;
}
counter++;
}

int main(){
char move[3][3] = {{0}};
bool victory = false;
int player = 0;
char p1 = 'x';
char p2 = 'o';
cout << "Let's play a game of Tic-Tac-Toe" << endl;
cout << "Let's flip a coin to see who goes first!" << endl;
int coinflip = rand() % 2;
string first_turn = PlayerMove(coinflip);
cout << first_turn << " goes first!" << endl;
int i,j;


while (victory != true){
if (player == 0){
cout << "Player 1 turn!" << endl;
//Get coordinate
GetMove(move,player);
player++;
}
else{
// player == 1
cout << "Player 2 turn!" << endl;
//Get coordinate
GetMove(move,player);
player--;
}
}



return 0;
}

This is the very bare basics of the TTT that I could come up with. I'm getting an issue when I play a move on the taken spot. For some reason, everything gets checked, displays the new board, but loops back to the beginning of CheckMove... I tried playing around with return statements in different spots of the code.. Am I missing something?
The problem is that CheckMove(...) and GetMove(...) are calling each other recursively and in a loop.

I would say that you don't need CheckMove(...). Instead do the check in GetMove(...). Like so:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void GetMove(char move[][3],int player){
int i,j;
bool is_failed;
do
{
  is_failed = false;
  cin >> i >> j;
  if(i < 0 || i > 2 || j < 0 || j > 2)
  {
    cout << "Invalid move. \n";
    is_failed = true;
  }
  else if(board[i][j] == 'o' || board[i][j] == 'x')
  {
    cout << "That spot is taken, please enter another move. \n";
    is_failed = true;
  }
}
while(is_failed);
DisplayBoard(move,i,j);
return;

}
Topic archived. No new replies allowed.