tic tac toe game, check is board is full and is position is taken

I have a beginner tic tac toe program to write. For this assignment we do not need to know who the winner is, the game just has to end when the board is filled.

I have two problems:
1. In the get move function, if the player enters a position that is already taken he should be asked to enter a different position, but instead it just overrides the answer.
2. for the checkboard function which checks to see if the board is full keeps giving me an extra move, so 10 instead of 9 moves in the game.

Here is my code below.

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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
 class TicTacToe
{
private:
    char board[3][3];
public:
    TicTacToe();
    void CreateBoard();
    void PrintBoard();
    void GetMove(int i , int j, int move);
    void CheckPosition (int& i, int& j);
    bool CheckBoard ();
    int numberOfmoves();
   
};

int main(int argc, char *argv[]){

    TicTacToe user;
    int row, column, Xs,Os, turns=0;
    user.CreateBoard();
    user.PrintBoard();
    int b= 9;
    while(!user.CheckBoard()){
        for(int index = 0; index <= 0; index++){
            Xs = 1;
            cout << "Player 1: Please enter a cell on the board.\n";
            cin>>row>>column;
            user.CheckPosition(row, column);
            user.GetMove(row, column, 1);
        }
        for(int idx = 0; idx <= 0; idx++){
            Os = 2;
            cout << "Player 2: Please enter a cell on the board.\n";
            cin>>row>>column;
            user.CheckPosition(row, column);
            user.GetMove(row, column, 2);       
        }
      }
    return 0;
}

void TicTacToe::CreateBoard(){
   int size =3;
     for (int row = 0; row < size; row++){        
        for (int column = 0; column < size; column++){       
           board[row][column] = '0'; 
        }
    } 
    cout<<"New board created!\n";
}

TicTacToe::TicTacToe(){
    board[3][3]=0;
}


void TicTacToe::PrintBoard(){
    int size =3;    
     for (int row = 0; row < size; row++){        
        for (int column = 0; column < size; column++){       
            cout <<'\t'<< board[row][column];
    }
    cout << endl;
     }
}


//Gets the move from the user
void TicTacToe::GetMove(int i, int j, int move){
    int size=3;
    int row, column; 
    bool r=false, b;    
    
    if(move==1){
               move='X';
               }
    if(move==2){
               move='O';
    }
    
    while(r=false){   //if the player enters a cell that is taken he should be prompted to enter a new cell, but instead it just keeps over riding the previous move
        for (int row = 0; row < size; row++){        
            for (int column = 0; column < size; column++){
                if(row==i && column==j){                        
                    if(board[row][column]=0){ //this checks to see if the cell is taken
                        r=true; 
                        break;                    
                    }
                }
            }
        }        
        if(r=false){        
            cout<<"This cell is taken. Please enter a new cell.\n";
            cin >>i>>j;
        }
    } 
        if(r=true){
            board[i][j]=move;
        }  
    PrintBoard();   
}

void TicTacToe::CheckPosition(int& i, int& j){
       
   while(i<0 || i>=3 && j<0 || j>=3){
              cout<<"Please enter proper coordinates for a 3x3 grid.\n";
              cin>>i>>j;
    } 
}
//checks to see if board is full and returns a bool
bool TicTacToe::CheckBoard(){//keeps giving me one extra move, so 10 instead of 9.
     bool b;
     int counter;
      for (int row = 0; row < 3; row++){        
            for (int column = 0; column < 3; column++){                       
                    if(board[row][column]=='0'){
                        return false;           
                    }
            }
      }
      return true;  
}

Lines 85, 92, 97 - odd to see an assignment operator = inside an if() statement. . .

Take a look at the logic operators in line 105 - it usually causes problems mixing ors with ands .

lines 81 through 96 or so can be replaced by a single comparison - entered values against the array value, don't need to iterate through all three rows/columns.

Hard to debug with three different locations for entry of a move. . .
Last edited on
I actually figured out the problem if the position is taken (I am going to edit the code).

Now I just need to figure out to end the program after the 9th move when the board is full.

Any ideas?
Can you repost your code?
Topic archived. No new replies allowed.