TTT

Hi, i'm running into 2 problems
1. I couldn't get it to change turn between user and ai.

2. I couldn't get it to check if user put in the same spot (line 93-100).

please help or any advice would be appreciate thank you.

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
// This program will allows the user to play tic-tac-toe against the computer.


#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;


// function Prototype.
void displayboard ();

void getusermove ();

void getcomputermove ();

int checkforwinner ();

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

int main ()
{
	int turn;

	for (turn = 1; checkforwinner() == 0 && turn < 9; turn++)
	{
		if (turn = 1)
		{
			getusermove ();
			turn = 2;
		}
		else 
		{
			getcomputermove ();
			turn = 1;
		}

	}

	// Display who win
    if (turn == 9 && !checkforwinner()) 
	{
      
      displayboard ();
      cout << " It a tie game! " << endl;
    
	} 
    
	if (checkforwinner() == 2) 
	{
      
      displayboard ();
      cout << " X wins!!! " << endl;
    
	} 
    
	if (checkforwinner() == 3) 
	{
      
      displayboard ();
      cout << " O wins!!! " << endl;

	}
		
	return 0;

}

// Display Board
void displayboard ()
{
	cout << endl;
	cout << board[0] << " | " << board[1] << " | " << board[2] << endl;
	cout << "---------" << endl;
	cout << board[3] << " | " << board[4] << " | " << board[5] << endl;
	cout << "---------" << endl;
	cout << board[6] << " | " << board[7] << " | " << board[8] << endl;
}


// Get x for user move.
void getusermove ()
{
	int move;

		displayboard ();
		cout << "Enter your move: ";
		cin >> move;

		move--;

		if (board[move] != 'x' || board[move] != 'o')
		{
			board[move] = 'x';
		}
		else
		{
			cout << "Invalid please reenter: ";
		}

}

// Get o for computer move.
void getcomputermove ()
{
	srand (unsigned int (time(NULL)));

	int move = rand() % 9;

		if (board[move] == ' ')
		{
			displayboard ();
			cout << "Computer is moving to: " << board[move];
		
			board[move] = 'o';
		}

}

// Winning conditions
int checkforwinner ()
{
	if (board[0] == board[1] && board[1] == board[2]) 
	{
		if (board[0] == 'x') 
		{
			return 2;
		} 
		else 
		{
			return 3;
		}
	}
	if (board[3] == board[4] && board[4] == board[5]) 
	{
		if (board[3] == 'x') 
		{
			return 2;
		} 
		else 
		{
			return 3;
		}
	}
	if (board[6] == board[7] && board[7] == board[8]) 
	{
		if (board[6] == 'x') 
		{
			return 2;
		} 
		else
		{
			return 3;
		}
	}
	if (board[0] == board[3] && board[3] == board[6])
	{
		if (board[0] == 'x') 
		{
			return 2;
		} 
		else 
		{
			return 3;
		}
	}
	if (board[1] == board[4] && board[4] == board[7]) 
	{
		if (board[1] == 'x') 
		{
			return 2;
		} 
		else 
		{
			return 3;
		}
	}
	if (board[2] == board[5] && board[5] == board[8]) 
	{
		if (board[2] == 'x') 
		{
			return 2;
		} 
		else 
		{
			return 3;
		}
	}
	if (board[0] == board[4] && board[4] == board[8]) 
	{
		if (board[0] == 'x') 
		{
			return 2;
		} 
		else 
		{
			return 3;
		}
	}
	if (board[2] == board[4] && board[4] == board[6]) 
	{
		if (board[2] == 'x') 
		{
			return 2;
		} 
		else 
		{
			return 3;
		}
	}

	return 0;

}
> I couldn't get it to change turn between user and ai.
if (turn = 1) compile with warnings
$ g++ -W{all,extra,pedantic} foo.cpp
foo.cpp: In function ‘int main()’:
foo.cpp:28:15: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
foo.cpp: In function ‘void getcomputermove()’:
foo.cpp:107:9: error: expected primary-expression before ‘unsigned’
== is comparison
= is assignment.

Also, it seems that your casting is incorrect. srand ( (unsigned int) (time(NULL)) );
However, you should initialize the random generator just one time in your program.

> I couldn't get it to check if user put in the same spot (line 93-100).
if (board[move] != 'x' || board[move] != 'o')
|| is or. && is and.
You can in fact write `or' `and' `not' there. http://www.cplusplus.com/reference/ciso646/
If already knew that, review your logic.
Last edited on
 
if (board[move] != 'x' || board[move] != 'o')


That's not going to work the way you want it.
It's always going to return true.
You want an and condition.

 
if (board[move] != 'x' && board[move] != 'o')

Also, it seems that your casting is incorrect. srand ( (unsigned int) (time(NULL)) );
However, you should initialize the random generator just one time in your program.


should i put outside main or inside?
1
2
3
int main(){
   srand(42); //first line, done you've initialized the rng
//don't ever call that function again (unless you've got a good reason) 
cool, it work! but i still can't get it to change back to user turn after ai turn.

1
2
3
4
5
6
7
8
9
10
11
12
13
// Switch turn
	for (turn = 1; checkforwinner() == 0 && turn < 9; turn++)
	{
		if (turn == 1)
		{
			getusermove ();
		}
		else
		{
			getcomputermove ();
			turn = 1;
		}
	}


I also can't get ai not to pick the same #.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void getcomputermove ()
{
	int move = rand() %8 + 1;

		if (board[move] != 'x' && board[move] != 'o')
		{
			displayboard ();
			cout << "Computer is moving to: " << move;

			move--;
	
			board[move] = 'o';
		}
}


http://www.cplusplus.com/articles/EywTURfi/ it didn't say anything about how to pick different #.
> i still can't get it to change back to user turn after ai turn.
`turn++' would execute before checking the condition.
You assign it in line 11, and later increment it. So `turn' would be 2. (¿how would the loop terminate?)
1
2
3
4
5
for (turn = 1; checkforwinner() == 0 && turn < 9; turn++)
	if (turn%2 == 1)
		getusermove();
	else
		getcomputermove();


> I also can't get ai not to pick the same #.
Try again. (or select from a valid move set)
Last edited on
Thank you for helping dude, one last question. turn%2 What this mean?
Topic archived. No new replies allowed.