Skip to content

Commit d79af32

Browse files
committed
五子棋AI初步完成
1 parent 583da34 commit d79af32

File tree

10 files changed

+568
-46
lines changed

10 files changed

+568
-46
lines changed

Assets/Prefab/07-gameTree/Canvas.prefab

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,8 @@ MonoBehaviour:
117117
m_Calls: []
118118
m_TypeName: UnityEngine.UI.MaskableGraphic+CullStateChangedEvent, UnityEngine.UI,
119119
Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
120-
m_Sprite: {fileID: 21300000, guid: 646ff42f487a44c42baa2a3b9fe2a639, type: 3}
121-
m_Type: 0
120+
m_Sprite: {fileID: 10917, guid: 0000000000000000f000000000000000, type: 0}
121+
m_Type: 1
122122
m_PreserveAspect: 0
123123
m_FillCenter: 1
124124
m_FillMethod: 4
@@ -196,6 +196,7 @@ MonoBehaviour:
196196
piecePrefab: {fileID: 1173731510901354}
197197
Black: {fileID: 21300000, guid: d7dbedad51921334995c768d5af2aad1, type: 3}
198198
White: {fileID: 21300000, guid: 646ff42f487a44c42baa2a3b9fe2a639, type: 3}
199+
around: 0
199200
--- !u!114 &114620794079083108
200201
MonoBehaviour:
201202
m_ObjectHideFlags: 1

Assets/Scene/07-gameTree.unity

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,11 @@ Prefab:
275275
propertyPath: m_Pivot.y
276276
value: 0
277277
objectReference: {fileID: 0}
278+
- target: {fileID: 114534604070485664, guid: 599ac715c85810f4485b51072b9f59c4,
279+
type: 2}
280+
propertyPath: Simple
281+
value:
282+
objectReference: {fileID: 10917, guid: 0000000000000000f000000000000000, type: 0}
278283
m_RemovedComponents: []
279284
m_ParentPrefab: {fileID: 100100000, guid: 599ac715c85810f4485b51072b9f59c4, type: 2}
280285
m_IsPrefabParent: 0
Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using UnityEngine;
6+
7+
namespace LinHoweGameTree
8+
{
9+
/// <summary>
10+
/// 棋型
11+
/// </summary>
12+
public enum ChessType
13+
{
14+
None,
15+
连五,
16+
活四,
17+
冲四,
18+
活三,
19+
眠三,
20+
活二,
21+
眠二,
22+
活一,
23+
眠一
24+
}
25+
/// <summary>
26+
/// 评估类
27+
/// </summary>
28+
public class Evaluation
29+
{
30+
//检查越界
31+
Func<int, int, bool> OutOfRange =
32+
(x, y) =>
33+
x >= 0 && x < 15 && y >= 0 && y < 15;
34+
35+
//四个方向计数 水平、垂直、左斜、右斜
36+
public readonly static Lazi[][] direction = new[]
37+
{
38+
new[]{ new Lazi(-1, 0), new Lazi(1, 0) },
39+
new[]{ new Lazi(0 ,-1), new Lazi(0, 1) },
40+
new[]{ new Lazi(-1, 1), new Lazi(1,-1) },
41+
new[]{ new Lazi(-1,-1), new Lazi(1, 1) }
42+
};
43+
44+
45+
//棋盘信息
46+
private int[,] _board;
47+
48+
//是否胜利
49+
public bool win = false;
50+
public bool lose = false;
51+
52+
public int scoreSum;
53+
54+
public int Evaluate(int[,] board)
55+
{
56+
//Init
57+
_board = board;
58+
win = false;
59+
lose = false;
60+
scoreSum = 0;
61+
62+
List<List<int>> graph = new List<List<int>>();
63+
64+
for (int i =0;i<15;++i)
65+
{
66+
List<int> line = new List<int>();
67+
for (int j =0;j<15;++j)
68+
{
69+
70+
line.Add(_board[0 + direction[0][1].x * j, i + direction[0][1].y * j]);
71+
}
72+
graph.Add(line);
73+
}
74+
for (int i = 0; i < 15; ++i)
75+
{
76+
List<int> line = new List<int>();
77+
for (int j = 0; j < 15; ++j)
78+
{
79+
80+
line.Add(_board[i + direction[1][1].x * j, 0 + direction[1][1].y * j]);
81+
}
82+
graph.Add(line);
83+
}
84+
for (int i = 0; i < 15; ++i)
85+
{
86+
List<int> line = new List<int>();
87+
for (int j = 0; j < 15; ++j)
88+
{
89+
if (!OutOfRange(i + direction[2][1].x * j, 14 + direction[2][1].y * j)) break;
90+
line.Add(_board[i + direction[2][1].x * j, 14 + direction[2][1].y * j]);
91+
}
92+
graph.Add(line);
93+
line = new List<int>();
94+
for (int j = 0; j < 15; ++j)
95+
{
96+
if (!OutOfRange(0 + direction[2][1].x * j, i + direction[2][1].y * j)) break;
97+
line.Add(_board[0 + direction[2][1].x * j, i + direction[2][1].y * j]);
98+
}
99+
graph.Add(line);
100+
101+
}
102+
for (int i = 0; i < 15; ++i)
103+
{
104+
List<int> line = new List<int>();
105+
for (int j = 0; j < 15; ++j)
106+
{
107+
if (!OutOfRange(i + direction[3][1].x * j, 0 + direction[3][1].y * j)) break;
108+
line.Add(_board[i + direction[3][1].x * j, 0 + direction[3][1].y * j]);
109+
}
110+
graph.Add(line);
111+
line = new List<int>();
112+
for (int j = 0; j < 15; ++j)
113+
{
114+
if (!OutOfRange(0 + direction[3][1].x * j, i + direction[3][1].y * j)) break;
115+
line.Add(_board[0 + direction[3][1].x * j, i + direction[3][1].y * j]);
116+
}
117+
graph.Add(line);
118+
119+
}
120+
foreach(var line in graph)
121+
{
122+
for (int i = 0; i < line.Count; i++)
123+
{
124+
int count = 0; // 连子数
125+
int block = 0; // 封闭数
126+
int value = 0; //分数
127+
128+
if (line[i] == -1)
129+
{ // 发现第一个己方棋子
130+
count = 1;
131+
block = 0;
132+
if (i == 0) block = 1;
133+
else if (line[i - 1] != 0) block = 1;
134+
for (i = i + 1; i < line.Count; i++)
135+
{
136+
if (line[i] == -1) count++;
137+
else break;
138+
}
139+
if (i == line.Count || line[i] != 0) block++;
140+
value += score(count, block);
141+
}
142+
scoreSum += value;
143+
}
144+
}
145+
foreach (var line in graph)
146+
{
147+
for (int i = 0; i < line.Count; i++)
148+
{
149+
int count = 0; // 连子数
150+
int block = 0; // 封闭数
151+
int value = 0; //分数
152+
153+
if (line[i] == 1)
154+
{ // 发现第一个己方棋子
155+
count = 1;
156+
block = 0;
157+
if (i == 0) block = 1;
158+
else if (line[i - 1] != 0) block = 1;
159+
for (i = i + 1; i < line.Count; i++)
160+
{
161+
if (line[i] == 1) count++;
162+
else break;
163+
}
164+
if (i == line.Count || line[i] != 0) block++;
165+
value += score(count, block);
166+
}
167+
scoreSum -= value;
168+
}
169+
}
170+
return scoreSum;
171+
}
172+
173+
private int score(int count,int block) {
174+
175+
if(count >= 5) return 10000000;
176+
177+
if(block == 0) {
178+
switch(count) {
179+
case 1: return 10;
180+
case 2: return 100;
181+
case 3: return 1000;
182+
case 4: return 100000;
183+
}
184+
}
185+
186+
if(block == 1) {
187+
switch(count) {
188+
case 1: return 1;
189+
case 2: return 10;
190+
case 3: return 100;
191+
case 4: return 10000;
192+
}
193+
}
194+
195+
return 0;
196+
}
197+
198+
199+
200+
}
201+
}

Assets/Scripts/07-gameTree/AI/Evaluation.cs.meta

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)