/
game.cpp
307 lines (275 loc) · 5.95 KB
/
game.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#include "MySocket.h"
#include "connect.h"
int game::start()
{
char stone[3][3]={"","●","○"}; // initialize stone
int totalturn = 0, turn2 = 0; //turn2: flag for two turn
initialize();
while(decideWinner() == 0) //yet winner occur
{
if(turn2==2)
{//after place two stones
//change player
if (turn == 2)
turn = 1;
else
turn = 2;
turn2=0; //initialize flag
setting::gotoxy(0, 21);
cout<<stone[turn]<<"'s turn";
}
while(1)
{
input(&y, &x,turn);//input position to move
if(board[y][x]==1 || board[y][x]==2)
{
setting::gotoxy(0, 24);
cout<<"please input valied position";
setting::gotoxy(0, 23);
cout<<" ";
}
else
break;
}
// go(&y, &x); //for debug
put(y, x, turn);
turn2++;
if(turn==1) //turn==white, print total num of white stone
{
setting::gotoxy(42, 0);
cout <<"White Stone : "<< ++turn1Cnt;
}
else //turn==black, print total num of black stone
{
setting::gotoxy(42, 1);
cout << "Black Stone : "<<++turn2Cnt;
}
setting::gotoxy(42, 2);
cout <<"Total : "<<++totalturn;
}
return winner_Message(turn^=3);
}
// int game::start_ai()
// {
// char stone[3][3]={"","●","○"};
// initialize();
// while(decideWinner() == 0) //yet winner occur
// {
// if(turn2==2)
// {//after place two stones
// //change player
// if (turn == 2)
// turn = 1;
// else
// turn = 2;
// turn2=0; //initialize flag
// setting::gotoxy(0, 21);
// cout<<stone[turn]<<"'s turn";
// }
// //else turn2++;
// while(turn==1 && turn2<2)//user
// {
// go(&y, &x);
// put(y, x, turn);
// //input(&y, &x,turn);//input position to move
// update_total_stone_num();
// turn2++;
// }
// while(turn==2 && turn2<2)//ai
// {
// // ai->place_point();
// // ai->place_stone(turn);
// update_total_stone_num();
// turn2++;
// }
// }
// return winner_Message(turn^=3);
// }
int game::start_network(int gameMode, MySocket& player, game& gameData)
{
char stone[3][3]={"","●","○"}; // initialize stone
map();
//turn==1: white, turn==2: black
int totalturn = 0, turn2 = 0; //turn2: flag for two turn
turn = 2; //first turn is black
setting::Cursor();
first_move(&y, &x); //first randum move
while(decideWinner() == 0) //yet winner occur
{
if(turn2==1)
{//after place two stones
//change player
if (turn == 2)
turn = 1;
else
turn = 2;
turn2=0; //initialize flag
}
else turn2++;
setting::gotoxy(0, 21);
cout<<stone[turn]<<"'s turn";
while(!put(y, x, turn)) //
{
//go(&y, &x); //
if (gameMode == SE){
if (turn == 2){
input(&y, &x, turn);//input position to move
player.SendData(&gameData);
}
else if (turn == 1){
player.RecvData(&gameData);
}
}
if (gameMode == CL){
if (turn == 2){
player.RecvData(&gameData);
}
else if (turn == 1){
input(&y, &x, turn);//input position to move
player.SendData(&gameData);
}
}
}
if(turn==1) //turn==white, print num of white stone
{
setting::gotoxy(42, 0);
cout <<"White Stone : "<< ++turn1Cnt;
}
else //turn==black, print num of black stone
{
setting::gotoxy(42, 1);
cout << "Black Stone : "<<++turn2Cnt;
}
setting::gotoxy(42, 2);
cout <<"Total : "<<++totalturn;
}
return winner_Message(turn^=3);
}
void game::initialize()//draw board and put first stone
{
map();
//turn==1: white, turn==2: black
turn = 2; //first turn is black
setting::Cursor();
first_move(&y, &x); //first randum move
put(y, x, turn);
turn=1;//next is white turn
setting::gotoxy(42, 1);
cout << "Black Stone : "<<++turn2Cnt;
//while (1);//for debug
}
void game::change_turn()
{
char stone[3][3]={"","●","○"}; // initialize stone
if(turn2==1)
{//after place two stones
//change player
if (turn == 2)
turn = 1;
else
turn = 2;
turn2=0; //initialize flag
}
else turn2++;
setting::gotoxy(0, 21);
cout<<stone[turn]<<"'s turn";
}
void game::update_total_stone_num()
{
if(turn==1) //turn==white, print num of white stone
{
setting::gotoxy(42, 0);
cout <<"White Stone : "<< ++turn1Cnt;
}
else //turn==black, print num of black stone
{
setting::gotoxy(42, 1);
cout << "Black Stone : "<<++turn2Cnt;
}
setting::gotoxy(42, 2);
cout <<"Total : "<<++totalturn;
}
//print board
void game::map()
{
memset(board, 0, sizeof(int)*17*17); //
//corner
strcpy(pan[0][0], "┌");
strcpy(pan[0][16] ,"┐");
strcpy(pan[16][0],"└");
strcpy(pan[16][16],"┘");
//side
for(y=1; y<16; y++)
{
strcpy(pan[y][0], "├");
strcpy(pan[y][16],"┤");
}
//bottom and top
for(x=1; x<16; x++)
{
strcpy(pan[0][x], "┬");
strcpy(pan[16][x],"┴");
}
//else
for(y=1; y<16; y++)
for(x=1; x<16; x++)
strcpy(pan[y][x],"┼");
system("CLS"); //
for(y=0; y<17; y++,cout<<endl)
for(x=0; x<17; x++)
cout<<pan[y][x];
//position number
setting::gotoxy(0, 17);//bottom board number(1st liine)
cout << " ";
for(int i=1;i<18;i++)
{
setting::gotoxy(i*2-1, 17);
if (i<10)
cout<< i;
else
cout << '1';
}
setting::gotoxy(0, 18);//bottom board number(2nd liine)
cout << " ";
for (int i = 1; i<18; i++)
{
setting::gotoxy(i * 2 - 1, 18);
if (i>9)
cout << i-10;
}
for (int i = 1; i<18; i++)//right borad number
{
setting::gotoxy(34, i-1);
cout << i;
}
}
int game::put(int y_, int x_, int turn)
{
char stone[3][3]={"","●","○"};
if(board[y_][x_] == 0) //
{
x=x_; y=y_;
board[y_][x_]=turn; //
strcpy(pan[y_][x_], stone[turn]); //
setting::gotoxy(x_*2, y_);
cout<<stone[turn];
//
return 1;
}
else if (board[y_][x_] != 0){
if (board[y_][x_] == 1){
setting::gotoxy(x_ * 2, y_);
cout << stone[1];
}
else{
setting::gotoxy(x_ * 2, y_);
cout << stone[2];
}
}
return 0;
}
void game::pause()
{
_getch(); //
puts(""); //
}