Tic Tac Toe program, if statement to change 2d array contents

For a class project we have to create a tic tac toe game using c++. I have wrote a program that works, but have time before it is due so was thinking of making it better.

Right now, as the program is written, the user must input coordinates of the array to mark a spot. I would like to instead have them input a letter (much easier for the user) and have the program determine the corresponding coordinates.

The first code listed below is the current working code that requires the column and row input.

The second code is my attempt at having the user input a letter to mark their spot.


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




//Function Prototypes

void description();
void menu();
void instruction();
void game();





int main()
{

	 
	 
    //Calling description function.
    description();


    //Calling menu function.
    menu();

	 

    return 0;

}



void description()
{
    cout << "-------------------------------------------------------\n"
         << "\tThis program is a game of Tic Tac Toe!\n\n"
         << "It requires two players. Each player will specify a row\n"
         << "and a column to place either X or O. Enjoy!\n"
         << "-------------------------------------------------------\n"
         << endl << endl << endl;
}



void menu()
{
    int choice;

    cout << "\t\t MENU\n\n"
         << "\t1. Instruction\n"
         << "\t2. Play\n"
         << "\t3. Exit\n\n"
         << "Make a selection by typing the number corresponding to your"
         << " choice\n\n";

    cin >> choice;
    cout << "\n\n\n";


    while(choice < 1 || choice > 3)
    {
        cout << "Please enter a valid choice number!\n";
        cin >> choice;
    }

    if(choice == 1)
    {
        instruction();
    }

    else if(choice == 2)
    {
        game();
    }
	 
	 else
	 {
	 	exit(0);
	 }

}





void instruction()
{
    cout << "This is instruction\n\n\n";


    menu();
}





void game()
{
	 char player1;
	 char player2;
	 
	 cout << "Player 1, please choose X or O: ";
	 cin >> player1;
	 
	 if(player1 == 'O' || player1 == 'o')
	 {
	 	 cout << "Player 2, you are X.\n\n";
		 player2 = 'X';
		 player1 = 'O';
	 }
	 
	 else if(player1 == 'X' || player1 == 'x')
	 {
	 	 cout << "Player 2, you are O.\n\n";
		 player2 = 'O';
		 player1 = 'X';
	 }
	 
	 else 
	 {
	 	 cout << "Please type a valid choice.\n";
		 game();
	 }



    char game[3][3] = {{'_', '_', '_'}, {'_', '_', '_'}, {'_', '_', '_'}};



    for(int i = 0; i < 9; i++)
    {




        int row;
        int column;

        cout << game[0][0] << "  " << game[0][1] << "  " << game[0][2] << "\n\n"
             << game[1][0] << "  " << game[1][1] << "  " << game[1][2] << "\n\n"
             << game[2][0] << "  " << game[2][1] << "  " << game[2][2] << "\n\n";





        if(i == 0 || i == 2 || i == 4 || i == 6 || i == 8)
        {

				int x;
				
            cout << "Player 1, please enter the row: ";
            cin >> row;


            cout << "Now, enter the colomn: ";
            cin >> column;
            cout << endl;
				
				while(game[row][column] != '_')
            {
					cout << "Please enter an unused coordinate: \n";
					cout << "Enter the row: ";
           	   cin >> row;
					
           	   cout << "Enter the colomn: ";
            	cin >> column;
           		cout << endl;
				}



            game[row][column] = player1;
				
				
				if(game[0][0] == player1 && game[0][1] == player1 && game[0][2] == player1 || 
					game[1][0] == player1 && game[1][1] == player1 && game[1][2] == player1 ||
					game[2][0] == player1 && game[2][1] == player1 && game[2][2] == player1 ||
					game[0][0] == player1 && game[1][0] == player1 && game[2][0] == player1 ||
					game[0][1] == player1 && game[1][1] == player1 && game[2][1] == player1 ||
					game[0][2] == player1 && game[1][2] == player1 && game[2][2] == player1 ||
					game[0][0] == player1 && game[1][1] == player1 && game[2][2] == player1 ||
				   game[2][0] == player1 && game[1][1] == player1 && game[0][2] == player1)
					
				{
					cout << game[0][0] << "  " << game[0][1] << "  " << game[0][2] << "\n\n"
             		  << game[1][0] << "  " << game[1][1] << "  " << game[1][2] << "\n\n"
            		  << game[2][0] << "  " << game[2][1] << "  " << game[2][2] << "\n\n";
	
					cout << "Player 1 wins !! \n\n\n";
					
					menu();
					
				}
				
				
        	}


        	else
        	{

				int y;

            cout << "Player 2, please enter the row: ";
            cin >> row;


            cout << "Now, enter the colomn: ";
            cin >> column;
            cout << endl;

				while(game[row][column] != '_')
            {
					cout << "Please enter an unused coordinate: \n";
					cout << "Enter the row: ";
           	   cin >> row;
					
           	   cout << "Enter the colomn: ";
            	cin >> column;
           		cout << endl;
				}
				
				game[row][column] = player2;
				
				if(game[0][0] == player2 && game[0][1] == player2 && game[0][2] == player2 ||
					game[1][0] == player2 && game[1][1] == player2 && game[1][2] == player2 ||
					game[2][0] == player2 && game[2][1] == player2 && game[2][2] == player2 ||
					game[0][0] == player2 && game[1][0] == player2 && game[2][0] == player2 ||
					game[0][1] == player2 && game[1][1] == player2 && game[2][1] == player2 ||
					game[0][2] == player2 && game[1][2] == player2 && game[2][2] == player2 ||
					game[0][0] == player2 && game[1][1] == player2 && game[2][2] == player2 ||
					game[2][0] == player2 && game[1][1] == player2 && game[0][2] == player2)
					
				{
					cout << game[0][0] << "  " << game[0][1] << "  " << game[0][2] << "\n\n"
             		  << game[1][0] << "  " << game[1][1] << "  " << game[1][2] << "\n\n"
            		  << game[2][0] << "  " << game[2][1] << "  " << game[2][2] << "\n\n";


					
					cout << "Player 2 wins !! \n\n\n";
					menu();
					
				}

        }
	

    }

	 cout << "There is no winner!\n\n\n";

    menu();
}


I wanted to change:
char game[3][3] = {{'_', '_', '_'}, {'_', '_', '_'}, {'_', '_', '_'}};
to
char game[3][3] = {{'A', 'B', 'C'}, {'D', 'E', 'F'}, {'G', 'H', 'I'}};

then I need to have the letters correspond to a specific column and row so I wanted to change lines 156- 178 to

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
if(i == 0 || i == 2 || i == 4 || i == 6 || i == 8)
        {

				int x, letter, a, A;
				
            cout << "Player 1, please enter letter: ";
            cin >> letter;
				
				if (letter == A)
				{
					int row = 0;
					int column = 0;
				}


				
				
				while(game[row][column] != 'A')            
				{
					cout << "Please enter an unused letter: \n";
           	   cin >> letter;
					
					if (letter == A)
				{
					int row = 0;
					int column = 0;
				}

				}


I would then have to change the code for player 2 also.

Unfortunately my program compiles fine, but when I input "A" the program either crashes or loops forever telling to to input an unused letter.

Any ideas on how to fix this would be greatly appreciated.

Thank you,
Colby
On line 9, in your if statement, you need to change A to 'A' for the right comparison. Same thing for line 23.
closed account (o1vk4iN6)
Why not make it object oriented ?
You can still use numbers ( but not coords) for user input.

if the board looks like this:

123
456
789

you can use integer division and remainder to calc the row & col. Subtract 1 from what they enter, integer division by 3 gives the row, the remainder is the col. This is easier than using chars I think.

To check for a win, you check the rows, cols and diagonals. This is easily done with nested for loops - for example to check one of the diagonals, check 0,0 1,1 2,2

I would have a CheckWin function, with code separated into check rows, check cols, check diagonals. This would cut down your code because lines 185 to 200 is very similar to lines 235 to 251. The same could be achieved with one CheckWin function that takes a player as an argument, and returns a bool.

You could also make use of the std::toupper function to avoid testing variables twice to cover upper or lower case:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <locale>

char player1;

cin >> player1;

player1 = std::toupper(player1);

if (player1 == 'O') {
     //your code
}
else if (player1 == 'X' ) {
     //your code
}
else {
     //your code
}



Also note that when the variable is an integral type ( including chars), you can use a switch statement instead of a load of else if clauses.


You need to make more use of functions - the game function has 160 lines which is too much.

Hope all goes well.
Topic archived. No new replies allowed.