-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathchess.cs
More file actions
419 lines (407 loc) · 17.9 KB
/
chess.cs
File metadata and controls
419 lines (407 loc) · 17.9 KB
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
///**********************************************************************
///作者信息
///开发者:雁回晴空
///时间:2017.01.11
///联系方式:http://blog.csdn.net/zzlyw/article/details/54345250
///**********************************************************************
using UnityEngine;
using System.Collections;
using System;
public class chess : MonoBehaviour
{
//四个锚点位置,用于计算棋子落点
public GameObject LeftTop;
public GameObject RightTop;
public GameObject LeftBottom;
public GameObject RightBottom;
//主摄像机
public Camera cam;
//锚点在屏幕上的映射位置
Vector3 LTPos;
Vector3 RTPos;
Vector3 LBPos;
Vector3 RBPos;
Vector3 PointPos;//当前点选的位置
float gridWidth = 1; //棋盘网格宽度
float gridHeight = 1; //棋盘网格高度
float minGridDis; //网格宽和高中较小的一个
Vector2[,] chessPos; //存储棋盘上所有可以落子的位置
int[,] chessState; //存储棋盘位置上的落子状态
enum turn { black, white };
turn chessTurn; //落子顺序
public Texture2D white; //白棋子
public Texture2D black; //黑棋子
public Texture2D blackWin; //白子获胜提示图
public Texture2D whiteWin; //黑子获胜提示图
int winner = 0; //获胜方,1为黑子,-1为白子
bool isPlaying = true; //是否处于对弈状态
void Start()
{
chessPos = new Vector2[15, 15];
chessState = new int[15, 15];
chessTurn = turn.black;
//计算锚点位置
LTPos = cam.WorldToScreenPoint(LeftTop.transform.position);
RTPos = cam.WorldToScreenPoint(RightTop.transform.position);
LBPos = cam.WorldToScreenPoint(LeftBottom.transform.position);
RBPos = cam.WorldToScreenPoint(RightBottom.transform.position);
//计算网格宽度
gridWidth = (RTPos.x - LTPos.x) / 14;
gridHeight = (LTPos.y - LBPos.y) / 14;
minGridDis = gridWidth < gridHeight ? gridWidth : gridHeight;
//计算落子点位置
for (int i = 0; i < 15; i++)
{
for (int j = 0; j < 15; j++)
{
chessPos[i, j] = new Vector2(LBPos.x + gridWidth * i, LBPos.y + gridHeight * j);
}
}
}
void Update()
{
//检测鼠标输入并确定落子状态
if (isPlaying && Input.GetMouseButtonDown(0))
{
PointPos = Input.mousePosition;
for (int i = 0; i < 15; i++)
{
for (int j = 0; j < 15; j++)
{
//找到最接近鼠标点击位置的落子点,如果空则落子
if (Dis(PointPos, chessPos[i, j]) < minGridDis / 2 && chessState[i, j] == 0)
{
//根据下棋顺序确定落子颜色
chessState[i, j] = chessTurn == turn.black ? 1 : -1;
//落子成功,更换下棋顺序
chessTurn = chessTurn == turn.black ? turn.white : turn.black;
}
}
}
//调用判断函数,确定是否有获胜方
int re = result();
if (re == 1)
{
Debug.Log("黑棋胜");
winner = 1;
isPlaying = false;
}
else if (re == -1)
{
Debug.Log("白棋胜");
winner = -1;
isPlaying = false;
}
}
//按下空格重新开始游戏
if (Input.GetKeyDown(KeyCode.Space))
{
for (int i = 0; i < 15; i++)
{
for (int j = 0; j < 15; j++)
{
chessState[i, j] = 0;
}
}
isPlaying = true;
chessTurn = turn.black;
winner = 0;
}
}
//计算平面距离函数
float Dis(Vector3 mPos, Vector2 gridPos)
{
return Mathf.Sqrt(Mathf.Pow(mPos.x - gridPos.x, 2) + Mathf.Pow(mPos.y - gridPos.y, 2));
}
void OnGUI()
{
//绘制棋子
for (int i = 0; i < 15; i++)
{
for (int j = 0; j < 15; j++)
{
if (chessState[i, j] == 1)
{
GUI.DrawTexture(new Rect(chessPos[i, j].x - gridWidth / 2, Screen.height - chessPos[i, j].y - gridHeight / 2, gridWidth, gridHeight), black);
}
if (chessState[i, j] == -1)
{
GUI.DrawTexture(new Rect(chessPos[i, j].x - gridWidth / 2, Screen.height - chessPos[i, j].y - gridHeight / 2, gridWidth, gridHeight), white);
}
}
}
//根据获胜状态,弹出相应的胜利图片
if (winner == 1)
{
GUI.DrawTexture(new Rect(Screen.width * 0.25f, Screen.height * 0.25f, Screen.width * 0.5f, Screen.height * 0.25f), blackWin);
Debug.Log("repeat");
}
if (winner == -1)
GUI.DrawTexture(new Rect(Screen.width * 0.25f, Screen.height * 0.25f, Screen.width * 0.5f, Screen.height * 0.25f), whiteWin);
}
//使用异常来简化原来的result函数
int result()
{
int flag = 0;
if (chessTurn == turn.white)
{
for (int i = 0; i<=14; i++)
{
for (int j = 0; j <= 14; j++)
{
try
{
if ((chessState[i, j] == 1 && chessState[i, j + 1] == 1 && chessState[i, j + 2] == 1&& chessState[i, j + 3] == 1 && chessState[i, j + 4] == 1)
|| (chessState[i, j] == 1&& chessState[i + 1, j] == 1 && chessState[i + 2, j] == 1 && chessState[i + 3, j] == 1&& chessState[i + 4, j] == 1)
|| (chessState[i, j] == 1 && chessState[i + 1, j + 1] == 1&& chessState[i + 2, j + 2] == 1 && chessState[i + 3, j + 3] == 1 && chessState[i + 4, j + 4] == 1)
|| (chessState[i, j] == 1 && chessState[i + 1, j - 1] == 1 && chessState[i + 2, j - 2] == 1&& chessState[i + 3, j - 3] == 1 && chessState[i + 4, j - 4] == 1))
{
flag = 1;
}
}
catch (Exception ee)
{
Debug.Log(ee);
}
}
}
}
else if (chessTurn == turn.black)
{
for (int i = 0; i <= 14; i++)
{
for (int j = 0; j <= 14; j++)
{
try
{
if ((chessState[i, j] == -1 && chessState[i, j + 1] == -1 && chessState[i, j + 2] == -1 && chessState[i, j + 3] == -1 && chessState[i, j + 4] == -1)
|| (chessState[i, j] == -1&& chessState[i + 1, j] == -1 && chessState[i + 2, j] == -1 && chessState[i + 3, j] == -1&& chessState[i + 4, j] == -1)
|| (chessState[i, j] == -1 && chessState[i + 1, j + 1] == -1&& chessState[i + 2, j + 2] == -1 && chessState[i + 3, j + 3] == -1 && chessState[i + 4, j + 4] == -1)
|| (chessState[i, j] == -1 && chessState[i + 1, j - 1] == -1 && chessState[i + 2, j - 2] == -1&& chessState[i + 3, j - 3] == -1 && chessState[i + 4, j - 4] ==-1))
{
flag = -1;
}
}
catch (Exception ee)
{
Debug.Log(ee);
}
}
}
}
return flag;
}
/*
//检测是够获胜的函数,不含黑棋禁手检测
int result()
{
int flag = 0;
//如果当前该白棋落子,标定黑棋刚刚下完一步,此时应该判断黑棋是否获胜
if (chessTurn == turn.white)
{
for (int i = 0; i < 11; i++)
{
for (int j = 0; j < 15; j++)
{
if (j < 4)
{
//横向
if (chessState[i, j] == 1 && chessState[i, j + 1] == 1 && chessState[i, j + 2] == 1 && chessState[i, j + 3] == 1 && chessState[i, j + 4] == 1)
{
flag = 1;
return flag;
}
//纵向
if (chessState[i, j] == 1 && chessState[i + 1, j] == 1 && chessState[i + 2, j] == 1 && chessState[i + 3, j] == 1 && chessState[i + 4, j] == 1)
{
flag = 1;
return flag;
}
//右斜线
if (chessState[i, j] == 1 && chessState[i + 1, j + 1] == 1 && chessState[i + 2, j + 2] == 1 && chessState[i + 3, j + 3] == 1 && chessState[i + 4, j + 4] == 1)
{
flag = 1;
return flag;
}
//左斜线
//if (chessState[i, j] == 1 && chessState[i + 1, j - 1] == 1 && chessState[i + 2, j - 2] == 1 && chessState[i + 3, j - 3] == 1 && chessState[i + 4, j - 4] == 1)
//{
// flag = 1;
// return flag;
//}
}
else if (j >= 4 && j < 11)
{
//横向
if (chessState[i, j] == 1 && chessState[i, j + 1] == 1 && chessState[i, j + 2] == 1 && chessState[i, j + 3] == 1 && chessState[i, j + 4] == 1)
{
flag = 1;
return flag;
}
//纵向
if (chessState[i, j] == 1 && chessState[i + 1, j] == 1 && chessState[i + 2, j] == 1 && chessState[i + 3, j] == 1 && chessState[i + 4, j] == 1)
{
flag = 1;
return flag;
}
//右斜线
if (chessState[i, j] == 1 && chessState[i + 1, j + 1] == 1 && chessState[i + 2, j + 2] == 1 && chessState[i + 3, j + 3] == 1 && chessState[i + 4, j + 4] == 1)
{
flag = 1;
return flag;
}
//左斜线
if (chessState[i, j] == 1 && chessState[i + 1, j - 1] == 1 && chessState[i + 2, j - 2] == 1 && chessState[i + 3, j - 3] == 1 && chessState[i + 4, j - 4] == 1)
{
flag = 1;
return flag;
}
}
else
{
//横向
//if (chessState[i, j] == 1 && chessState[i, j + 1] == 1 && chessState[i, j + 2] == 1 && chessState[i, j + 3] == 1 && chessState[i, j + 4] == 1)
//{
// flag = 1;
// return flag;
//}
//纵向
if (chessState[i, j] == 1 && chessState[i + 1, j] == 1 && chessState[i + 2, j] == 1 && chessState[i + 3, j] == 1 && chessState[i + 4, j] == 1)
{
flag = 1;
return flag;
}
//右斜线
//if (chessState[i, j] == 1 && chessState[i + 1, j + 1] == 1 && chessState[i + 2, j + 2] == 1 && chessState[i + 3, j + 3] == 1 && chessState[i + 4, j + 4] == 1)
//{
// flag = 1;
// return flag;
//}
//左斜线
if (chessState[i, j] == 1 && chessState[i + 1, j - 1] == 1 && chessState[i + 2, j - 2] == 1 && chessState[i + 3, j - 3] == 1 && chessState[i + 4, j - 4] == 1)
{
flag = 1;
return flag;
}
}
}
}
for (int i = 11; i < 15; i++)
{
for (int j = 0; j < 11; j++)
{
//只需要判断横向
if (chessState[i, j] == 1 && chessState[i, j + 1] == 1 && chessState[i, j + 2] == 1 && chessState[i, j + 3] == 1 && chessState[i, j + 4] == 1)
{
flag = 1;
return flag;
}
}
}
}
//如果当前该黑棋落子,标定白棋刚刚下完一步,此时应该判断白棋是否获胜
else if (chessTurn == turn.black)
{
for (int i = 0; i < 11; i++)
{
for (int j = 0; j < 15; j++)
{
if (j < 4)
{
//横向
if (chessState[i, j] == -1 && chessState[i, j + 1] == -1 && chessState[i, j + 2] == -1 && chessState[i, j + 3] == -1 && chessState[i, j + 4] == -1)
{
flag = -1;
return flag;
}
//纵向
if (chessState[i, j] == -1 && chessState[i + 1, j] == -1 && chessState[i + 2, j] == -1 && chessState[i + 3, j] == -1 && chessState[i + 4, j] == -1)
{
flag = -1;
return flag;
}
//右斜线
if (chessState[i, j] == -1 && chessState[i + 1, j + 1] == -1 && chessState[i + 2, j + 2] == -1 && chessState[i + 3, j + 3] == -1 && chessState[i + 4, j + 4] == -1)
{
flag = -1;
return flag;
}
//左斜线
//if (chessState[i, j] == -1 && chessState[i + 1, j - 1] == -1 && chessState[i + 2, j - 2] == -1 && chessState[i + 3, j - 3] == -1 && chessState[i + 4, j - 4] == -1)
//{
// flag = -1;
// return flag;
//}
}
else if (j >= 4 && j < 11)
{
//横向
if (chessState[i, j] == -1 && chessState[i, j + 1] == -1 && chessState[i, j + 2] == -1 && chessState[i, j + 3] == -1 && chessState[i, j + 4] == -1)
{
flag = -1;
return flag;
}
//纵向
if (chessState[i, j] == -1 && chessState[i + 1, j] == -1 && chessState[i + 2, j] == -1 && chessState[i + 3, j] == -1 && chessState[i + 4, j] == -1)
{
flag = -1;
return flag;
}
//右斜线
if (chessState[i, j] == -1 && chessState[i + 1, j + 1] == -1 && chessState[i + 2, j + 2] == -1 && chessState[i + 3, j + 3] == -1 && chessState[i + 4, j + 4] == -1)
{
flag = -1;
return flag;
}
//左斜线
if (chessState[i, j] == -1 && chessState[i + 1, j - 1] == -1 && chessState[i + 2, j - 2] == -1 && chessState[i + 3, j - 3] == -1 && chessState[i + 4, j - 4] == -1)
{
flag = -1;
return flag;
}
}
else
{
//横向
//if (chessState[i, j] == -1 && chessState[i, j + 1] ==- 1 && chessState[i, j + 2] == -1 && chessState[i, j + 3] == -1 && chessState[i, j + 4] == -1)
//{
// flag = -1;
// return flag;
//}
//纵向
if (chessState[i, j] == -1 && chessState[i + 1, j] == -1 && chessState[i + 2, j] == -1 && chessState[i + 3, j] == -1 && chessState[i + 4, j] == -1)
{
flag = -1;
return flag;
}
//右斜线
//if (chessState[i, j] == -1 && chessState[i + 1, j + 1] == -1 && chessState[i + 2, j + 2] == -1 && chessState[i + 3, j + 3] == -1 && chessState[i + 4, j + 4] == -1)
//{
// flag = -1;
// return flag;
//}
//左斜线
if (chessState[i, j] == -1 && chessState[i + 1, j - 1] == -1 && chessState[i + 2, j - 2] == -1 && chessState[i + 3, j - 3] == -1 && chessState[i + 4, j - 4] == -1)
{
flag = -1;
return flag;
}
}
}
}
for (int i = 11; i < 15; i++)
{
for (int j = 0; j < 11; j++)
{
//只需要判断横向
if (chessState[i, j] == -1 && chessState[i, j + 1] == -1 && chessState[i, j + 2] == -1 && chessState[i, j + 3] == -1 && chessState[i, j + 4] == -1)
{
flag = -1;
return flag;
}
}
}
}
return flag;
}*/
}