コード例 #1
0
        public virtual void MoveBack()
        {
            if (AllMoves.Count == 0)
            {
                return;
            }

            var move = AllMoves.Last();

            PreviousPlayer = Players[currentPlayerIndex];
            currentPlayerIndex--;
            if (currentPlayerIndex < 0)
            {
                currentPlayerIndex = Players.Count - 1;
            }


            LastPlayerAction = PlayerAction.Nothing;


            PreviousSelectedPosition = new BoardPosition(move.to_x, move.to_y);
            CurrentSelectedPosition  = new BoardPosition(move.from_x, move.from_y, true);

            BoardKit.SetFigureOnBoard(CurrentSelectedPosition, BoardKit.GetFigureFromBoard(PreviousSelectedPosition));
            BoardKit.GetFigureFromBoard(CurrentSelectedPosition).Steps -= 2;

            var             chessFactory = new ChessItemModelFactory();
            IChessItemModel killed_chess;

            if (move.killed_isnull)
            {
                killed_chess = new NullChessItemModel();
            }
            else
            {
                killed_chess = chessFactory.CreateChessItemModel(new BoardPosition(move.killed_x, move.killed_y), move.killed_type, move.killed_steps, move.killed_side);
            }

            BoardKit.FiguresPlacement[PreviousSelectedPosition.horizontal, PreviousSelectedPosition.vertical] = killed_chess;

            IChessItemModel recoveryItem = null;

            if (!move.killed_isnull)
            {
                recoveryItem = ChessEngineFunctions.GetKilledChessItemFromMove(move, chessGameType);
                BoardKit.FiguresPlacement[move.killed_x, move.killed_y] = recoveryItem;
            }

            AllMoves.RemoveAt(AllMoves.Count - 1);
            LastPlayerAction = PlayerAction.Move;
            CurrentPlayer    = Players[currentPlayerIndex];
            OnChanged(this, new ChessSideArgs(move.side));

            if (!move.killed_isnull)
            {
                OnFigureRecovery(this, new ChessItemArgs(recoveryItem));
            }
        }
コード例 #2
0
        protected bool CheckForCheck(ChessSide side, byte king_index)
        {
            IChessItemModel cm = new NullChessItemModel();

            for (int i = 0; i < FiguresPlacement.GetLength(0); i++)
            {
                for (int j = 0; j < FiguresPlacement.GetLength(1); j++)
                {
                    var cm_ij = FiguresPlacement[i, j];
                    if (!cm_ij.IsNullObject && cm_ij.Type == king_index && cm_ij.Side == side)
                    {
                        cm = cm_ij;
                        break;
                    }
                }
            }
            if (cm.IsNullObject)
            {
                return(false);
            }


            foreach (var cm_ij in FiguresPlacement)
            {
                if (!cm_ij.IsNullObject && cm_ij != cm && cm_ij.Side != side)
                {
                    List <bool> isJumpTemp;
                    List <bool> killPossibilityList;

                    var poss_moves = cm_ij.GetPossibleMoves(FiguresPlacement, out isJumpTemp, out killPossibilityList);
                    int k          = -1;
                    foreach (var poss_move in poss_moves)
                    {
                        k++;
                        if (poss_move == cm.Pos && killPossibilityList[k])
                        {
                            return(true);
                        }
                    }
                }
            }

            return(false);
        }
コード例 #3
0
        public virtual bool CheckForMate(ChessSide side, byte king_index)
        {
            IChessItemModel kingModel = new NullChessItemModel();

            for (int i = 0; i < FiguresPlacement.GetLength(0); i++)
            {
                for (int j = 0; j < FiguresPlacement.GetLength(1); j++)
                {
                    var cm_ij = FiguresPlacement[i, j];
                    if (!cm_ij.IsNullObject && cm_ij.Type == king_index && cm_ij.Side == side)
                    {
                        kingModel = cm_ij;
                        break;
                    }
                }
            }

            if (kingModel.IsNullObject)
            {
                throw new Exception(side.ToString() + " king is missing!");
            }

            foreach (var cm_ij in FiguresPlacement)
            {
                if (!cm_ij.IsNullObject && cm_ij.Side == side)
                {
                    List <bool> isJumpTemp;
                    List <bool> killPossibilityList;
                    foreach (var poss_move in cm_ij.GetPossibleMoves(FiguresPlacement, out isJumpTemp, out killPossibilityList))
                    {
                        var  prev_pos        = cm_ij.Pos;
                        var  temp_chess_item = MoveChessItemPredictForward(cm_ij.Pos, poss_move);
                        bool isCheck         = CheckForCheck(side);
                        MoveChessItemPredictBackward(temp_chess_item, prev_pos, cm_ij.Pos);

                        if (!isCheck && poss_move != prev_pos)
                        {
                            return(false);
                        }
                    }
                }
            }
            return(true);
        }
コード例 #4
0
        public virtual bool TryMoveChessItem(ChessSide curr_side, BoardPosition from, BoardPosition to, out BoardPosition captureOnTheIsle, out IChessItemModel killed_model)
        {
            killed_model     = new NullChessItemModel();
            captureOnTheIsle = BoardPosition.None;
            //If figure wasn't chosen before or new selected figure is the same
            if (GetFigureFromBoard(from).IsNullObject || from == to)
            {
                captureOnTheIsle = BoardPosition.None;
                return(false);
            }
            else
            {
                //If selected board item has figure
                if (!GetFigureFromBoard(to).IsNullObject)
                {
                    //If sides of previous selected figure and current are same
                    if (GetFigureFromBoard(to).Side == curr_side)
                    {
                        captureOnTheIsle = BoardPosition.None;

                        return(castlingPossibility &&
                               GetFigureFromBoard(to).Type == (byte)ClassicChessItemType.king && GetFigureFromBoard(from).Type == (byte)ClassicChessItemType.rook ||
                               GetFigureFromBoard(to).Type == (byte)ClassicChessItemType.rook && GetFigureFromBoard(from).Type == (byte)ClassicChessItemType.king);
                    }
                    //If side of previous selected and current are different
                    else
                    {
                        captureOnTheIsle = BoardPosition.None;

                        //MoveChessItem(from, to, false);
                        var temp_chess_item = MoveChessItemPredictForward(from, to);
                        if (CheckForCheck(curr_side))
                        {
                            killed_model = new NullChessItemModel();
                            MoveChessItemPredictBackward(temp_chess_item, from, to);
                            return(false);
                        }
                        else
                        {
                            MoveChessItemPredictBackward(temp_chess_item, from, to);
                            killed_model = GetFigureFromBoard(to);
                            MoveChessItem(from, to);
                            return(true);
                        }
                    }
                }
                //If selected board item doesn't have figure
                else
                {
                    //If it's pawn, check for capture on the isle
                    if (GetFigureFromBoard(from).Type == (byte)ClassicChessItemType.pawn)
                    {
                        if (System.Math.Abs(from.vertical - to.vertical) == 2)
                        {
                            byte hor  = 0;
                            byte vert = 0;
                            switch (GetFigureFromBoard(from).Side)
                            {
                            case ChessSide.white:
                                hor  = from.horizontal;
                                vert = (byte)(from.vertical + 1);
                                break;

                            case ChessSide.black:
                                hor  = from.horizontal;
                                vert = (byte)(from.vertical - 1);
                                break;

                            case ChessSide.red:
                                hor  = (byte)(from.horizontal + 1);
                                vert = from.vertical;
                                break;

                            case ChessSide.green:
                                hor  = (byte)(from.horizontal - 1);
                                vert = from.vertical;
                                break;

                            default:
                                break;
                            }

                            captureOnTheIsle = new BoardPosition(hor, vert);
                        }
                    }

                    var temp_chess_item = MoveChessItemPredictForward(from, to);
                    if (CheckForCheck(curr_side))
                    {
                        MoveChessItemPredictBackward(temp_chess_item, from, to);
                        return(false);
                    }
                    else
                    {
                        MoveChessItemPredictBackward(temp_chess_item, from, to);
                        MoveChessItem(from, to);

                        var  side = GetFigureFromBoard(to).Side;
                        bool isMomentToMakeQueenFromPawn = false;
                        if (side == ChessSide.white && to.vertical == Max_Y - 1)
                        {
                            isMomentToMakeQueenFromPawn = true;
                        }
                        else if (side == ChessSide.black && to.vertical == 0)
                        {
                            isMomentToMakeQueenFromPawn = true;
                        }
                        else if (side == ChessSide.red && to.horizontal == Max_X - 1)
                        {
                            isMomentToMakeQueenFromPawn = true;
                        }
                        else if (side == ChessSide.green && to.horizontal == 0)
                        {
                            isMomentToMakeQueenFromPawn = true;
                        }

                        if (isMomentToMakeQueenFromPawn)
                        {
                            if (GetFigureFromBoard(to).Type == (byte)ClassicChessItemType.pawn)
                            {
                                var ci = GetFigureFromBoard(to);
                                SetFigureOnBoard(to, new ClassicQueenModel(ci.Side, to));
                            }
                        }

                        return(true);
                    }
                }
            }
        }
コード例 #5
0
        public static List <BoardPosition> GetRookPossibleMoves(IChessItemModel[,] items, ChessSide side, BoardPosition pos, int Steps, bool checkCastling, bool isCircled, out List <bool> isJumpList, out List <bool> killPossibilityList)
        {
            List <BoardPosition> result = new List <BoardPosition>();

            isJumpList          = new List <bool>();
            killPossibilityList = new List <bool>();

            //Check for castling
            if (Steps == 0 && checkCastling)
            {
                int vert_pos = -1;
                if (pos.vertical == 0 && side == ChessSide.white)
                {
                    vert_pos = 0;
                }
                else if (pos.vertical == items.GetLength(1) - 1 && side == ChessSide.black)
                {
                    vert_pos = items.GetLength(1) - 1;
                }

                IChessItemModel el4h = new NullChessItemModel();
                if (vert_pos != -1)
                {
                    el4h = items[4, vert_pos];
                }

                if (vert_pos != -1 && !el4h.IsNullObject && el4h.Type == (byte)ClassicChessItemType.king && el4h.Steps == 0 && el4h.Side == side)
                {
                    if (pos.horizontal == 0)
                    {
                        if (items[1, vert_pos].IsNullObject && items[2, vert_pos].IsNullObject && items[3, vert_pos].IsNullObject)
                        {
                            bool areNotUnderCheck = true;
                            foreach (var bp in new BoardPosition[] { new BoardPosition(2, vert_pos), new BoardPosition(3, vert_pos) })
                            {
                                foreach (var item in items)
                                {
                                    if (!item.IsNullObject && item.Side != side)
                                    {
                                        List <bool> bools;
                                        List <bool> bools2;
                                        var         moves = GetRookPossibleMoves(items, side, pos, Steps, false, false, out bools, out bools2);
                                        foreach (var move in moves)
                                        {
                                            if (pos == move)
                                            {
                                                areNotUnderCheck = false;
                                                break;
                                            }
                                        }
                                    }
                                }
                                if (areNotUnderCheck)
                                {
                                    isJumpList.Add(false);
                                    killPossibilityList.Add(false);
                                    result.Add(new BoardPosition(4, vert_pos));
                                }
                            }
                        }
                    }
                    if (pos.horizontal == 7)
                    {
                        if (items[5, vert_pos].IsNullObject && items[6, vert_pos].IsNullObject)
                        {
                            bool areNotUnderCheck = true;
                            foreach (var bp in new BoardPosition[] { new BoardPosition(5, vert_pos), new BoardPosition(6, vert_pos) })
                            {
                                foreach (var item in items)
                                {
                                    if (!item.IsNullObject && item.Side != side)
                                    {
                                        List <bool> bools;
                                        List <bool> bools1;
                                        var         moves = GetRookPossibleMoves(items, side, pos, Steps, false, false, out bools, out bools1);
                                        foreach (var move in moves)
                                        {
                                            if (pos == move)
                                            {
                                                areNotUnderCheck = false;
                                                break;
                                            }
                                        }
                                    }
                                }
                                if (areNotUnderCheck)
                                {
                                    result.Add(new BoardPosition(4, vert_pos));
                                    isJumpList.Add(false);
                                    killPossibilityList.Add(false);
                                }
                            }
                        }
                    }
                }
            }

            int  min_x = 0;
            int  min_y = 0;
            int  max_x = items.GetLength(0) - 1;
            int  max_y = items.GetLength(1) - 1;
            bool isJump;

            int k = pos.horizontal;

            if (isCircled && k <= min_x)
            {
                k = max_x + 1;
            }
            isJump = false;
            while (k > min_x)
            {
                if (items[k - 1, pos.vertical].IsNullObject)
                {
                    result.Add(new BoardPosition(k - 1, pos.vertical));
                    isJumpList.Add(isJump);
                    killPossibilityList.Add(true);
                }
                else
                {
                    if (items[k - 1, pos.vertical].Side != side)
                    {
                        result.Add(new BoardPosition(k - 1, pos.vertical));
                        isJumpList.Add(isJump);
                        killPossibilityList.Add(true);
                    }

                    break;
                }
                k--;

                if (isCircled && k <= min_x)
                {
                    k      = max_x + 1;
                    isJump = true;
                }
            }

            k = pos.horizontal;
            if (isCircled && k >= max_x)
            {
                k = min_x - 1;
            }
            isJump = false;
            while (k < max_x)
            {
                if (items[k + 1, pos.vertical].IsNullObject)
                {
                    result.Add(new BoardPosition(k + 1, pos.vertical));
                    isJumpList.Add(isJump);
                    killPossibilityList.Add(true);
                }
                else
                {
                    if (items[k + 1, pos.vertical].Side != side)
                    {
                        result.Add(new BoardPosition(k + 1, pos.vertical));
                        isJumpList.Add(isJump);
                        killPossibilityList.Add(true);
                    }

                    break;
                }
                k++;

                if (isCircled && k >= max_x)
                {
                    k      = min_x - 1;
                    isJump = true;
                }
            }

            k      = pos.vertical;
            isJump = false;
            while (k > 0)
            {
                if (items[pos.horizontal, k - 1].IsNullObject)
                {
                    result.Add(new BoardPosition(pos.horizontal, k - 1));
                    isJumpList.Add(isJump);
                    killPossibilityList.Add(true);
                }
                else
                {
                    if (items[pos.horizontal, k - 1].Side != side)
                    {
                        result.Add(new BoardPosition(pos.horizontal, k - 1));
                        isJumpList.Add(isJump);
                        killPossibilityList.Add(true);
                    }

                    break;
                }
                k--;
            }

            k      = pos.vertical;
            isJump = false;
            while (k < items.GetLength(1) - 1)
            {
                if (items[pos.horizontal, k + 1].IsNullObject)
                {
                    result.Add(new BoardPosition(pos.horizontal, k + 1));
                    isJumpList.Add(isJump);
                    killPossibilityList.Add(true);
                }
                else
                {
                    if (items[pos.horizontal, k + 1].Side != side)
                    {
                        result.Add(new BoardPosition(pos.horizontal, k + 1));
                        isJumpList.Add(isJump);
                        killPossibilityList.Add(true);
                    }

                    break;
                }
                k++;
            }

            result.Add(pos);
            isJumpList.Add(false);
            killPossibilityList.Add(false);
            return(result);
        }