/// <summary> /// 游戏初始化 /// </summary> public void Inix(bool isPair) { isBlack = true; isOver = false; hasWinner = false; focusChess = null; lastBlack = null; lastWhite = null; chessMen = new ChessMan[15, 15]; Power = new int[15, 15]; number = 0; this.isPair = isPair; }
//显示最新下的棋子 private void ShowTheNewChess(ChessMan chessMan) { if (chessMan == null) { return; } Pen pen = new Pen(chessMan.IsBlack ? Color.White : Color.Black); Graphics gp = lblGame.CreateGraphics(); int x = chessMan.X * 30; int y = chessMan.Y * 30; gp.DrawLine(pen, x + 8, y + 15, x + 13, y + 15); gp.DrawLine(pen, x + 17, y + 15, x + 22, y + 15); gp.DrawLine(pen, x + 15, y + 8, x + 15, y + 13); gp.DrawLine(pen, x + 15, y + 17, x + 15, y + 22); gp.Dispose(); }
void gobang_PutChessManEvent(ChessMan chessMan) { if (chessMan == null) { return; } theLastChess = theNewChess; theNewChess = chessMan; Graphics gp = lblGame.CreateGraphics(); gp.DrawImage(chessMan.IsBlack ? Resource1.black : Resource1.white, chessMan.X * 30 + 2, chessMan.Y * 30 + 2, 26, 26); if (theLastChess != null) { gp.DrawImage(chessMan.IsBlack ? Resource1.white : Resource1.black, theLastChess.X * 30 + 2, theLastChess.Y * 30 + 2, 26, 26); } gp.Dispose(); }
/// <summary> /// 计算周围5各单位内的各点的权值 /// </summary> private void CountPower(ChessMan chessMan) { if (isPair) { return; } int minx = Math.Max(chessMan.X - 5, 0); int maxx = Math.Min(chessMan.X + 5, 14); int miny = Math.Max(chessMan.Y - 5, 0); int maxy = Math.Min(chessMan.Y + 5, 14); for (int i = minx; i <= maxx; i++) { for (int j = miny; j <= maxy; j++) { if (chessMen[i, j] == null) { Power[i, j] = GetPower(i, j); } } } }
/// <summary> /// 下子 /// </summary> /// <param name="chessMan"></param> public void PutChessMan(ChessMan chessMan) { if (hasWinner || isOver) { return; } focusChess = chessMan; this.chessMen[chessMan.X, chessMan.Y] = chessMan; if (chessMan.IsBlack) { lastBlack = chessMan; } else { lastWhite = chessMan; } if (PutChessManEvent != null) { PutChessManEvent(chessMan); } if (ChangeFocusEvent != null) { ChangeFocusEvent(focusChess); } if (IsWin(chessMan)) { hasWinner = true; return; } else if (number++ == 225) { isOver = true; return; } CountPower(chessMan); isBlack = !isBlack; }
/// <summary> /// b是否已经胜利 /// </summary> /// <param name="pos"></param> /// <param name="b"></param> /// <returns></returns> public bool IsWin(ChessMan chessMan) { int x = chessMan.X, y = chessMan.Y; bool b = chessMan.IsBlack; int count = 0; for (int i = 0; i < 5 && x - i >= 0; i++) { if (IsInBoard(x - i, y, b)) { count++; } else { break; } } for (int i = 1; i < 5 && x + i < 15; i++) { if (IsInBoard(x + i, y, b)) { count++; } else { break; } } if (count > 4) { return(true); } else { count = 0; } for (int i = 0; i < 5 && y - i >= 0; i++) { if (IsInBoard(x, y - i, b)) { count++; } else { break; } } for (int i = 1; i < 5 && y + i < 15; i++) { if (IsInBoard(x, y + i, b)) { count++; } else { break; } } if (count > 4) { return(true); } else { count = 0; } for (int i = 0; i < 5 && x - i >= 0 && y - i >= 0; i++) { if (IsInBoard(x - i, y - i, b)) { count++; } else { break; } } for (int i = 1; i < 5 && x + i < 15 && y + i < 15; i++) { if (IsInBoard(x + i, y + i, b)) { count++; } else { break; } } if (count > 4) { return(true); } else { count = 0; } for (int i = 0; i < 5 && x - i >= 0 && y + i < 15; i++) { if (IsInBoard(x - i, y + i, b)) { count++; } else { break; } } for (int i = 1; i < 5 && y - i >= 0 && x + i < 15; i++) { if (IsInBoard(x + i, y - i, b)) { count++; } else { break; } } if (count > 4) { return(true); } return(false); }