TicTacToeTrouble

Hi, I am a complete beginner trying to create a tic tac toe game. My code compiled and ran; however, after prompting the user to indicate a slot for their letter, my code immediately ended. I was wonder if anyone could help me fix it? Also for else (PlayerChoice=2), my compiler prompted me to use a semicolon, but I am unsure as to why. Thanks!
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
# include <iostream>
using namespace std;

char board[3][3]={'1', '2', '3', '4', '5', '6', '7', '8', '9'};

int main()
{
        cout<<"Welcome to Tic-Tac-Toe!\n";
        int PlayerChoice;
        int player(1);
        bool GameFinish(true);
        cout<<"Would you like to play Tic-Tac-Toe?"<<" If yes, press 1. If no, press 2.\n";
        cin>>PlayerChoice;

        if (PlayerChoice == 1){
                do {
                        int player(1);
                        bool GameFinish(true);

                        cout<<"| "<<board[0][0]<<" | "<<board[0][1]<<" | "<<board[0][2]<<" |\n";
                        cout<<"|___|___|___|\n";
                        cout<<"| "<<board[1][0]<<" | "<<board[1][1]<<" | "<<board[1][2]<<" |\n";
                        cout<<"|___|___|___|\n";
                        cout<<"| "<<board[2][0]<<" | "<<board[2][1]<<" | "<<board[2][2]<<" |\n";
                        cout<<"|___|___|___|\n";

                        bool validturn;

                        do{

                                char playerMove;
                                char playerLetter = 'X';

                                cout<<"It's Player "<<player<<"'s turn. Press any number on the board.\n";
                                cin>>playerMove;

                                validturn= true;

                                //Create an if-else to determine if the move is valid
                                if(playerMove == '1' && board[0][0] == '1'){
                                        board[0][0]= playerLetter;
                                }
                                else if (playerMove == '2' && board[0][1] == '2'){
                                        board[0][1]= playerLetter;
                                }
                                else if (playerMove == '3' && board[0][2] == '3'){
                                        board[0][2]= playerLetter;
  }
                                else if (playerMove == '4' && board[1][0] == '4'){
                                        board[1][0]= playerLetter;
                                }
                                else if(playerMove == '5' && board[1][1] == '5'){
                                        board[1][1]= playerLetter;
                                }
                                else if(playerMove == '6' && board[1][2] == '6'){
                                        board[1][2]= playerLetter;
                                }
                                else if(playerMove == '7' && board[2][0] == '7'){
                                        board[2][0]= playerLetter;
                                }
                                else if(playerMove == '8' && board[2][1] == '8'){
                                        board[2][1]= playerLetter;
                                }
                                else if(playerMove == '9' && board[2][2] == '9'){
                                        board[2][2]= playerLetter;
                                }
                                else{
                                        cout<<"Invalid Move, Try Again."<<endl;
                                        validturn=false;
                                }


                                if(playerLetter == 'X'){
                                        playerLetter == 'O';
                                }
                                else
                                        playerLetter == 'X';

                        }while(!validturn);

                        GameFinish= false;
                        bool WinGame= true;

                        //player x
                        //If player x wins with rows

                        if(board[0][0]== 'X' && board[0][1]== 'X' && board[0][2]== 'X')
                                GameFinish=true;
                        if(board[1][0]== 'X' && board[1][1]== 'X' && board[1][2]== 'X')
                                GameFinish=true;
                        if(board[2][0]== 'X' && board[2][1]== 'X' && board[2][2]== 'X')
                                GameFinish=true;
                        //if player x wins with columns
 if(board[0][0]== 'X' && board[1][0]== 'X' && board[2][0]== 'X')
                                GameFinish=true;
                        if(board[0][1]== 'X' && board[1][1]== 'X' && board[2][1]== 'X')
                                GameFinish=true;
                        if(board[0][2]== 'X' && board[1][2]== 'X' && board[2][2]== 'X')
                                GameFinish=true;
                        //if player x wins diagonally
                        if(board[0][0]== 'X' && board[1][1]== 'X' && board[2][2]== 'X')
                                GameFinish=true;
                        if(board[0][2]== 'X' && board[1][1]== 'X' && board[2][0]== 'X')
                                GameFinish=true;

                        //player o
                        //If player o wins with rows

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

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

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

                        //If player o wins with columns
                        if(board[0][0]== 'O' && board[1][0]== 'O' && board[2][0]== 'O')
                                GameFinish=true;

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

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

                        //if player o wins diagonally
                        if(board[0][0]== 'O' && board[1][1]== 'O' && board[2][2]== 'O')
                                GameFinish=true;
                        if(board[0][2]== 'O' && board[1][1]== 'O' && board[2][0]== 'O')
                                GameFinish=true;

                        //In the event of  tie
                        if(board[0][0] != '1' && board[0][1] != '2' && board[0][2] != '3' && board[1][0] != '4' && board[1][1] != '5' && board[1][2] != '6' && board[2][0] != '7' &&
                        board[2][1] != '8' && board[2][2] !='9' && !GameFinish){
                                GameFinish= true;
                                WinGame=false;
                                cout<<"It's a DRAW!"<<endl;
                        }
                        else { 
                                if(player == 1)
                                        player=2;
                                else
                                        player=1;
                        }

                } while(!GameFinish);
        }

        else(PlayerChoice == 2);{
                cout<<"Ok, See you next time!"<<endl;
        }
return 0;
}



Last edited on
When you press the new topic button paste your code between the code blocks as shown.

sorry I'm new here, I revised it. Would anyone know how to fix my problem? or where I'm going wrong?
The problem doesn't jump out at me and but hopefully someone will have time to figure it out.
Of course being a programmer means finding bugs are part of the process although if you go about it the right way bugs are less likely.
One way is to keep it modular.

This is my effort although the squares are numbered 0 to 8 and I haven't used your good idea of numbering the actual display output. Ideally you would select a square with the mouse but unfortunately that isn't possible with a simple console window.

Note I used player = 1 and opponent = -1 This can make code simpler if you ever go on to write a checker player or even chess player program.

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
#include <iostream>

using namespace std;

// global variables
int b[9];
int player = 1;   // swap players with player = -player

// test for win position
// 3 horizontal wins
int TestWin(int t)
{
  int win;
  win = 0;
  if (b[0]== t && b[1]== t && b[2]== t) {
    win = t;
  }
  if (b[3]== t && b[4]== t && b[5]== t) {
    win = t;
  }
  if (b[6]== t && b[7]== t && b[8]== t) {
    win = t;
  }
  // 3 vertical wins
  if (b[0]== t && b[3]== t && b[6]== t) {
    win = t;
  }
  if (b[1]== t && b[4]== t && b[7]== t) {
    win = t;
  }
  if (b[2]== t && b[5]== t && b[8]== t) {
    win = t;
  }
  // cross wins
  if (b[0]== t && b[4]== t && b[8]== t) {
    win = t;
  }
  if (b[2]== t && b[4]== t && b[6]== t) {
    win = t;
  }
  return win;
}

void InitializeBoard()
{
    for (int j = 0; j<9; j++)
    {
        b[j] = 0;
    }
}


// display board
void DisplayBoard()
{
     int k;
     k = 0;
     cout << "+---+---+---+" << endl;
     for (int j = 0; j<3; j++)
     {
         cout  << "|";
         for (int i = 0; i<3; i++)
         {
             if (b[k] == 0)
                cout  << "   |";
             if (b[k] == 1)
                cout  << " x |";
             if (b[k] == 2)
                cout  << " o |";
             k++;
         }
         cout << endl;
         cout  << "+---+---+---+" << endl;
     }
     cout << endl;
}

int getBoardState()
{
    int c = 1;
    int n = 0;
    for (int j = 0; j<9; j++)
    {
       if (b[j] == 1)
          n = n + c;
       if (b[j] == 2)
          n = n + c*2;
       c = c * 10;
     }
     return n;
}


void MakeAMove(int player)
{
    int choice = 0;
        cout << "Choose a square ";
        cin  >> choice;
        while (b[choice] != 0)
        {
            cout << "invalid choice try again: ";
            cin  >> choice;
        }
        if (player == 1)
            b[choice] = 1;
        else
            b[choice] = 2;
}

int PlayAGame()
{
    int gamesCount = 0;
    int result = 0;   // 1 = x win , 2 = y win and 3 = draw

    InitializeBoard();

    DisplayBoard();

    while (result == 0)
    {
      gamesCount = gamesCount + 1;

      MakeAMove(player);
      result = TestWin(1);
      DisplayBoard();

      if (result != 1  && gamesCount < 5)
      {
        MakeAMove(-player);
        result = TestWin(2);
        DisplayBoard();
      }

      if (result == 0 && gamesCount == 5)
      {
        result = 3;
      }

      cout << getBoardState() << endl;

    } // end while

    return result;
}

int main(int argc, char *argv[])
{
    int result;
    char reply;
    do
    {
        result = PlayAGame();
        // print result
        if (result == 1)
        {
            cout  << "x won" << endl;
        }
        else if (result == 2)
        {
            cout  << "o won" << endl;
        }
        else if (result == 3)
        {
            cout  << "DRAW" << endl;
        }

        cout << "Do you want to continue? y/n ";
        cin  >> reply;

    }while (reply == 'y');

return 0;
}

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
# include <iostream>
using namespace std;

char board[3][3] = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };

//////////////////////  *** added (moved) *** //////////////////////////////////////////////
void print_board()
{
	cout << "| " << board[0][0] << " | " << board[0][1] << " | " << board[0][2] << " |\n";
	cout << "|___|___|___|\n";
	cout << "| " << board[1][0] << " | " << board[1][1] << " | " << board[1][2] << " |\n";
	cout << "|___|___|___|\n";
	cout << "| " << board[2][0] << " | " << board[2][1] << " | " << board[2][2] << " |\n";
	cout << "|___|___|___|\n";
}
/////////////////////////////////////////////////////////////////////////////////////

//////////////////////  *** added (moved) *** //////////////////////////////////////////////
bool game_finish()
{
	bool GameFinish = false;
	bool WinGame = true;

	//player x
	//If player x wins with rows

	if (board[0][0] == 'X' && board[0][1] == 'X' && board[0][2] == 'X')
		GameFinish = true;
	if (board[1][0] == 'X' && board[1][1] == 'X' && board[1][2] == 'X')
		GameFinish = true;
	if (board[2][0] == 'X' && board[2][1] == 'X' && board[2][2] == 'X')
		GameFinish = true;
	//if player x wins with columns
	if (board[0][0] == 'X' && board[1][0] == 'X' && board[2][0] == 'X')
		GameFinish = true;
	if (board[0][1] == 'X' && board[1][1] == 'X' && board[2][1] == 'X')
		GameFinish = true;
	if (board[0][2] == 'X' && board[1][2] == 'X' && board[2][2] == 'X')
		GameFinish = true;
	//if player x wins diagonally
	if (board[0][0] == 'X' && board[1][1] == 'X' && board[2][2] == 'X')
		GameFinish = true;
	if (board[0][2] == 'X' && board[1][1] == 'X' && board[2][0] == 'X')
		GameFinish = true;

	if (GameFinish) {
		std::cout << "player 1 ('X') has won\n";
		return true;
	}

	//player o
	//If player o wins with rows

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

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

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

	//If player o wins with columns
	if (board[0][0] == 'O' && board[1][0] == 'O' && board[2][0] == 'O')
		GameFinish = true;

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

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

	//if player o wins diagonally
	if (board[0][0] == 'O' && board[1][1] == 'O' && board[2][2] == 'O')
		GameFinish = true;
	if (board[0][2] == 'O' && board[1][1] == 'O' && board[2][0] == 'O')
		GameFinish = true;
	if (GameFinish) {
		std::cout << "player 2 ('O') has won\n";
		return true;
	}

	//In the event of  tie
	if (board[0][0] != '1' && board[0][1] != '2' && board[0][2] != '3' && board[1][0] != '4' && board[1][1] != '5' && board[1][2] != '6' && board[2][0] != '7' &&
		board[2][1] != '8' && board[2][2] != '9' && !GameFinish) {
		GameFinish = true;
		WinGame = false;
		cout << "It's a DRAW!" << endl;
		return true;
	}

	return false;
}
////////////////////////////////////////////////////////////////////////////////////

int main()
{
	cout << "Welcome to Tic-Tac-Toe!\n";
	int PlayerChoice;
	int player(1);
	bool GameFinish(true);
	cout << "Would you like to play Tic-Tac-Toe?" << " If yes, press 1. If no, press 2.\n";
	cin >> PlayerChoice;

	if (PlayerChoice == 1) {
		// do { // *************
			int player(1);
			bool GameFinish(true);

			/*
			cout << "| " << board[0][0] << " | " << board[0][1] << " | " << board[0][2] << " |\n";
			cout << "|___|___|___|\n";
			cout << "| " << board[1][0] << " | " << board[1][1] << " | " << board[1][2] << " |\n";
			cout << "|___|___|___|\n";
			cout << "| " << board[2][0] << " | " << board[2][1] << " | " << board[2][2] << " |\n";
			cout << "|___|___|___|\n";
			*/

			bool validturn;

			char playerLetter = 'X'; // **** added ****

			do {

				char playerMove;
				// char playerLetter = 'X';  // **** removed ******

				print_board(); // **** added ****

				cout << "It's Player " << player << "'s turn. Press any number on the board.\n";
				cin >> playerMove;

				validturn = true;

				//Create an if-else to determine if the move is valid
				if (playerMove == '1' && board[0][0] == '1') {
					board[0][0] = playerLetter;
				}
				else if (playerMove == '2' && board[0][1] == '2') {
					board[0][1] = playerLetter;
				}
				else if (playerMove == '3' && board[0][2] == '3') {
					board[0][2] = playerLetter;
				}
				else if (playerMove == '4' && board[1][0] == '4') {
					board[1][0] = playerLetter;
				}
				else if (playerMove == '5' && board[1][1] == '5') {
					board[1][1] = playerLetter;
				}
				else if (playerMove == '6' && board[1][2] == '6') {
					board[1][2] = playerLetter;
				}
				else if (playerMove == '7' && board[2][0] == '7') {
					board[2][0] = playerLetter;
				}
				else if (playerMove == '8' && board[2][1] == '8') {
					board[2][1] = playerLetter;
				}
				else if (playerMove == '9' && board[2][2] == '9') {
					board[2][2] = playerLetter;
				}
				else {
					cout << "Invalid Move, Try Again." << endl;
					validturn = false;
				}


				if (playerLetter == 'X') {
					playerLetter = 'O'; // **** modified = insted of == *****
					player = 2;  // **** added *****
				}
				else {
					playerLetter = 'X'; // **** modified = insted of == *****
					player = 1; // **** added *****
				}

			} while (validturn && !game_finish()); // ******
		// } // ***************

			/*/////////////////////////  **** moved **** ///////////////////////////////
			GameFinish = false;
			bool WinGame = true;

			//player x
			//If player x wins with rows

			if (board[0][0] == 'X' && board[0][1] == 'X' && board[0][2] == 'X')
				GameFinish = true;
			if (board[1][0] == 'X' && board[1][1] == 'X' && board[1][2] == 'X')
				GameFinish = true;
			if (board[2][0] == 'X' && board[2][1] == 'X' && board[2][2] == 'X')
				GameFinish = true;
			//if player x wins with columns
			if (board[0][0] == 'X' && board[1][0] == 'X' && board[2][0] == 'X')
				GameFinish = true;
			if (board[0][1] == 'X' && board[1][1] == 'X' && board[2][1] == 'X')
				GameFinish = true;
			if (board[0][2] == 'X' && board[1][2] == 'X' && board[2][2] == 'X')
				GameFinish = true;
			//if player x wins diagonally
			if (board[0][0] == 'X' && board[1][1] == 'X' && board[2][2] == 'X')
				GameFinish = true;
			if (board[0][2] == 'X' && board[1][1] == 'X' && board[2][0] == 'X')
				GameFinish = true;

			//player o
			//If player o wins with rows

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

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

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

			//If player o wins with columns
			if (board[0][0] == 'O' && board[1][0] == 'O' && board[2][0] == 'O')
				GameFinish = true;

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

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

			//if player o wins diagonally
			if (board[0][0] == 'O' && board[1][1] == 'O' && board[2][2] == 'O')
				GameFinish = true;
			if (board[0][2] == 'O' && board[1][1] == 'O' && board[2][0] == 'O')
				GameFinish = true;

			//In the event of  tie
			if (board[0][0] != '1' && board[0][1] != '2' && board[0][2] != '3' && board[1][0] != '4' && board[1][1] != '5' && board[1][2] != '6' && board[2][0] != '7' &&
				board[2][1] != '8' && board[2][2] != '9' && !GameFinish) {
				GameFinish = true;
				WinGame = false;
				cout << "It's a DRAW!" << endl;
			}
			else {
				if (player == 1)
					player = 2;
				else
					player = 1;
			}

		} while (!GameFinish);
	}
	///////////////////////////////////////////////////////////////////////////*/
	}

	/* else(PlayerChoice == 2); */{
		cout << "Ok, See you next time!" << endl;
	}
	return 0;
}
Last edited on
Topic archived. No new replies allowed.