[HELP!] Artificial intelligence tic-tac-toe

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


#include<iostream> // my first EVER game 27/03/12 // update in 08/04/12.Attempting to make an artificial intellgence in it.
#include<ctime>
using namespace std;

int main() {


char board[3][3] = {0,0,0,0,0,0,0,0,0};
char mark;
int turn = 0;
int player;
bool gameover = false;


do {
    
cout << board[0][0] << "  |  " <<  board[0][1] << " |  " << board[0][2] << endl;
cout << "------------" << endl;
cout << board[1][0] << "  |  " <<  board[1][1] << " |  " << board[1][2] << endl;
cout << "------------" << endl;
cout << board[2][0] << "  |  " <<  board[2][1] << " |  " << board[2][2] << endl; // the board

turn++;


if(turn % 2 == 0) {

player = 2;
mark = 'O'; // the 'turn'
}

else {
     
player = 1;
mark = 'X';

}

int x = 0,y = 0;
bool valid = false;

do {
if(player == 2) {

          
srand(time(0));


x = rand() % 2 + 1; // inperfect artificial intelligence
y = rand() % 2 + 1;

}






else {

cout << "enter row (1 - 3)." << endl;
cin >> x;

cout << "enter column (1 - 3)." << endl;
cin >> y;

}





if(x == 1 && y == 1 && board[0][0] == 0) { // start of the move check
        
board[0][0] = mark;
valid = true;

}

else if(x == 1 && y == 2 && board[0][1] == 0) {
     
board[0][1] = mark;
valid = true;

}

else if(x == 1 && y == 3 && board[0][2] == 0) {
     
board[0][2] = mark;
valid = true;

}

else if(x == 2 && y == 1 && board[1][0] == 0) {
     
board[1][0] = mark;
valid = true;

}

else if(x == 2 && y == 2 && board[1][1] == 0) {
     
board[1][1] = mark;
valid = true;

}

else if(x == 2 && y == 3 && board[1][2] == 0) {
     
board[1][2] = mark;
valid = true;

}

else if(x == 3 && y == 1 && board[2][0] == 0) {
     
board[2][0] = mark;
valid = true;

}

else if(x == 3 && y == 2 && board[2][1] == 0) {
     
board[2][1] = mark;
valid = true;

}

else if(x == 3 && y == 3 && board[2][2] == 0) {
     
board[2][2] = mark;
valid = true;

} // end of move check.


else {
     
cout << "invalid move,try again." << endl;
valid = false;
}

}while(valid == false); // loop until the move is valid.


if (board[0][0] == mark && board[0][1] == mark && board[0][2] == mark) { // all the first row complete
                
cout << "player" << player << " wins!" << endl;
gameover = true;

}

else if (board[1][0] == mark && board[1][1] == mark && board[1][2] == mark) { // all the second row complete
                
cout << "player" << player << " wins!" << endl;
gameover = true;

}

else if (board[2][0] == mark && board[2][1] == mark && board[2][2] == mark) { // all the third row complete
                
cout << "player" << player << " wins!" << endl;
gameover = true;

}

else if (board [0][0] == mark && board[1][0] == mark && board[2][0] == mark) { // all the first row complete
     
cout << "player" << player << " wins!" << endl;
gameover = true;

}

else if (board[0][1] == mark && board[1][1] == mark && board[2][1] == mark) { // all the second row complete
                
cout << "player" << player << " wins!" << endl;
gameover = true;

}

else if (board[0][2] == mark && board[1][2] == mark && board[1][2] == mark) { // all the third row complete
                
cout << "player" << player << " wins!" << endl;
gameover = true;

}

else if (board[0][0] == mark && board[1][1] == mark && board[2][2] == mark) { // horizontal combination
                
cout << "player" << player << " wins!" << endl;
gameover = true;

}

else if (board[2][0] == mark && board[1][1] == mark && board[0][2]== mark) { // another horizontal combination
                
cout << "player" << player << " wins!" << endl;
gameover = true;

}

else if (board[0][0] != 0 && board[0][1] != 0 &&  board[0][2] != 0 &&  board[1][0] != 0 &&   // draw situation 
board[1][1] != 0 &&  board[1][2] != 0 &&  board[2][0] != 0 &&  board[2][1] != 0 &&  board[2][2] != 0 ) {
            
cout << "draw for both players!" << endl;
gameover = true;

}

}while(gameover == false);

cout << board[0][0] << "  |  " <<  board[0][1] << " |  " << board[0][2] << endl;
cout << "------------" << endl;
cout << board[1][0] << "  |  " <<  board[1][1] << " |  " << board[1][2] << endl;
cout << "------------" << endl;
cout << board[2][0] << "  |  " <<  board[2][1] << " |  " << board[2][2] << endl;




system("pause");
return 0;
}






Okay.I'll be totally honest with you.It doesn't work.Yeah,it does the job,it IS artificial intelligence,just a dumb one.I have no idea how to make it check player's moves.I'm a beginner,so please understand and help me.I really need help.Thank you in advance.
if i remember correctly there is an artificial intelligence engine called minimax. that is a very simple artificial intelligence that is really well made for this. also using functions and/or classes makes this so much simpler.
Minimax huh...I guess I went to far ahead beyond my abilities.I'll probaly leave this project for a while until I grasp more knowledge from C++.Thank you for you reply: D
Topic archived. No new replies allowed.