|
| 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 | +} |
0 commit comments