public void PlayChess(int[] pos) { if (pos == null || isGameOver) { return; } // 限制当前值在0和Max_LINE之间 //pos[0] = Mathf.Clamp(pos[0], 0, Max_LINE); //pos[1] = Mathf.Clamp(pos[1], 0, Max_LINE); if (grid[pos[0], pos[1]] != (int)GameDefine.DotType.NONE) { Debug.Log("grid的值为:" + grid[pos[0], pos[1]]); return; } int i = Mathf.FloorToInt(Max_LINE / 2); if (curTurn == GameDefine.ChessType.Black) { // 生成黑棋子 GameObject prefabsB = Instantiate(chessPrefabs[0], new Vector3(pos[0] - i, pos[1] - i, 0), Quaternion.identity); NetworkServer.Spawn(prefabsB); Debug.Log("生成了黑色棋子"); chessStack.Push(prefabsB.transform); chessMap[pos[0], pos[1]] = prefabsB.transform; grid[pos[0], pos[1]] = (int)GameDefine.DotType.BLACK; if (CheckChess(pos)) { isGameOver = true; } else { curTurn = GameDefine.ChessType.White; timer = 0.0f; } } else if (curTurn == GameDefine.ChessType.White) { // 生成白棋子 GameObject prefabsW = Instantiate(chessPrefabs[1], new Vector3(pos[0] - i, pos[1] - i, 0), Quaternion.identity); NetworkServer.Spawn(prefabsW); Debug.Log("生成了白色棋子"); chessStack.Push(prefabsW.transform); chessMap[pos[0], pos[1]] = prefabsW.transform; grid[pos[0], pos[1]] = (int)GameDefine.DotType.WHITE; if (CheckChess(pos)) { isGameOver = true; } else { curTurn = GameDefine.ChessType.Black; timer = 0.0f; } } }
// 进行评分 protected virtual float CheckOneLine(int[] pos, int[] offpos, GameDefine.ChessType type) { string str = "a"; // 正向检测:跳过第一次循环,避免重复存储监测点 for (int i = pos[0] + offpos[0], j = pos[1] + offpos[1]; i >= 0 && i < ChessBoard.Max_LINE && j >= 0 && j < ChessBoard.Max_LINE; i += offpos[0], j += offpos[1]) { // 如果对应当前所下的棋子颜色 if (ChessBoard.Instance.GetGrid()[i, j] == (int)type) { str += "a"; } else { str += "_"; break; } } // 反向检测:跳过第一次循环,避免重复存储监测点 for (int i = pos[0] - offpos[0], j = pos[1] - offpos[1]; i >= 0 && i < ChessBoard.Max_LINE && j >= 0 && j < ChessBoard.Max_LINE; i -= offpos[0], j -= offpos[1]) { // 如果对应当前所下的棋子颜色 if (ChessBoard.Instance.GetGrid()[i, j] == (int)type) { str = "a" + str; } else { str = "_" + str; break; } } // 返回当前位置的得分 float temScore = 0; // 因为后面的分值高,所以采用倒序查询,减少遍历次数 foreach (string ikey in scoreDic.Keys) { if (str.Contains(ikey)) { temScore = scoreDic[ikey]; break; } } return(temScore); }
private float GetMaxScore(int[] pos, GameDefine.ChessType type) { float temScore = 0; temScore += CheckOneLine(pos, new int[2] { 1, 0 }, type); temScore += CheckOneLine(pos, new int[2] { 0, 1 }, type); temScore += CheckOneLine(pos, new int[2] { 1, -1 }, type); temScore += CheckOneLine(pos, new int[2] { 1, 1 }, type); return(scoreValue[pos[0], pos[1]] = temScore); }
private float GetMaxScore(int[,] grid, int[] pos, GameDefine.ChessType type) { float temScore = 0; temScore += CheckOneLine(grid, pos, new int[2] { 1, 0 }, type); temScore += CheckOneLine(grid, pos, new int[2] { 0, 1 }, type); temScore += CheckOneLine(grid, pos, new int[2] { 1, -1 }, type); temScore += CheckOneLine(grid, pos, new int[2] { 1, 1 }, type); return(temScore); }
public void CmdSetPlayer() { NetChessBoard.Instance.PlayerNumber++; Debug.Log("PlayerNumber:" + NetChessBoard.Instance.PlayerNumber); if (NetChessBoard.Instance.PlayerNumber == 1) { text.text += " 设置黑棋玩家"; chessType = GameDefine.ChessType.Black; } else if (NetChessBoard.Instance.PlayerNumber == 2) { text.text += " 设置白棋玩家"; chessType = GameDefine.ChessType.White; } else { text.text += " 设置观众玩家"; chessType = GameDefine.ChessType.Watch; } }
// 更换先手 public virtual void ChangeColor() { chessType = chessType == GameDefine.ChessType.Black ? GameDefine.ChessType.White : GameDefine.ChessType.Black; }
// 获取当前受益值最大或者最小Node列表 private List <AILevelNode> GetChildList(int[,] grid, bool isSelf, GameDefine.ChessType type) { List <AILevelNode> temList = new List <AILevelNode>(); AILevelNode node; // 获取三个极大或极小的子节点child; for (int i = 0; i < ChessBoard.Max_LINE; i++) { for (int j = 0; j < ChessBoard.Max_LINE; j++) { if (grid[i, j] != (int)GameDefine.DotType.NONE) { continue; } int[] pos = new int[2] { i, j }; node = new AILevelNode(); node.chessType = type; node.pos = pos; // 获取当前点的得分 if (isSelf) { // 最大分(当前位置的初始score = 白+黑的得分) node.score = (GetMaxScore(grid, pos, GameDefine.ChessType.Black) + GetMaxScore(grid, pos, GameDefine.ChessType.White)); } else { // 最小分 负号可取得最小值的点 node.score = (-GetMaxScore(grid, pos, GameDefine.ChessType.Black) - GetMaxScore(grid, pos, GameDefine.ChessType.White)); } // 取出极大极小点 if (temList.Count < 4) { temList.Add(node); } else { foreach (AILevelNode temNode in temList) { if (isSelf) { if (temNode.score < node.score) { temList.Remove(temNode); temList.Add(node); break; } } else { if (temNode.score > node.score) { temList.Remove(temNode); temList.Add(node); break; } } } } } } return(temList); }
// 进行评分 此时grid为假设的grid,在变化中,所以需要传入当前假设情况下的grid; protected virtual float CheckOneLine(int[,] grid, int[] pos, int[] offpos, GameDefine.ChessType type) { string str = "a"; int allTimes = 0; bool isTurnLeft = false; bool leftStop = false; bool rightStop = false; // 跳过第一次循环,避免重复存储监测点 int ri = pos[0] + offpos[0], rj = pos[1] + offpos[1]; int li = pos[0] - offpos[0], lj = pos[1] - offpos[1]; // 为了避免取到的均为空位,即“_”,所以限制取到空位时,调头取另一端所在位置的类型,若也是空位则再调头,就可取到有效的TimeLimit位数的str值 while (allTimes < ChessBoard.TimeLimit && (!leftStop || !rightStop)) { // 正向检测 if (!isTurnLeft) { if (ri >= 0 && ri < ChessBoard.Max_LINE && rj >= 0 && rj < ChessBoard.Max_LINE && !rightStop) { // 如果对应当前所下的棋子颜色 if (grid[ri, rj] == (int)type) { str += "a"; allTimes++; } // 如果对应为空棋子 else if (grid[ri, rj] == (int)GameDefine.DotType.NONE) { str += "_"; allTimes++; if (!leftStop) { isTurnLeft = true; } } // 被堵住了 else { rightStop = true; if (!leftStop) { isTurnLeft = true; } } ri += offpos[0]; rj += offpos[1]; } else { rightStop = true; if (!leftStop) { isTurnLeft = true; } } } // 反向检测 else { if (li >= 0 && li < ChessBoard.Max_LINE && lj >= 0 && lj < ChessBoard.Max_LINE && !leftStop) { // 如果对应当前所下的棋子颜色 if (grid[li, lj] == (int)type) { str = "a" + str; allTimes++; } else if (grid[li, lj] == (int)GameDefine.DotType.NONE) { str = "_" + str; allTimes++; if (!rightStop) { isTurnLeft = false; } } else { leftStop = true; if (!rightStop) { isTurnLeft = false; } } li -= offpos[0]; lj -= offpos[1]; } else { leftStop = true; if (!rightStop) { isTurnLeft = false; } } } } // 返回当前位置的得分 float temScore = 0; foreach (string ikey in scoreDic.Keys) { if (str.Contains(ikey)) { temScore = scoreDic[ikey]; break; } } return(temScore); }