forked from samuelowino/TicTac-ML
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tictacmain.cpp
269 lines (206 loc) · 5.57 KB
/
tictacmain.cpp
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
#include <iostream>
#include <string>
using namespace std;
const int P1[2] = {0,0};
const int P2[2] = {0,1};
const int P3[2] = {0,2};
const int P4[2] = {2,0};
const int P5[2] = {2,1};
const int P6[2] = {2,2};
const int P7[2] = {3,0};
const int P8[2] = {3,1};
const int P9[2] = {3,2};
/**
* This check whether one of the players
* has won the game round, or if the players
* are stuck in a tie.
*
* @returns char representing either
* X - player X winds
* O - O player wins
* T - Players are in a tie
* N - None of the players won
* */
char check_winning_player(const string tic_tac_board_state[3][3]);
/**
* Defines playing rules and strategies
* to AI player
*
* 1. If human player has a "threat" (that is, two in a row), take the remaining square. Otherwise
* 2. If a move "forks" to create two threats at once, play that move. Otherwise
* 3. take the center square if it is freee. Otherwise
* 4. if your opponent has played in a corner, take the opposite corner. Otherwise,
* 5. take an empty corner if one exists. Otherwise
* 6. take any empty square.
*
**/
/**
* Maps the specified cell index with the player code
*
* **/
void update_board_state(const string cell_index[3][3],const char& player_code);
void outline_strategy_to_machine_player(const string tic_tac_board_state[3][3]);
/**
* Validate move and reverse if neccessary
* Move is invalid if the following happens;
* 1.The cell is not empty
* 2.The game is over
* 3.Check turn to play
*
* @param string[3][3] board state
*
* @returns const int
*
* VALID_MOVE = 200
* INVALID_MOVE = 500
* OUT_OF_MOVES = 404
*
* */
int validate_move(const char move[2], const string tic_tac_board_state[3][3]);
/**
*Draws the board in its updated state on screen when called
*@param board_state 3-D matrix
***/
void draw_board(const string tic_tac_board_state[3][3]);
void get_player_move();
void updateBoard(string slected_point);
char human_player_code = 'X';
char machine_player_code = 'O';
char player_move[6];
string board_state[3][3] = {
{"0,0","0,1","0,2"},
{"1,0","1,1","1,2"},
{"2,0","2,1","2,2"}
};
int main(){
cout<<"=============================== " <<endl;
cout<<"| TIC-TAC-TOE |"<<endl;
cout<<"=============================== " <<endl;
cout<<"Select Player Code:" <<endl;
cout<<"Player X [X] "<<endl;
cout<<"Player O [O] "<<endl;
cout<<"===================================================="<<endl;
cin.get(human_player_code);
switch(human_player_code){
case '0':
cout<<"Computer | X"<<endl;
cout<<"You are O "<<endl;
machine_player_code='X';
human_player_code = 'O';
break;
case 'X':
cout<<"You are [Xs]"<<endl;
cout<<"Computer is [Os]"<<endl;
machine_player_code='O';
break;
case 'x':
cout<<"you are [Xs]"<<endl;
cout<<"computer is [Os]"<<endl;
machine_player_code='O';
break;
case 'o':
cout<<"you are [Os]"<<endl;
cout<<"Computer is [Xs]"<<endl;
human_player_code='O';
machine_player_code='X';
case 'O':
cout<<"You are [Os]"<<endl;
cout<<"Computer is [Xs]"<<endl;
machine_player_code='X';
break;
default:
cout<<"Computer is [Xs]"<<endl;
cout<<"You are [Os]"<<endl;
break;
}
cout << " ============ START =============== \n"<<endl;
draw_board(board_state);
cout<<"======== Player Xs Move ==========="<<endl;
get_player_move();
//cin.get(player_move,2);
validate_move(player_move, board_state);
draw_board(board_state);
// board_state
//define rules to machine player
//check if one player has won
//check tie
//draw board before move
//draw board after move
//draw board at end of game with results summary
}
void get_player_move(){
cin.ignore();
cin.get(player_move,6);
cout<<"Before 2nd ignore move == ["<<player_move[0]<<"] ["<<player_move[2]<<"]"<<endl;
cin.ignore();
cout<<"After 2nd ignore move == ["<<player_move[0]<<"] ["<<player_move[2]<<"]"<<endl;
}
/**
*Draws the board in its updated state on screen when called
*@param board_state 3-D matrix
***/
void draw_board(const string tic_tac_board_state[3][3]){
for(int i = 0; i<3;i++){
cout<<" ---------------------- \n";
for(int j = 0; j<3;j++){
cout<<" | ";
if(tic_tac_board_state[i][j] == "X"){
cout<<" X ";
}else if(tic_tac_board_state[i][j] == "O"){
cout<<" O ";
}else
cout<<" "<<tic_tac_board_state[i][j]<<" ";
}
cout<<" | "<<endl;
}
cout<<" ---------------------- \n";
cout<<endl;
}
int validate_move(const char move[6], const string board_state[3][3],char& player_code){
cout<<move[1]<<endl;
string existing_value;
/**
* Check if index is used; if used warn the user and re-run game play
* if not used; call update_board_state(cell_index, and value to write)
* **/
for(int i = 0; i<3;i++){
//column
for(int j = 0; j<3;j++){
//row
if(j = move[2] && i = move[0]){
if(board_state[i][j] != "X" && board_state[i][j] != "O"){
update_board_state(board_state[i][j],player_code);
}else
cout<<"This index is already used, try a diffrent cell"<<endl;
break;
}
}
}
/**
if(move[1] == '0'){
existing_value = board_state[0][0];
cout<<"Existing value == " <<existing_value;
}else if(move[1] == '1'){
existing_value = board_state[0][1];
cout<<"Existing value == "<< existing_value;
}
else if(move[1] == '2'){
existing_value = board_state[0][2];
cout<<"Val =="<< existing_value;
}
else if (move[1] == '3'){
existing_value = board_state[1][0];
cout<<"val =="<< ex
}
else if (move[1] == '4'){
}
else if (move[1] == '5'){
}
else if (move[1] == '6'){
}
else if (move[1] == '7'){
}
else if (move[1] == '8'){
}else
cout<<"I dont know"; **/
}