tic-tac-doh!

Hello,

Am starting off on the tic-tac-toe exercise and have just tried to cout to terminal a 2-D array of lines but have received the segmentation fault message. The code is as follows:

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

using namespace std;


int main ()
{
int h=0, w=0;
char grid [3][3];
char blank = '_';


for (h=0; h<=4; h++)
	{
	cout << '\n';
		for (w=0; w<=4; w++)
		{
		grid [h][w] = blank;
		cout << grid [h][w] << " ";
		}
	}
	
return 0;
}


Could someone tell me the origin of this fault, the program compiles fine and displays 'some kind' of matrix before delivering the message.

Where have I taken a miss-turn?

Thanx in advance,
Last edited on
What line does it say the fault is on?

I just compiled it and it worked fine...


Edit: Oh, you said it was compiling fine... I dunno what would be wrong with your code, it works fine for me, like I said. Tho its printing a 5x5 grid and not a 3x3 grid.
Last edited on
Undefined behavior.

Change
for (h=0; h <= 4; h++)


to
for (h=0; h < 3; h++)

3 is the size of each dimension therefore the valid range of offsets is 0-2. Use a size constant that can be used for array construction and for the for loop. That will minimize the chances of having this problem in the future.
Last edited on
Thanks for the replies - duuh! Forgot about the arrays finishing at 2,

However When I run the program still get strange output - changes each time I run it see examples:

� dan@dan-laptop:~/Documents/code$ ./tic_tac

_ _ _ _ _
_ _ _ _ _
_ _ _ �
� dan@dan-laptop:~/Documents/code$ ./tic_tac

_ _ _ _ _
_ _ _ _ _
_ _ _ (
� dan@dan-laptop:~/Documents/code$ ./tic_tac

_ _ _ _ _
_ _ _ _ _
_ _ _ �
� dan@dan-laptop:~/Documents/code$ ./tic_tac

_ _ _ _ _
_ _ _ _ _
_ _ _ 8
� dan@dan-laptop:~/Documents/code$ ./tic_tac

_ _ _ _ _
_ _ _ _ _
_ _ _ X
� dan@dan-laptop:~/Documents/code$ ./tic_tac

_ _ _ _ _
_ _ _ _ _
_ _ _ �
� dan@dan-laptop:~/Documents/code$ ./tic_tac

_ _ _ _ _
_ _ _ _ _
_ _ _ h
� dan@dan-laptop:~/Documents/code$ ./tic_tac

_ _ _ _ _
_ _ _ _ _
_ _ _ �
� dan@dan-laptop:~/Documents/code$ ./tic_tac

_ _ _ _ _
_ _ _ _ _
_ _ _ �
� dan@dan-laptop:~/Documents/code$ ./tic_tac

_ _ _ _ _
_ _ _ _ _
_ _ _ x
� dan@dan-laptop:~/Documents/code$ ./tic_tac

_ _ _ _ _
_ _ _ _ _
_ _ _ H
� dan@dan-laptop:~/Documents/code$ ./tic_tac

_ _ _ _ _
_ _ _ _ _
_ _ _ h
� dan@dan-laptop:~/Documents/code$


strange huh? and this is with the
for (h=0; h < 3; h++)


Any ideas?
Did you do the same thing to the innermost for loop?

Cause that last one is a random bit pattern.
Try changing the one with w, as well. :)
yes, it's running perfectly on my system. Try changing the one with w as well.. :D
Thanks for the heads up guys,

turns out that I had left them as:
for (w=0; w<=3; h++)

as well as not changing the second loop, took me a while to realise why I kept getting a 4x4 matrix!

Note to self: Must not code late at night when have been doing it for 8 hrs during the day.

Further note to self: programming can be addictive!

Thanx:
the size of ur array is 3 x 3 ....but in the loop u r making it 5 x 5 .....so just change ur for loop to
for(w=0;w<3;w++)

and

for(h=0;h<3;h++)
Was examining my pathetic starting grid and decided to do away with it in favour of something altogether more impresseive - BEHOLD...

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



int main ()
{

char array [9];						// location for X and 0's
char x = 'x', o = '0';				// those cheeky characters themselves
int cycle;							// just temporary measure to cycle through the array

string line = "-----+-----+-----";		//
string space = "     |     |     ";		// The constant building blocks of the grid as strings
string bit = "  |  ";					//	

for (cycle =0; cycle <9; cycle++)	// Just filling whole array with 'x' to test positions
	{
	array[cycle] = x;
	}

cout << space << '\n';														//
cout << "  " << array[0] << bit << array[1] << bit << array[2] << '\n';		//
cout << space << '\n' << line << '\n' << space << '\n';						//Printing the grid with array entries
cout << "  " << array[3] << bit << array[4] << bit << array[5] << '\n';		//
cout << space << '\n' << line << '\n' << space << '\n';						//
cout << "  " << array[6] << bit << array[7] << bit << array[8] << '\n';		//
cout << space << '\n';														//

return 0;
}


Now I need to get started on the game itself - will have a crack over the weekend and report back,
thanx,
Dan
Have managed to get the basics of a working game up and running - pat on back, jump up in air!

Next phase, for computer to output who has won and when.

I'm thinking this is going to take a lot of 'if' statements - correct me IF i'm wrong.
Here is the code so far, changed the grid numbers round so that they match the locations of a number pad.

Any little tips -- always welcomed (bearing in mind I can only use the following:

variables, data types, and numerical operators
basic input/output
logic (if statements, switch statements)
loops (for, while, do-while)
arrays

that's according to the exercise - in actuality i don't even know all those!)

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



int main ()
{

char array [9] = {'1','2','3','4','5','6','7','8','9'};		// location for X and 0's
int n=0;													// position in array corresponding to position in grid
char x_or_0 = 'x';											// to choose whether x or 0 - x starts
int turn_num = 0;											// to keep track of number of turns					

string line = "-----+-----+-----";		//
string space = "     |     |     ";		// The constant building blocks of the grid as strings
string bit = "  |  ";					//	

cout << string(50, '\n');				//Gives some clear space

cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n" << space << '\n';						//
cout << "  " << array[6] << bit << array[7] << bit << array[8] << '\n';		//
cout << space << '\n' << line << '\n' << space << '\n';						//Printing the grid with array entries
cout << "  " << array[3] << bit << array[4] << bit << array[5] << '\n';		//Have the grid this way round to reflect number pad
cout << space << '\n' << line << '\n' << space << '\n';						//
cout << "  " << array[0] << bit << array[1] << bit << array[2] << '\n';		//
cout << space;																//

while (turn_num <9)
{
cout << "\nPlease enter the number of the square selected:\n";	
cin >> n;
n--;
array [n] = x_or_0;

cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n" << space << '\n';						//
cout << "  " << array[6] << bit << array[7] << bit << array[8] << '\n';		//
cout << space << '\n' << line << '\n' << space << '\n';						//Printing the grid with array entries
cout << "  " << array[3] << bit << array[4] << bit << array[5] << '\n';		//
cout << space << '\n' << line << '\n' << space << '\n';						//
cout << "  " << array[0] << bit << array[1] << bit << array[2] << '\n';		//
cout << space;																//

if (x_or_0 == 'x')
	{
	x_or_0= 'o';
	}
	else if (x_or_0 == 'o')
		{
		x_or_0='x';
		}
		
turn_num ++;

}
cout << "\n\n";
return 0;
}


I would like to be able to shorten the code and not have to repeat the two lots of drawing the array, any suggestions?
Have moved on and managed to complete the two player tic-tac-toe game, code as follows - any feedback on method or how to simplify:

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


int main ()
{

char array [9] = {'1','2','3','4','5','6','7','8','9'};		// location for X and 0's
int n=0;													// position in array corresponding to position in grid
char x_or_0 = 'O';											// to choose whether x or 0 - x starts
int turn_num = 0;											// to keep track of number of turns					
bool win = 0;												// for if game has been one changes to 1

string line = "-----+-----+-----";		//
string space = "     |     |     ";		// The constant building blocks of the grid as strings
string bit = "  |  ";					//	

cout << string(50, '\n');				//Gives some clear space

//Printing the grid with array entries
//Have the grid this way round to reflect number pad

cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n" << "\t\t\t\t" << space << '\n';						
cout << "\t\t\t\t" << "  " << array[6] << bit << array[7] << bit << array[8] << '\n';		
cout << "\t\t\t\t" << space << '\n' << "\t\t\t\t" << line << '\n' << "\t\t\t\t" << space << '\n';	
cout << "\t\t\t\t" << "  " << array[3] << bit << array[4] << bit << array[5] << '\n';		
cout << "\t\t\t\t" << space << '\n' << "\t\t\t\t" << line << '\n' << "\t\t\t\t" << space << '\n';	
cout << "\t\t\t\t" << "  " << array[0] << bit << array[1] << bit << array[2] << '\n';		
cout << "\t\t\t\t" << space;																
	
while (win !=1)					// main while loop ends when game is won
{
	if (x_or_0 == 'O')			//code to change from 'x' to 'o' after each turn
	{							//
	x_or_0= 'X';				//
	}							//
	else if (x_or_0 == 'X')		//
		{						//
		x_or_0='O';				//
		}						//
		
while ((cout << "\n\n\nPlayer " << x_or_0 << " Please enter the number of the square selected:\n")
		&& (!(cin >> n) 
			|| n < 1 || n > 9 
				|| array[n-1]=='X' 
					|| array[n-1]=='O'))			//checks for unwanted input
		{																					
		cout << "\nThat's NOT an acceptable number....try again.....HARDER!\n";
		cin.clear();
		cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
		}
n--;										//array starts at zero and not 1
array [n] = x_or_0;							//placing an 'X' or 'O' into the array


// printing out new version of grid
cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n" << "\t\t\t\t" << space << '\n';						
cout << "\t\t\t\t" << "  " << array[6] << bit << array[7] << bit << array[8] << '\n';		
cout << "\t\t\t\t" << space << '\n' << "\t\t\t\t" << line << '\n' << "\t\t\t\t" << space << '\n';	
cout << "\t\t\t\t" << "  " << array[3] << bit << array[4] << bit << array[5] << '\n';		
cout << "\t\t\t\t" << space << '\n' << "\t\t\t\t" << line << '\n' << "\t\t\t\t" << space << '\n';	
cout << "\t\t\t\t" << "  " << array[0] << bit << array[1] << bit << array[2] << '\n';		
cout << "\t\t\t\t" << space;

// check for win situation
if ((array[6]==array[7] && array[7]==array[8]) || (array[6]==array[3] && array[3]==array[0]))
	{
	win =1;
	break;
	}
		else if ((array[0]==array[1] && array[1]==array[2]) || (array[2]==array[5] && array[5]==array[8]))
		{
		win =1;
		}
			else if ((array[6]==array[4] && array[4]==array[2]) || (array[0]==array[4] && array[4]==array[8]))
			{
			win =1;
			}
				else if ((array[7]==array[4] && array[4]==array[1]) || (array[3]==array[4] && array[4]==array[5]))
				{
				win =1;
				}
turn_num++;

if (turn_num ==9 && win==0)
	{
	cout << "\n\nThis game is a draw.\n\n";
	break;
	}
if (win==1)
	{
	cout << "\n\n";
	cout << "Player " << x_or_0 << " has won!\n";
	break;
	}
}
return 0;
}


Thanks

Dan
Am running in to a bit of a problem when it comes to playing against the 'semi-intelligent' computer. I'm so close to doing it that it's driving me mad......!!!....!!!.

If i thought the previous post was rather long-winded, take a look at where things are now!!

I know things would be much simpler to use 'functions' but i'm trying to follow the exercise directions and it says not to use them..hmmm.

Could someone please point out what I have done wrong here please, sometimes the computer will overwrite a grid (very occasionally).

Also, why does my call to a random number not compile in g++?

Most times it works fine so that I think it's done, but then it goes 'tits-up' and swaps one of my 'X's for a 'O'. If I didn't know better, I'd swear it's trying to cheat!!!

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
#include <iostream>
#include <limits>
#include <string>
#include <ctime>

using namespace std;


int main ()
{
int random_numb;
char array [9] = {'1','2','3','4','5','6','7','8','9'};		// location for X and 0's
int n=0;													// position in array corresponding to position in grid
int turn_num = 0;											// to keep track of number of turns					
bool win = 0;												// for if game has been one changes to 1

string line = "-----+-----+-----";		//
string space = "     |     |     ";		// The constant building blocks of the grid as strings
string bit = "  |  ";					//	

cout << string(50, '\n');				//Gives some clear space

//Printing the grid with array entries
//Have the grid this way round to reflect number pad

cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n" << "\t\t\t\t" << space << '\n';						
cout << "\t\t\t\t" << "  " << array[6] << bit << array[7] << bit << array[8] << '\n';		
cout << "\t\t\t\t" << space << '\n' << "\t\t\t\t" << line << '\n' << "\t\t\t\t" << space << '\n';	
cout << "\t\t\t\t" << "  " << array[3] << bit << array[4] << bit << array[5] << '\n';		
cout << "\t\t\t\t" << space << '\n' << "\t\t\t\t" << line << '\n' << "\t\t\t\t" << space << '\n';	
cout << "\t\t\t\t" << "  " << array[0] << bit << array[1] << bit << array[2] << '\n';		
cout << "\t\t\t\t" << space;																
	
while (win !=1)					// main while loop ends when game is won
{
//need to check win at start
 
	
while ((cout << "\n\n\nWhere will you place your 'X'? Please select a number:\n")
		&& (!(cin >> n) 
			|| n < 1 || n > 9 
				|| array[n-1]=='X' 
					|| array[n-1]=='O'))			//checks for unwanted input
		{																					
		cout << "\nThat's NOT an acceptable number....try again.....HARDER!\n";
		cin.clear();
		cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
		}
n--;
array [n] = 'X';							//placing an 'X' into the array

// move turn counter on
turn_num++;

// printing out new version of grid
cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n" << "\t\t\t\t" << space << '\n';						
cout << "\t\t\t\t" << "  " << array[6] << bit << array[7] << bit << array[8] << '\n';		
cout << "\t\t\t\t" << space << '\n' << "\t\t\t\t" << line << '\n' << "\t\t\t\t" << space << '\n';	
cout << "\t\t\t\t" << "  " << array[3] << bit << array[4] << bit << array[5] << '\n';		
cout << "\t\t\t\t" << space << '\n' << "\t\t\t\t" << line << '\n' << "\t\t\t\t" << space << '\n';	
cout << "\t\t\t\t" << "  " << array[0] << bit << array[1] << bit << array[2] << '\n';		
cout << "\t\t\t\t" << space;

// check for win situation
if ((array[6]==array[7] && array[7]==array[8]) || (array[6]==array[3] && array[3]==array[0]))
	{
	win =1;
	}
		else if ((array[0]==array[1] && array[1]==array[2]) || (array[2]==array[5] && array[5]==array[8]))
		{
		win =1;
		}
			else if ((array[6]==array[4] && array[4]==array[2]) || (array[0]==array[4] && array[4]==array[8]))
			{
			win =1;
			}
				else if ((array[7]==array[4] && array[4]==array[1]) || (array[3]==array[4] && array[4]==array[5]))
				{
				win =1;
				}

if (win==1 && (turn_num==5 || turn_num==7 || turn_num==9))
	{
	cout << "\n\n";
	cout << "You win, well done, ... i suppose.\n\n";
	break;
	}
	if (win==1 && (turn_num==6 || turn_num==8))
		{
		cout << "\n\n";
		cout << "I win, WA HA HAAAAA!!!!!\n";
		break;
		}
		
if (turn_num ==9 && win==0)
	{
	cout << "\n\nThis game is a draw.\n\n";
	break;
	}
	
//computers move
srand((unsigned)time(0));
random_numb =rand()% 9;   // random between 0 and 9

while (array[random_numb]== 'X' || array[random_numb]=='O')
	{
	random_numb =rand()% 9;
	}
	
if (array[6]==array[7] || array[0]==array[4] || array[2]==array[5] && (array[8]!= 'O' || array[8]!= 'X'))
	{
	array[8]= 'O';
	}
		else if (array[6]==array[3] || array[8]==array[4] || array[2]==array[1]&& (array[0]!= 'O' || array[0]!= 'X'))
		{
		array[0]= 'O';
		}
			else if (array[8]==array[7] || array[2]==array[4] || array[0]==array[3]&& (array[6]!= 'O' || array[6]!= 'X'))
			{
			array[6]= 'O';
			}
				else if (array[8]==array[5] || array[6]==array[4] || array[0]==array[1]&& (array[2]!= 'O' || array[2]!= 'X'))
				{
				array[2]= 'O';
				}
					else if (array[8]==array[0] || array[2]==array[6] || array[7]==array[1] || array[3]==array[5]&& (array[4]!= 'O' || array[4]!= 'X'))
					{
					array[4]= 'O';
					}
						else if (array[6]==array[0] || array[5]==array[4]&& (array[3]!= 'O' || array[3]!= 'X'))
						{
						array[3]='O';
						}
							else if (array[0]==array[2] || array[7]==array[4]&& (array[1]!= 'O' || array[1]!= 'X'))
							{
							array[1]='O';
							}
								else if (array[8]==array[2] || array[4]==array[3]&& (array[5]!= 'O' || array[5]!= 'X'))
								{
								array[5]='O';
								}
									else if (array[8]==array[6] || array[4]==array[1]&& (array[7]!= 'O' || array[7]!= 'X'))
									{
									array[7]='O';
									}
     else 
	 {
	 array[random_numb]= 'O';
	 }

// Move turn counter on again
turn_num++;

// printing out new version of grid
cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n" << "\t\t\t\t" << space << '\n';						
cout << "\t\t\t\t" << "  " << array[6] << bit << array[7] << bit << array[8] << '\n';		
cout << "\t\t\t\t" << space << '\n' << "\t\t\t\t" << line << '\n' << "\t\t\t\t" << space << '\n';	
cout << "\t\t\t\t" << "  " << array[3] << bit << array[4] << bit << array[5] << '\n';		
cout << "\t\t\t\t" << space << '\n' << "\t\t\t\t" << line << '\n' << "\t\t\t\t" << space << '\n';	
cout << "\t\t\t\t" << "  " << array[0] << bit << array[1] << bit << array[2] << '\n';		
cout << "\t\t\t\t" << space;

// check for win situation
if ((array[6]==array[7] && array[7]==array[8]) || (array[6]==array[3] && array[3]==array[0]))
	{
	win =1;
	}
		else if ((array[0]==array[1] && array[1]==array[2]) || (array[2]==array[5] && array[5]==array[8]))
		{
		win =1;
		}
			else if ((array[6]==array[4] && array[4]==array[2]) || (array[0]==array[4] && array[4]==array[8]))
			{
			win =1;
			}
				else if ((array[7]==array[4] && array[4]==array[1]) || (array[3]==array[4] && array[4]==array[5]))
				{
				win =1;
				}

if (win==1 && (turn_num==5 || turn_num==7 || turn_num==9))
	{
	cout << "\n\n";
	cout << "You win, well done, ... i suppose.\n\n";
	break;
	}
	if (win==1 && (turn_num==6 || turn_num==8))
		{
		cout << "\n\n";
		cout << "I win, WA HA HAAAAA!!!!!\n";
		break;
		}
		
if (turn_num ==9 && win==0)
	{
	cout << "\n\nThis game is a draw.";
	break;
	}
	

	
}

return 0;
}


Thanx,

Dan






closed account (jwC5fSEw)
Well, you can't use rand() because you're not including <cstdlib>.

And I recently completed the same exercise. As far as your problem with the computer writing over grids, I used an array of 9 bools that correspond to each square. When a square gets filled, its corresponding bool in the array becomes true. Whenever the user inputs a square or the computer generates a random square, it's compared with the corresponding bool in the array to see if it's filled. If it is, ask for another number or generate another number depending on the turn. If it's not, continue with the game.

Also, you only need to use srand once to seed.
Many thanks for your input, have taken your advice about the bool array to heart.

By day i am coding on a windows system which has 'visual studio c++' on it and so i use the compiler from that - it must automatically include the <cstdlib> when compiling - whereas at home i run a Linux system and forgot to include said file before compiling - Thanks for the heads up.

Still have problems though with the code. Have been fiddling with it during all free time today. It looks better - more streamlined, but it seems to be functioning WORSE. The computer cheats even worse than before - maybe it's because i'm running on Microsoft! (don't be offended - only a digg at their business practices and not their op, sys)

I can't really tell what the problem stems from....

could I have a pointer 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
#include <iostream>
#include <limits>
#include <string>
#include <ctime>
#include <cstdlib>

using namespace std;


int main ()
{
int random_numb;
char array [9] = {'1','2','3','4','5','6','7','8','9'};		// location for X and 0's
bool check [9] = {0,0,0,0,0,0,0,0,0};						// to show which squares have been used
int n=0;													// position in array corresponding to position in grid
int turn_num = 0;											// to keep track of number of turns					
bool win = 0;												// for if game has been one changes to 1

srand((unsigned)time(0));
random_numb=rand() % 9;

string line = "-----+-----+-----";		//
string space = "     |     |     ";		// The constant building blocks of the grid as strings
string bit = "  |  ";					//	


cout << string(50, '\n');				//Gives some clear space

while (win ==0)					// main while loop ends when game is won
{

//Printing the grid with array entries
//Have the grid this way round to reflect number pad

cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n" << "\t\t\t\t" << space << '\n';						
cout << "\t\t\t\t" << "  " << array[6] << bit << array[7] << bit << array[8] << '\n';		
cout << "\t\t\t\t" << space << '\n' << "\t\t\t\t" << line << '\n' << "\t\t\t\t" << space << '\n';	
cout << "\t\t\t\t" << "  " << array[3] << bit << array[4] << bit << array[5] << '\n';		
cout << "\t\t\t\t" << space << '\n' << "\t\t\t\t" << line << '\n' << "\t\t\t\t" << space << '\n';	
cout << "\t\t\t\t" << "  " << array[0] << bit << array[1] << bit << array[2] << '\n';		
cout << "\t\t\t\t" << space;																
	

// check for win situation
if ((array[6]==array[7] && array[7]==array[8]) || (array[6]==array[3] && array[3]==array[0]))
	{
	win =1;
	break;
	}
		else if ((array[0]==array[1] && array[1]==array[2]) || (array[2]==array[5] && array[5]==array[8]))
		{
		win =1;
		break;
		}
			else if ((array[6]==array[4] && array[4]==array[2]) || (array[0]==array[4] && array[4]==array[8]))
			{
			win =1;
			break;
			}
				else if ((array[7]==array[4] && array[4]==array[1]) || (array[3]==array[4] && array[4]==array[5]))
				{
				win =1;
				break;
				}

	
while ((cout << "\n\n\nWhere will you place your 'X'? Please select a number:\n")
		&& (!(cin >> n) 
			|| n < 1 || n > 9 
				|| check[n-1]==1))			//checks for already used spaces
		{																					
		cout << "\nThat's NOT an acceptable number....try again.....HARDER!\n";
		cin.clear();
		cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
		}
n--;
array [n] = 'X';						//placing an 'X' into the array		
check [n] = 1;							//showing that this square has been used					

// move turn counter on
turn_num++;

// check for win situation
if ((array[6]==array[7] && array[7]==array[8]) || (array[6]==array[3] && array[3]==array[0]))
	{
	win =1;
	break;
	}
		else if ((array[0]==array[1] && array[1]==array[2]) || (array[2]==array[5] && array[5]==array[8]))
		{
		win =1;
		break;
		}
			else if ((array[6]==array[4] && array[4]==array[2]) || (array[0]==array[4] && array[4]==array[8]))
			{
			win =1;
			break;
			}
				else if ((array[7]==array[4] && array[4]==array[1]) || (array[3]==array[4] && array[4]==array[5]))
				{
				win =1;
				break;
				}

//computers move


		if (array[6]==array[7] || array[0]==array[4] || array[2]==array[5] && (check[8] == 0))
		{
		array[8]= 'O';
		check[8]= 1;
		}
			else if (array[6]==array[3] || array[8]==array[4] || array[2]==array[1]&& (check[0] == 0))
			{
			array[0]= 'O';
			check[0]=1;
			}
				else if (array[8]==array[7] || array[2]==array[4] || array[0]==array[3] && (check[6] == 0))
				{
				array[6]= 'O';
				check[6]= 1;
				}
					else if (array[8]==array[5] || array[6]==array[4] || array[0]==array[1] && (check[2]==0))
					{
					array[2]= 'O';
					check[2]= 1;
					}
						else if (array[8]==array[0] || array[2]==array[6] || array[7]==array[1] || array[3]==array[5] && (check[4]==0))
						{
						check[4]= 1;
						array[4]= 'O';
						}
							else if (array[6]==array[0] || array[5]==array[4] && (check[3]==0))
							{
							array[3]='O';
							check[3]= 1;
							}
								else if (array[0]==array[2] || array[7]==array[4] && (check[1]==0))
								{
								array[1]='O';
								check[1]= 1;
								}
									else if (array[8]==array[2] || array[4]==array[3] && (check[5]==0))
									{
									array[5]='O';
									check[5]= 1;
									}
										else if (array[8]==array[6] || array[4]==array[1] && (check[7]== 0))
										{
										array[7]='O';
										check[7]= 1;
										}
											
										else 
											{
												while (check[random_numb]==1)		//if random number was already taken generates another and checks6
												{
												random_numb =rand()% 9;
												}
											array[random_numb]= 'O';
											check[random_numb]=1;
											}
																					
// Move turn counter on again
turn_num++;

} //end of game loop finish


if (turn_num ==9 && win==0)
	{
	cout << "\n\nThis game is a draw.";
	}
if (win==1 && (turn_num==5 || turn_num==7 || turn_num==9))
	{
	cout << "\n\n";
	cout << "You win, well done, ... i suppose.\n\n";
	}
	if (win==1 && (turn_num==6 || turn_num==8))
		{
		cout << "\n\n";
		cout << "I win, WA HA HAAAAA!!!!!\n";
		}
		

	
return 0;
}
It seems that on turn 7 is where you're problem is. The computer wins because turn 7 (the player's turn) is somehow skipped.
What I have found is that the turn where the error occurs varies which leads me to conclude that it's something to do with the 'random' part of the code, either that or with what happens when the code finds that a square is already occupied.

Will have a look today and see if I can get anywhere

Am open to further suggestions.

thx.

Dan
I seem to have cracked it - Works a treat in terms of blocking and plays a proper game. Haven't managed to program in any of the 'strategy' yet. Also annoying at present that the computer would rather 'block' the player than take a 'win'.

As ever, any suggestions much appreciated:

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
#include <iostream>
#include <limits>
#include <string>
#include <ctime>
#include <cstdlib>

using namespace std;


int main ()
{
int random_numb;
char array [9] = {'1','2','3','4','5','6','7','8','9'};		// location for X and 0's
bool check [9] = {0,0,0,0,0,0,0,0,0};						// to show which squares have been used
int n=0;													// position in array corresponding to position in grid														
bool win = 0;												// for if game has been one changes to 1
bool turn=0;												// to keep track of number of turns	
int turn_num=0;

string line = "-----+-----+-----";		//
string space = "     |     |     ";		// The constant building blocks of the grid as strings
string bit = "  |  ";					//	


cout << string(50, '\n');				//Gives some clear space

while (win ==0)					// main while loop ends when game is won
{

srand((unsigned)time(0));
random_numb=rand() % 9;

//Printing the grid with array entries
//Have the grid this way round to reflect number pad

cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n" << "\t\t\t\t" << space << '\n';						
cout << "\t\t\t\t" << "  " << array[6] << bit << array[7] << bit << array[8] << '\n';		
cout << "\t\t\t\t" << space << '\n' << "\t\t\t\t" << line << '\n' << "\t\t\t\t" << space << '\n';	
cout << "\t\t\t\t" << "  " << array[3] << bit << array[4] << bit << array[5] << '\n';		
cout << "\t\t\t\t" << space << '\n' << "\t\t\t\t" << line << '\n' << "\t\t\t\t" << space << '\n';	
cout << "\t\t\t\t" << "  " << array[0] << bit << array[1] << bit << array[2] << '\n';		
cout << "\t\t\t\t" << space;																
	

// check for win situation
if ((array[6]==array[7] && array[7]==array[8]) || (array[6]==array[3] && array[3]==array[0]))
	{
	win =1;
	break;
	}
		else if ((array[0]==array[1] && array[1]==array[2]) || (array[2]==array[5] && array[5]==array[8]))
		{
		win =1;
		break;
		}
			else if ((array[6]==array[4] && array[4]==array[2]) || (array[0]==array[4] && array[4]==array[8]))
			{
			win =1;
			break;
			}
				else if ((array[7]==array[4] && array[4]==array[1]) || (array[3]==array[4] && array[4]==array[5]))
				{
				win =1;
				break;
				}

	
while ((cout << "\n\n\nWhere will you place your 'X'? Please select a number:\n")
		&& (!(cin >> n) 
			|| n < 1 || n > 9 
				|| check[n-1]==1))			//checks for already used spaces
		{																					
		cout << "\a\a" << "\nThat's NOT an acceptable number....try again.....HARDER!\n";
		cin.clear();
		cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
		}
n--;
array [n] = 'X';						//placing an 'X' into the array		
check [n] = 1;							//showing that this square has been used					

// move turn counter on
turn_num++;
turn=1;

//Printing the grid with array entries
//Have the grid this way round to reflect number pad

cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n" << "\t\t\t\t" << space << '\n';						
cout << "\t\t\t\t" << "  " << array[6] << bit << array[7] << bit << array[8] << '\n';		
cout << "\t\t\t\t" << space << '\n' << "\t\t\t\t" << line << '\n' << "\t\t\t\t" << space << '\n';	
cout << "\t\t\t\t" << "  " << array[3] << bit << array[4] << bit << array[5] << '\n';		
cout << "\t\t\t\t" << space << '\n' << "\t\t\t\t" << line << '\n' << "\t\t\t\t" << space << '\n';	
cout << "\t\t\t\t" << "  " << array[0] << bit << array[1] << bit << array[2] << '\n';		
cout << "\t\t\t\t" << space;	

	
// check for win situation
if ((array[6]==array[7] && array[7]==array[8]) || (array[6]==array[3] && array[3]==array[0]))
	{
	win =1;
	break;
	}
		else if ((array[0]==array[1] && array[1]==array[2]) || (array[2]==array[5] && array[5]==array[8]))
		{
		win =1;
		break;
		}
			else if ((array[6]==array[4] && array[4]==array[2]) || (array[0]==array[4] && array[4]==array[8]))
			{
			win =1;
			break;
			}
				else if ((array[7]==array[4] && array[4]==array[1]) || (array[3]==array[4] && array[4]==array[5]))
				{
				win =1;
				break;
				}
							
															
	
if (turn_num ==9 && win==0)
	{
	cout << "\n\nThis game is a draw.\n\n";
	break;
	}
	
//computers move

while (check[random_numb]==1)			//if random number was already taken generates another and checks	
				{
				srand((unsigned)time(0));
				random_numb =rand()% 9;
				}
										
		if (check[8] == 0 && (array[6]==array[7] || array[0]==array[4] || array[2]==array[5]) &&  turn==1 )
		{
		array[8]= 'O';
		check[8]= 1;
		turn=0;
		}
			else if (check[0] == 0 && (array[6]==array[3] || array[8]==array[4] || array[2]==array[1]) &&  turn==1)
			{
			array[0]= 'O';
			check[0]=1;
			turn=0;
			}
				else if (check[6] == 0 && (array[8]==array[7] || array[2]==array[4] || array[0]==array[3]) && turn==1)
				{
				array[6]= 'O';
				check[6]= 1;
				turn=0;
				}
					else if (check[2]==0 && (array[8]==array[5] || array[6]==array[4] || array[0]==array[1]) && turn==1)
					{
					array[2]= 'O';
					check[2]= 1;
					turn=0;
					}
						else if (check[4]==0 && (array[8]==array[0] || array[2]==array[6] || array[7]==array[1] || array[3]==array[5]) && turn==1)
						{
						check[4]= 1;
						array[4]= 'O';
						turn=0;
						}
							else if (check[3]==0 && (array[6]==array[0] || array[5]==array[4]) &&  turn ==1)
							{
							array[3]='O';
							check[3]= 1;
							turn=0;
							}
								else if (check[1]==0 && (array[0]==array[2] || array[7]==array[4]) &&  turn==1)
								{
								array[1]='O';
								check[1]= 1;
								turn=0;
								}
									else if (check[5]==0 && (array[8]==array[2] || array[4]==array[3]) &&  turn ==1)
									{
									array[5]='O';
									check[5]= 1;
									turn=0;
									}
										else if (check[7]== 0 && (array[8]==array[6] || array[4]==array[1])  && turn ==1)
										{
										array[7]='O';
										check[7]= 1;
										turn=0;
										}
											
										else 
											{
											array[random_numb]= 'O';
											check[random_numb]=1;
											turn=0;
											}
																					
// Move turn counter on again
turn_num++;
} //end of game loop finish
													
if (win==1 && (turn==1))
	{
	cout << "\n\n";
	cout << "You win, well done, ... i suppose.\n\n";
	}
	if (win==1 && (turn==0))
		{
		cout << "\n\n";
		cout << "I win, WA HA HAAAAA!!!!!\n";
		}
			
return 0;
}


For the present, however, I'm moving on to the next exercise.
closed account (jwC5fSEw)
That's what I did when I got to that stage. Making it take a win over a block wouldn't be very different from making it block, so if you can make it block, you've pretty much gone as far as you can go.

Congrats! I felt great when I finally got the computer to block properly.
Topic archived. No new replies allowed.