public void UnHighlightLastMove() { if (LastMove == null) { Thread t = new Thread(new ThreadStart(SetDefaultBackground)); t.Start(); return; } UcChessCell cell = this.arrChessCell[LastMove.CurPos.X, LastMove.CurPos.Y]; cell.UnHighlightMove(); this.arrChessCell[LastMove.NewPos.X, LastMove.NewPos.Y].UnHighlightMove(); }
public void UnHighlightMoves() { if (arrMove.Count == 0) { return; } foreach (Point p in arrMove) { UcChessCell cell = this.arrChessCell[p.X, p.Y]; cell.UnHighlightMove(); } this.HighlightLastMove(); }
public UcChessPiece(ChessSide Side, ChessPieceType Type, ChessPieceStyle Style, int intCellSize, int intPieceSize, int PositionX, int PositionY, UcChessCell UcOnCell) { InitializeComponent(); this._Side = Side; this._Type = Type; this._Style = Style; this._image = clsImageProcess.GetChessPieceBitMap(_Side, _Type, _Style); this._PieceSize = intPieceSize; this._CellSize = intCellSize; this.Size = new Size(this._PieceSize, this._PieceSize); this._PositionX = PositionX; this._PositionY = PositionY; this._ucOnCell = UcOnCell; }
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ờ } } }
public void HighlightLastMove() { if (LastMove == null) { return; } UcChessCell cell = this.arrChessCell[LastMove.CurPos.X, LastMove.CurPos.Y]; cell.ShowLastMove(); cell = this.arrChessCell[LastMove.NewPos.X, LastMove.NewPos.Y]; cell.ShowLastMove(); this.DrawToBitmap(bmpBackImage, new Rectangle(0, 0, this.Width, this.Height)); Thread t = new Thread(new ThreadStart(SetBackGround)); t.Start(); }
public void HighlightPossibleMoves() { if (arrMove.Count == 0) { return; } foreach (Point p in arrMove) { UcChessCell cell = this.arrChessCell[p.X, p.Y]; cell.HighlightPossibleMove(); } this.DrawToBitmap(bmpBackImage, new Rectangle(0, 0, this.Width, this.Height)); Thread t = new Thread(new ThreadStart(SetBackGround)); t.Start(); }
/* * Góc dưới bên trái của bàn cờ vua là một ô đen * Quân Hậu Trắng sẽ nằm trên ô Trắng và Quân Hậu Đen sẽ nằm trên ô đen * Bàn cờ được đánh tọa độ như sau : * Từ A->I tính từ trái sang phải * Từ 1->8 tính từ dưới lên bắt đầu từ dòng đầu tiên của quân trắng * */ /* * Nếu x+y là số lẻ thì đó là ô Trắng * Nếu x+y là số chẵn thì đó là ô Đen */ public void CreateChessBoard() { for (int i = 1; i <= 8; i++)//-> tạo ô cờ từ trên xuông dưới từ trái sang phải { for (int j = 1; j <= 8; j++) { UcChessCell Cell; if ((9 - i + j) % 2 == 1)//ô Trắng { Cell = new UcChessCell(j, 9 - i, this._WhiteCellBitmap, ChessSide.White, this._BoardStyle); } else { Cell = new UcChessCell(j, 9 - i, this._BlackCellBitmap, ChessSide.Black, this._BoardStyle); } Cell.Size = new Size(this._CellSize, this._CellSize); Cell.Name = Convert.ToChar(64 + j).ToString() + (9 - i);//65 là kí tự A if (this._OwnSide == ChessSide.White) { Cell.Location = new Point((j - 1) * _CellSize, (i - 1) * _CellSize); } else { Cell.Location = new Point((8 - j) * _CellSize, (8 - i) * _CellSize); } arrChessCell[j, 9 - i] = Cell;//Đưa vào mảng các ô cờ this.Controls.Add(Cell); } } }
public void DoMove(UcChessPiece Piece, clsMove move) { if ((Piece.Side == ChessSide.Black && this._WhiteToMove == true) || (Piece.Side == ChessSide.White && this._WhiteToMove == false)) { return; } Point CurPos = move.CurPos; Point NewPos = move.NewPos; UcChessCell ct = (UcChessCell)this.arrChessCell[NewPos.X, NewPos.Y]; //*********************************Kiểm tra nước đi************************ //Kiểm tra Xem Quân Cờ có thể di chuyển từ CurPos đến NewPos hay không if (clsChessEngine.CanMove(arrMove, NewPos) == true) { UnHighlightLastMove(); PushState(); string strLastCell = Convert.ToChar(CurPos.X + 64) + CurPos.Y.ToString(); string strNewCell = Convert.ToChar(NewPos.X + 64) + NewPos.Y.ToString(); this._BoardState[CurPos.X, CurPos.Y] = 0;//Đánh dấu vị trí cũ không chứa quân cờ this._BoardState[NewPos.X, NewPos.Y] = (int)Piece.Type * 10 + (int)Piece.Side; //Kiểm tra xem quân vua có bị chiếu không //****************Chưa tính trường hợp chiếu bí và Hòa*************** bool bCapture = false; //Lấy vị trí Bắt tôt qua đường nếu có if (Piece.Type == ChessPieceType.Pawn) { if (Math.Abs(NewPos.Y - CurPos.Y) == 2) { _EnPassantPoint = clsPawn.GetEnPassantPoint(this._BoardState, NewPos); } else if (CurPos.X == NewPos.X) { _EnPassantPoint = new Point(); } //Thực hiện nước bắt tốt qua đường if (NewPos == _EnPassantPoint) { Point CapturedPoint; if (Piece.Side == ChessSide.White) { CapturedPoint = new Point(_EnPassantPoint.X, _EnPassantPoint.Y - 1); } else { CapturedPoint = new Point(_EnPassantPoint.X, _EnPassantPoint.Y + 1); } UcChessCell bt = arrChessCell[CapturedPoint.X, CapturedPoint.Y]; this._BoardState[CapturedPoint.X, CapturedPoint.Y] = 0;//Đánh dấu vị trí cũ không chứa quân cờ stkCapturedPieces.Push(bt.ChessPiece); bt.ChessPiece.Hide(); bt.ChessPiece = null; _EnPassantPoint = new Point(); bCapture = true; } } else { _EnPassantPoint = new Point(); } if (ct.ChessPiece != null && ct.ChessPiece.Side != Piece.Side)//Nếu đã có quân cờ đặt tại ô đến { UcChessPiece CapturedPiece = ct.ChessPiece; stkCapturedPieces.Push(CapturedPiece); CapturedPiece.Hide();//Xóa quân cờ tại ô Di chuyển đến if (CapturedPiece.Type == ChessPieceType.Rook) { if (CapturedPiece.Side == ChessSide.White) { if (CapturedPiece.PositionX == 1) { QUEENsideCastling = false; } if (CapturedPiece.PositionX == 8) { KINGsideCastling = false; } } else { if (CapturedPiece.PositionX == 1) { queensideCastling = false; } if (CapturedPiece.PositionX == 8) { kingsideCastling = false; } } } ct.ChessPiece = null; bCapture = true; } Piece.UConCell.ChessPiece = null;//Ô cờ trước đó ko còn chứa quân cờ này nữa //Dat Quan Co Tai Vi Tri Moi ct.ChessPiece = Piece; Piece.UConCell = ct; //Gán tọa độ mới cho quân cờ Piece.PositionX = NewPos.X; Piece.PositionY = NewPos.Y; string strPromote = ""; if (Piece.Type == ChessPieceType.Pawn) { if (clsPawn.Promotion(Piece, move.PromoteTo) == true) { this._BoardState[Piece.PositionX, Piece.PositionY] = (int)Piece.Type * 10 + (int)Piece.Side; switch (Piece.Type) { case ChessPieceType.Bishop: strPromote = "=B"; break; case ChessPieceType.Knight: strPromote = "=N"; break; case ChessPieceType.Rook: strPromote = "=R"; break; case ChessPieceType.Queen: strPromote = "=Q"; break; } move.PromoteTo = Piece.Type; } } if (bCapture == true) { PushMove(Piece.Side, strLastCell + "X" + strNewCell + strPromote); OnPieceCaptured(EventArgs.Empty); PlayMusic(CapturedSound); } else { PushMove(Piece.Side, strLastCell + "-" + strNewCell + strPromote); PlayMusic(MovedSound); } //Cập Nhật trạng thái nhập thành if (Piece.Type == ChessPieceType.Rook) { if (Piece.Side == ChessSide.White) { if (Piece.PositionX == 1) { QUEENsideCastling = false; } if (Piece.PositionX == 8) { KINGsideCastling = false; } } else { if (Piece.PositionX == 1) { queensideCastling = false; } if (Piece.PositionX == 8) { kingsideCastling = false; } } } if (Piece.Type == ChessPieceType.King) { if (Piece.Side == ChessSide.White) { QUEENsideCastling = false; KINGsideCastling = false; } else { queensideCastling = false; kingsideCastling = false; } if (Math.Abs(NewPos.X - CurPos.X) == 2)//Nhập Thành { if (NewPos.X == 3) { UcChessCell OldRookCell = (UcChessCell)this.arrChessCell[1, NewPos.Y]; UcChessCell NewRookCell = (UcChessCell)this.arrChessCell[4, NewPos.Y]; this._BoardState[1, NewPos.Y] = 0; //Đánh dấu quân xe không còn ở vị trí cũ this._BoardState[4, NewPos.Y] = 40 + (int)Piece.Side; //Đặt quân xe tại vị trí mới NewRookCell.ChessPiece = OldRookCell.ChessPiece; OldRookCell.ChessPiece = null; NewRookCell.ChessPiece.UConCell = NewRookCell; NewRookCell.ChessPiece.PositionX = NewRookCell.PositionX; NewRookCell.ChessPiece.PositionY = NewRookCell.PositionY; } else { UcChessCell OldRookCell = (UcChessCell)this.arrChessCell[8, NewPos.Y]; UcChessCell NewRookCell = (UcChessCell)this.arrChessCell[6, NewPos.Y]; this._BoardState[8, NewPos.Y] = 0; //Đánh dấu quân xe không còn ở vị trí cũ this._BoardState[6, NewPos.Y] = 40 + (int)Piece.Side; //Đặt quân xe tại vị trí mới NewRookCell.ChessPiece = OldRookCell.ChessPiece; OldRookCell.ChessPiece = null; NewRookCell.ChessPiece.UConCell = NewRookCell; NewRookCell.ChessPiece.PositionX = NewRookCell.PositionX; NewRookCell.ChessPiece.PositionY = NewRookCell.PositionY; } } } arrFEN.Add(clsFEN.GetPiecePlacementString(this._BoardState)); //********Play Sound, Hiển thị thông báo************* this._GameStatus = clsChessEngine.GetGameStatus(this._BoardState, arrFEN, Piece.Side); if (this._GameStatus == GameStatus.BlackWin) { PlayMusic(CheckMatedSound); DisplayMessage("Quân Đen Thắng !"); } if (this._GameStatus == GameStatus.WhiteWin) { PlayMusic(CheckMatedSound); DisplayMessage("Quân Trắng Thắng !"); } if (this._GameStatus == GameStatus.Draw) { if (clsChessEngine.CheckThreefoldRepetition(arrFEN)) { DisplayMessage("Ván cờ Hòa do \"Bất Biến 3 Lần\""); } else if (clsChessEngine.CheckInsufficientMaterial(this._BoardState, Piece.Side)) { DisplayMessage("Ván cờ Hòa do cả 2 bên không đủ quân chiếu bí đối phương"); } else { DisplayMessage("Ván cờ Hòa do không còn nước đi hợp lệ"); } } if (this._GameStatus == GameStatus.NowPlaying && (clsKing.IsChecked(this._BoardState, ChessSide.Black) == true || clsKing.IsChecked(this._BoardState, ChessSide.White) == true)) { PlayMusic(CheckedSound); if ((this._OwnSide == ChessSide.White && this._WhiteToMove == false) || (this._OwnSide == ChessSide.Black && this._WhiteToMove == true)) { DisplayMessage("Chiếu !"); } } if (Piece.Side == this._OwnSide) { AlreadyMakeMove = true; } //Chuyển lượt đi cho đối phương if (Piece.Side == ChessSide.Black) { this.WhiteToMove = true; } else { this.WhiteToMove = false; } //Cho máy đi if (Piece.Side == this.OwnSide && this.GameMode == GameMode.VsComputer) { IsThinking = true; timerComputerMove.Enabled = true; } LastMove = move; HighlightLastMove(); OnMoveMaked(EventArgs.Empty); } else { Piece.UConCell.ChessPiece = Piece;// tra về ô cũ } }
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; }