Tic Tac Toe

I need help Tic tac toe....

When i make it a Tie it does not display a Tie it just continues...

and Checking Diagonal from Right to left it does not count as a win it just continues playing can someone help me please

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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282

#include<iostream>
#include<iomanip>

using namespace std;

void drawBoard(char board[][3]);
char checkWinner3by3(char board[][3]);

//
// The main funciton is provided for you.
//
// DO NOT MODIFY THE MAIN FUNCTION
//
int main()
{
    // This array of chars represents the game board, and it holds the content
    // of each space. By default all spaces are set to a blank space.
    char board[3][3] = {{' ',' ',' '},{' ',' ',' '},{' ',' ',' '}};
    
    // The current player. Because X plays first, initialize to X
    char player = 'X';
	
    // The winner. either 'X', 'O', or 't' if it's a tie.
    // Or a blank space if the game has not finished.
    char winner = ' ';
	
    // These variables will hold the number of the row and column selected
    // by the players.
    int row;
    int column;
	
    // boolean variables used to verify if the move is valid.
    bool is_move;
    bool is_row;
    bool is_column;

    cout<<"************ TIC TAC TOE ************\n";

    // The game loops again and again until the game is over
    while(winner == ' ')
    {
		//set the boolean variables back to false for a new turn.
        is_move = false; 
        is_row = false;
        is_column = false;
		
        //draw the board.
        drawBoard(board);
				
        // If the game is not yet over show who`s the next player to move
        cout << "Player ";
        if(player == 'X')
        {
            cout << 1;
        }
        else
        {
            cout << 2;
        }
        cout << "'s turn:" << endl;

        // Loop until the player selects a valid space for their move
        is_move = false;
        while(!is_move)
        {  
            // Loop until the player selects a valid row
            // Assume the user inputs a valid integer
            is_row = false;
            while(!is_row)
            {
                cout << "Row: ";
                cin >> row;
                if(row == 1 || row == 2 || row == 3)
                {
                    is_row = true;
                }
                else
                {
                    cout << endl << "Invalid row!" << endl;
                }
            } // end of input row loop
            
            // Loop until the player selects a valid column
            // Assume the user inputs a valid integer
            is_column = false;
            while(!is_column)
            {
                cout << "Column: ";
                cin >> column;
                if(column == 1 || column == 2 || column == 3)
                {
                    is_column = true;
                }
                else
                {
                    cout << endl << "Invalid column!" << endl;
                }
            } // end of input column loop
			
            // If the space is empty, mark the chosen space and swich players
            if(board[row-1][column-1] == ' ')
            {
                // Mark the space and end the player's turn
                board[row-1][column-1] = player;
                is_move = true;
                
                // Switch to the other player
                if(player == 'X')
                {
                    player = 'O';
                }
                else
                {
                    player = 'X';
                }
            } // end of marking space and changing players
            
            // If the space is occupied
            else
            {
                cout<<"The selected space is occupied!" << endl;
                cout << "Select a different space:" << endl << endl;
                drawBoard(board);
            }
        } // end of player's move loop
        
        cout << endl; // for nice output formatting
        
        //check if the player won.
        winner = checkWinner3by3(board);
		
        //If there's a winner
        if(winner == 'X' || winner == 'O')
        {
            drawBoard(board);
            
            //Display congratulations message
            cout<<"Congratulations! Player ";
            if(winner == 'X')
            {
                cout << 1;
            }
            else
            {
                cout << 2;
            }
            cout<<" is the winner!"<<endl;
        }
        else if(winner == 'T')
        {
            drawBoard(board);
            
            //Display a message if it`s tie
            cout << "It's a tie!" << endl;
        }
        
    } // End of player's turn loop
	cout << endl;
    system("pause");
    return 0;
}


//
// Prints the game board
// We know the board is 3 by 3 so we don't need to have the number of rows as
// a parameter.
//
// WRITE THIS FUNCTION
//
void drawBoard(char board[][3])
{
	cout << "    1   2   3   " << endl;
	for(int i = 0; i < 3; i++)
	{
		
		cout << "  +---+---+---+";
		cout << endl;
		cout << i + 1;
		for(int j = 0; j < 3; j++)
		{
			
			cout << " | " << board[i][j];
		}
		cout << " | ";
		cout << endl;
	}
	cout << "  +---+---+---+";
	cout << endl;
}

//
// Checks the whole board if there is a winner.
// We know the board is 3 by 3 so we don't need to have the number of rows as
// a parameter.
//
// WRITE THIS FUNCTION
//
char checkWinner3by3(char board[][3])
{
    // Check all rows

		//Checking For 'X'
			//Rows
		if(board[0][0] == 'X' && board[0][1] == 'X' && board[0][2] == 'X')
			return 'X';

		if(board[1][0] == 'X' && board[1][1] == 'X' && board[1][2] == 'X')
			return 'X';

		if(board[2][0] == 'X' && board[2][1] == 'X' && board[2][2] == 'X')
			return 'X';

		//Checking For 'O'
			//Rows
		if(board[0][0] == 'O' && board[0][1] == 'O' && board[0][2] == 'O')
			return 'O';

		if(board[1][0] == 'O' && board[1][1] == 'O' && board[1][2] == 'O')
			return 'O';

		if(board[2][0] == 'O' && board[2][1] == 'O' && board[2][2] == 'O')
			return 'O';
	
    
    
    // Check all columns
		//Checking For 'X'
			//Columns
		if(board[0][0] == 'X' && board[1][0] == 'X' && board[2][0] == 'X')
			return 'X';

		if(board[0][1] == 'X' && board[1][1] == 'X' && board[2][1] == 'X')
			return 'X';

		if(board[0][2] == 'X' && board[1][2] == 'X' && board[2][2] == 'X')
			return 'X';
		//Checking For 'O'
			//Columns
		if(board[0][0] == 'O' && board[1][0] == 'O' && board[2][0] == 'O')
			return 'O';

		if(board[0][1] == 'O' && board[1][1] == 'O' && board[2][1] == 'O')
			return 'O';

		if(board[0][2] == 'O' && board[1][2] == 'O' && board[2][2] == 'O')
			return 'O';
    
    
    // Check diagional from upper left to lower right
		//Checking for 'X'
			//Diagonal Left --> Right
		if(board[0][0] == 'X' && board[1][1] == 'X' && board[2][2] == 'X')
			return 'X';
		//Checkin for 'O'
			//Diagonal Left --> Right
		if(board[0][0] == 'O' && board[1][1] == 'O' && board[2][2] == 'O')
			return 'O';
    
    
    // Check diagonal from upper right to lower left
		//Checking For 'X'
			//Diagonal Right --> Left
		if (board[2][0] == 'X' && board[1][1] == 'X' && board[0][2] == 'X')
        return 'X';

		//Checking For 'O'
			//Diagonal Right --> Left

		if(board[0][2] == 'O' && board[1][1] == 'O' && board[2][0] == 'O')
			return 'O';

    
    
    // Otherwise, if all spaces are filled, the game is a tie
		else
			return 'T';
    
    // If none of the conditions above are true, return a blank space

}
I just made some modification to your code to get it working
and obviously, it is not the best way, but it might gives you another perspective:

I haven't tested all the possible ending, and I have formatted the changed codes in bold.

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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#include<iostream>
#include<iomanip>

using namespace std;

void drawBoard(char board[][3]);
char checkWinner3by3(char board[][3], char player, int lastRow, int lastColumn);

//
// The main funciton is provided for you.
//
// DO NOT MODIFY THE MAIN FUNCTION
//
int main()
{
    // This array of chars represents the game board, and it holds the content
    // of each space. By default all spaces are set to a blank space.
    char board[3][3] = {{' ',' ',' '},{' ',' ',' '},{' ',' ',' '}};

   int steps = 3 * 3; // to check if the game is tie
    
    // The current player. Because X plays first, initialize to X
    char player = 'X';
	
    // The winner. either 'X', 'O', or 't' if it's a tie.
    // Or a blank space if the game has not finished.
    char winner = ' ';
	
    // These variables will hold the number of the row and column selected
    // by the players.
    int row;
    int column;
	
    // boolean variables used to verify if the move is valid.
    bool is_move;
    bool is_row;
    bool is_column;

    cout<<"************ TIC TAC TOE ************\n";

    // The game loops again and again until the game is over
    while(winner == ' ')
    {
		//set the boolean variables back to false for a new turn.
        is_move = false; 
        is_row = false;
        is_column = false;
		
        //draw the board.
        drawBoard(board);
				
        // If the game is not yet over show who`s the next player to move
        cout << "Player ";
        if(player == 'X')
        {
            cout << 1;
        }
        else
        {
            cout << 2;
        }
        cout << "'s turn:" << endl;

        // Loop until the player selects a valid space for their move
        is_move = false;
        while(!is_move)
        {  
            // Loop until the player selects a valid row
            // Assume the user inputs a valid integer
            is_row = false;
            while(!is_row)
            {
                cout << "Row: ";
                cin >> row;
                if(row == 1 || row == 2 || row == 3)
                {
                    is_row = true;
                }
                else
                {
                    cout << endl << "Invalid row!" << endl;
                }
            } // end of input row loop
            
            // Loop until the player selects a valid column
            // Assume the user inputs a valid integer
            is_column = false;
            while(!is_column)
            {
                cout << "Column: ";
                cin >> column;
                if(column == 1 || column == 2 || column == 3)
                {
                    is_column = true;
                }
                else
                {
                    cout << endl << "Invalid column!" << endl;
                }
            } // end of input column loop
			
            // If the space is empty, mark the chosen space and swich players
            if(board[row-1][column-1] == ' ')
            {
                // Mark the space and end the player's turn
                board[row-1][column-1] = player;
                is_move = true;
                --steps;

                // check if the player won.
        	winner = checkWinner3by3(board, player, row - 1, column - 1);

        	// check if steps are out and still no winner, game is tie
        	if(steps == 0 && winner == ' '){
        		winner = 'T';
        	}

                // Switch to the other player
                if(player == 'X')
                {
                    player = 'O';
                }
                else
                {
                    player = 'X';
                }
            } // end of marking space and changing players
            
            // If the space is occupied
            else
            {
                cout<<"The selected space is occupied!" << endl;
                cout << "Select a different space:" << endl << endl;
                drawBoard(board);
            }
        } // end of player's move loop
        
        cout << endl; // for nice output formatting
		
        //If there's a winner
        if(winner == 'X' || winner == 'O')
        {
            drawBoard(board);
            
            //Display congratulations message
            cout<<"Congratulations! Player ";
            if(winner == 'X')
            {
                cout << 1;
            }
            else
            {
                cout << 2;
            }
            cout<<" is the winner!"<<endl;
        }
        else if(winner == 'T')
        {
            drawBoard(board);
            
            //Display a message if it`s tie
            cout << "It's a tie!" << endl;
        }
        
    } // End of player's turn loop
	cout << endl;
    cin.get();
    return 0;
}

//
// Prints the game board
// We know the board is 3 by 3 so we don't need to have the number of rows as
// a parameter.
//
// WRITE THIS FUNCTION
//
void drawBoard(char board[][3])
{
	cout << "    1   2   3   " << endl;
	for(int i = 0; i < 3; i++)
	{
		
		cout << "  +---+---+---+";
		cout << endl;
		cout << i + 1;
		for(int j = 0; j < 3; j++)
		{
			
			cout << " | " << board[i][j];
		}
		cout << " | ";
		cout << endl;
	}
	cout << "  +---+---+---+";
	cout << endl;
}

//
// Checks the whole board if there is a winner.
// We know the board is 3 by 3 so we don't need to have the number of rows as
// a parameter.
//
// WRITE THIS FUNCTION
//
char checkWinner3by3(char board[][3], char player, int lastRow, int lastColumn)
{
	// check for both the diaglonals
	if(lastRow == lastColumn){
		for(int a = 0; a < 3; a++){
			if(board[a][a] != player)
				break;
			if(a == 2)
				return player;
		}
	}else if(lastRow + lastColumn == 2){
		for(int a = 0; a < 3; a++){
			if(board[a][2 - a] != player)
				break;
			if(a == 2)
				return player;
		}
	}

	// check for rows
	for(int a = 0; a < 3; a++){
		if(board[lastRow][a] != player)
			break;
		if(a == 2)
			return player;
	}

	// check for columns
	for(int a = 0; a < 3; a++){
		if(board[a][lastColumn] != player)
			break;
		if(a == 2)
			return player;
	}

	// check for tie
	// you may determine TIE here (pass the steps variable here)
	// or do it in the main function like I did

	// no winner yet
	return ' ';
}
Last edited on
Well it works the way it should... It did opened door of what i should do Thanks although i still dont know why my diagonal Right to left is not working.... and my Left to right is...

Thanks again i made a lil more modifications

Topic archived. No new replies allowed.