コード例 #1
0
ファイル: Game.cs プロジェクト: JakeFenley/Chess
        private Queen PromoteAiPawn(GamePiece piece)
        {
            Queen newPiece = new Queen(piece.Place, piece.Color);

            return(newPiece);
        }
コード例 #2
0
ファイル: Bishop.cs プロジェクト: NaitCTClub/RoboChess
 public Bishop(GamePiece piece) : base(piece)
 {
 }                                                // Virtual
コード例 #3
0
ファイル: PopUp.xaml.cs プロジェクト: NaitCTClub/RoboChess
 private void _btnQueen_Click(object sender, RoutedEventArgs e)
 {
     ReturnGamePiece = new Queen();
     Close();
 }
コード例 #4
0
 public King(GamePiece piece) : base(piece)
 {
 }                                              // Virtual
コード例 #5
0
ファイル: PopUp.xaml.cs プロジェクト: NaitCTClub/RoboChess
 private void _btnBishop_Click(object sender, RoutedEventArgs e)
 {
     ReturnGamePiece = new Bishop();
     Close();
 }
コード例 #6
0
ファイル: PopUp.xaml.cs プロジェクト: NaitCTClub/RoboChess
 private void _btnRook_Click(object sender, RoutedEventArgs e)
 {
     ReturnGamePiece = new Rook();
     Close();
 }
コード例 #7
0
 public Queen(GamePiece piece) : base(piece)
 {
 }                                               // Virtual
コード例 #8
0
ファイル: PopUp.xaml.cs プロジェクト: NaitCTClub/RoboChess
 private void _btnKnight_Click(object sender, RoutedEventArgs e)
 {
     ReturnGamePiece = new Knight();
     Close();
 }
コード例 #9
0
 public Rook(GamePiece piece) : base(piece)
 {
 }                                              // Virtual
コード例 #10
0
        // Investigates if a move is Legal
        private Condition MovePossibility(GamePiece piece, Cell fromCell, Cell toCell, Condition condition)
        {
            Condition result = Condition.Default;

            if (fromCell == toCell)
            {
                return(Condition.Active); // Already there
            }
            if (toCell.Piece is null)     // cell is Empty
            {
                // Legal Neutral
                if (condition == Condition.Default || condition == Condition.Neutral)
                {
                    result = Condition.Neutral;
                }
                // Legal Pawn Enpassant Move
                else if (piece is Pawn && condition == Condition.Attack && !(toCell.enPassantPawn is null))
                {
                    result = Condition.enPassant;
                }
            }
            else if (toCell.Piece != null && toCell.Piece.TeamColor != piece.TeamColor) // cell is Enemy
            {
                // Legal Attack
                if (condition == Condition.Default || condition == Condition.Attack)
                {
                    result = Condition.Attack;
                }
            }

            if (condition == Condition.Castling)
            {
                Player player = piece.TeamColor == PlayerOne.TeamColor ? PlayerOne : PlayerTwo;
                // get Rook
                int   xDirection   = fromCell.ID.X > toCell.ID.X ? -1 : 1;
                Point rookLocation = xDirection == 1 ? new Point(7, fromCell.ID.Y) : new Point(0, fromCell.ID.Y);
                Rook  rook         = (Rook)player.MyPieces.Find(gp => gp.ID == rookLocation && gp.moveCount == 0 && gp.isAlive);

                if (rook is null)
                {
                    result = Condition.Illegal; // Rook is missing
                }
                else
                {
                    if (piece.moveCount != 0 && rook.moveCount != 0)
                    {
                        result = Condition.Illegal; // Cannot Castle when King & Rook are not in original locations
                    }
                    else
                    {
                        Cell focus = Cells.NextCell(fromCell, new Point(xDirection, 0));
                        while (focus.Piece is null) // find next gamepiece in the movement direction
                        {
                            focus = Cells.NextCell(focus, new Point(xDirection, 0));
                        }

                        if (!ReferenceEquals(rook, focus.Piece))
                        {
                            result = Condition.Illegal; // Other Pieces in the way
                        }
                        else
                        {
                            if (player.isChecked == true)
                            {
                                result = Condition.Illegal; // Cannot Castle when in Check
                            }
                            else
                            {
                                Point passThrough = new Point(fromCell.ID.X + xDirection, fromCell.ID.Y);
                                if (!IsSafe(passThrough, WhosTurn))
                                {
                                    result = Condition.Illegal; // Cannot Castle through Check
                                }
                                else
                                {
                                    result = Condition.Castling; // Castling Permitted! (Well still one more test in PossibleMoves())
                                }
                            }
                        }
                    }
                }
            }

            return(result);
        }
コード例 #11
0
        /// <summary>
        /// Returns list of All legal ChessMoves for a GamePiece
        /// </summary>
        /// <param name="piece">GamePiece that is being investigated</param>
        public List <ChessMove> PossibleMoves(GamePiece piece)
        {
            if (piece is null)
            {
                return(null); // illegal - Empty space
            }
            Player           activePlayer  = piece.TeamColor == PlayerOne.TeamColor ? PlayerOne : PlayerTwo;
            Cell             fromCell      = Cells.GetCell(piece.Location);
            List <ChessMove> possibleMoves = new List <ChessMove>(); // Returning List of Possible Moves
            GamePiece        king          = activePlayer.MyPieces.Find(gp => gp is King);

            //List<BlindMove> blindMoves = fromCell.Piece.BlindMoves(); // Blind move instructions for Gamepiece
            foreach (BlindMove bMove in fromCell.Piece.BlindMoves())             // Blind move instructions for Gamepiece
            {
                Cell      focusCell = Cells.NextCell(fromCell, bMove.Direction); // The Starting Point
                int       moveCount = 0;
                Condition moveType  = Condition.Neutral;

                // Increment through Instruction
                while (!(focusCell is null) && bMove.Limit != moveCount && moveType == Condition.Neutral)
                {
                    moveType = MovePossibility(fromCell.Piece, fromCell, focusCell, bMove.Condition);

                    // ADD to List of Possible ChessMoves
                    if (moveType == Condition.Neutral || moveType == Condition.Attack || moveType == Condition.enPassant || moveType == Condition.Castling)
                    {
                        ChessMove move;
                        if (moveType == Condition.enPassant) // *Special Move - Pawn captures Pawn via Enpassant
                        {
                            GamePiece captured = Cells.Find(c => !(c.enPassantPawn is null)).enPassantPawn;
                            move = new ChessMove(fromCell, focusCell, fromCell.Piece, captured, moveType, Cells.GetCell(captured.Location));
                        }
                        else if (moveType == Condition.Castling) // *Special Move - Castling, insert Rook into ChessMove
                        {
                            Rook rook       = (Rook)Cells.GetCell((Point)bMove.OtherInfo).Piece;
                            int  xDirection = bMove.Direction.X / 2;
                            Cell rookFrom   = Cells.GetCell(rook.Location);
                            Cell rookTo     = Cells.GetCell(new Point(fromCell.ID.X + xDirection, fromCell.ID.Y));

                            ChessMove rookMove = new ChessMove(rookFrom, rookTo, rook, null, Condition.Castling);

                            move = new ChessMove(fromCell, focusCell, fromCell.Piece, null, moveType, rookMove);
                        }
                        else // Regular Move
                        {
                            move = new ChessMove(fromCell, focusCell, fromCell.Piece, focusCell.Piece, moveType);
                        }

                        //Look in the future
                        move = MovePiece(move, true);
                        bool isKingSafe = IsSafe(king.Location, activePlayer);
                        move = UndoMovePiece(move, true);

                        if (isKingSafe)
                        {
                            possibleMoves.Add(move);
                        }
                    }

                    focusCell = Cells.NextCell(focusCell, bMove.Direction);
                    moveCount++;
                }
            }

            return(possibleMoves);
        }
コード例 #12
0
 public Knight(GamePiece piece) : base(piece)
 {
 }                                                // Virtual
コード例 #13
0
 public Pawn(GamePiece piece) : base(piece)
 {
 }                                              // Virtual