Tic-Tac-Toe checkers game

Hello there, I'm trying to make a game that is a combination of tic-tac-toe and checkers, looks like this:
(BTW it's not homework, is just a project).

1__2___3
|_/_|_\_|
4---5----6
|_\_|_/_|
7---8---9

(there is already a game like this but i couldn'd fint images of it).

On the first part of the code (I wrote comments on it so it will be easier), is the place where both; Player 1 and 2 get their 3 tokens on the board, yeah just 3, and on part 2 is the part supposed to let the Player 1, for now, move one of the tokens to a free space; but it doesn't work well, I made it with "if" loops but in some cases it does works but on the mayotity makes things that it weren't even supposed to happen.
Example:

x__o___-
|_/_|_\_|
-----o----x
|_\_|_/_|
o---------x

(Player 1 x
Palyer 2 o

-Player 1 tries to move the token on the right up corner to a spot
down, it deletes one of the "o"s and duplicates the "x".

I'll really be thanked if anyone could help me with this. Here's the code.

The code is too long so I can't post it here, thats why I uploaded it here

https://docs.google.com/document/d/1YfHsGb-38YMVhE5Ne2BC-wiAWJMHxeaWgP7e5E13nFA/pub
Last edited on
Tzomby

I made a function to display the board, instead of all those areas you would print it out,

1
2
3
4
5
6
7
8
void Display_Board(char Blanks[9])
{
	cout << Blanks[0] << "___" << Blanks[1] << "___" << Blanks[2] << endl;
	cout << "| / | \\ |" << endl; // Use \\ to display one \
	cout << Blanks[3] << "---" << Blanks[4] << "---" << Blanks[5] << endl;
	cout << "| \\ | / |" << endl; // Use \\ to display one \
	cout << Blanks[6] << "---" << Blanks[7] << "---" << Blanks[8] << endl;
}


And a small for loop to enter the pieces onto the board.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
for (int x = 0; x < 9; x++)
		Blanks[x] = '\x31'+x; //Place 1 thru 9 in Blanks array Hex 31 is '1'
                                                 // x added to 31, so it becomes '1' to '9'
char P[2] = { 'O', 'X' };
bool player = false; // Starts with a 0

for (int x = 0; x < 6; x++)
	{
		do
		{
			cout << "PLayer with " << P[player] << ", what spot do you wanna play? (1-9)" << endl;
			cin >> op;
			if (Blanks[op - 1] < '\x3A' )
				cout << "Thank you. Your checker is being placed.." << endl;
			else
				cout << "Sorry, that space already used!! Pick another" << endl;
		} while (Blanks[op - 1] > '\x39');
		Blanks[op - 1] = P[player];
		Display_Board(Blanks);
		player = !player; //The 0, or false, becomes 1 or true.
                                           //If true, or 1, it becomes a 0 or false. Changed players 
	}


I'm now working on placing the players, and removing them. The way I understand what happens, is you can only move along a connecting line, so you can't go from space 1 to space 5. Can you jump an opponent? Would they be removed? If yes, then must they place it back on the board in any empty space on their turn? I need better instructions on the game play, to better understand the programming, thanks.
Last edited on
Hi, thanks for your help.
Yeah I should have had give more intructions about the game itself but here they are now.

Yes you can only move over those lines and just one space, like 1 to 2 or 4, but if the token of player 2 is there; then you won't be able to move it there.

Since is just moving one space, no, you can't jump another token, neither if its yours or from the opponent.

A token need a space to move if there are no spaces near it, then it won't move.
And you win when you get yours 3 token on a line, just like the normal tic-tac-toe.

And sorry if its to messy but I'm just starting.
@Tzomby

Thanks for the added instructions. So far, I let players move their checker to an empty space, and ONLY if it's a legal move. I do not yet check if it's a winning move. I'll let you try your hand at the checking part. I'll add to it a bit later, like color, etc. Hope this helps a bit..

Good idea for a game, but it might be better ir it was a 5x5 board, and you still try to get three of yours in a row. Still allow only one move, etc.

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
283
284
285
// Tic-Tac-Toe Checkers.cpp : main project file.

#include <iostream>
#include <ctime>
//#include <cstdlib>
#include <string.h>

using namespace std;

void Display_Board(char Blanks[9])
{
	cout << Blanks[0] << "___" << Blanks[1] << "___" << Blanks[2] << endl;  //it display the board but now using the array
	cout << "| / | \\ |" << endl;
	cout << Blanks[3] << "---" << Blanks[4] << "---" << Blanks[5] << endl;
	cout << "| \\ | / |" << endl;
	cout << Blanks[6] << "---" << Blanks[7] << "---" << Blanks[8] << endl;
}

int main()
{
	int E1 = 0;
	int Sh = 0; //This takes the place where the player1 wants his marker to be moved to.
	char P[2] = { 'O', 'X' }; //The tokens of player 1 and 2
	bool Winner = false; 
	bool player = false;
	bool ok = false;
	char Blanks[9];  //An array for the spaces on the board
	for (int x = 0; x < 9; x++)
		Blanks[x] = '\x31' + x;

	Display_Board(Blanks);

	cout << "Do not enter a letter or something different from a number, it will cause the program so crash and to your pc to explode" << '\n' << endl;

	for (int x = 0; x < 6; x++)
	{
		do
		{
			cout << "Player with " << P[player] << ", what spot do you wanna play? (1-9)" << endl;
			cin >> E1;
			if (Blanks[E1 - 1] < '\x3A')
				cout << "Thank you. Your checker is being placed.." << endl;
			else
				cout << "Sorry, that space already used!! Pick anaother" << endl;
		} while (Blanks[E1 - 1] > '\x39');
		Blanks[E1 - 1] = P[player];
		Display_Board(Blanks);
		player = !player;
	}

	do
	{  
		player = !player;

		cout << "Now that all of the markers are on the board, let's start the real game.. " << endl << endl;

		do{
			Display_Board(Blanks);
			
			ok = false;
			do
			{
				cout << "Player " << P[player] << ", your turn. Enter the number of the piece that you wanna move" << endl;
				cin >> E1;
				E1--;
				if (Blanks[E1] != P[player])
					cout << "That is NOT one of your markers! Try again.." << endl;
			} while (Blanks[E1] != P[player]);
			do
			{
				cout << "Now enter the space where you wanna move it." << endl;
				cin >> Sh;
				Sh--;
				if (Blanks[Sh] > '\x39')
					cout << "That is NOT a blank space! Try again.." << endl;
			} while (Blanks[Sh] > '\x39');
			
			if (E1 == 0 && (Sh == 1 || Sh == 3))
			{ 
				ok = true; 
				if (Sh == 1)
				{
					Blanks[Sh] = P[player];
					Blanks[E1] = '\x31' + E1;
				}
				if (Sh == 3)
				{
					Blanks[Sh] = P[player];
					Blanks[E1] = '\x31' + E1;
				}
			}
			if (E1 == 1 && (Sh == 0 || Sh == 2 || Sh == 3 || Sh == 4 || Sh == 5))
			{
				ok = true; 
				if (Sh == 0)
				{
					Blanks[Sh] = P[player];
					Blanks[E1] = '\x31' + E1;
				}
				if (Sh == 2)
				{
					Blanks[Sh] = P[player];
					Blanks[E1] = '\x31' + E1;
				}
				if (Sh == 3)
				{
					Blanks[Sh] = P[player];
					Blanks[E1] = '\x31' + E1;
				}
				if (Sh == 4)
				{
					Blanks[Sh] = P[player];
					Blanks[E1] = '\x31' + E1;
				}
				if (Sh == 5)
				{
					Blanks[Sh] = P[player];
					Blanks[E1] = '\x31' + E1;
				}
			}
			if (E1 == 2 && (Sh == 1 || Sh == 5))
			{
				ok = true; 
				if (Sh == 1)
					{
						Blanks[Sh] = P[player];
						Blanks[E1] = '\x31' + E1;
					}
					if (Sh == 5)
					{
						Blanks[Sh] = P[player];
						Blanks[E1] = '\x31' + E1;
					}
			}
			if (E1 == 3 && (Sh == 0 || Sh == 1 || Sh == 4 || Sh == 6 || Sh == 7))
			{
				ok = true; 
				if (Sh == 0)
					{
						Blanks[Sh] = P[player];
						Blanks[E1] = '\x31' + E1;
					}
					if (Sh == 1)
					{
						Blanks[Sh] = P[player];
						Blanks[E1] = '\x31' + E1;
					}
					if (Sh == 4)
					{
						Blanks[Sh] = P[player];
						Blanks[E1] = '\x31' + E1;
					}
					if (Sh == 6)
					{
						Blanks[Sh] = P[player];
						Blanks[E1] = '\x31' + E1;
					}
					if (Sh == 7)
					{
						Blanks[Sh] = P[player];
						Blanks[E1] = '\x31' + E1;
					}
			}
			if (E1 == 4 && (Sh == 1 || Sh == 3 || Sh == 5 || Sh == 7))
			{
				ok = true; 
				if (Sh == 1)
					{
						Blanks[Sh] = P[player];
						Blanks[E1] = '\x31' + E1;
					}
					if (Sh == 3)
					{
						Blanks[Sh] = P[player];
						Blanks[E1] = '\x31' + E1;
					}
					if (Sh == 5)
					{
						Blanks[Sh] = P[player];
						Blanks[E1] = '\x31' + E1;
					}
					if (Sh == 7)
					{
						Blanks[Sh] = P[player];
						Blanks[E1] = '\x31' + E1;
					}
			}
			if (E1 == 5 && (Sh == 1 || Sh == 2 || Sh == 4 || Sh == 7 || Sh == 8))
			{
				ok = true; 
				if (Sh == 1)
					{
						Blanks[Sh] = P[player];
						Blanks[E1] = '\x31' + E1;
					}
					if (Sh == 2)
					{
						Blanks[Sh] = P[player];
						Blanks[E1] = '\x31' + E1;
					}
					if (Sh == 4)
					{
						Blanks[Sh] = P[player];
						Blanks[E1] = '\x31' + E1;
					}
					if (Sh == 7)
					{
						Blanks[Sh] = P[player];
						Blanks[E1] = '\x31' + E1;
					}
					if (Sh == 8)
					{
						Blanks[Sh] = P[player];
						Blanks[E1] = '\x31' + E1;
					}
			}
			if (E1 == 6 && (Sh == 3 || Sh == 7))
			{
				ok = true; 
				if (Sh == 3)
					{
						Blanks[Sh] = P[player];
						Blanks[E1] = '\x31' + E1;
					}
					if (Sh == 7)
					{
						Blanks[Sh] = P[player];
						Blanks[E1] = '\x31' + E1;
					}
			}
			if (E1 == 7 && (Sh == 3 || Sh == 4 || Sh == 5 || Sh == 6 || Sh == 8))
			{
				ok = true; 
				if (Sh == 3)
					{
						Blanks[Sh] = P[player];
						Blanks[E1] = '\x31' + E1;
					}
					if (Sh == 4)
					{
						Blanks[Sh] = P[player];
						Blanks[E1] = '\x31' + E1;
					}
					if (Sh == 5)
					{
						Blanks[Sh] = P[player];
						Blanks[E1] = '\x31' + E1;
					}
					if (Sh == 6)
					{
						Blanks[Sh] = P[player];
						Blanks[E1] = '\x31' + E1;
					}
					if (Sh == 8)
					{
						Blanks[Sh] = P[player];
						Blanks[E1] = '\x31' + E1;
					}
			}
			if (E1 == 8 && (Sh == 5 || Sh == 7))
			{
				ok = true;
				if (Sh == 5)
				{
					Blanks[Sh] = P[player];
					Blanks[E1] = '\x31' + E1;
				}
				if (Sh == 7)
				{
					Blanks[Sh] = P[player];
					Blanks[E1] = '\x31' + E1;
				}
			}
			if (!ok)
				cout << "Illegal move. Try again.." << endl;
			if (ok)
				player = !player;
			if (E1 < 0 || E1 > 8)
			{
				cout << "Sorry this spot is not legal" << '\n' << endl;
			}
		} while (!Winner);
		player = !player;
	} while (Winner == true); //Ends the part 2 loop
} //Ends main function 
Last edited on
Hi, thanks for your help whitenite1 I really appreciate it. I'll now make the checking for winners part but, could you answer some questions that I have about the code?

-What does '\x31' does/means?

-Why or what made the part 2 of my first code to not work properly? (the part where you move the tokens).
@Tzomby

-What does '\x31' does/means?


That's hex for one, '1'. So ''\x31' + 4, is 5. You're using char, so I couldn't just put Blanks[E1] = 4. Had to enter it as a char.. It's to put back the number in the space you moved from.

-Why or what made the part 2 of my first code to not work properly? (the part where you move the tokens).


The main part I had trouble with, was you had everything using with a string, using double quotes, instead of single quotes, for char. So, I just redid it all, and shortened it.

You also had things like if (E1 == 6 && (Sh == 3 || 7)), which is not really checking if Sh is 3 or Sh is 7. Just checks if 7 IS 7.

Let me see how you make out on the winning part of the code. I'll help out if you need it.
Last edited on
Topic archived. No new replies allowed.