描述

C++ 构造棋盘游戏类,初始化静态变量,判断函数,构造函数决定棋手

代码

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
305
306
307
308
309
310
311
312
#include<string>
#include<iostream>
using namespace std;


class CGobang
{
private :
char chSort;//棋子的类别
public:
int nWin;//赢棋次数
int nLose;//输棋次数
static int SpotsOnBoard;//下棋步数
static int nDraw;//平局次数,公用数据,所以用静态
static char board[4][4];//用数组来定义一个4*4的棋盘,静态元素,公用
static int nSize;//棋盘的尺寸nSize^nSize
CGobang(char chSort);//构造函数,决定一方棋子类别
void PlayTrue(void);//走一步棋
int Judge();//判断有没有三子连线,是返回1,否则0
void Win();//赢棋
void Lose();//输棋
static void Draw();//平局
void PrintInfo();//打印总体信息
void PrintBoard();//打印棋盘
int GetFull();//判断棋盘有没有布满
void IntitalBoard();//初始化棋盘
void Clear_but_reserve_score();//不清除分数

};
//---1初始化静态变量
char CGobang::board[4][4] = { {' ',' ',' ',' '},{' ',' ',' ',' '} ,{' ',' ',' ',' '},{' ',' ',' ',' '} };
int CGobang::nDraw = 0;
int CGobang::nSize = 16;
int CGobang::SpotsOnBoard = 0;
//--2

CGobang::CGobang(char m_chSort)

{

chSort = m_chSort;
nWin = 0;
nLose = 0;

}
void CGobang::Clear_but_reserve_score()
{
SpotsOnBoard = 0;//初始步数为0
///清空棋盘,棋盘初始化
for (int i = 0; i <= 3; i++)
{
for (int j = 0; j <= 3; j++)
{
board[i][j] = ' ';
}
}
///

}
void CGobang::IntitalBoard()
{
SpotsOnBoard = 0;//初始步数为0
nWin = 0;
nLose = 0;
///清空棋盘,棋盘初始化
for (int i = 0; i <= 3; i++)
{
for (int j = 0; j <= 3; j++)
{
board[i][j] = ' ';
}
}
///
}
void CGobang::Win()
{
cout << "玩家" << chSort << "胜利" << endl;
nWin++;
}
void CGobang::Lose()
{
cout << "玩家" << chSort << "失败" << endl;
nLose++;
}
void CGobang::Draw()
{
cout << "平局" << endl;
nDraw++;

}
int CGobang::Judge()//判断是否三子连心
{
int O_array[4][4];
for (int i = 0; i <= 3; i++)
{
for (int j = 0; j <= 3; j++)
if (board[i][j] == 'O')
O_array[i][j] = 1;
else
O_array[i][j] = 0;

}
int X_array[4][4];
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
if (board[i][j] == 'O')
X_array[i][j] = 1;
else
X_array[i][j] = 0;

}
//横
for (int i = 0; i <= 3; i++)
{
for (int j = 0; j <= 1; j++)
{
if ((O_array[i][j] & O_array[i][j + 1] & O_array[i][j + 2]) == 1)
{

return 1;

}

if ((X_array[i][j] & X_array[i][j + 1] & X_array[i][j + 2]) == 1)
{

return 2;
}
}



}
//竖
for (int j = 0; j <= 3; j++)
{
for (int i = 0; i <= 1; i++)
{
if ((O_array[i][j] & O_array[i + 1][j] & O_array[i + 2][j]) == 1)
{

return 1;

}

if ((X_array[i][j] & X_array[i + 1][j] & X_array[i + 2][j]) == 1)
{

return 2;
}
}



}
//正斜
for (int i = 0; i <= 1; i++)
{
for (int j = i; j <= 1; j++)
{
if ((O_array[i][j] & O_array[i + 1][j + 1] & O_array[i + 2][j + 2]) == 1)
return 1;
if ((X_array[i][j] & X_array[i + 1][j + 1] & X_array[i + 2][j + 2]) == 1)
return 2;
}

}
//反斜
for (int i = 3; i >= 2; i--)
{
for (int j = i; j <= 1; j++)
{
if ((O_array[i][j] & O_array[i -1][j + 1] & O_array[i -2][j + 2]) == 1)
return 1;
if ((X_array[i][j] & X_array[i -1][j + 1] & X_array[i - 2][j + 2]) == 1)
return 2;
}

}
return 0;
}
void CGobang::PrintInfo()
{
cout << "第一个玩家选择O棋子,第二个玩家选择X棋子." << endl;
cout << "输入坐标(x,y)." << endl;
cout << "敲击enter继续." << endl;
}
void CGobang::PlayTrue()
{

int x;
int y;
cout<< chSort << "玩家 " << endl;
cout << "横坐标;" << endl;
cin >> x;
cout << "纵坐标;" << endl;
cin >> y;
while (x> 4 || x < 1|| y > 4 || y < 1 || (board[x - 1][y - 1] != ' '))
{
cout << "输入错误: " << x << endl;
cout << "横坐标;" << endl;
cin>>x;
cout << "纵坐标;" << endl;
cin >> y;
}

board[x-1][y-1] = chSort;
SpotsOnBoard++;
}
void CGobang::PrintBoard()
{
////-4用for循环输出,而不是穷举输出,使代码更加简洁,便于升级
cout << endl;
cout << " 1 2 3 4" << endl;
for (int i = 0; i <=3; i++)
{
cout << i+1<<" ";
for (int j = 0; j <= 2; j++)
{
cout<<board[i][j] << " " << "|";
}
cout << board[i][3] << endl;

}
cout << endl << endl;
}
int CGobang::GetFull()
{

if (SpotsOnBoard == 16)
return 1;
else
return 0;

}

void ChoiceOfChar(char&);//询问玩家是否继续下一局
void PrintStats(CGobang O_player, CGobang X_player);//打印比分
int main()
{
system("chcp 65001");

char choice;
choice = 'y';//初始选择,可以让循环进行下去

CGobang O_player('O');//O玩家
CGobang X_player('X');//X玩家
O_player.PrintInfo();
O_player.IntitalBoard();
X_player.IntitalBoard();
while (choice == 'y' ) //判断继续循环的条件
{

while (O_player.SpotsOnBoard < 16)//小循环
{
if (O_player.SpotsOnBoard == 0)//这是一局新游戏,还没下棋
{
O_player.IntitalBoard();//初始化棋盘
}
O_player.PlayTrue();
O_player.PrintBoard();
if (O_player.Judge() == 1)
{
O_player.Win();
X_player.Lose();
break;//跳出小循环
}
X_player.PlayTrue();
X_player.PrintBoard();
if (X_player.Judge() == 2)
{
X_player.Lose();
X_player.Win();
break;//跳出小循环
}
if (CGobang::SpotsOnBoard == 16)
CGobang::Draw();//平局

}
PrintStats(O_player, X_player);//打印比分
ChoiceOfChar(choice);//一局结束,是否继续,如果是继续游戏
O_player.Clear_but_reserve_score();//清除棋子和步数,保留分数
X_player.Clear_but_reserve_score();

}
system("PAUSE");
return 0;




}
void PrintStats(CGobang O_player, CGobang X_player)//打印比分
{
cout << "O胜利场数:" << O_player.nWin<<endl;
cout << "x胜利场数:" << X_player.nWin<<endl;
cout << "O失败场数:" << O_player.nLose<<endl;
cout << "X失败场数:" << X_player.nLose<<endl;
cout << "平局场数:" << CGobang::nDraw<<endl;

}
void ChoiceOfChar(char& choice)//接收玩家输入是否继续下棋
{

cout << endl;
cout << "是否继续,是则按y,否则按其他任意键 " << endl;
cin >> choice;


}