New to C++ Help needed

How can i make this player vs computer? C++ please help?

HI, I'm in high school and making a tic tac toe problem player vs computer. I already have player vs player can you please change it to player vs computer thanks for the help.

here is the code

#include <iostream>
using namespace std;

char square[10] = {'o','1','2','3','4','5','6','7','8','9'…

int checkwin();
void board();

int main()
{
int player = 1,i,choice;
char ch='y';
char mark;
do
{
board();
player=(player%2)?1:2;

cout << "Player " << player << ", enter a number: ";
cin >> choice;

mark=(player == 1) ? 'X' : 'O';

if (choice == 1 && square[1] == '1')

square[1] = mark;
else if (choice == 2 && square[2] == '2')

square[2] = mark;
else if (choice == 3 && square[3] == '3')

square[3] = mark;
else if (choice == 4 && square[4] == '4')

square[4] = mark;
else if (choice == 5 && square[5] == '5')

square[5] = mark;
else if (choice == 6 && square[6] == '6')

square[6] = mark;
else if (choice == 7 && square[7] == '7')

square[7] = mark;
else if (choice == 8 && square[8] == '8')

square[8] = mark;
else if (choice == 9 && square[9] == '9')

square[9] = mark;
else
{
cout<<"Invalid move, Press Enter and Try Again ";

player--;
cin.ignore();
cin.get();
}
i=checkwin();

player++;
}while(i==-1);
board();
if(i==1)

cout<<"==>\aPlayer "<<--player<<" win ";
else
cout<<"==>\aGame draw";

cin.ignore();
cin.get();
return 0;
}

int checkwin()
{
if (square[1] == square[2] && square[2] == square[3])

return 1;
else if (square[4] == square[5] && square[5] == square[6])

return 1;
else if (square[7] == square[8] && square[8] == square[9])

return 1;
else if (square[1] == square[4] && square[4] == square[7])

return 1;
else if (square[2] == square[5] && square[5] == square[8])

return 1;
else if (square[3] == square[6] && square[6] == square[9])

return 1;
else if (square[1] == square[5] && square[5] == square[9])

return 1;
else if (square[3] == square[5] && square[5] == square[7])

return 1;
else if (square[1] != '1' && square[2] != '2' && square[3] != '3' && square[4] != '4'
&& square[5] != '5' && square[6] != '6' && square[7] != '7' && square[8] != '8' && square[9] != '9')

return 0;
else
return -1;

}

void board()
{
system("cls");
cout << "\n\n\tTic Tac Toe\n\n";

cout << "Player 1 (X) - Computer (O)" << endl << endl;
cout << endl;

cout << " | | " << endl;
cout << " " << square[1] << " | " << square[2] << " | " << square[3] << endl;

cout << "_____|_____|_____" << endl;
cout << " | | " << endl;

cout << " " << square[4] << " | " << square[5] << " | " << square[6] << endl;

cout << "_____|_____|_____" << endl;
cout << " | | " << endl;

cout << " " << square[7] << " | " << square[8] << " | " << square[9] << endl;

cout << " | | " << endl << endl;
}
Double Post: http://cplusplus.com/forum/beginner/88482/

Besides, you've already been answered here.: http://cplusplus.com/forum/general/85257/

You need to code the logic of Tic Tac Toe to simulate a computer playing like a human would. Post what you have tried...

AND USE CODE TAGS! (Select your code and press the <> button).
Last edited on
Take this :
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
#include <iostream>
#include <time.h>
using namespace std;
char square[10] = {'o','1','2','3','4','5','6','7','8','9'};

int checkwin();
void board();

#define X 1
#define Y 0

bool SetItem(int nPosition, int mark){
if(square[nPosition] != nPosition + '0' || nPosition < 1 || nPosition > 9)return false;
square[nPosition] = mark;return true;}

bool IsEmpty(int nPosition){return (square[nPosition] == '0' + nIndex);}
bool IsPlayer(bool Player, int nPosition){return (square[nPosition] == ((Player == X) ? 'X' : 'O'));}

int ThinkColumn(bool Player, int nColumn){nColumn--;nColumn *= 3;//Try calculating this expression (E.g : 1;2;3)
int nItems = 0;
if(IsPlayer(Player, nColumn + 1) == true)nItems++;
if(IsPlayer(Player, nColumn + 2) == true)nItems++;
if(IsPlayer(Player, nColumn + 3) == true)nItems++;

if(nItems == 2)
{
if(IsEmpty(nColumn + 1) == true)return nColumn + 1;
if(IsEmpty(nColumn + 2) == true)return nColumn + 2;
if(IsEmpty(nColumn + 3) == true)return nColumn + 3;

}
return 0;
}

int ThinkRow(bool Player, int nRow){
int nItems = 0;
if(IsPlayer(Player, nRow + 0) == true)nItems++;
if(IsPlayer(Player, nRow + 3) == true)nItems++;
if(IsPlayer(Player, nRow + 6) == true)nItems++;

if(nItems == 2)
{
if(IsEmpty(nRow + 0) == true)return nRow + 0;
if(IsEmpty(nRow + 3) == true)return nRow + 3;
if(IsEmpty(nRow + 6) == true)return nRow + 6;
}
return 0;
}

int ThinkOther(bool Player, int nLine){
int nItems = 0;
if(nLine == 1){
	if(IsPlayer(Player, 1) == true)nItems++;
	if(IsPlayer(Player, 5) == true)nItems++;
	if(IsPlayer(Player, 9) == true)nItems++;
}
else if(nLine == 2){
	if(IsPlayer(Player, 3) == true)nItems++;
	if(IsPlayer(Player, 5) == true)nItems++;
	if(IsPlayer(Player, 7) == true)nItems++;
}


if(nItems == 2) //There are two player pieces
{
if(nLine == 1){
	if(IsEmpty(1) == true)return 1;
	if(IsEmpty(5) == true)return 5;
	if(IsEmpty(9) == true)return 9;
}
else if(nLine == 2){
	if(IsEmpty(3) == true)return 3;
	if(IsEmpty(5) == true)return 5;
	if(IsEmpty(7) == true)return 7;
}
}
return 0;
}

int ThinkSpecificItem(bool Player, int nIndex)
{
if(IsPlayer(Player, nIndex))
{
if(nIndex == 2 || nIndex == 8)
{
	if(IsEmpty(4) == true && (nIndex == 2 && IsEmpty(1) == true) || (nIndex == 8 && IsEmpty(7) == true))return 4;
	if(IsEmpty(6) == true && (nIndex == 2 && IsEmpty(3) == true) || (nIndex == 8 && IsEmpty(9) == true))return 6;
	if(IsEmpty(5) == true)return 5;
}
else if(nIndex == 4 || nIndex == 6)
{
	if(IsEmpty(2) == true && (nIndex == 4 && IsEmpty(1) == true) || (nIndex == 6 && IsEmpty(3) == true))return 2;
	if(IsEmpty(8) == true && (nIndex == 4 && IsEmpty(7) == true) || (nIndex == 6 && IsEmpty(9) == true))return 8;
	if(IsEmpty(5) == true)return 5;
}
else if(nIndex == 1)
{
if(IsPlayer(Player, 5) == true && IsEmpty(3) == true && (IsEmpty(2) == true || IsEmpty(9) == true))return 3;
if(IsEmpty(2) == true && IsEmpty(3) == true)return 2;
//if(IsPlayer(Player, 2)  && IsPlayer(Player, 3))return 4;
if(IsEmpty(4) == true && IsEmpty(7) == true)return 4;
if(IsEmpty(5) == true)return 5;
}
else if(nIndex == 3)
{
if(IsPlayer(Player, 5) == true && IsEmpty(1) == true && (IsEmpty(2) == true || IsEmpty(7) == true))return 1;
if(IsEmpty(2) == true && IsEmpty(1) == true)return 2;
if(IsEmpty(6) == true && IsEmpty(9) == true)return 6;
if(IsEmpty(5) == true)return 5;
}
else if(nIndex == 7)
{
if(IsPlayer(Player, 5) == true && IsEmpty(9) == true && (IsEmpty(8) == true || IsEmpty(3) == true))return 9;
if(IsEmpty(4) == true && IsEmpty(1) == true)return 4;
if(IsEmpty(8) == true && IsEmpty(9) == true)return 8;
if(IsEmpty(5) == true)return 5;
}
else if(nIndex == 9)
{
if(IsPlayer(Player, 5) == true && IsEmpty(7) == true && (IsEmpty(8) == true || IsEmpty(1) == true))return 7;
if(IsEmpty(6) == true && IsEmpty(3) == true)return 6;
if(IsEmpty(8) == true && IsEmpty(7) == true)return 8;
if(IsEmpty(5) == true)return 5;
}
}
return 0;
}

void Think(bool Player)
{
char mark = (Player == X) ? 'X' : 'O';
bool bResult;
int choice;
bool Enemy = !Player;
///////////////Method 1...////////////////////////////////////////////////////
if((choice = ThinkOther(Player, 1)) != 0){square[choice] = mark;return;}
if((choice = ThinkOther(Player, 2)) != 0){square[choice] = mark;return;}
for(int b = 1; b <= 3; b++){
if((choice = ThinkColumn(Player, b)) != 0){square[choice] = mark;return;}
if((choice = ThinkRow(Player, b)) != 0){square[choice] = mark;return;}}
if((choice = ThinkOther(Enemy, 1)) != 0){square[choice] = mark;return;}
if((choice = ThinkOther(Enemy, 2)) != 0){square[choice] = mark;return;}

///////////////Method 2...////////////////////////////////////////////////////////
bResult = (IsPlayer(Player, 2) == true && IsPlayer(Player, 4) == true);
if(bResult == true && IsEmpty(1) == true){square[1] = mark;return;}
bResult = (IsPlayer(Player, 2) == true && IsPlayer(Player, 6) == true);
if(bResult == true && IsEmpty(3) == true){square[3] = mark;return;}
bResult = (IsPlayer(Player, 4) == true && IsPlayer(Player, 8) == true);
if(bResult == true && IsEmpty(7) == true){square[7] = mark;return;}
bResult = (IsPlayer(Player, 6) == true && IsPlayer(Player, 8) == true);
if(bResult == true && IsEmpty(7) == true){square[9] = mark;return;}

///////////////Method 3...////////////////////////////////////////////////////////
for(int a = 1; a <= 3; a++){
if((choice = ThinkColumn(Enemy, a)) != 0){square[choice] = mark;return;}
if((choice = ThinkRow(Enemy, a)) != 0){square[choice] = mark;return;}}

///////////////Method 4...////////////////////////////////////////////////////////
for(int c = 1; c <= 9; c++)
if((choice = ThinkSpecificItem(Player, c)) != 0){square[choice] = mark;return;}

///////////////Method 5...////////////////////////////////////////////////////////
do choice = rand() % 9 + 1;while(IsEmpty(choice) == false);square[choice] = mark;
}



//////////////////////////////////////////////////////////////////////////////////
int main()
{
srand(time(NULL));
bool player = X;
char mark;
int i, choice;
cout << "Computer first? (Press Y) : ";
cin >> mark;
if(mark == 'y' || mark == 'Y')player = Y;
system("cls");
////////////////////
do
{
board();
mark = (player == X) ? 'X' : 'O';
if(player == X)
{
cout << "Player " << player << " ("<< mark <<"), please enter a number : ";
cin >> choice;
if(SetItem(choice, mark) == false){
	cout<<"Invalid move ";
	player = !player; //mark = (player == 1) ? 'O' : 'X';
	cin.ignore();
	cin.get();
}
}
else Think(player);



i = checkwin();

player = !player;}while(i == -1);

board();
if(i == 1) cout<<"==>\a Player "<< ((player == X) ? 'O' : 'X') <<" win ";
return 0;
}

//////////////////////////////////////////////////////////////////////////////////

int checkwin()
{
if (square[1] == square[2] && square[2] == square[3])return 1;
if (square[4] == square[5] && square[5] == square[6])return 1;
if (square[7] == square[8] && square[8] == square[9])return 1;
if (square[1] == square[4] && square[4] == square[7])return 1;
if (square[2] == square[5] && square[5] == square[8])return 1;
if (square[3] == square[6] && square[6] == square[9])return 1;
if (square[1] == square[5] && square[5] == square[9])return 1;
if (square[3] == square[5] && square[5] == square[7])return 1;

for(int i = 1;i <= 9;i++)if(IsEmpty(i) == true)return -1;

board();
cout<<"==>\aGame draw\n";
system("pause");
exit(0);
return 0;
}


void board()
{
system("cls");
cout << "\n\n\tTic Tac Toe\n\n";
cout << "Player 1 (X) - Computer (O)" << endl << endl << endl;
cout << "____________" << endl;
cout << "|  |   |   |  " << endl;
cout << "|" << square[1] << " | " << square[2] << " | " << square[3] << " | " << endl;
cout << "____________" << endl;
cout << "|  |   |   |  " << endl;
cout << "|" << square[4] << " | " << square[5] << " | " << square[6] <<  " | " << endl;
cout << "____________" << endl;
cout << "|  |   |   |  " << endl;
cout << "|" << square[7] << " | " << square[8] << " | " << square[9] <<  " | " << endl;
cout << "____________" << endl;
}


Enjoy!
If you need help with anything just ask here...
Last edited on
Topic archived. No new replies allowed.