public static string GetFEN(UcChessBoard Board) { string FEN = ""; string strActiveSide = ""; string strCastling = ""; string strEnPassant = "-"; int intHalfMoveClock = Board.HalfMoveClock; int intFullMoveNumBer = Board.FullMovesNumber; string strPiecePlacemen = GetPiecePlacementString(Board._BoardState); if (Board.WhiteToMove == true) { strActiveSide = "w"; } else { strActiveSide = "b"; } if (UcChessBoard._EnPassantPoint != new Point()) { strEnPassant = Board.arrChessCell[UcChessBoard._EnPassantPoint.X, UcChessBoard._EnPassantPoint.Y].Name.ToLower(); } if (UcChessBoard.KINGsideCastling == true) { strCastling += "K"; } if (UcChessBoard.QUEENsideCastling == true) { strCastling += "Q"; } if (UcChessBoard.kingsideCastling == true) { strCastling += "k"; } if (UcChessBoard.queensideCastling == true) { strCastling += "q"; } if (strCastling == "") { strCastling = "-"; } FEN = strPiecePlacemen + " " + strActiveSide + " " + strCastling + " " + strEnPassant + " " + intHalfMoveClock + " " + intFullMoveNumBer; return(FEN); }
private void UcChessPiece_MouseUp(object sender, MouseEventArgs e) { if (UcChessBoard.IsCapturedMode) { return; } UcChessBoard Board = (UcChessBoard)this.Parent;//Lấy bàn cờ if (this.Side == Board.OwnSide || Board.GameMode == GameMode.VsHuman) { if ((this.Side == ChessSide.White && Board.WhiteToMove == true) || (this.Side == ChessSide.Black && Board.WhiteToMove == false)) { Board.UnHighlightMoves(); //*********************************Lấy tọa độ ô cờ*********************** //Lấy hai vị trí trọng tâm của PieceSize/2 int a = (this.Location.X + (this._PieceSize / 2)) / this._CellSize; int b = (this.Location.Y + (this._PieceSize / 2)) / this._CellSize; UcChessCell ct = new UcChessCell(); ///////CODECHANGE NGÀY 0605 string n; int NewPos_X; int NewPos_Y; if (Board.OwnSide == ChessSide.White) { n = Convert.ToChar(65 + a).ToString() + (8 - b);//65 là kí tự A NewPos_X = a + 1; NewPos_Y = 8 - b; } else { n = Convert.ToChar(64 + 8 - a).ToString() + (b + 1);//65 là kí tự A NewPos_X = 8 - a; NewPos_Y = b + 1; } Point NewPos = new Point(NewPos_X, NewPos_Y); Point CurPos = new Point(this._PositionX, this._PositionY); Board.DoMove(this, new clsMove(CurPos, NewPos)); //Di chuyển quân cờ } } }
private void UcChessPiece_MouseDown(object sender, MouseEventArgs e) { if (UcChessBoard.IsCapturedMode) { return; } LastMousePos = MousePosition; UcChessBoard Board = (UcChessBoard)this.Parent;//Lấy bàn cờ Board.arrMove = new ArrayList(); //Hiển thị các nước có thể đi if ((Board.OwnSide == this.Side || Board.GameMode == GameMode.VsHuman) && Board.GameStatus == GameStatus.NowPlaying) { if ((Board.WhiteToMove == true && this._Side == ChessSide.White) || (Board.WhiteToMove == false && this._Side == ChessSide.Black)) { int[,] BoardState = Board._BoardState;//Lấy trạng thái bàn cờ Point CurPos = new Point(this._PositionX, this._PositionY); Board.arrMove = clsChessEngine.FindAllLegalMove(BoardState, CurPos, this._Type); Board.HighlightPossibleMoves(); } } }
private void UcChessPiece_MouseMove(object sender, MouseEventArgs e) { if (UcChessBoard.IsCapturedMode) { return; } this.BringToFront(); this.Cursor = Cursors.Hand; UcChessBoard Board = (UcChessBoard)this.Parent;//Lấy bàn cờ if (this.Side == Board.OwnSide || Board.GameMode == GameMode.VsHuman) { if ((this.Side == ChessSide.White && Board.WhiteToMove == true) || (this.Side == ChessSide.Black && Board.WhiteToMove == false)) { if (MouseButtons == MouseButtons.Left) { CurrentMousePos = MousePosition; int x, y; x = CurrentMousePos.X - LastMousePos.X; y = CurrentMousePos.Y - LastMousePos.Y; Point ControlPos = this.Location; ControlPos.X += x; ControlPos.Y += y; ///Không cho phép di chuyển con cờ ra ngoài biên if (ControlPos.X < 0) { ControlPos = new Point(0, ControlPos.Y); Cursor.Position = LastMousePos; return; } if (ControlPos.Y < 0) { ControlPos = new Point(ControlPos.X, 0); Cursor.Position = LastMousePos; return; } if (ControlPos.X + this.Width > this.Parent.Width) { ControlPos = new Point(this.Parent.Width - this.Width, ControlPos.Y); Cursor.Position = LastMousePos; return; } if (ControlPos.Y + this.Height > this.Parent.Height) { ControlPos = new Point(ControlPos.X, this.Parent.Height - this.Height); Cursor.Position = LastMousePos; return; } this.Location = ControlPos; LastMousePos = CurrentMousePos; } else { this.Cursor = Cursors.Hand; } } } }
public static void SetFEN(UcChessBoard Board, string strFEN) { strFEN = strFEN.Trim(); string[] s = strFEN.Split(' '); if (s.Length == 1) { SetPiecePlacement(Board._BoardState, strFEN); return; } string strPiecePlacemen = s[0]; string strActiveSide = s[1]; string strCastling = s[2]; string strEnPassant = s[3]; int intHalfMoveClock = 0; int intFullMoveNumBer = 0; if (s.Length == 5) { intHalfMoveClock = Convert.ToInt32(s[4]); } if (s.Length == 6) { intFullMoveNumBer = Convert.ToInt32(s[5]); } SetPiecePlacement(Board._BoardState, strPiecePlacemen); if (strActiveSide.ToUpper() == "W") { Board.WhiteToMove = true; } else { Board.WhiteToMove = false; } UcChessBoard.KINGsideCastling = false; UcChessBoard.kingsideCastling = false; UcChessBoard.QUEENsideCastling = false; UcChessBoard.queensideCastling = false; if (strCastling != "-") { foreach (char c in strCastling) { switch (c) { case 'Q': UcChessBoard.QUEENsideCastling = true; break; case 'K': UcChessBoard.KINGsideCastling = true; break; case 'q': UcChessBoard.queensideCastling = true; break; case 'k': UcChessBoard.kingsideCastling = true; break; } } } if (strEnPassant != "-") { UcChessCell cell = (UcChessCell)Board.Controls.Find(strEnPassant, true)[0]; UcChessBoard._EnPassantPoint = new Point(cell.PositionX, cell.PositionY); } else { UcChessBoard._EnPassantPoint = new Point(); } Board.HalfMoveClock = intHalfMoveClock; }