Ejemplo n.º 1
0
        ///// <summary>
        ///// Trả về độ di động của phe ta trừ phe địch
        ///// </summary>
        //static int Mobility(int[,] BoardState)
        //{

        //    int intSide = 0;
        //    if (Myside == ChessSide.White)
        //    {
        //        intSide = 2;
        //    }
        //    else
        //    {
        //        intSide = 1;
        //    }
        //    int intMobility = 0;
        //    for (int y = 1; y <= 8; y++)
        //        for (int x = 1; x <= 8; x++)
        //            if (BoardState[x, y] > 0)
        //            {
        //                int side = BoardState[x, y] % 10;

        //                int intType = BoardState[x, y] / 10;
        //                if (side == intSide)
        //                {
        //                    intMobility += FindAllLegalMove(BoardState, new Point(x, y), (ChessPieceType)intType).Count;
        //                }
        //                else
        //                {
        //                    intMobility -= FindAllLegalMove(BoardState, new Point(x, y), (ChessPieceType)intType).Count;
        //                }
        //            }
        //    return intMobility;
        //}

        public static ArrayList FindAllLegalMove(int[,] arrState, Point CurPos, ChessPieceType eType)
        {
            ArrayList arrPossibleMove = FindAllPossibleMove(arrState, CurPos, eType);

            if (arrPossibleMove.Count == 0)
            {
                return(arrPossibleMove);
            }

            ArrayList arrLegalMove = new ArrayList();

            //Những nước đi làm cho quân Vua phe mình bị chiếu được xem là không hợp lệ
            int[,] arrNewState = new int[10, 10];
            Array.Copy(arrState, arrNewState, arrState.Length);
            ChessSide eSide = (ChessSide)(arrState[CurPos.X, CurPos.Y] % 10);

            foreach (Point p in arrPossibleMove)
            {
                int tmp = arrNewState[p.X, p.Y];                                //Quân cờ tại vị trí mới
                arrNewState[p.X, p.Y]           = (int)eType * 10 + (int)eSide; //Thay quân cờ tại vị trí mới
                arrNewState[CurPos.X, CurPos.Y] = 0;                            //Xóa quân cờ tại vị trí cũ

                if (clsKing.IsChecked(arrNewState, eSide) == false)
                {
                    arrLegalMove.Add(p);
                }
                arrNewState[CurPos.X, CurPos.Y] = arrNewState[p.X, p.Y]; //Cho quân cờ quay lại vị trí cũ
                arrNewState[p.X, p.Y]           = tmp;                   //Trả lại quân cờ nằm ở vị trí mới
            }
            return(arrLegalMove);
        }
Ejemplo n.º 2
0
        /// <param name="eBoardStyle">Kiểu bàn cờ</param>
        /// <param name="ePieceStyle">Kiểu quân cờ</param>
        /// <param name="eOwnSide">Quân Mà Người Chơi Chọn</param>
        /// <param name="eGameMode">Chế Độ Chơi</param>
        /// <param name="CellSize">Kích thước ô cờ</param>
        /// <param name="PieceSize">Kích thước quân cờ (Mặc định nếu =0)</param>
        /// <param name="bPlaySound">Âm Thanh</param>
        /// <param name="strFEN">Forsyth-Edwards Notation</param>
        /// Chơi Với Người
        public UcChessBoard(ChessBoardStyle eBoardStyle, ChessPieceStyle ePieceStyle, ChessSide eOwnSide, GameMode eGameMode, int CellSize, int PieceSize, bool bPlaySound, string strFEN)
        {
            InitializeComponent();
            this.Size             = new Size(CellSize * 8, CellSize * 8);
            this._BlackCellBitmap = clsImageProcess.GetChessBoardBitMap(ChessSide.Black, eBoardStyle);
            this._WhiteCellBitmap = clsImageProcess.GetChessBoardBitMap(ChessSide.White, eBoardStyle);

            this._CellSize   = CellSize;
            this._PieceSize  = PieceSize;
            this._PieceStyle = ePieceStyle;
            this._BoardStyle = eBoardStyle;
            this._PlaySound  = bPlaySound;
            this._OwnSide    = eOwnSide;
            this._GameMode   = eGameMode;


            CreateChessBoard();       //Tạo các ô cờ
            kingsideCastling  = true; //Nhập thành gần, quân đen
            queensideCastling = true; //Nhập thành xa, quân đen

            KINGsideCastling  = true; //Nhập thành gần, quân trắng
            QUEENsideCastling = true; //Nhập thành xa, quân trắng
            _EnPassantPoint   = new Point();
            clsFEN.SetFEN(this, strFEN);
            AddChessPiece(ePieceStyle, this._BoardState);
        }
Ejemplo n.º 3
0
 public static int GetPositionValue(Point pos, ChessSide eSide, bool IsEndGame)
 {
     if (IsEndGame == false)
     {
         if (eSide == ChessSide.Black)
         {
             return(KingTable[pos.Y, pos.X]);
         }
         else
         {
             return(KingTable[9 - pos.Y, 9 - pos.X]);
         }
     }
     else
     {
         if (eSide == ChessSide.Black)
         {
             return(KingTableEndGame[pos.Y, pos.X]);
         }
         else
         {
             return(KingTableEndGame[9 - pos.Y, 9 - pos.X]);
         }
     }
 }
Ejemplo n.º 4
0
    public void SetCheck(ChessSide side)
    {
        string message = "Check for " + side.ToString();

        Debug.Log(message);
        MainMenu.Singleton.AppendConsoleText(message);
    }
Ejemplo n.º 5
0
 public static int GetPositionValue(Vector2 pos, ChessSide eSide, bool IsEndGame)
 {
     if (IsEndGame == false)
     {
         if (eSide == ChessSide.Black)
         {
             return(KingTable[(int)(int)pos.y, (int)(int)pos.x]);
         }
         else
         {
             return(KingTable[9 - (int)(int)pos.y, 9 - (int)(int)pos.x]);
         }
     }
     else
     {
         if (eSide == ChessSide.Black)
         {
             return(KingTableEndGame[(int)pos.y, (int)pos.x]);
         }
         else
         {
             return(KingTableEndGame[9 - (int)pos.y, 9 - (int)pos.x]);
         }
     }
 }
Ejemplo n.º 6
0
 public ComputerPlayer(ChessSide side, byte skill, IChessUCIEngine chessUCIEngine) : base(PlayerType.computer, side)
 {
     this.skill          = skill;
     this.chessUCIEngine = chessUCIEngine;
     this.chessUCIEngine.StartEngine();
     this.chessUCIEngine.SetSkill(skill);
 }
Ejemplo n.º 7
0
        private void btnBegin_Click(object sender, EventArgs e)
        {
            if (cboGameMode.SelectedIndex == 0)
            {
                eGameMode = GameMode.VsComputer;
            }
            else
            {
                eGameMode = GameMode.VsHuman;
            }

            if (radWhite.Checked)
            {
                eOwnSide = ChessSide.White;
            }
            else
            {
                eOwnSide = ChessSide.Black;
            }

            if (radEasy.Checked)
            {
                eDifficulty = GameDifficulty.Easy;
            }
            else if (radNormal.Checked)
            {
                eDifficulty = GameDifficulty.Normal;
            }
            else
            {
                eDifficulty = GameDifficulty.Hard;
            }
            TimeLimit = Convert.ToInt16(iTimeLimit.Value);
            TimeBonus = Convert.ToInt16(iTImeBonus.Value);
        }
Ejemplo n.º 8
0
    public override void MoveFigure(Vector2Int prev_pos, Vector2Int new_pos, ChessSide side)
    {
        foreach (var item in boardItemsMono)
        {
            item.StopAnimation();
        }

        GetBoardItemMono(prev_pos).IsSelected = false;

        var figure_path = PathFinder.GetPath(
            GetChessItemMono(prev_pos).props.type,
            prev_pos.ToBoardPosition(),
            new_pos.ToBoardPosition(),
            new BoardPosition(iFigureOnBoard.BoardSizeX, iFigureOnBoard.BoardSizeY),
            IsJump(new_pos));

        GetChessItemMono(prev_pos).MoveFigureToPosition(figure_path.ToVector2List());



        currentProps = chessItemProtertiesFactory.CreateNullChessItemProperties();
        PrevProps    = chessItemProtertiesFactory.CreateNullChessItemProperties();

        mm.SetMoveTextValue(prev_pos, new_pos);
    }
Ejemplo n.º 9
0
        /// <summary>
        /// Trả về độ di động của Side cần kiểm tra
        /// </summary>
        static int Mobility(ChessSide eSide, int[,] BoardState)
        {
            int intSide = 0;

            if (eSide == ChessSide.White)
            {
                intSide = 2;
            }
            else
            {
                intSide = 1;
            }
            int intMobility = 0;

            for (int y = 1; y <= 8; y++)
            {
                for (int x = 1; x <= 8; x++)
                {
                    if (BoardState[x, y] > 0)
                    {
                        int side = BoardState[x, y] % 10;
                        if (side == intSide)
                        {
                            int intType = BoardState[x, y] / 10;
                            intMobility += FindAllLegalMove(BoardState, new Point(x, y), (ChessPieceType)intType).Count;
                        }
                    }
                }
            }
            return(intMobility);
        }
Ejemplo n.º 10
0
 public ChessItemProperties(Vector2Int pos, byte type, ChessSide side, bool isNullObject = false)
 {
     this.pos          = pos;
     this.type         = type;
     this.side         = side;
     this.isNullObject = isNullObject;
 }
Ejemplo n.º 11
0
 public ChessItemProperties(Vector2Int pos)
 {
     this.pos          = pos;
     this.type         = (byte)ClassicChessItemType.pawn;
     this.side         = ChessSide.white;
     this.isNullObject = true;
 }
Ejemplo n.º 12
0
        private void btnNewGame_Click(object sender, EventArgs e)
        {
            frmSelectGame frm = new frmSelectGame();

            if (frm.ShowDialog() == DialogResult.OK)
            {
                ChessSide      eOwnSide    = frm.eOwnSide;
                GameMode       eGameMode   = frm.eGameMode;
                GameDifficulty eDifficulty = frm.eDifficulty;
                string         strFen      = clsFEN.DefaultFENstring;

                this.TimeLimit = frm.TimeLimit;
                this.TimeBonus = frm.TimeBonus;

                if (eGameMode == GameMode.VsComputer)
                {
                    CreateChessBoard(eOwnSide, eGameMode, eDifficulty, strFen);
                }
                else
                {
                    CreateChessBoard(eOwnSide, eGameMode, strFen);
                }
                frmMessageBox.ShowMessage = true;

                PlayMusic(soundNewGame);
            }
            frm.Dispose();
        }
Ejemplo n.º 13
0
    IEnumerator ComputerThinking()
    {
        string strFen = clsFEN.GetFENWithoutMoveNumber(this);

        MyMove = clsChessEngine.ReadFromBook(strFen);
        if (MyMove == null)
        {
            MyMove  = new clsMove(new Vector2(), new Vector2());
            arrMove = clsChessEngine.GenerateMove(this._BoardState, this.arrFEN, ChessSide.Black, ref MyMove, Difficult);
            if (Difficult == GameDifficulty.Hard)
            {
                clsChessEngine.WriteToBook(strFen, MyMove);
            }
        }
        else
        {
            ChessPieceType eType = (ChessPieceType)(this._BoardState[(int)MyMove.CurPos.x, (int)MyMove.CurPos.y] / 10);
            arrMove = clsChessEngine.FindAllLegalMove(this._BoardState, MyMove.CurPos, eType);
        }
        LastMove = MyMove;
        yield return(null);

        AiMove();
        Debug.LogError(MyMove.CurPos + "   " + MyMove.NewPos);
        isThinking = false;
        isWhite    = ChessSide.White;
        arrFEN.Add(clsFEN.GetPiecePlacementString(this._BoardState));
    }
Ejemplo n.º 14
0
 protected virtual void MoveFigure(ChessSide side)
 {
     MatchView_ControllerAPI.MoveFigure(
         ChessMatchCurrentState.PreviousSelectedPosition.ToVector2Int(),
         ChessMatchCurrentState.CurrentSelectedPosition.ToVector2Int(),
         side);
 }
Ejemplo n.º 15
0
            public override string getNotation(ChessSide side,
							    ChessPiece[,]
							    positions, int sr,
							    int sf, int dr,
							    int df,
							    PromotionType
							    promotion_type)
            {
                string str;
                if (sf == df)
                  {
                      str = "" + (char) ('a' + df) + (dr +
                                      1);
                  }
                else
                  {
                      str = "" + (char) ('a' + sf);
                      if (positions[dr, df] != null)
                          str += 'x';
                      str += "" + (char) ('a' + df) +
                          (dr + 1);
                  }
                if (dr == 7 || dr == 0)
                  {
                      /* No need to verify for specific colors
                       * only whites can reach 7 and blacks can reach 0
                       */
                      str += ChessPiece.
                          getPromotionString
                          (promotion_type);
                  }
                return str;
            }
Ejemplo n.º 16
0
    public virtual void MoveFigure(Vector2Int prev_pos, Vector2Int new_pos, ChessSide side)
    {
        foreach (var item in boardItemsMono)
        {
            item.StopAnimation();
        }

        GetBoardItemMono(prev_pos).IsSelected = false;
        GetChessItemMono(prev_pos).MoveFigureToPosition(new Vector2Int[1] {
            new_pos
        });

        //Check for pawn becomes queen
        var ch_m = GetChessItemMono(new_pos);

        if (!ch_m.props.isNullObject && ch_m.props.type == (byte)ClassicChessItemType.pawn)
        {
            if (ch_m.props.side == ChessSide.white && ch_m.props.pos.y == boardSizeY - 1 ||
                (ch_m.props.side == ChessSide.black && ch_m.props.pos.y == 0))
            {
                ch_m.SetChessItem((byte)ClassicChessItemType.queen, ch_m.props.side);
            }
        }

        currentProps = chessItemProtertiesFactory.CreateNullChessItemProperties();
        PrevProps    = chessItemProtertiesFactory.CreateNullChessItemProperties();

        mm.SetMoveTextValue(prev_pos, new_pos);
    }
Ejemplo n.º 17
0
    public void SetChessItem(byte type, ChessSide side)
    {
        props.side = side;
        props.type = type;

        if (new byte[] {
            (byte)ClassicChessItemType.pawn,
            (byte)LosAlamosChessItemType.pawn,
            (byte)ChaturangaChessItemType.pawn,
            (byte)CircledChessItemType.pawn_left,
            (byte)CircledChessItemType.pawn_right
        }.Contains(type))
        {
            sprRend.sprite = MainMenu.Singleton.GetCurrentBoardPrefabs().chessPackList[(byte)side].pawn.GetComponent <SpriteRenderer>().sprite;
        }
        else if (new byte[] {
            (byte)ClassicChessItemType.rook,
            (byte)LosAlamosChessItemType.rook,
            (byte)ChaturangaChessItemType.rook,
            (byte)CircledChessItemType.rook
        }.Contains(type))
        {
            sprRend.sprite = MainMenu.Singleton.GetCurrentBoardPrefabs().chessPackList[(byte)side].rook.GetComponent <SpriteRenderer>().sprite;
        }
        else if (new byte[] {
            (byte)ClassicChessItemType.knight,
            (byte)LosAlamosChessItemType.knight,
            (byte)ChaturangaChessItemType.knight,
            (byte)CircledChessItemType.knight
        }.Contains(type))
        {
            sprRend.sprite = MainMenu.Singleton.GetCurrentBoardPrefabs().chessPackList[(byte)side].knight.GetComponent <SpriteRenderer>().sprite;
        }
        else if (new byte[] {
            (byte)ClassicChessItemType.bishop,
            (byte)CircledChessItemType.bishop
        }.Contains(type))
        {
            sprRend.sprite = MainMenu.Singleton.GetCurrentBoardPrefabs().chessPackList[(byte)side].bishop.GetComponent <SpriteRenderer>().sprite;
        }
        else if (new byte[] {
            (byte)ClassicChessItemType.queen,
            (byte)LosAlamosChessItemType.queen,
            (byte)ChaturangaChessItemType.queen,
            (byte)CircledChessItemType.queen
        }.Contains(type))
        {
            sprRend.sprite = MainMenu.Singleton.GetCurrentBoardPrefabs().chessPackList[(byte)side].queen.GetComponent <SpriteRenderer>().sprite;
        }
        else if (new byte[] {
            (byte)ClassicChessItemType.king,
            (byte)LosAlamosChessItemType.king,
            (byte)ChaturangaChessItemType.king,
            (byte)CircledChessItemType.king
        }.Contains(type))
        {
            sprRend.sprite = MainMenu.Singleton.GetCurrentBoardPrefabs().chessPackList[(byte)side].king.GetComponent <SpriteRenderer>().sprite;
        }
    }
Ejemplo n.º 18
0
    public void SetMate(ChessSide side)
    {
        string message = "Mate for " + side.ToString();

        Debug.Log(message);
        MainMenu.Singleton.AppendConsoleText(message);
        isMate = true;
    }
Ejemplo n.º 19
0
 public ChessItemModelShort(ChessSide Side, BoardPosition Pos, byte Type, int Steps, bool isNullObject)
 {
     this.Side         = Side;
     this.Pos          = Pos;
     this.Type         = Type;
     this.Steps        = Steps;
     this.IsNullObject = isNullObject;
 }
Ejemplo n.º 20
0
 public ChessItemModelBase(ChessSide side, byte type, BoardPosition pos, int steps, bool isNullObject = false)
 {
     Side         = side;
     Type         = type;
     Pos          = pos;
     Steps        = steps;
     IsNullObject = isNullObject;
 }
Ejemplo n.º 21
0
 public clsSavedGame(GameMode eGameMode, GameDifficulty eDifficulty, ChessSide eOwnSide, string strFEN, string strMoveList)
 {
     this._GameMode = eGameMode;
     this._Diff     = eDifficulty;
     this._OwnSide  = eOwnSide;
     this._FEN      = strFEN;
     this._MoveList = strMoveList;
 }
Ejemplo n.º 22
0
            public King(ColorType color, int rank, int file,
				     ChessSide myside,
				     ChessSide oppside)
                : base(PieceType.KING,
							      color, rank,
							      file, myside,
							      oppside)
            {
            }
Ejemplo n.º 23
0
            public Rook(ColorType color, int rank, int file,
				     ChessSide myside,
				     ChessSide oppside)
                : base(PieceType.ROOK,
							      color, rank,
							      file, myside,
							      oppside)
            {
            }
Ejemplo n.º 24
0
            public Pawn(ColorType color, int rank, int file,
				     ChessSide myside,
				     ChessSide oppside)
                : base(PieceType.PAWN,
							      color, rank,
							      file, myside,
							      oppside)
            {
            }
Ejemplo n.º 25
0
 public UcChessCell(int PositionX, int PositionY, Bitmap BackImage, ChessSide Side, ChessBoardStyle Style)
 {
     InitializeComponent();
     this._PositionX  = PositionX;
     this._PositionY  = PositionY;
     this._Side       = Side;
     this._BoardStyle = Style;
     this._BackImage  = BackImage;
 }
Ejemplo n.º 26
0
            public Knight(ColorType color, int rank, int file,
				       ChessSide myside,
				       ChessSide oppside)
                : base(PieceType.
								KNIGHT, color,
								rank, file,
								myside,
								oppside)
            {
            }
Ejemplo n.º 27
0
            public Queen(ColorType color, int rank, int file,
				      ChessSide myside,
				      ChessSide oppside)
                : base(PieceType.
							       QUEEN, color,
							       rank, file,
							       myside,
							       oppside)
            {
            }
Ejemplo n.º 28
0
            public override string getNotation(ChessSide side,
							    ChessPiece[,]
							    positions, int sr,
							    int sf, int dr,
							    int df,
							    PromotionType
							    promotion)
            {
                return "K" + (char) ('a' + df) + (dr + 1);
            }
Ejemplo n.º 29
0
            public Bishop(ColorType color, int rank, int file,
				       ChessSide myside,
				       ChessSide oppside)
                : base(PieceType.
								BISHOP, color,
								rank, file,
								myside,
								oppside)
            {
            }
Ejemplo n.º 30
0
 public static int GetPositionValue(Vector2 pos, ChessSide eSide)
 {
     if (eSide == ChessSide.White)
     {
         return(BishopTable [(int)pos.x, (int)pos.y]);
     }
     else
     {
         return(BishopTable[9 - (int)pos.y, 9 - (int)pos.x]);
     }
 }
Ejemplo n.º 31
0
        public UcChessCell(int PositionX, int PositionY, ChessSide Side, ChessBoardStyle Style)
        {
            InitializeComponent();

            this._PositionX  = PositionX;
            this._PositionY  = PositionY;
            this._Side       = Side;
            this._BoardStyle = Style;
            this._BackImage  = clsImageProcess.GetChessBoardBitMap(Side, Style);
            this.BackImage   = _BackImage;
        }
Ejemplo n.º 32
0
 public static int GetPositionValue(Vector2 pos, ChessSide eSide)
 {
     if (eSide == ChessSide.Black)
     {
         return(QueenTable [(int)pos.y, (int)pos.x]);
     }
     else
     {
         return(QueenTable[9 - (int)pos.y, 9 - (int)pos.x]);
     }
 }
Ejemplo n.º 33
0
 public static int GetPositionValue(Point pos, ChessSide eSide)
 {
     if (eSide == ChessSide.Black)
     {
         return(QueenTable [pos.Y, pos.X]);
     }
     else
     {
         return(QueenTable[9 - pos.Y, 9 - pos.X]);
     }
 }
Ejemplo n.º 34
0
        //Hàm kiểm tra quân vua của 1 phe có đang bị chiếu hay không
        public static bool IsChecked(int[,] arrState, ChessSide eSide)
        {
            int[,] State = new int[10, 10];
            Array.Copy(arrState, State, arrState.Length);    //copy mảng

            Point pKingPos = FindKingPosition(State, eSide); //Tìm vị trí quân vua đang kiểm tra

            int intSide = (int)eSide;                        //Phe của quân đang kiểm tra

            for (int y = 1; y <= 8; y++)
            {
                for (int x = 1; x <= 8; x++)
                {
                    if (State[x, y] > 0 && State[x, y] % 10 != intSide)//Nếu là quân khác phe
                    {
                        ArrayList      arrMove = new ArrayList();
                        ChessPieceType eType   = (ChessPieceType)(State[x, y] / 10);

                        if (eType == ChessPieceType.Bishop)
                        {
                            arrMove = clsBishop.FindAllPossibleMove(State, new Point(x, y));
                        }
                        if (eType == ChessPieceType.King)
                        {
                            arrMove = clsKing.FindAllPossibleMove(State, new Point(x, y));
                        }
                        if (eType == ChessPieceType.Knight)
                        {
                            arrMove = clsKnight.FindAllPossibleMove(State, new Point(x, y));
                        }
                        if (eType == ChessPieceType.Pawn)
                        {
                            arrMove = clsPawn.FindAllPossibleMove(State, new Point(x, y));
                        }
                        if (eType == ChessPieceType.Queen)
                        {
                            arrMove = clsQueen.FindAllPossibleMove(State, new Point(x, y));
                        }
                        if (eType == ChessPieceType.Rook)
                        {
                            arrMove = clsRook.FindAllPossibleMove(State, new Point(x, y));
                        }
                        foreach (Point p in arrMove)
                        {
                            if (p == pKingPos)
                            {
                                return(true);
                            }
                        }
                    }
                }
            }
            return(false);
        }
Ejemplo n.º 35
0
 void PushMove(ChessSide side, string strMove)
 {
     if (side == ChessSide.White)
     {
         stkWhiteMoves.Push(strMove);
     }
     else
     {
         stkBlackMoves.Push(strMove);
     }
 }
Ejemplo n.º 36
0
        public UcChessPiece(ChessSide Side, ChessPieceType Type, ChessPieceStyle Style, int intCellSize, int intPieceSize)
        {
            InitializeComponent();

            this._Side      = Side;
            this._Type      = Type;
            this._Style     = Style;
            this._image     = clsImageProcess.GetChessPieceBitMap(_Side, _Type, _Style);
            this._CellSize  = intCellSize;
            this._PieceSize = intPieceSize;
            this.Size       = new Size(this._PieceSize, this._PieceSize);
        }
Ejemplo n.º 37
0
            protected ChessPiece(PieceType type, ColorType color,
					      int rank, int file,
					      ChessSide myside,
					      ChessSide oppside)
            {
                this.type = type;
                this.myside = myside;
                this.oppside = oppside;
                this.color = color;
                this.rank = rank;
                this.file = file;
            }
Ejemplo n.º 38
0
        private int[] positions = new int[32]; //记录棋子位置

        #endregion Fields

        #region Constructors

        public Situation(ChessSide side = ChessSide.Red)
        {
            Side = side;
            ChessPiece piece = null;
            for (int n = 0; n < 32; n++)
            {
                piece = BuildChessPices(n);
                pieces[POSITIONS[n]] = piece;
                codes[n] = piece;
                positions[n] = POSITIONS[n];
            }
            init();
        }
Ejemplo n.º 39
0
 public FigureMove(BoardPosition from, BoardPosition to, IChessItemModel killed_item, ChessSide side)
 {
     from_x        = from.horizontal;
     from_y        = from.vertical;
     to_x          = to.horizontal;
     to_y          = to.vertical;
     killed_side   = killed_item.Side;
     killed_type   = killed_item.Type;
     killed_steps  = (short)killed_item.Steps;
     killed_x      = killed_item.Pos.horizontal;
     killed_y      = killed_item.Pos.vertical;
     killed_isnull = killed_item.IsNullObject;
     this.side     = side;
 }
Ejemplo n.º 40
0
        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;
        }
Ejemplo n.º 41
0
            public static void GetDefaultSides(out ChessSide
							    white,
							    out ChessSide
							    black)
            {
                white = new ChessSide (ColorType.WHITE);
                black = new ChessSide (ColorType.BLACK);

                FillDefaultPieces (ColorType.WHITE, white,
                           black);
                FillDefaultPieces (ColorType.BLACK, black,
                           white);
            }
Ejemplo n.º 42
0
 public void SwitchPlayer()
 {
     if (Side == ChessSide.Red)
     {
         Side = ChessSide.Black;
     }
     else
     {
         Side = ChessSide.Red;
     }
 }
Ejemplo n.º 43
0
            public static ChessGamePlayer CreateFromFEN(string
								     fen)
            {
                PositionInfo info = new PositionInfo (fen);
                string[]lines = info.position_str.Split ('/');
                if (lines.Length != 8)
                    throw new
                        ChessException
                        (Catalog.GetString("Invalid number tokens in the FEN position"));
                ChessSide whites =
                    new ChessSide (ColorType.WHITE);
                ChessSide blacks =
                    new ChessSide (ColorType.BLACK);
                for (int i = 0; i < lines.Length; i++)
                  {
                      string line = lines[i];
                      for (int j = 0; j < line.Length;
                           j++)
                        {
                            char ch = line[j];
                            if (Char.IsNumber (ch))
                              {
                                  j += ch - '1';	// starting from 1 since j++ will increment 1
                                  continue;
                              }
                            int rank = 7 - i;
                            int file = j;
                            ChessPiece piece;
                            GetPieceForFENChar (ch,
                                    whites,
                                    blacks,
                                    rank,
                                    file,
                                    out
                                    piece);
                            piece.addToSide ();
                        }
                  }
                whites.King.CanCastle = info.WhiteCanCastle;
                blacks.King.CanCastle = info.BlackCanCastle;
                ChessGamePlayer game = new ChessGamePlayer ();
                game.turn = info.Turn;
                game.StartGame (whites, blacks);
                return game;
            }
Ejemplo n.º 44
0
            public bool castle(ChessSide side, ColorType color,
					    CastleType castle)
            {
                if (!side.King.CanCastle)
                  {
                      debug (Catalog.GetString("Cant castle (") + side.King);
                      return false;
                  }

                if (side.King.File !=
                    ChessBoardConstants.FILE_E)
                  {
                      debug (Catalog.GetString("King not available ") +
                         side.King);
                      return false;
                  }

                int file;
                if (castle == CastleType.LONG_CASTLE)
                  {
                      file = ChessBoardConstants.FILE_A;
                  }
                else
                  {
                      file = ChessBoardConstants.FILE_H;
                  }

                int rank;
                if (color == ColorType.WHITE)
                    rank = 0;
                else
                    rank = 7;

                if (positions[rank, file] == null)
                  {
                      debug (Catalog.GetString("Rook not available"));
                      return false;
                  }

                int i1, i2;
                if (castle == CastleType.LONG_CASTLE)
                  {
                      i1 = ChessBoardConstants.FILE_B;
                      i2 = ChessBoardConstants.FILE_E;
                  }
                else
                  {
                      i1 = ChessBoardConstants.FILE_F;
                      i2 = ChessBoardConstants.FILE_H;
                  }

                for (int i = i1; i < i2; i++)
                  {
                      if (positions[rank, i] != null)
                        {	// some pieces blocking.. cannot castle
                            return false;
                        }
                  }

                IList attackers = new ArrayList ();
                ChessPiece.
                    getAttackers ((color ==
                               ColorType.
                               WHITE ? whites :
                               blacks),
                              (color ==
                               ColorType.
                               WHITE ? blacks :
                               whites), rank,
                              ChessBoardConstants.
                              FILE_E, positions, null,
                              attackers);
                // check if king is under attack.
                // cannot castle in that case
                if (attackers.Count > 0)
                  {
                      return false;
                  }

                if (castle == CastleType.LONG_CASTLE)
                    i1 = ChessBoardConstants.FILE_C;
                for (int i = i1; i < i2; i++)
                  {
                      attackers.Clear ();
                      ChessPiece.
                          getAttackers ((color ==
                                 ColorType.
                                 WHITE ?
                                 whites :
                                 blacks),
                                (color ==
                                 ColorType.
                                 WHITE ?
                                 blacks :
                                 whites),
                                rank, i,
                                positions,
                                null,
                                attackers);
                      if (attackers.Count > 0)
                        {	// king will be under attack in this case
                            return false;
                        }
                  }

                if (castle == CastleType.SHORT_CASTLE)
                  {
                      setPiece (side.King, rank,
                            ChessBoardConstants.
                            FILE_G);
                      setPiece (positions
                            [rank,
                             ChessBoardConstants.
                             FILE_H], rank,
                            ChessBoardConstants.
                            FILE_F);
                      removePiece (rank,
                               ChessBoardConstants.
                               FILE_E);
                      removePiece (rank,
                               ChessBoardConstants.
                               FILE_H);

                      lastMoveInfo.movedPiece = side.King;
                      lastMoveInfo.SetInfo (rank,
                                ChessBoardConstants.
                                FILE_E, rank,
                                ChessBoardConstants.
                                FILE_G, true);
                  }
                else
                  {
                      setPiece (side.King, rank,
                            ChessBoardConstants.
                            FILE_C);
                      setPiece (positions
                            [rank,
                             ChessBoardConstants.
                             FILE_A], rank,
                            ChessBoardConstants.
                            FILE_D);
                      removePiece (rank,
                               ChessBoardConstants.
                               FILE_E);
                      removePiece (rank,
                               ChessBoardConstants.
                               FILE_A);

                      lastMoveInfo.movedPiece = side.King;
                      lastMoveInfo.SetInfo (rank,
                                ChessBoardConstants.
                                FILE_E, rank,
                                ChessBoardConstants.
                                FILE_C, true);
                  }

                FlipTurn ();
                return true;
            }
Ejemplo n.º 45
0
            protected void getSource(PieceType pieceType,
						  out int rank, out int file,
						  ChessSide side,
						  int dest_rank,
						  int dest_file, string move)
            {
                rank = -1;
                file = -1;
                ChessPiece destPiece =
                    positions[dest_rank, dest_file];
                int exchange = 0;
                if (destPiece != null)
                    exchange =
                        ChessBoardConstants.
                        MOVE_EXCHANGE;

                if (pieceType == PieceType.KING)
                  {
                      ChessPiece cp = side.King;
                      rank = cp.Rank;
                      file = cp.File;
                      return;
                  }

                IList list = side.getPiecesOfType (pieceType);
                if (pieceType == PieceType.PAWN
                    && move.Length > 2)
                    exchange =
                        destPiece ==
                        null ? ChessBoardConstants.
                        MOVE_ENPASSANT :
                        ChessBoardConstants.
                        MOVE_EXCHANGE;

                ArrayList cands = new ArrayList ();
                foreach (ChessPiece cp in list)
                {
                    if (cp.
                        isValidMove (dest_rank, dest_file,
                             positions,
                             exchange |
                             ChessBoardConstants.
                             MOVE_DEBUG))
                      {
                          cands.Add (cp);
                      }
                }

                if (cands.Count == 0)
                  {
                      Console.Error.
                          WriteLine
                          (GetPositionsString ());
                      throw new
                          InvalidMoveException
                          (Catalog.GetString("Invalid move ") + move +
                           Catalog.GetString(". Couldn't find any candidate for this."));
                  }

                if (cands.Count == 1)
                  {
                      ChessPiece cp =
                          (ChessPiece) cands[0];
                      rank = cp.Rank;
                      file = cp.File;
                      return;
                  }

                // Ambiguity
                // More than one candidates
                char ch;
                int movelen = move.Length;
                if (cands.Count == 2)
                  {
                      if (movelen < 3)
                          throw new
                              InvalidMoveException
                              (Catalog.GetString("Insufficient chars in move ")
                               + move);
                      ch = move[move.Length - 3];
                      if (ch >= 'a' && ch <= 'h')
                        {
                            // find the piece with matching file
                            foreach (ChessPiece temp
                                 in cands)
                            {
                                if (ch ==
                                'a' +
                                temp.File)
                                  {
                                      rank = temp.Rank;
                                      file = temp.File;
                                      return;
                                  }
                            }
                        }
                      else if (ch >= '1' && ch <= '8')
                        {
                            foreach (ChessPiece temp
                                 in cands)
                            {
                                if (ch ==
                                '1' +
                                temp.Rank)
                                  {
                                      rank = temp.Rank;
                                      file = temp.File;
                                      return;
                                  }
                            }
                        }
                      else
                        {
                            throw new
                                InvalidMoveException
                                (Catalog.GetString("Invalid move ") +
                                 move);
                        }
                  }
                else
                  {
                      if (movelen < 4)
                          throw new
                              InvalidMoveException
                              (Catalog.GetString("Invalid move ") +
                               move);
                      char file_ch =
                          move[move.Length - 3];
                      char rank_ch =
                          move[move.Length - 4];
                      rank = rank_ch - 'a';
                      file = file_ch - '1';
                  }
            }
Ejemplo n.º 46
0
            private void StartGame(ChessSide whites,
						ChessSide blacks)
            {
                /* clear */
                for (int i = 0; i < 8; i++)
                    for (int j = 0; j < 8; j++)
                        positions[i, j] = null;

                turn = ColorType.WHITE;
                this.whites = whites;
                this.blacks = blacks;
                updatePositions ();

                gameStatus = GAME_STATUS_PLAYING;
            }
Ejemplo n.º 47
0
            private static void GetPieceForFENChar(char fench,
								ChessSide
								whites,
								ChessSide
								blacks,
								int rank,
								int file,
								out ChessPiece
								piece)
            {
                ColorType color;
                ChessSide myside, oppside;
                if (Char.IsUpper (fench))
                  {
                      color = ColorType.WHITE;
                      myside = whites;
                      oppside = blacks;
                  }
                else
                  {
                      color = ColorType.BLACK;
                      myside = blacks;
                      oppside = whites;
                  }

                char ch = Char.ToLower (fench);
                switch (ch)
                  {
                  case 'p':
                      piece = new Pawn (color, rank, file,
                                myside, oppside);
                      break;
                  case 'k':
                      piece = new King (color, rank, file,
                                myside, oppside);
                      break;
                  case 'q':
                      piece = new Queen (color, rank,
                                 file, myside,
                                 oppside);
                      break;
                  case 'b':
                      piece = new Bishop (color, rank,
                                  file, myside,
                                  oppside);
                      break;
                  case 's':
                      piece = new Knight (color, rank,
                                  file, myside,
                                  oppside);
                      break;
                  case 'n':
                      piece = new Knight (color, rank,
                                  file, myside,
                                  oppside);
                      break;
                  case 'r':
                      piece = new Rook (color, rank, file,
                                myside, oppside);
                      break;
                  default:
                      throw new
                          ChessException
                          (Catalog.GetString("Invalid piece type ") +
                           ch);
                  }
            }
Ejemplo n.º 48
0
            public virtual string getNotation(ChessSide side,
							   ChessPiece[,]
							   positions, int sr,
							   int sf, int dr,
							   int df,
							   PromotionType
							   promotion_type)
            {
                int count = 0;
                IList cands = getNotationCandidates ();

                if (cands != null && cands.Count > 1)
                  {
                      foreach (ChessPiece cand in cands)
                      {
                          if (cand.
                              isValidMove (dr, df,
                                   positions,
                                   ChessBoardConstants.
                                   MOVE_EXCHANGE))
                              count++;
                      }
                  }

                string str = "";
                string dest_square =
                    "" + (char) ('a' + df) + (dr + 1);
                if (positions[dr, df] != null)
                    dest_square = 'x' + dest_square;
                if (count <= 1)
                  {
                      str = getNotationPrefix () +
                          dest_square;
                  }
                else if (count == 2)
                  {
                      str = getNotationPrefix ();
                      if (((ChessPiece) cands[0]).File == ((ChessPiece) cands[1]).File)	// Both on the same file
                          str += (sr + 1);
                      else
                          str += (char) ('a' + sf);
                      str += dest_square;
                  }
                else
                  {
                      // more than two candidates
                      str = getNotationPrefix ();
                      str = str + (char) ('a' + sf) +
                          (sr + 1) + dest_square;
                  }

                switch (promotion_type)
                  {
                  case PromotionType.QUEEN:
                      str += "=Q";
                      break;
                  case PromotionType.ROOK:
                      str += "=R";
                      break;
                  case PromotionType.BISHOP:
                      str += "=B";
                      break;
                  case PromotionType.KNIGHT:
                      str += "=N";
                      break;
                  }

                return str;
            }
Ejemplo n.º 49
0
            private static void FillDefaultPieces(ColorType
							       color,
							       ChessSide side,
							       ChessSide opp)
            {
                /* Add pawns */
                int rank;
                rank = color ==
                    ColorType.WHITE ? ChessBoardConstants.
                    RANK_2 : ChessBoardConstants.RANK_7;
                for (int file = ChessBoardConstants.FILE_A;
                     file <= ChessBoardConstants.FILE_H;
                     file++)
                  {
                      side.addPiece (new
                             Pawn (color, rank,
                                   file, side,
                                   opp));
                  }

                rank = color ==
                    ColorType.WHITE ? ChessBoardConstants.
                    RANK_1 : ChessBoardConstants.RANK_8;
                side.addPiece (new
                           Rook (color, rank,
                             ChessBoardConstants.
                             FILE_A, side, opp));
                side.addPiece (new
                           Knight (color, rank,
                               ChessBoardConstants.
                               FILE_B, side, opp));
                side.addPiece (new
                           Bishop (color, rank,
                               ChessBoardConstants.
                               FILE_C, side, opp));
                side.addPiece (new
                           Queen (color, rank,
                              ChessBoardConstants.
                              FILE_D, side, opp));
                side.addPiece (new
                           King (color, rank,
                             ChessBoardConstants.
                             FILE_E, side, opp));
                side.addPiece (new
                           Bishop (color, rank,
                               ChessBoardConstants.
                               FILE_F, side, opp));
                side.addPiece (new
                           Knight (color, rank,
                               ChessBoardConstants.
                               FILE_G, side, opp));
                side.addPiece (new
                           Rook (color, rank,
                             ChessBoardConstants.
                             FILE_H, side, opp));
            }
Ejemplo n.º 50
0
 public BattleSituation(Situation situation, ChessSide side = ChessSide.Red)
 {
     this.situation = situation;
     InitEvaluate();
 }
Ejemplo n.º 51
0
 private static void GetPositions(ChessSide white, ChessSide black)
 {
 }
Ejemplo n.º 52
0
            /* This returns the attackers of (myking_rank,myking_file) square in the position
             * described by positions. 'ignore' will be ignored.
             */
            public static void getAttackers(ChessSide myside,
							 ChessSide oppside,
							 int myking_rank,
							 int myking_file,
							 ChessPiece[,]
							 positions,
							 ChessPiece ignore,
							 IList attackers)
            {
                ArrayList enemy_pieces = new ArrayList ();
                enemy_pieces.AddRange (oppside.Queens);
                enemy_pieces.AddRange (oppside.Rooks);
                enemy_pieces.AddRange (oppside.Bishops);
                enemy_pieces.AddRange (oppside.Knights);
                enemy_pieces.AddRange (oppside.Pawns);

                foreach (ChessPiece cp in enemy_pieces)
                {
                    if (ignore != null
                        && cp.Equals (ignore))
                        continue;
                    if (cp.
                        isValidMove (myking_rank,
                             myking_file,
                             positions,
                             ChessBoardConstants.
                             MOVE_DONT_CHECK_KINGS_EXPOSURE
                             |
                             ChessBoardConstants.
                             MOVE_EXCHANGE))
                      {
                          attackers.Add (cp);
                      }
                }
            }