Example #1
0
        public GameState()
        {
            List <Piece> pieces = new List <Piece>();

            var pieceTypes = new List <PieceType>()
            {
                PieceType.Rook,
                PieceType.Knight,
                PieceType.Bishop,
                PieceType.Queen,
                PieceType.King,
                PieceType.Bishop,
                PieceType.Knight,
                PieceType.Rook
            };

            for (int col = 0; col < 8; col++)
            {
                pieces.Add(PieceFactory.CreatePiece(Field.GetField(0, col), pieceTypes[col], Color.White));
                pieces.Add(PieceFactory.CreatePiece(Field.GetField(1, col), PieceType.Pawn, Color.White));
                pieces.Add(PieceFactory.CreatePiece(Field.GetField(7, col), pieceTypes[col], Color.Black));
                pieces.Add(PieceFactory.CreatePiece(Field.GetField(6, col), PieceType.Pawn, Color.Black));
            }

            this.Pieces     = pieces;
            this.NextToMove = Color.White;
            this.LastMove   = null;
            this.GenerateBoard();
        }
Example #2
0
    public static Piece CreateChessPiece(Vector2Int squareIndex, PieceType pieceType, ChessGridInfo grid, ChessPlayer player)
    {
        Piece piece = PieceFactory.CreatePiece(pieceType.ToString());

        grid.SetChessPieceOnGrid(piece, squareIndex);
        return(piece);
    }
Example #3
0
        public GameState MoveWithoutValidation(Move move)
        {
            Piece        pieceToMove = this.GetPiece(move.Start);
            List <Piece> newPieces   = new List <Piece>(this.Pieces);
            MoveActions  moveActions = pieceToMove.Move(this, move);

            foreach (var pieceRemoveAction in moveActions.PieceRemoveActions)
            {
                newPieces.Remove(pieceRemoveAction.Piece);
            }

            foreach (var pieceMoveAction in moveActions.PieceMoveActions)
            {
                Piece piece = pieceMoveAction.Piece;
                newPieces.Remove(piece);
                newPieces.Add(PieceFactory.CreatePiece(pieceMoveAction.Goal, piece.Type, piece.Color, piece.MoveCount + 1));
            }

            foreach (var piecePromotionAction in moveActions.PiecePromotionActions)
            {
                Piece piece = piecePromotionAction.Piece;
                newPieces.Remove(piece);
                newPieces.Add(PieceFactory.CreatePiece(piecePromotionAction.Goal, piecePromotionAction.PromotionType, piece.Color, piece.MoveCount + 1));
            }

            return(new GameState(newPieces, this.NextToMove.Enemy(), move));
        }
Example #4
0
        private void NewGame(object sender, EventArgs e)
        {
            ResetColors();
            ResetCoordinates();
            ResetPieceImages();

            playerTurn   = PieceColor.White;
            lblTurn.Text = playerTurn.ToString() + " Turn";

            List <string> piecesCodifications = ReadInitialSetup();
            PieceFactory  pieceFactory        = new PieceFactory();

            board = new Board();

            for (int y = 0; y < 10; y++)
            {
                for (int x = 0; x < 10; x++)
                {
                    string pieceCodification = piecesCodifications[y * 10 + x];
                    Point  position          = new Point(x, y);
                    board.squares[y, x] = new Square(pieceFactory.CreatePiece(pieceCodification, position));

                    if (board.squares[y, x].piece != null)
                    {
                        ((PictureBox)chessBoardPanel.Controls[y * 10 + x]).Image = board.squares[y, x].piece.Image;
                    }
                }
            }
            //AiTimer();
        }
Example #5
0
        public void CreateRookTest()
        {
            var rook    = new Rook(2, 2, true);
            var factory = new PieceFactory();

            var testRook = factory.CreatePiece(PieceType.ROOK, 2, 2, true);

            Assert.Equal(rook, testRook);
        }
Example #6
0
        public void CreateQueenTest()
        {
            var queen   = new Queen(2, 2, true);
            var factory = new PieceFactory();

            var testQueen = factory.CreatePiece(PieceType.QUEEN, 2, 2, true);

            Assert.Equal(queen, testQueen);
        }
Example #7
0
        public void CreateKnightTest()
        {
            var knight  = new Knight(2, 2, true);
            var factory = new PieceFactory();

            var testKnight = factory.CreatePiece(PieceType.KNIGHT, 2, 2, true);

            Assert.Equal(knight, testKnight);
        }
Example #8
0
        public void CreateKingTest()
        {
            var king    = new King(2, 2, true);
            var factory = new PieceFactory();

            var testKing = factory.CreatePiece(PieceType.KING, 2, 2, true);

            Assert.Equal(king, testKing);
        }
Example #9
0
        public void CreateBishopTest()
        {
            var bishop  = new Bishop(2, 2, true);
            var factory = new PieceFactory();

            var testBishop = factory.CreatePiece(PieceType.BISHOP, 2, 2, true);

            Assert.Equal(bishop, testBishop);
        }
Example #10
0
        public void CreatePawnTest()
        {
            var pawn    = new Pawn(2, 2, true);
            var factory = new PieceFactory();

            var testPawn = factory.CreatePiece(PieceType.PAWN, 2, 2, true);

            Assert.Equal(pawn, testPawn);
        }
Example #11
0
        public Move MakeMove(Board board, Piece piece, Square destination)
        {
            var currentPosition = piece.CurrentPosition;

            var move = new Move();

            move.From          = new Square(piece.CurrentPosition.Rank, piece.CurrentPosition.Column);
            move.To            = new Square(destination.Rank, destination.Column);
            move.CapturedPiece = destination.Piece;
            if (destination.IsOccupied)
            {
                RemovePiece(destination.Piece);
            }
            destination.Place(piece.CurrentPosition.Pick());
            if (IsPromotionalMove(piece, destination))
            {
                move.PromotedToPiece = PieceFactory.CreatePiece(Turn, Players[Turn].GetPromotedToPiece());
                RemovePiece(piece);
                destination.Place(move.PromotedToPiece);
            }
            return(move);
        }