Ejemplo n.º 1
0
        public Set(Color color)
        {
            int rank;
            this.color = color;
            pieces = new Piece[16];

            rank = (color == Color.Black) ? 8 : 1;
            pieces[0] = rookA = new Rook(color, new Position(rank, 'a'));
            pieces[1] = knightA = new Knight(color, new Position(rank, 'b'));
            pieces[2] = bishopA = new Bishop(color, new Position(rank, 'c'));
            pieces[3] = king = new King(color, new Position(rank, 'd'));
            pieces[4] = queen = new Queen(color, new Position(rank, 'e'));
            pieces[5] = bishopB = new Bishop(color, new Position(rank, 'f'));
            pieces[6] = knightB = new Knight(color, new Position(rank, 'g'));
            pieces[7] = rookB = new Rook(color, new Position(rank, 'h'));

            rank = (color == Color.Black) ? 7 : 2;
            pieces[8] = pawnA = new Pawn(color, new Position(rank, 'a'));
            pieces[9] = pawnB = new Pawn(color, new Position(rank, 'b'));
            pieces[10] = pawnC = new Pawn(color, new Position(rank, 'c'));
            pieces[11] = pawnD = new Pawn(color, new Position(rank, 'd'));
            pieces[12] = pawnE = new Pawn(color, new Position(rank, 'e'));
            pieces[13] = pawnF = new Pawn(color, new Position(rank, 'f'));
            pieces[14] = pawnG = new Pawn(color, new Position(rank, 'g'));
            pieces[15] = pawnH = new Pawn(color, new Position(rank, 'h'));
        }
Ejemplo n.º 2
0
        public List<Tuple<int, int>> getValidMoves(Piece currentPiece, Gameboard gameboard, bool doRecurse = true)
        {
            this.copyOfCurrentPiece = new Piece(currentPiece);
            this.gameboard = new Gameboard(gameboard);

            switch (currentPiece.type)
            {
                case (int)type.rock: // tower
                    Rock rock = new Rock();
                    this.validDestinations = rock.Rules(currentPiece, gameboard);
                    if (doRecurse)
                        cleanUp();
                    return validDestinations;
                case (int)type.knight: // horsie
                    Knight knight = new Knight();
                    this.validDestinations = knight.Rules(currentPiece, gameboard);
                    if (doRecurse)
                        cleanUp();
                    return validDestinations;
                case (int)type.bishop: // springare
                    Bishop bishop = new Bishop();
                    this.validDestinations = bishop.Rules(currentPiece, gameboard);
                    if (doRecurse)
                        cleanUp();
                    return validDestinations;
                case (int)type.queen: //los quuenos
                    Queen queen = new Queen();
                    this.validDestinations = queen.Rules(currentPiece, gameboard);
                    if (doRecurse)
                        cleanUp();
                    return validDestinations;
                case (int)type.king: // los kingos
                    King king = new King();
                    this.validDestinations = king.Rules(currentPiece, gameboard);
                    if (doRecurse)
                        cleanUp();
                    return validDestinations;
                case (int)type.pawn: // los farmeros
                    Pawn pawn = new Pawn();
                    this.validDestinations = pawn.Rules(currentPiece, gameboard);
                    if (doRecurse)
                        cleanUp();
                    return validDestinations;
            }
            return new List<Tuple<int,int>>();
        }
Ejemplo n.º 3
0
        /*
         * This constructor is used in the clone method
         */
        public List(ArrayList l)
        {
            list = new ArrayList();

            foreach (Piece p in l)
            {
                Position pos = new Position(p.getPosition().getRow(), p.getPosition().getColumn());
                Color color = p.getColor();
                Image image = p.getImage();
                Piece piece = null;

                if (p is Bishop)
                {
                    piece = new Bishop(pos, color);
                }
                else if (p is Knight)
                {
                    piece = new Knight(pos, color);
                }
                else if (p is Pawn)
                {
                    piece = new Pawn(pos, color);
                }
                else if (p is Queen)
                {
                    piece = new Queen(pos, color);
                }
                else if (p is Rook)
                {
                    piece = new Rook(pos, color);
                }
                else if (p is King)
                {
                    piece = new King(pos, color);
                }

                list.Add(piece);
            }

        }
        public void KingShouldBeCorrectMove()
        {
            ChessFigure figure = new King("E1");

            Assert.AreEqual(true, figure.Move("E2"));
        }
Ejemplo n.º 5
0
        internal static Piece[,] GetFairyDefault()
        {
            Piece[,] b = new Piece[BOARDSIZE, BOARDSIZE];

            // Set Antelope
            for (int seed = 0; seed < 4; seed++)
            {
                byte team = (byte)(seed / 2);
                byte posX = (byte)((seed % 2) * (BOARDSIZE - 1));

                b[posX, (team * (BOARDSIZE - 1))] = new Antelope(team);
            }

            // Set Camel
            for (int seed = 0; seed < 4; seed++)
            {
                byte team = (byte)(seed / 2);
                byte posX = (byte)((seed % 2) * (BOARDSIZE - 3) + 1);

                b[posX, (team * (BOARDSIZE - 1))] = new Camel(team);
            }

            // Set Bow
            for (int seed = 0; seed < 4; seed++)
            {
                byte team = (byte)(seed / 2);
                byte posX = (byte)((seed % 2) * (BOARDSIZE - 5) + 2);

                b[posX, (team * (BOARDSIZE - 1))] = new Bow(team);
            }

            // Set Bull
            for (int seed = 0; seed < 4; seed++)
            {
                byte team = (byte)(seed / 2);
                byte posX = (byte)((seed % 2) * (BOARDSIZE - 7) + 3);

                b[posX, (team * (BOARDSIZE - 1))] = new Bull(team);
            }

            // Set Cannon
            for (int seed = 0; seed < 4; seed++)
            {
                byte team = (byte)(seed / 2);
                byte posX = (byte)((seed % 2) * (BOARDSIZE - 9) + 4);

                b[posX, (team * (BOARDSIZE - 1))] = new Cannon(team);
            }

            // Set Buffalo
            for (int seed = 0; seed < 2; seed++)
            {
                byte team = (byte)(seed % 2);
                b[5, (team * (BOARDSIZE - 1))] = new Buffalo(team);
            }

            // Set Rhinoceros
            for (int seed = 0; seed < 2; seed++)
            {
                byte team = (byte)(seed % 2);
                b[6, (team * (BOARDSIZE - 1))] = new Rhinoceros(team);
            }

            // Set Star
            for (int seed = 0; seed < 2; seed++)
            {
                byte team = (byte)(seed % 2);
                b[7, (team * (BOARDSIZE - 1))] = new Star(team);
            }

            // Set Unicorn
            for (int seed = 0; seed < 2; seed++)
            {
                byte team = (byte)(seed % 2);
                b[8, (team * (BOARDSIZE - 1))] = new Unicorn(team);
            }

            // Set DragonWoman
            for (int seed = 0; seed < 2; seed++)
            {
                byte team = (byte)(seed % 2);
                b[9, (team * (BOARDSIZE - 1))] = new DragonWoman(team);
            }

            // Set Diablo
            for (int seed = 0; seed < 2; seed++)
            {
                byte team = (byte)(seed % 2);
                b[10, (team * (BOARDSIZE - 1))] = new Diablo(team);
            }

            // Set Centurion
            for (int seed = 0; seed < 28; seed++)
            {
                byte team = (byte)((seed / 14) % 2);
                byte posX = (byte)(((9 + seed) + (team * 2)) % BOARDSIZE);
                b[posX, ((team * (BOARDSIZE - 3)) + 1)] = new Centurion(team);
            }

            // Set Lion
            for (int seed = 0; seed < 2; seed++)
            {
                byte team = (byte)(seed % 2);
                b[7, ((team * (BOARDSIZE - 3)) + 1)] = new Lion(team);
            }

            // Set Gryphon
            for (int seed = 0; seed < 2; seed++)
            {
                byte team = (byte)(seed % 2);
                b[8, ((team * (BOARDSIZE - 3)) + 1)] = new Gryphon(team);
            }

            // Set Elephant
            for (int seed = 0; seed < 4; seed++)
            {
                byte team = (byte)(seed / 2);
                byte posX = (byte)((seed % 2) * (BOARDSIZE - 1));

                b[posX, (team * (BOARDSIZE - 5)) + 2] = new Elephant(team);
            }

            // Set Machine
            for (int seed = 0; seed < 4; seed++)
            {
                byte team = (byte)(seed / 2);
                byte posX = (byte)(((seed % 2) * (BOARDSIZE - 3)) + 1);

                b[posX, (team * (BOARDSIZE - 5)) + 2] = new Machine(team);
            }

            // Set Rook
            for (int seed = 0; seed < 4; seed++)
            {
                byte team = (byte)(seed / 2);
                byte posX = (byte)(((seed % 2) * (BOARDSIZE - 5)) + 2);

                b[posX, (team * (BOARDSIZE - 5)) + 2] = new Rook(team);
            }

            // Set Knight
            for (int seed = 0; seed < 4; seed++)
            {
                byte team = (byte)(seed / 2);
                byte posX = (byte)(((seed % 2) * (BOARDSIZE - 7)) + 3);

                b[posX, (team * (BOARDSIZE - 5)) + 2] = new Knight(team);
            }

            // Set Bishop
            for (int seed = 0; seed < 4; seed++)
            {
                byte team = (byte)(seed / 2);
                byte posX = (byte)(((seed % 2) * (BOARDSIZE - 9)) + 4);

                b[posX, (team * (BOARDSIZE - 5)) + 2] = new Bishop(team);
            }

            // Set Ship
            for (int seed = 0; seed < 4; seed++)
            {
                byte team = (byte)(seed / 2);
                byte posX = (byte)(((seed % 2) * (BOARDSIZE - 11)) + 5);

                b[posX, (team * (BOARDSIZE - 5)) + 2] = new Ship(team);
            }

            // Set Buffoon
            for (int seed = 0; seed < 4; seed++)
            {
                byte team = (byte)(seed / 2);
                byte posX = (byte)(((seed % 2) * (BOARDSIZE - 13)) + 6);

                b[posX, (team * (BOARDSIZE - 5)) + 2] = new Buffoon(team);
            }

            // Set Queen
            for (int seed = 0; seed < 2; seed++)
            {
                byte team = (byte)(seed % 2);
                b[7, ((team * (BOARDSIZE - 5)) + 2)] = new Queen(team);
            }

            // Set King
            for (int seed = 0; seed < 2; seed++)
            {
                byte team = (byte)(seed % 2);
                b[8, ((team * (BOARDSIZE - 5)) + 2)] = new King(team);
            }

            // Set FairyPawn
            for (int seed = 0; seed < 32; seed++)
            {
                byte team = (byte)(seed / 16);
                b[(seed % 16), (byte)((team * (BOARDSIZE - 7)) + 3)] = new FairyPawn(team);
            }

            return(b);
        }
Ejemplo n.º 6
0
        public bool ChecktestWhite(GameBoard _Game)
        {
            int  i;
            King Kingmover1 = new King("White");

            for (i = 0; i < _Game.White.Count; i++)
            {
                if (_Game.White[i].GetType().ToString().Contains("King"))
                {
                    Kingmover1 = (King)_Game.White[i];
                    break;
                }
            }
            Coordinate _Next = Kingmover1.Current;
            string     typecheck;

            for (i = 0; i < _Game.Black.Count; i++)
            {
                typecheck = _Game.Black[i].GetType().ToString();
                if (_Game.Black[i].Current.X == 99)
                {
                    continue;
                }
                switch (typecheck)
                {
                case "Chess.Pawn":
                    Pawn Pawnmover = new Pawn("a");
                    Pawnmover = (Pawn)_Game.Black[i];
                    if (Pawnmover.isMovable(_Game, _Next))
                    {
                        return(true);
                    }
                    break;

                case "Chess.Bishop":
                    Bishop Bishopmover = new Bishop("a");
                    Bishopmover = (Bishop)_Game.Black[i];
                    if (Bishopmover.isMovable(_Game, _Next))
                    {
                        return(true);
                    }
                    break;

                case "Chess.Queen":
                    Queen Queenmover = new Queen("a");
                    Queenmover = (Queen)_Game.Black[i];
                    if (Queenmover.isMovable(_Game, _Next))
                    {
                        return(true);
                    }
                    break;

                case "Chess.Knight":
                    Knight Knightmover = new Knight("a");
                    Knightmover = (Knight)_Game.Black[i];
                    if (Knightmover.isMovable(_Game, _Next))
                    {
                        return(true);
                    }

                    break;

                case "Chess.Castle":
                    Castle Castlemover = new Castle("a");
                    Castlemover = (Castle)_Game.Black[i];
                    if (Castlemover.isMovable(_Game, _Next))
                    {
                        return(true);
                    }
                    break;

                case "Chess.King":
                    King Kingmover2 = new King("a");
                    Kingmover2 = (King)_Game.Black[i];
                    if (Kingmover2.isMovable(_Game, _Next))
                    {
                        return(true);
                    }
                    break;
                }
            }

            return(false);
        }
Ejemplo n.º 7
0
        public static void CheckEnemyKingBeAttaced(int startX,
                                                   int startY,
                                                   int finishX,
                                                   int finishY,
                                                   bool[,] chessmanPresenceSign,
                                                   ControlCollection controls,
                                                   object sender,
                                                   Dictionary <string, int> moveOrder)
        {
            //Клонируем массив признака расположения шахмат, для фальсификации
            bool[,] fakeChessmanPresenceSign = new bool[8, 8];
            for (var i = 0; i < 8; i++)
            {
                for (var j = 0; j < 8; j++)
                {
                    fakeChessmanPresenceSign[i, j] = chessmanPresenceSign[i, j];
                }
            }
            fakeChessmanPresenceSign[startY, startX]   = false;
            fakeChessmanPresenceSign[finishY, finishX] = true;

            //Клонируем свойство расположения шахмат для возможности фальсификации
            foreach (var control in controls)
            {
                if (control is Chessman)
                {
                    ((Chessman)control).FakeLocation = ((Chessman)control).Location;
                }
            }

            //Проверка возможности шаха
            foreach (var control in controls)
            {
                if (control is King)
                {
                    if (((Chessman)control).ChessColor != ((Chessman)sender).ChessColor)
                    {
                        var kingfinishX = (((King)control).Location.X - 27) / 50;
                        var kingFinishY = (((King)control).Location.Y - 27) / 50;

                        ((Chessman)control).FakeCheck = true;
                        ((Chessman)sender).ShahSigne  = King.CheckKingBeAttacked(kingfinishX, kingFinishY,
                                                                                 fakeChessmanPresenceSign,
                                                                                 controls, control, false, sender);
                        ((Chessman)control).FakeCheck = false;
                    }
                }
            }


            //Проверка возможности мата
            if (((Chessman)sender).ShahSigne)
            {
                var  flag = true;
                int  chessStartX, chessStartY;
                bool result;

                //Клонируем свойство расположения шахмат для возможности фальсификации
                foreach (var control in controls)
                {
                    if (control is Chessman)
                    {
                        ((Chessman)control).FakeLocation = ((Chessman)control).Location;
                    }
                }

                foreach (var control in controls)
                {
                    if (control is Chessman && flag)
                    {
                        ((Chessman)sender).MateSigne = true;
                        if (((Chessman)control).ChessColor != ((Chessman)sender).ChessColor)
                        {
                            result = true;

                            // Делаем фэйковые координаты
                            chessStartX = (((Chessman)control).Location.X - 27) / 50;
                            chessStartY = (((Chessman)control).Location.Y - 27) / 50;


                            if (control is King)
                            {
                                for (var i = 0; i < 8 && flag; i++)
                                {
                                    for (var j = 0; j < 8 && flag; j++)
                                    {
                                        var chessFinishX = i;
                                        var chessFinishY = j;

                                        if (chessFinishX != chessStartX || chessFinishY != chessStartY)
                                        {
                                            ((Chessman)control).FakeCheck = true;
                                            result = King.CheckKingMove(chessStartX, chessStartY, chessFinishX,
                                                                        chessFinishY,
                                                                        fakeChessmanPresenceSign, controls, control, sender);
                                            ((Chessman)control).FakeCheck = false;

                                            if (!result)
                                            {
                                                flag = false;
                                                break;
                                            }
                                        }
                                    }
                                }
                            }
                            if (control is Queen)
                            {
                                for (var i = 0; i < 8 && flag; i++)
                                {
                                    for (var j = 0; j < 8 && flag; j++)
                                    {
                                        //Задаем фэйковое положение
                                        var chessFinishX = i;
                                        var chessFinishY = j;
                                        ((Chessman)control).FakeLocation = new Point(chessFinishX * 50 + 27,
                                                                                     chessFinishY * 50 + 27);

                                        if (chessFinishX != chessStartX || chessFinishY != chessStartY)
                                        {
                                            ((Chessman)control).FakeCheck = true;
                                            result = Queen.CheckQueenMove(chessStartX, chessStartY, chessFinishX,
                                                                          chessFinishY,
                                                                          fakeChessmanPresenceSign, controls, control, moveOrder);
                                            ((Chessman)control).FakeCheck = false;

                                            if (!result)
                                            {
                                                flag = false;
                                                break;
                                            }
                                        }
                                    }
                                }
                            }


                            if (control is Castle)
                            {
                                for (var i = 0; i < 8 && flag; i++)
                                {
                                    for (var j = 0; j < 8 && flag; j++)
                                    {
                                        var chessFinishX = i;
                                        var chessFinishY = j;
                                        ((Chessman)control).FakeLocation = new Point(chessFinishX * 50 + 27,
                                                                                     chessFinishY * 50 + 27);

                                        if (chessFinishX != chessStartX || chessFinishY != chessStartY)
                                        {
                                            ((Chessman)control).FakeCheck = true;
                                            result = Castle.CheckCastleMove(chessStartX, chessStartY, chessFinishX,
                                                                            chessFinishY,
                                                                            fakeChessmanPresenceSign, controls, control, moveOrder);
                                            ((Chessman)control).FakeCheck = false;

                                            if (!result)
                                            {
                                                flag = false;
                                                break;
                                            }
                                        }
                                    }
                                }
                            }

                            if (control is Elephant)
                            {
                                for (var i = 0; i < 8 && flag; i++)
                                {
                                    for (var j = 0; j < 8 && flag; j++)
                                    {
                                        var chessFinishX = i;
                                        var chessFinishY = j;
                                        ((Chessman)control).FakeLocation = new Point(chessFinishX * 50 + 27,
                                                                                     chessFinishY * 50 + 27);

                                        if (chessFinishX != chessStartX || chessFinishY != chessStartY)
                                        {
                                            ((Chessman)control).FakeCheck = true;
                                            result = Elephant.CheckElephantMove(chessStartX, chessStartY, chessFinishX,
                                                                                chessFinishY,
                                                                                fakeChessmanPresenceSign, controls, control, moveOrder);
                                            ((Chessman)control).FakeCheck = false;

                                            if (!result)
                                            {
                                                flag = false;
                                                break;
                                            }
                                        }
                                    }
                                }
                            }


                            if (control is Horse)
                            {
                                for (var i = 0; i < 8 && flag; i++)
                                {
                                    for (var j = 0; j < 8 && flag; j++)
                                    {
                                        var chessFinishX = i;
                                        var chessFinishY = j;
                                        ((Chessman)control).FakeLocation = new Point(chessFinishX * 50 + 27,
                                                                                     chessFinishY * 50 + 27);

                                        if (chessFinishX != chessStartX || chessFinishY != chessStartY)
                                        {
                                            ((Chessman)control).FakeCheck = true;
                                            result = Horse.CheckHorseMove(chessStartX, chessStartY, chessFinishX,
                                                                          chessFinishY,
                                                                          fakeChessmanPresenceSign, controls, control, moveOrder);
                                            ((Chessman)control).FakeCheck = false;

                                            if (!result)
                                            {
                                                flag = false;
                                                break;
                                            }
                                        }
                                    }
                                }
                            }

                            if (control is Pawn)
                            {
                                for (var i = 0; i < 8 && flag; i++)
                                {
                                    for (var j = 0; j < 8 && flag; j++)
                                    {
                                        var chessFinishX = i;
                                        var chessFinishY = j;
                                        ((Chessman)control).FakeLocation = new Point(chessFinishX * 50 + 27,
                                                                                     chessFinishY * 50 + 27);

                                        if (chessFinishX != chessStartX || chessFinishY != chessStartY)
                                        {
                                            ((Chessman)control).FakeCheck = true;
                                            result = Pawn.CheckPawnMove(chessStartX, chessStartY, chessFinishX,
                                                                        chessFinishY,
                                                                        fakeChessmanPresenceSign, controls, control, moveOrder);
                                            ((Chessman)control).FakeCheck = false;

                                            if (!result)
                                            {
                                                flag = false;
                                                break;
                                            }
                                        }
                                    }
                                }
                            }
                            if (!result)
                            {
                                ((Chessman)sender).MateSigne = false;
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Clear the board and place each piece on the board
        /// </summary>
        /// <param name="board"></param>
        public void placePieces(Board board)
        {
            board.clearBoard(board);
            //Instancing each piece
            Rook   newBlackRook1   = new Rook("black", 0, 0);
            Knight newBlackKnight1 = new Knight("black", 0, 1);
            Bishop newBlackBishop1 = new Bishop("black", 0, 2);
            Queen  newBlackQueen   = new Queen("black", 0, 3);
            King   newBlackKing    = new King("black", 0, 4);
            Bishop newBlackBishop2 = new Bishop("black", 0, 5);
            Knight newBlackKnight2 = new Knight("black", 0, 6);
            Rook   newBlackRook2   = new Rook("black", 0, 7);

            Pawn newBlackPawn1 = new Pawn("black", 1, 0);
            Pawn newBlackPawn2 = new Pawn("black", 1, 1);
            Pawn newBlackPawn3 = new Pawn("black", 1, 2);
            Pawn newBlackPawn4 = new Pawn("black", 1, 3);
            Pawn newBlackPawn5 = new Pawn("black", 1, 4);
            Pawn newBlackPawn6 = new Pawn("black", 1, 5);
            Pawn newBlackPawn7 = new Pawn("black", 1, 6);
            Pawn newBlackPawn8 = new Pawn("black", 1, 7);


            Rook   newWhiteRook1   = new Rook("white", 7, 0);
            Knight newWhiteKnight1 = new Knight("white", 7, 1);
            Bishop newWhiteBishop1 = new Bishop("white", 7, 2);
            Queen  newWhiteQueen   = new Queen("white", 7, 3);
            King   newWhiteKing    = new King("white", 7, 4);
            Bishop newWhiteBishop2 = new Bishop("white", 7, 5);
            Knight newWhiteKnight2 = new Knight("white", 7, 6);
            Rook   newWhiteRook2   = new Rook("white", 7, 7);

            Pawn newWhitePawn1 = new Pawn("white", 6, 0);
            Pawn newWhitePawn2 = new Pawn("white", 6, 1);
            Pawn newWhitePawn3 = new Pawn("white", 6, 2);
            Pawn newWhitePawn4 = new Pawn("white", 6, 3);
            Pawn newWhitePawn5 = new Pawn("white", 6, 4);
            Pawn newWhitePawn6 = new Pawn("white", 6, 5);
            Pawn newWhitePawn7 = new Pawn("white", 6, 6);
            Pawn newWhitePawn8 = new Pawn("white", 6, 7);

            //Placing each piece on the board
            board.Grid[0, 0, 0].WhoIsOnIt = newBlackRook1;
            board.Grid[0, 1, 0].WhoIsOnIt = newBlackKnight1;
            board.Grid[0, 2, 0].WhoIsOnIt = newBlackBishop1;
            board.Grid[0, 3, 0].WhoIsOnIt = newBlackQueen;
            board.Grid[0, 4, 0].WhoIsOnIt = newBlackKing;
            board.Grid[0, 5, 0].WhoIsOnIt = newBlackBishop2;
            board.Grid[0, 6, 0].WhoIsOnIt = newBlackKnight2;
            board.Grid[0, 7, 0].WhoIsOnIt = newBlackRook2;

            board.Grid[1, 0, 0].WhoIsOnIt = newBlackPawn1;
            board.Grid[1, 1, 0].WhoIsOnIt = newBlackPawn2;
            board.Grid[1, 2, 0].WhoIsOnIt = newBlackPawn3;
            board.Grid[1, 3, 0].WhoIsOnIt = newBlackPawn4;
            board.Grid[1, 4, 0].WhoIsOnIt = newBlackPawn5;
            board.Grid[1, 5, 0].WhoIsOnIt = newBlackPawn6;
            board.Grid[1, 6, 0].WhoIsOnIt = newBlackPawn7;
            board.Grid[1, 7, 0].WhoIsOnIt = newBlackPawn8;

            board.Grid[7, 0, 0].WhoIsOnIt = newWhiteRook1;
            board.Grid[7, 1, 0].WhoIsOnIt = newWhiteKnight1;
            board.Grid[7, 2, 0].WhoIsOnIt = newWhiteBishop1;
            board.Grid[7, 3, 0].WhoIsOnIt = newWhiteQueen;
            board.Grid[7, 4, 0].WhoIsOnIt = newWhiteKing;
            board.Grid[7, 5, 0].WhoIsOnIt = newWhiteBishop2;
            board.Grid[7, 6, 0].WhoIsOnIt = newWhiteKnight2;
            board.Grid[7, 7, 0].WhoIsOnIt = newWhiteRook2;

            board.Grid[6, 0, 0].WhoIsOnIt = newWhitePawn1;
            board.Grid[6, 1, 0].WhoIsOnIt = newWhitePawn2;
            board.Grid[6, 2, 0].WhoIsOnIt = newWhitePawn3;
            board.Grid[6, 3, 0].WhoIsOnIt = newWhitePawn4;
            board.Grid[6, 4, 0].WhoIsOnIt = newWhitePawn5;
            board.Grid[6, 5, 0].WhoIsOnIt = newWhitePawn6;
            board.Grid[6, 6, 0].WhoIsOnIt = newWhitePawn7;
            board.Grid[6, 7, 0].WhoIsOnIt = newWhitePawn8;
        }
Ejemplo n.º 9
0
        static void Main(string[] args)
        {
            Position pawn1StartingPosition = new Position(0, 1);
            Pawn     pawn1 = new Pawn(pawn1StartingPosition);

            Position[] pawn1MovementOptions = pawn1.MovementOptions();
            Console.WriteLine("A posição inicial do Peão 1 é:");
            pawn1.Pos.Print();
            Console.WriteLine("A posição válida para movimento dele é:");
            pawn1MovementOptions[0].Print();
            Console.WriteLine();

            Position rook1StartingPosition = new Position(0, 0);
            Rook     rook1 = new Rook(rook1StartingPosition);

            Position[] rook1MovementOptions = rook1.MovementOptions();
            Console.WriteLine("A posição inicial da Torre 1 é:");
            rook1.Pos.Print();
            Console.WriteLine("As posições válidas para movimento dele são:");
            for (int i = 0; i < rook1.MovementOptions().Length; i++)
            {
                rook1MovementOptions[i].Print();
            }

            Position bishop1StartingPosition = new Position(2, 0);
            Bishop   bishop1 = new Bishop(bishop1StartingPosition);

            Position[] bishop1MovementOptions = bishop1.MovementOptions();
            Console.WriteLine("A posição inicial do Bispo 1 é:");
            bishop1.Pos.Print();
            Console.WriteLine("As posições válidas para movimento dele são:");
            for (int i = 0; i < bishop1.MovementOptions().Length; i++)
            {
                bishop1MovementOptions[i].Print();
            }

            Position knight1StartingPosition = new Position(1, 0);
            Knight   knight1 = new Knight(knight1StartingPosition);

            Position[] knight1MovementOptions = knight1.MovementOptions();
            Console.WriteLine("A posição inicial do Cavaleiro 1 é:");
            knight1.Pos.Print();
            Console.WriteLine("As posições válidas para movimento dele são:");
            for (int i = 0; i < knight1.MovementOptions().Length; i++)
            {
                knight1MovementOptions[i].Print();
            }

            Position queen1StartingPosition = new Position(4, 0);
            Queen    queen1 = new Queen(queen1StartingPosition);

            Position[] queen1MovementOptions = queen1.MovementOptions();
            Console.WriteLine("A posição inicial da Rainha 1 é:");
            queen1.Pos.Print();
            Console.WriteLine("As posições válidas para movimento dela são:");
            for (int i = 0; i < queen1.MovementOptions().Length; i++)
            {
                queen1MovementOptions[i].Print();
            }

            Position king1StartingPosition = new Position(3, 0);
            King     king1 = new King(king1StartingPosition);

            Position[] king1MovementOptions = king1.MovementOptions();
            Console.WriteLine("A posição inicial do Rei 1 é:");
            king1.Pos.Print();
            Console.WriteLine("As posições válidas para movimento dele são:");
            for (int i = 0; i < king1.MovementOptions().Length; i++)
            {
                king1MovementOptions[i].Print();
            }
            Console.ReadKey();
        }
Ejemplo n.º 10
0
        // part of pawn move function
        private bool pawnMovements(ref Tile startingTile, ref Tile destinationTile, ref ChessBoard chess, bool white)
        {
            int  mult;
            King blackKing = (King)chess.Pieces[20];
            King whiteKing = (King)chess.Pieces[21];

            if (white)
            {
                mult = 1;
            }
            else
            {
                mult = -1;
            }
            if (!HasMoved && (Position.RowInBoard - destinationTile.RowInBoard == 2 * mult) && destinationTile.ColumnInBoard == Position.ColumnInBoard && allowMove(ref chess, ref destinationTile))
            {
                if (white)
                {
                    if (!whiteKing.canMoveBeforeKing(ref chess, ref destinationTile, this))
                    {
                        return(false);
                    }
                }
                else
                {
                    if (!blackKing.canMoveBeforeKing(ref chess, ref destinationTile, this))
                    {
                        return(false);
                    }
                }
                if (destinationTile.PieceInside != null)
                {
                    return(false);
                }
                changeTileForPiece(ref startingTile, ref destinationTile, ref chess);
                HasMoved          = true;
                JustMovedTwoSteps = true;
                return(true);
            }
            if ((Position.RowInBoard - destinationTile.RowInBoard == 1 * mult) && destinationTile.ColumnInBoard == Position.ColumnInBoard && allowMove(ref chess, ref destinationTile))
            {
                if (white)
                {
                    if (!whiteKing.canMoveBeforeKing(ref chess, ref destinationTile, this))
                    {
                        return(false);
                    }
                }
                else
                {
                    if (!blackKing.canMoveBeforeKing(ref chess, ref destinationTile, this))
                    {
                        return(false);
                    }
                }
                if (destinationTile.PieceInside != null)
                {
                    return(false);
                }
                changeTileForPiece(ref startingTile, ref destinationTile, ref chess);
                JustMovedTwoSteps = false;
                return(true);
            }
            else if ((Position.RowInBoard - destinationTile.RowInBoard == 1 * mult) && Position.ColumnInBoard - destinationTile.ColumnInBoard == 1 && destinationTile.PieceInside != null && allowMove(ref chess, ref destinationTile))
            {
                if (white)
                {
                    if (!whiteKing.canMoveBeforeKing(ref chess, ref destinationTile, this))
                    {
                        return(false);
                    }
                }
                else
                {
                    if (!blackKing.canMoveBeforeKing(ref chess, ref destinationTile, this))
                    {
                        return(false);
                    }
                }
                if (destinationTile.PieceInside.IsWhite != white)
                {
                    changeTileForPiece(ref startingTile, ref destinationTile, ref chess);
                    JustMovedTwoSteps = false;
                    return(true);
                }
            }
            else if ((Position.RowInBoard - destinationTile.RowInBoard == 1 * mult) && Position.ColumnInBoard - destinationTile.ColumnInBoard == -1 && destinationTile.PieceInside != null && allowMove(ref chess, ref destinationTile))
            {
                if (white)
                {
                    if (!whiteKing.canMoveBeforeKing(ref chess, ref destinationTile, this))
                    {
                        return(false);
                    }
                }
                else
                {
                    if (!blackKing.canMoveBeforeKing(ref chess, ref destinationTile, this))
                    {
                        return(false);
                    }
                }
                if (destinationTile.PieceInside.IsWhite != white)
                {
                    changeTileForPiece(ref startingTile, ref destinationTile, ref chess);
                    JustMovedTwoSteps = false;
                    return(true);
                }
            }
            return(false);
        }
Ejemplo n.º 11
0
        public void initBoard()
        {
            Console.WriteLine("INIT Board");

            /* Queens */
            chessPiece QueenW = new Queen(false, 7, 3);

            chessBoard[QueenW.getX(), QueenW.getY()] = QueenW;
            chessPiece QueenB = new Queen(true, 0, 3);

            chessBoard[QueenB.getX(), QueenB.getY()] = QueenB;

            // /* Kings */
            chessPiece KingW = new King(false, 7, 4);

            chessBoard[KingW.getX(), KingW.getY()] = KingW;
            setKingLocation(false, 7, 4);
            chessPiece KingB = new King(true, 0, 4);

            chessBoard[KingB.getX(), KingB.getY()] = KingB;
            setKingLocation(true, 0, 4);

            // /* Bishops */
            chessPiece BishW1 = new Bishop(false, 7, 2);

            chessBoard[BishW1.getX(), BishW1.getY()] = BishW1;
            chessPiece BishW2 = new Bishop(false, 7, 5);

            chessBoard[BishW2.getX(), BishW2.getY()] = BishW2;
            // Blacks
            chessPiece BishB1 = new Bishop(true, 0, 2);

            chessBoard[BishB1.getX(), BishB1.getY()] = BishB1;
            chessPiece BishB2 = new Bishop(true, 0, 5);

            chessBoard[BishB2.getX(), BishB2.getY()] = BishB2;

            // /* Knights */
            chessPiece KnW1 = new Knight(false, 7, 1);

            chessBoard[KnW1.getX(), KnW1.getY()] = KnW1;
            chessPiece KnW2 = new Knight(false, 7, 6);

            chessBoard[KnW2.getX(), KnW2.getY()] = KnW2;
            // Blacks
            chessPiece KnB1 = new Knight(true, 0, 1);

            chessBoard[KnB1.getX(), KnB1.getY()] = KnB1;
            chessPiece KnB2 = new Knight(true, 0, 6);

            chessBoard[KnB2.getX(), KnB2.getY()] = KnB2;

            /* Rooks */
            chessPiece RookW1 = new Rook(false, 7, 0);

            chessBoard[RookW1.getX(), RookW1.getY()] = RookW1;
            chessPiece RookW2 = new Rook(false, 7, 7);

            chessBoard[RookW2.getX(), RookW2.getY()] = RookW2;
            // Blacks
            chessPiece RookB1 = new Rook(true, 0, 0);

            chessBoard[RookB1.getX(), RookB1.getY()] = RookB1;
            chessPiece RookB2 = new Rook(true, 0, 7);

            chessBoard[RookB2.getX(), RookB2.getY()] = RookB2;

            /* Pawns */
            // chessPiece PawnW1 = new Pawn(false, 4, 4);
            // chessBoard[PawnW1.getX(), PawnW1.getY()] = PawnW1;
            // chessPiece PawnW2 = new Pawn(false, 6, 1);
            // chessBoard[PawnW2.getX(), PawnW2.getY()] = PawnW2;
            // chessPiece PawnW3 = new Pawn(false, 6, 2);
            // chessBoard[PawnW3.getX(), PawnW3.getY()] = PawnW3;
            // chessPiece PawnW4 = new Pawn(false, 6, 3);
            // chessBoard[PawnW4.getX(), PawnW4.getY()] = PawnW4;
            // chessPiece PawnW5 = new Pawn(false, 6, 4);
            // chessBoard[PawnW5.getX(), PawnW5.getY()] = PawnW5;
            // chessPiece PawnW6 = new Pawn(false, 6, 5);
            // chessBoard[PawnW6.getX(), PawnW6.getY()] = PawnW6;
            // chessPiece PawnW7 = new Pawn(false, 6, 6);
            // chessBoard[PawnW7.getX(), PawnW7.getY()] = PawnW7;
            // chessPiece PawnW8 = new Pawn(false, 6, 7);
            // chessBoard[PawnW8.getX(), PawnW8.getY()] = PawnW8;
            //  // Blacks
            // chessPiece PawnB1 = new Pawn(true, 1, 0);
            // chessBoard[PawnB1.getX(), PawnB1.getY()] = PawnB1;
            // chessPiece PawnB2 = new Pawn(true, 1, 1);
            // chessBoard[PawnB2.getX(), PawnB2.getY()] = PawnB2;
            // chessPiece PawnB3 = new Pawn(true, 1, 2);
            // chessBoard[PawnB3.getX(), PawnB3.getY()] = PawnB3;
            // chessPiece PawnB4 = new Pawn(true, 1, 3);
            // chessBoard[PawnB4.getX(), PawnB4.getY()] = PawnB4;
            // chessPiece PawnB5 = new Pawn(true, 1, 4);
            // chessBoard[PawnB5.getX(), PawnB5.getY()] = PawnB5;
            // chessPiece PawnB6 = new Pawn(true, 1, 5);
            // chessBoard[PawnB6.getX(), PawnB6.getY()] = PawnB6;
            // chessPiece PawnB7 = new Pawn(true, 1, 6);
            // chessBoard[PawnB7.getX(), PawnB7.getY()] = PawnB7;
            // chessPiece PawnB8 = new Pawn(true, 1, 7);
            // chessBoard[PawnB8.getX(), PawnB8.getY()] = PawnB8;
        }
Ejemplo n.º 12
0
        /**
         * StartGame()
         *
         * Remplit le plateau et lance le premier tour
         *
         * @author Axel Floquet-Trillot
         * */
        public void StartGame()
        {
            //Remplissage du plateau
            for (int i = 0; i < GameBoard.GetLength(0); i++)
            {
                switch (i)
                {
                case 0:
                    for (int j = 0; j < GameBoard.GetLength(1); j++)
                    {
                        switch (j)
                        {
                        case 0:
                        case 7: GameBoard[i, j] = new Rook(Piece.Color.Black);
                            break;

                        case 1:
                        case 6:  GameBoard[i, j] = new Knight(Piece.Color.Black);
                            break;

                        case 2:
                        case 5: GameBoard[i, j] = new Bishop(Piece.Color.Black);
                            break;

                        case 3: GameBoard[i, j] = new Queen(Piece.Color.Black);
                            break;

                        case 4: GameBoard[i, j] = new King(Piece.Color.Black);
                            break;
                        }
                    }
                    break;

                case 1:
                    for (int j = 0; j < GameBoard.GetLength(1); j++)
                    {
                        GameBoard[i, j] = new Pawn(Piece.Color.Black);
                    }
                    break;

                case 2:
                case 3:
                case 4:
                case 5:
                    for (int j = 0; j < GameBoard.GetLength(1); j++)
                    {
                        GameBoard[i, j] = null;
                    }
                    break;

                case 6:
                    for (int j = 0; j < GameBoard.GetLength(1); j++)
                    {
                        GameBoard[i, j] = new Pawn(Piece.Color.White);
                    }
                    break;

                case 7:
                    for (int j = 0; j < GameBoard.GetLength(1); j++)
                    {
                        switch (j)
                        {
                        case 0:
                        case 7: GameBoard[i, j] = new Rook(Piece.Color.White);
                            break;

                        case 1:
                        case 6: GameBoard[i, j] = new Knight(Piece.Color.White);
                            break;

                        case 2:
                        case 5: GameBoard[i, j] = new Bishop(Piece.Color.White);
                            break;

                        case 3: GameBoard[i, j] = new Queen(Piece.Color.White);
                            break;

                        case 4: GameBoard[i, j] = new King(Piece.Color.White);
                            break;
                        }
                    }
                    break;
                }
            }

            PlayTurn();
        }
Ejemplo n.º 13
0
        /**
         * FindPiecesCoordinatesAndMovesToProtectMyKing()
         *
         * Analyse la totalité du plateau afin de trouver toutes les pièces et leur déplacement(s) associé(s) permettant de protéger le roi en cas d'échec
         *
         * @return List<Coordinates>[]
         * FindPiecesCoordinatesAndMovesToProtectMyKing()[0] permet de récupérer les pièces pouvant protéger un roi
         * FindPiecesCoordinatesAndMovesToProtectMyKing()[1] permet de récupérer les déplacements pouvant protéger un roi
         *
         * @author Axel Floquet-Trillot
         * */
        private List <Coordinates>[] FindPiecesCoordinatesAndMovesToProtectMyKing()
        {
            Coordinates        MyKing;
            List <Coordinates> MateThreat = new List <Coordinates>();
            List <Coordinates> PiecesCoordinatesToProtectMyKing = new List <Coordinates>();
            List <Coordinates> MatePossibleMoves = new List <Coordinates>();

            if (PlayingNow == Piece.Color.White)
            {
                MyKing     = King.locateKing(GameBoard, Piece.Color.White);
                MateThreat = WhiteMateThreats;
            }
            else
            {
                MyKing     = King.locateKing(GameBoard, Piece.Color.Black);
                MateThreat = BlackMateThreats;
            }

            //Exception levée si un des rois n'est pas trouvé
            if (MyKing == null)
            {
                throw (new Exception("Un des rois n'a pas pu être trouvé"));
            }

            //Recherche des pièces pouvant protéger le roi
            if (MateThreat.Count() > 0 && PlayingNow == GameBoard[MyKing.X, MyKing.Y].PieceColor)
            {
                foreach (Coordinates Threat in MateThreat)
                {
                    for (int x = 0; x < GameBoard.GetLength(0); x++)
                    {
                        for (int y = 0; y < GameBoard.GetLength(1); y++)
                        {
                            if (GameBoard[x, y] != null &&
                                GameBoard[x, y].PieceColor == PlayingNow)
                            {
                                foreach (Coordinates Coord in GameBoard[x, y].GetPossibleMoves(GameBoard, new Coordinates(x, y)))
                                {
                                    if (!GameBoard[x, y].Equals(GameBoard[MyKing.X, MyKing.Y]))
                                    {
                                        //Si la menace peut être mangée
                                        if (Coord.X == Threat.X && Coord.Y == Threat.Y)
                                        {
                                            PiecesCoordinatesToProtectMyKing.Add(new Coordinates(x, y));
                                            MatePossibleMoves.Add(new Coordinates(Coord.X, Coord.Y));
                                        }

                                        //Si une pièce peut s'interposer entre le roi et la menace sur la même ligne
                                        if (Threat.X == MyKing.X &&
                                            Threat.X == Coord.X &&
                                            ((Coord.Y > MyKing.Y && Coord.Y < Threat.Y) || (Coord.Y < MyKing.Y && Coord.Y > Threat.Y)))
                                        {
                                            PiecesCoordinatesToProtectMyKing.Add(new Coordinates(x, y));
                                            MatePossibleMoves.Add(new Coordinates(Coord.X, Coord.Y));
                                        }

                                        //Si une pièce peut s'interposer entre le roi et la menace sur la même colonne
                                        if (Threat.Y == MyKing.Y &&
                                            Threat.Y == Coord.Y &&
                                            ((Coord.X > MyKing.X && Coord.X < Threat.X) || (Coord.X < MyKing.X && Coord.X > Threat.X)))
                                        {
                                            PiecesCoordinatesToProtectMyKing.Add(new Coordinates(x, y));
                                            MatePossibleMoves.Add(new Coordinates(Coord.X, Coord.Y));
                                        }

                                        //Si une pièce peut s'interposer entre le roi et la menace en diagonale gauche->droite (\)
                                        if (MyKing.X - Threat.X == MyKing.Y - Threat.Y &&
                                            MyKing.X - Coord.X == MyKing.Y - Coord.Y &&
                                            ((MyKing.X - Coord.X < MyKing.X - Threat.X && MyKing.Y - Coord.Y < MyKing.Y - Threat.Y) ||
                                             (MyKing.X - Coord.X > MyKing.X - Threat.X && MyKing.Y - Coord.Y > MyKing.Y - Threat.Y)))
                                        {
                                            PiecesCoordinatesToProtectMyKing.Add(new Coordinates(x, y));
                                            MatePossibleMoves.Add(new Coordinates(Coord.X, Coord.Y));
                                        }

                                        //Si une pièce peut s'interposer entre le roi et la menace en diagonale droite->gauche (/)
                                        if (MyKing.X - Threat.X == Threat.Y - MyKing.Y &&
                                            MyKing.X - Coord.X == Coord.Y - MyKing.Y &&
                                            ((MyKing.X - Coord.X < MyKing.X - Threat.X && MyKing.Y - Coord.Y > MyKing.Y - Threat.Y) ||
                                             (MyKing.X - Coord.X > MyKing.X - Threat.X && MyKing.Y - Coord.Y < MyKing.Y - Threat.Y)))
                                        {
                                            PiecesCoordinatesToProtectMyKing.Add(new Coordinates(x, y));
                                            MatePossibleMoves.Add(new Coordinates(Coord.X, Coord.Y));
                                        }
                                    }
                                    //Si le roi peut bouger et sortir de l'échec
                                    if ((x == MyKing.X && y == MyKing.Y) &&
                                        !VirtualMoveAndMateTest(new Coordinates(MyKing.X, MyKing.Y), new Coordinates(Coord.X, Coord.Y)))
                                    {
                                        PiecesCoordinatesToProtectMyKing.Add(new Coordinates(x, y));
                                        MatePossibleMoves.Add(new Coordinates(Coord.X, Coord.Y));
                                    }
                                }
                            }
                        }
                    }
                }
            }

            return(new List <Coordinates>[] { PiecesCoordinatesToProtectMyKing, MatePossibleMoves });
        }
Ejemplo n.º 14
0
        /// <summary>
        /// Move a peice from one location on the board to another.
        /// </summary>
        /// <param name="from">The location of the piece that is moving.</param>
        /// <param name="to">The location to move to.</param>
        /// <returns>Returns true on success or false on failure.</returns>
        public bool ActionPiece(Point from, Point to, bool bypassValidaiton = false)
        {
            if (bypassValidaiton || PieceActions(from).Contains(to))
            {
                ChessPiece movingPeice = boardArray[from.x, from.y];
                if (movingPeice is Pawn)
                {
                    Pawn pawn = (Pawn)movingPeice;
                    // If this was a double jump, check enpassant
                    if (Math.Abs(from.y - to.y) == 2)
                    {
                        int adjasentX = to.x - 1;
                        if (adjasentX > -1 &&
                            boardArray[adjasentX, to.y] != null &&
                            boardArray[adjasentX, to.y].Player != movingPeice.Player &&
                            boardArray[adjasentX, to.y] is Pawn)
                        {
                            if (!bypassValidaiton)
                            {
                                ((Pawn)boardArray[adjasentX, to.y]).CanEnPassantRight = true;
                            }
                        }
                        adjasentX += 2;
                        if (adjasentX < COLUMNS &&
                            boardArray[adjasentX, to.y] != null &&
                            boardArray[adjasentX, to.y].Player != movingPeice.Player &&
                            boardArray[adjasentX, to.y] is Pawn)
                        {
                            if (!bypassValidaiton)
                            {
                                ((Pawn)boardArray[adjasentX, to.y]).CanEnPassantLeft = true;
                            }
                        }
                    }
                    // If this was a sideways jump to null, it was enpassant!
                    if (from.x != to.x && boardArray[to.x, to.y] == null)
                    {
                        boardArray[to.x, from.y] = null;
                    }

                    if (!bypassValidaiton) // Pawns can't double jump after they move.
                    {
                        pawn.CanDoubleJump = false;
                    }
                }
                if (movingPeice is CastlePiece)
                {
                    CastlePiece rookOrKing = (CastlePiece)movingPeice;
                    if (!bypassValidaiton) // Castling can't be done after moving
                    {
                        rookOrKing.CanCastle = false;
                    }
                }
                if (movingPeice is King)
                {
                    King king = (King)movingPeice;
                    if (from.x - to.x == 2)
                    {   // Move rook for Queenside castle
                        boardArray[to.x + 1, from.y] = boardArray[0, from.y];
                        boardArray[0, from.y]        = null;
                    }
                    if (from.x - to.x == -2)
                    {   // Move rook for Kingside castle
                        boardArray[to.x - 1, from.y]    = boardArray[COLUMNS - 1, from.y];
                        boardArray[COLUMNS - 1, from.y] = null;
                    }
                }
                movingPeice.CalculateMoves();
                boardArray[from.x, from.y] = null;
                boardArray[to.x, to.y]     = movingPeice;
                return(true);
            }
            return(false);
        }
Ejemplo n.º 15
0
        public static void AddPiece(this Game game, Square square, PieceType type)
        {
            Piece piece = null;

            switch (type)
            {
            case PieceType.NoPiece:
                break;

            case PieceType.WhiteKing:
                piece = new King(Color.White);
                break;

            case PieceType.WhiteQueen:
                piece = new Queen(Color.White);
                break;

            case PieceType.WhiteRook:
                piece = new Rook(Color.White);
                break;

            case PieceType.WhiteBishop:
                piece = new Bishop(Color.White);
                break;

            case PieceType.WhiteNight:
                piece = new Knight(Color.White);
                break;

            case PieceType.WhitePawn:
                piece = new Pawn(Color.White);
                break;

            case PieceType.BlackKing:
                piece = new King(Color.Black);
                break;

            case PieceType.BlackQueen:
                piece = new Queen(Color.Black);
                break;

            case PieceType.BlackRook:
                piece = new Rook(Color.Black);
                break;

            case PieceType.BlackBishop:
                piece = new Bishop(Color.Black);
                break;

            case PieceType.BlackKnight:
                piece = new Knight(Color.Black);
                break;

            case PieceType.BlackPawn:
                piece = new Pawn(Color.Black);
                break;

            default:
                throw new NotImplementedException();
            }
            game.AddPiece(square.File, square.Rank, piece);
        }
Ejemplo n.º 16
0
        void assingRow(int row, int col)
        {
            colors            color           = row == 0 ? colors.BLACK : colors.WHITE;
            Location          settingLocation = new Location();
            List <ChessPiece> alivesList      = row == 0 ? blacksAlive : whitesAlive;

            switch (col)
            {
            case 0:
                settingLocation      = new Location(row, col);
                boardState[row, col] = new Tile(new Rook(color, pieceType.ROOK, settingLocation));
                alivesList.Add(this[settingLocation]);
                break;

            case 7:
                settingLocation      = new Location(row, col);
                boardState[row, col] = new Tile(new Rook(color, pieceType.ROOK, settingLocation));
                alivesList.Add(this[settingLocation]);
                break;

            case 1:
                settingLocation      = new Location(row, col);
                boardState[row, col] = new Tile(new Knight(color, pieceType.KNIGHT, settingLocation));
                alivesList.Add(this[settingLocation]);
                break;

            case 6:
                settingLocation      = new Location(row, col);
                boardState[row, col] = new Tile(new Knight(color, pieceType.KNIGHT, settingLocation));
                alivesList.Add(this[settingLocation]);
                break;

            case 2:
                settingLocation      = new Location(row, col);
                boardState[row, col] = new Tile(new Bishop(color, pieceType.BISHOP, settingLocation));
                alivesList.Add(this[settingLocation]);
                break;

            case 5:
                settingLocation      = new Location(row, col);
                boardState[row, col] = new Tile(new Bishop(color, pieceType.BISHOP, settingLocation));
                alivesList.Add(this[settingLocation]);
                break;

            case 4:
                if (row == 0)
                {
                    settingLocation      = new Location(row, col);
                    boardState[row, col] = new Tile(new King(color, pieceType.KING, settingLocation));
                    blackKing            = this[settingLocation] as King;
                }
                else
                {
                    settingLocation      = new Location(row, col);
                    boardState[row, col] = new Tile(new King(color, pieceType.QUEEN, settingLocation));
                    whiteKing            = this[settingLocation] as King;
                }
                alivesList.Add(this[settingLocation]);
                break;

            case 3:
                settingLocation      = new Location(row, col);
                boardState[row, col] = new Tile(new Queen(color, pieceType.QUEEN, settingLocation));
                alivesList.Add(this[settingLocation]);
                break;
            }
            col++;
        }
Ejemplo n.º 17
0
 public void BeforeTest()
 {
     whiteKing = new King(true);
     blackKing = new King(false);
 }
Ejemplo n.º 18
0
        public byte[][] GetCastle(Piece[,] board)
        {
            // Make sure can castle
            if (hasMoved)
            {
                return(null);
            }

            byte[] pos       = { 0, 0 };
            bool   foundThis = false;

            byte[] kingPos   = { 0, 0 };
            bool   foundKing = false;
            King   king      = new King(Byte.MaxValue);

            for (byte x = 0; x < board.GetLength(0) && !(foundKing && foundThis); x++)
            {
                for (byte y = 0; y < board.GetLength(0) && !(foundKing && foundThis); y++)
                {
                    if (board[x, y] != null)
                    {
                        if (board[x, y] == this)
                        {
                            foundThis = true;
                            pos[0]    = x;
                            pos[1]    = y;
                        }
                        else if (board[x, y].team == this.team && board[x, y].GetType() == typeof(King))
                        {
                            King temp = (King)board[x, y];
                            if (!temp.hasMoved)
                            {
                                foundKing  = true;
                                kingPos[0] = x;
                                kingPos[1] = y;
                                king       = temp;
                            }
                        }
                        if (foundKing && foundThis)
                        {
                            break;
                        }
                    }
                }
            }

            if (kingPos[1] != pos[1])
            {
                return(null);
            }
            else if (!king.hasMoved)
            {
                int   dX  = kingPos[0] - pos[0];
                sbyte dir = (sbyte)((dX) / (Math.Abs(dX)));
                for (int i = 1; i < board.GetLength(0); i++)
                {
                    int currX = pos[0] + (dir * i);
                    if (currX == kingPos[0])
                    {
                        byte[][] move = new byte[4][];
                        move[0] = new byte[] { (byte)(kingPos[0] + (-dir * 2)), kingPos[1], kingPos[0], kingPos[1] };
                        move[1] = new byte[] { kingPos[0], kingPos[1], byte.MaxValue - 1, byte.MaxValue };
                        move[2] = new byte[] { (byte)(pos[0] + (dX * (((double)3) / ((double)4)))), pos[1], pos[0], pos[1] };
                        move[3] = new byte[] { pos[0], pos[1], byte.MaxValue, byte.MaxValue - 1 };
                        return(move);
                    }
                    else if (board[currX, pos[1]] != null)
                    {
                        return(null);
                    }
                }
            }

            return(null);
        }
Ejemplo n.º 19
0
        public static void SetupBoard()
        {
            Pawn   p1W = new Pawn(0, 6, "WHITE");
            Pawn   p2W = new Pawn(1, 6, "WHITE");
            Pawn   p3W = new Pawn(2, 6, "WHITE");
            Pawn   p4W = new Pawn(3, 6, "WHITE");
            Pawn   p5W = new Pawn(4, 6, "WHITE");
            Pawn   p6W = new Pawn(5, 6, "WHITE");
            Pawn   p7W = new Pawn(6, 6, "WHITE");
            Pawn   p8W = new Pawn(7, 6, "WHITE");
            Rook   r1W = new Rook(0, 7, "WHITE");
            Knight n1W = new Knight(1, 7, "WHITE");
            Bishop b1W = new Bishop(2, 7, "WHITE");
            Queen  q1W = new Queen(3, 7, "WHITE");
            King   k1W = new King(4, 7, "WHITE");
            Bishop b2W = new Bishop(5, 7, "WHITE");
            Knight n2W = new Knight(6, 7, "WHITE");
            Rook   r2W = new Rook(7, 7, "WHITE");

            Pawn   p1B = new Pawn(0, 1, "BLACK");
            Pawn   p2B = new Pawn(1, 1, "BLACK");
            Pawn   p3B = new Pawn(2, 1, "BLACK");
            Pawn   p4B = new Pawn(3, 1, "BLACK");
            Pawn   p5B = new Pawn(4, 1, "BLACK");
            Pawn   p6B = new Pawn(5, 1, "BLACK");
            Pawn   p7B = new Pawn(6, 1, "BLACK");
            Pawn   p8B = new Pawn(7, 1, "BLACK");
            Rook   r1B = new Rook(0, 0, "BLACK");
            Knight n1B = new Knight(1, 0, "BLACK");
            Bishop b1B = new Bishop(2, 0, "BLACK");
            Queen  q1B = new Queen(3, 0, "BLACK");
            King   k1B = new King(4, 0, "BLACK");
            Bishop b2B = new Bishop(5, 0, "BLACK");
            Knight n2B = new Knight(6, 0, "BLACK");
            Rook   r2B = new Rook(7, 0, "BLACK");


            board[p1W.Y, p1W.X] = p1W;
            board[p2W.Y, p2W.X] = p2W;
            board[p3W.Y, p3W.X] = p3W;
            board[p4W.Y, p4W.X] = p4W;
            board[p5W.Y, p5W.X] = p5W;
            board[p6W.Y, p6W.X] = p6W;
            board[p7W.Y, p7W.X] = p7W;
            board[p8W.Y, p8W.X] = p8W;
            board[r1W.Y, r1W.X] = r1W;
            board[n1W.Y, n1W.X] = n1W;
            board[b1W.Y, b1W.X] = b1W;
            board[q1W.Y, q1W.X] = q1W;
            board[k1W.Y, k1W.X] = k1W;
            board[b2W.Y, b2W.X] = b2W;
            board[n2W.Y, n2W.X] = n2W;
            board[r2W.Y, r2W.X] = r2W;

            board[p1B.Y, p1B.X] = p1B;
            board[p2B.Y, p2B.X] = p2B;
            board[p3B.Y, p3B.X] = p3B;
            board[p4B.Y, p4B.X] = p4B;
            board[p5B.Y, p5B.X] = p5B;
            board[p6B.Y, p6B.X] = p6B;
            board[p7B.Y, p7B.X] = p7B;
            board[p8B.Y, p8B.X] = p8B;
            board[r1B.Y, r1B.X] = r1B;
            board[n1B.Y, n1B.X] = n1B;
            board[b1B.Y, b1B.X] = b1B;
            board[q1B.Y, q1B.X] = q1B;
            board[k1B.Y, k1B.X] = k1B;
            board[b2B.Y, b2B.X] = b2B;
            board[n2B.Y, n2B.X] = n2B;
            board[r2B.Y, r2B.X] = r2B;
        }
Ejemplo n.º 20
0
 private void CastlingFalse(King k)
 {
     k.castlingPossible = false;
 }
Ejemplo n.º 21
0
        public void attemptsToMove(MouseState click, ref ChessBoard chess) // move logic for each player
        {
            if (selectionTile == null)
            {
                return;
            }
            King checkedBlackKing = (King)chess.Pieces[20];
            King checkedWhiteKing = (King)chess.Pieces[21];

            if (click.LeftButton == ButtonState.Pressed)
            {
                for (int i = 0; i < 8; i++)
                {
                    for (int j = 0; j < 8; j++)
                    {
                        if (chess.Tiles[i, j].getBorder().Contains(new Point(click.X, click.Y)) && selectionTile.PieceInside != null)
                        {
                            if (WhitePlayerTurn && selectionTile.PieceInside.IsWhite)
                            {
                                if (selectionTile.PieceInside.move(ref selectionTile, ref chess.Tiles[i, j], chess))
                                {
                                    //winning condition
                                    chess.resetAttackedTiles();
                                    chess.promotePawn(ChessGame.Content);
                                    for (int k = 0; k < 32; k++)
                                    {
                                        if (chess.Pieces[k] != null)
                                        {
                                            chess.Pieces[k].attackTile(ref chess);
                                        }
                                    }
                                    checkedBlackKing.clearKing();

                                    if (checkedBlackKing.checkPath(ref chess))
                                    {
                                        chess.resetAttackedTiles();
                                        for (int k = 0; k < 32; k++)
                                        {
                                            if (chess.Pieces[k] != null)
                                            {
                                                if ((chess.Pieces[k] is Pawn && !chess.Pieces[k].IsWhite) || k == 20)
                                                {
                                                    continue;
                                                }
                                                chess.Pieces[k].attackTile(ref chess);
                                            }
                                        }
                                    }
                                    checkedBlackKing.checkmate(ChessGame.player1, ChessGame.player2, ref chess);
                                    WhitePlayerTurn = !WhitePlayerTurn;
                                    return;
                                }
                            }
                            else if (!(WhitePlayerTurn) && !(selectionTile.PieceInside.IsWhite))
                            {
                                if (selectionTile.PieceInside.move(ref selectionTile, ref chess.Tiles[i, j], chess))
                                {
                                    chess.resetAttackedTiles();
                                    chess.promotePawn(ChessGame.Content);
                                    for (int k = 0; k < 32; k++)
                                    {
                                        if (chess.Pieces[k] != null)
                                        {
                                            chess.Pieces[k].attackTile(ref chess);
                                        }
                                    }
                                    checkedWhiteKing.clearKing();
                                    if (checkedWhiteKing.checkPath(ref chess))
                                    {
                                        chess.resetAttackedTiles();
                                        for (int k = 0; k < 32; k++)
                                        {
                                            if (chess.Pieces[k] != null)
                                            {
                                                if ((chess.Pieces[k] is Pawn && chess.Pieces[k].IsWhite) || k == 21)
                                                {
                                                    continue;
                                                }
                                                chess.Pieces[k].attackTile(ref chess);
                                            }
                                        }
                                    }
                                    checkedWhiteKing.checkmate(ChessGame.player1, ChessGame.player2, ref chess);
                                    WhitePlayerTurn = !WhitePlayerTurn;
                                    return;
                                }
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 22
0
        internal bool CheckKingsSafety(Piece pieceMoving, Position newPos, Chessboard board)
        {
            bool       mat       = false;
            Side       defSide   = pieceMoving.Side;
            Chessboard tempBoard = Chessboard.DeepClone(board);
            //Piece pieceMirrored = DeepClone(pieceMoving);
            King defKing = tempBoard.FindKing(defSide);

            int Dx = pieceMoving.CurrentPosition.X - defKing.CurrentPosition.X;
            int Dy = pieceMoving.CurrentPosition.Y - defKing.CurrentPosition.Y;

            // Perform mirror operations when needed
            if (Dx < 0)
            {
                tempBoard.State = HorizontalMirror(tempBoard.State);
            }
            if (Dy < 0)
            {
                tempBoard.State = VerticalMirror(tempBoard.State);
            }

            // Find king again if board was mirrored
            defKing = tempBoard.FindKing(defSide);

            // Store possible enemy piece that might has mat.
            Piece possibleMatPiece = null;

            if (Dx == 0)
            {
                for (int iy = defKing.CurrentPosition.Y + 1; iy < 8; iy++)
                {
                    if (tempBoard.State[defKing.CurrentPosition.X, iy]?.Side != defSide)
                    {
                        possibleMatPiece = tempBoard.State[defKing.CurrentPosition.X, iy];
                    }
                }
            }
            else if (Dy == 0)
            {
                for (int ix = defKing.CurrentPosition.X + 1; ix < 8; ix++)
                {
                    if (tempBoard.State[ix, defKing.CurrentPosition.Y]?.Side != defSide)
                    {
                        possibleMatPiece = tempBoard.State[ix, defKing.CurrentPosition.Y];
                    }
                }
            }
            else
            {
                int ix = defKing.CurrentPosition.X + 1;
                int iy = defKing.CurrentPosition.Y + 1;

                for (int i = defKing.CurrentPosition.X; i < 8; i++)
                {
                    if (tempBoard.State[ix, iy] == null)
                    {
                        ix++;
                        iy++;
                        continue;
                    }

                    if (tempBoard.State[ix, iy].Side != defKing.Side)
                    {
                        possibleMatPiece = tempBoard.State[ix, iy];
                    }

                    ix++;
                    iy++;
                }
            }

            // Try to kill defendant King.


            return(false);
        }
Ejemplo n.º 23
0
        public override bool move(ref Tile startingTile, ref Tile destinationTile, ChessBoard chess)
        {
            if (destinationTile.PieceInside != null)
            {
                if (destinationTile.PieceInside.IsWhite == IsWhite)
                {
                    return(false);
                }
            }
            King whiteKing = (King)chess.Pieces[21];
            King blackKing = (King)chess.Pieces[20];
            bool isClear   = true;

            if (destinationTile.RowInBoard > Position.RowInBoard && destinationTile.ColumnInBoard > Position.ColumnInBoard && Math.Abs(Position.RowInBoard - destinationTile.RowInBoard) == Math.Abs(Position.ColumnInBoard - destinationTile.ColumnInBoard))
            {
                for (int i = 1; i < Math.Abs(Position.ColumnInBoard - destinationTile.ColumnInBoard); i++)
                {
                    if (chess.Tiles[Position.RowInBoard + i, Position.ColumnInBoard + i].PieceInside != null)
                    {
                        isClear = false;
                        break;
                    }
                }
                if (IsWhite)
                {
                    if (isClear && allowMove(ref chess, ref destinationTile) && whiteKing.canMoveBeforeKing(ref chess, ref destinationTile, this) && IsWhite)
                    {
                        changeTileForPiece(ref startingTile, ref destinationTile, ref chess);
                        return(true);
                    }
                }
                else
                {
                    if (isClear && allowMove(ref chess, ref destinationTile) && blackKing.canMoveBeforeKing(ref chess, ref destinationTile, this) && !IsWhite)
                    {
                        changeTileForPiece(ref startingTile, ref destinationTile, ref chess);
                        return(true);
                    }
                }
                isClear = true;
            }
            if (destinationTile.RowInBoard > Position.RowInBoard && destinationTile.ColumnInBoard < Position.ColumnInBoard && Math.Abs(Position.RowInBoard - destinationTile.RowInBoard) == Math.Abs(Position.ColumnInBoard - destinationTile.ColumnInBoard))
            {
                for (int i = 1; i < Math.Abs(Position.ColumnInBoard - destinationTile.ColumnInBoard); i++)
                {
                    if (chess.Tiles[Position.RowInBoard + i, Position.ColumnInBoard - i].PieceInside != null)
                    {
                        isClear = false;
                        break;
                    }
                }
                if (IsWhite)
                {
                    if (isClear && allowMove(ref chess, ref destinationTile) && whiteKing.canMoveBeforeKing(ref chess, ref destinationTile, this) && IsWhite)
                    {
                        changeTileForPiece(ref startingTile, ref destinationTile, ref chess);
                        return(true);
                    }
                }
                else
                {
                    if (isClear && allowMove(ref chess, ref destinationTile) && blackKing.canMoveBeforeKing(ref chess, ref destinationTile, this) && !IsWhite)
                    {
                        changeTileForPiece(ref startingTile, ref destinationTile, ref chess);
                        return(true);
                    }
                }
                isClear = true;
            }
            if (destinationTile.RowInBoard < Position.RowInBoard && destinationTile.ColumnInBoard < Position.ColumnInBoard && Math.Abs(Position.RowInBoard - destinationTile.RowInBoard) == Math.Abs(Position.ColumnInBoard - destinationTile.ColumnInBoard))
            {
                for (int i = 1; i < Math.Abs(Position.ColumnInBoard - destinationTile.ColumnInBoard); i++)
                {
                    if (chess.Tiles[Position.RowInBoard - i, Position.ColumnInBoard - i].PieceInside != null)
                    {
                        isClear = false;
                        break;
                    }
                }
                if (IsWhite)
                {
                    if (isClear && allowMove(ref chess, ref destinationTile) && whiteKing.canMoveBeforeKing(ref chess, ref destinationTile, this) && IsWhite)
                    {
                        changeTileForPiece(ref startingTile, ref destinationTile, ref chess);
                        return(true);
                    }
                }
                else
                {
                    if (isClear && allowMove(ref chess, ref destinationTile) && blackKing.canMoveBeforeKing(ref chess, ref destinationTile, this) && !IsWhite)
                    {
                        changeTileForPiece(ref startingTile, ref destinationTile, ref chess);
                        return(true);
                    }
                }
                isClear = true;
            }
            if (destinationTile.RowInBoard < Position.RowInBoard && destinationTile.ColumnInBoard > Position.ColumnInBoard && Math.Abs(Position.RowInBoard - destinationTile.RowInBoard) == Math.Abs(Position.ColumnInBoard - destinationTile.ColumnInBoard))
            {
                for (int i = 1; i < Math.Abs(Position.ColumnInBoard - destinationTile.ColumnInBoard); i++)
                {
                    if (chess.Tiles[Position.RowInBoard - i, Position.ColumnInBoard + i].PieceInside != null)
                    {
                        isClear = false;
                        break;
                    }
                }
                if (IsWhite)
                {
                    if (isClear && allowMove(ref chess, ref destinationTile) && whiteKing.canMoveBeforeKing(ref chess, ref destinationTile, this) && IsWhite)
                    {
                        changeTileForPiece(ref startingTile, ref destinationTile, ref chess);
                        return(true);
                    }
                }
                else
                {
                    if (isClear && allowMove(ref chess, ref destinationTile) && blackKing.canMoveBeforeKing(ref chess, ref destinationTile, this) && !IsWhite)
                    {
                        changeTileForPiece(ref startingTile, ref destinationTile, ref chess);
                        return(true);
                    }
                }
                isClear = true;
            }
            return(false);
        }
Ejemplo n.º 24
0
        public bool ChecktestforRookBlack(GameBoard _Game, byte _X, byte _Y)
        {
            int i;

            for (i = 0; i < _Game.White.Count; i++)
            {
                Coordinate _Next     = new Coordinate(_X, _Y);
                string     typecheck = _Game.White[i].GetType().ToString();
                switch (typecheck)
                {
                case "Chess.Pawn":
                    Pawn Pawnmover = new Pawn("a");
                    Pawnmover = (Pawn)_Game.White[i];
                    if (Pawnmover.isMovable(_Game, _Next))
                    {
                        return(true);
                    }
                    break;

                case "Chess.Bishop":
                    Bishop Bishopmover = new Bishop("a");
                    Bishopmover = (Bishop)_Game.White[i];
                    if (Bishopmover.isMovable(_Game, _Next))
                    {
                        return(true);
                    }
                    break;

                case "Chess.Queen":
                    Queen Queenmover = new Queen("a");
                    Queenmover = (Queen)_Game.White[i];
                    if (Queenmover.isMovable(_Game, _Next))
                    {
                        return(true);
                    }
                    break;

                case "Chess.Knight":
                    Knight Knightmover = new Knight("a");
                    Knightmover = (Knight)_Game.White[i];
                    if (Knightmover.isMovable(_Game, _Next))
                    {
                        return(true);
                    }

                    break;

                case "Chess.Castle":
                    Castle Castlemover = new Castle("a");
                    Castlemover = (Castle)_Game.White[i];
                    if (Castlemover.isMovable(_Game, _Next))
                    {
                        return(true);
                    }
                    break;

                case "Chess.King":
                    King Kingmover2 = new King("a");
                    Kingmover2 = (King)_Game.White[i];
                    if (Kingmover2.isMovable(_Game, _Next))
                    {
                        return(true);
                    }
                    break;
                }
            }

            return(false);
        }
Ejemplo n.º 25
0
        public void Initialize()
        {
            //Initialize all squares
            for (int i = 0; i < 8; i++)
            {
                for (int j = 0; j < 8; j++)
                {
                    Square square = new Square();
                    square.File = (File)(i);
                    square.Rank = j;
                    BoardCoordinates.Add(square);
                }
            }

            //Initialize all pieces

            int whiteMajorPieceRank = 1;
            int whitePawnRank       = 2;
            int blackPawnRank       = 7;
            int blackMajorPieceRank = 8;

            File queenRookFile   = File.A;
            File queenKnightFile = File.B;
            File queenBishopFile = File.C;
            File queenFile       = File.D;
            File kingFile        = File.E;
            File kingBishopFile  = File.F;
            File kingKnightFile  = File.G;
            File kingRookFile    = File.H;

            #region Pawns
            for (int i = 0; i < 16; i++)
            {
                Pawn pawn = new Pawn();
                pawn.PieceColor       = i < 8 ? PieceColor.White : PieceColor.Black;
                pawn.Coordinates.Rank = i < 8 ? whitePawnRank : blackPawnRank;
                pawn.Coordinates.File = (File)(i % 8);
                pawn.IsOnBoard        = true;
                Pieces.Add(pawn);
            }
            #endregion

            #region Rooks
            Rook whiteKingRook = new Rook();
            whiteKingRook.Coordinates.Rank = whiteMajorPieceRank;
            whiteKingRook.Coordinates.File = kingRookFile;
            whiteKingRook.IsOnBoard        = true;
            whiteKingRook.PieceColor       = PieceColor.White;
            Pieces.Add(whiteKingRook);

            Rook whiteQueenRook = new Rook();
            whiteQueenRook.Coordinates.Rank = whiteMajorPieceRank;
            whiteQueenRook.Coordinates.File = queenRookFile;
            whiteQueenRook.IsOnBoard        = true;
            whiteQueenRook.PieceColor       = PieceColor.White;
            Pieces.Add(whiteQueenRook);

            Rook blackKingRook = new Rook();
            blackKingRook.Coordinates.Rank = blackMajorPieceRank;
            blackKingRook.Coordinates.File = kingRookFile;
            blackKingRook.IsOnBoard        = true;
            blackKingRook.PieceColor       = PieceColor.Black;
            Pieces.Add(blackKingRook);

            Rook blackQueenRook = new Rook();
            blackQueenRook.Coordinates.File = queenRookFile;
            blackQueenRook.Coordinates.Rank = blackMajorPieceRank;
            blackQueenRook.IsOnBoard        = true;
            blackQueenRook.PieceColor       = PieceColor.Black;
            Pieces.Add(blackQueenRook);

            #endregion

            #region Knights
            Knight whiteQueenKnight = new Knight();
            whiteQueenKnight.Coordinates.File = queenKnightFile;
            whiteQueenKnight.Coordinates.Rank = whiteMajorPieceRank;
            whiteQueenKnight.IsOnBoard        = true;
            whiteQueenKnight.PieceColor       = PieceColor.White;
            Pieces.Add(whiteQueenKnight);

            Knight whiteKingKnight = new Knight();
            whiteKingKnight.Coordinates.File = kingKnightFile;
            whiteKingKnight.Coordinates.Rank = whiteMajorPieceRank;
            whiteKingKnight.IsOnBoard        = true;
            whiteKingKnight.PieceColor       = PieceColor.White;
            Pieces.Add(whiteKingKnight);

            Knight blackQueenKnight = new Knight();
            blackQueenKnight.Coordinates.File = queenKnightFile;
            blackQueenKnight.Coordinates.Rank = blackMajorPieceRank;
            blackQueenKnight.IsOnBoard        = true;
            blackQueenKnight.PieceColor       = PieceColor.Black;
            Pieces.Add(blackQueenKnight);

            Knight blackKingKnight = new Knight();
            blackKingKnight.Coordinates.File = kingKnightFile;
            blackKingKnight.Coordinates.Rank = blackMajorPieceRank;
            blackQueenKnight.IsOnBoard       = true;
            blackQueenKnight.PieceColor      = PieceColor.Black;
            Pieces.Add(blackKingKnight);
            #endregion

            #region Bishop
            Bishop whiteKingBishop = new Bishop();
            whiteKingBishop.Coordinates.Rank = whiteMajorPieceRank;
            whiteKingBishop.Coordinates.File = kingBishopFile;
            whiteKingBishop.IsOnBoard        = true;
            whiteKingBishop.PieceColor       = PieceColor.White;
            Pieces.Add(whiteKingBishop);

            Bishop whiteQueenBishop = new Bishop();
            whiteQueenBishop.Coordinates.Rank = whiteMajorPieceRank;
            whiteQueenBishop.Coordinates.File = queenBishopFile;
            whiteQueenBishop.IsOnBoard        = true;
            whiteQueenBishop.PieceColor       = PieceColor.White;
            Pieces.Add(whiteQueenBishop);

            Bishop blackKingBishop = new Bishop();
            blackKingBishop.Coordinates.Rank = blackMajorPieceRank;
            blackKingBishop.Coordinates.File = kingBishopFile;
            blackKingBishop.IsOnBoard        = true;
            blackKingBishop.PieceColor       = PieceColor.Black;
            Pieces.Add(blackKingBishop);

            Bishop blackQueenBishop = new Bishop();
            blackQueenBishop.Coordinates.Rank = blackMajorPieceRank;
            blackQueenBishop.Coordinates.File = queenBishopFile;
            blackQueenBishop.IsOnBoard        = true;
            blackQueenBishop.PieceColor       = PieceColor.Black;
            Pieces.Add(blackQueenBishop);

            #endregion

            #region Queens
            Queen whiteQueen = new Queen();
            whiteQueen.Coordinates.Rank = whiteMajorPieceRank;
            whiteQueen.Coordinates.File = queenFile;
            whiteQueen.IsOnBoard        = true;
            whiteQueen.PieceColor       = PieceColor.White;
            Pieces.Add(whiteQueen);

            Queen blackQueen = new Queen();
            blackQueen.Coordinates.Rank = blackMajorPieceRank;
            blackQueen.Coordinates.File = queenFile;
            blackQueen.IsOnBoard        = true;
            blackQueen.PieceColor       = PieceColor.Black;
            Pieces.Add(blackQueen);

            #endregion

            #region Kings
            King whiteKing = new King();
            whiteKing.Coordinates.Rank = whiteMajorPieceRank;
            whiteKing.Coordinates.File = kingFile;
            whiteKing.IsOnBoard        = true;
            whiteKing.PieceColor       = PieceColor.White;
            Pieces.Add(whiteKing);

            King blackKing = new King();
            blackKing.Coordinates.File = kingFile;
            blackKing.Coordinates.Rank = blackMajorPieceRank;
            blackKing.IsOnBoard        = true;
            blackKing.PieceColor       = PieceColor.Black;
            Pieces.Add(blackKing);
            #endregion
        }
Ejemplo n.º 26
0
        public void SetupBoard(string setup)
        {
            switch (setup)
            {
            case "new_game_setup":

                // WEISSE SPIELFIGUREN
                PawnWhite pawn_w_1   = factory.GetPawnWhite("A2");
                PawnWhite pawn_w_2   = factory.GetPawnWhite("B2");
                PawnWhite pawn_w_3   = factory.GetPawnWhite("C2");
                PawnWhite pawn_w_4   = factory.GetPawnWhite("D2");
                PawnWhite pawn_w_5   = factory.GetPawnWhite("E2");
                PawnWhite pawn_w_6   = factory.GetPawnWhite("F2");
                PawnWhite pawn_w_7   = factory.GetPawnWhite("G2");
                PawnWhite pawn_w_8   = factory.GetPawnWhite("H2");
                Rook      rook_w_1   = factory.GetRook("white", "A1");
                Knight    knight_w_1 = factory.GetKnight("white", "B1");
                Bishop    bishop_w_1 = factory.GetBishop("white", "C1");
                Queen     queen_w    = factory.GetQueen("white", "D1");
                King      king_w     = factory.GetKing("white", "E1");
                Bishop    bishop_w_2 = factory.GetBishop("white", "F1");
                Knight    knight_w_2 = factory.GetKnight("white", "G1");
                Rook      rook_w_2   = factory.GetRook("white", "H1");

                // SCHWARZE SPIELFIGUREN
                PawnBlack pawn_b_1   = factory.GetPawnBlack("A7");
                PawnBlack pawn_b_2   = factory.GetPawnBlack("B7");
                PawnBlack pawn_b_3   = factory.GetPawnBlack("C7");
                PawnBlack pawn_b_4   = factory.GetPawnBlack("D7");
                PawnBlack pawn_b_5   = factory.GetPawnBlack("E7");
                PawnBlack pawn_b_6   = factory.GetPawnBlack("F7");
                PawnBlack pawn_b_7   = factory.GetPawnBlack("G7");
                PawnBlack pawn_b_8   = factory.GetPawnBlack("H7");
                Rook      rook_b_1   = factory.GetRook("black", "A8");
                Knight    knight_b_1 = factory.GetKnight("black", "B8");
                Bishop    bishop_b_1 = factory.GetBishop("black", "C8");
                Queen     queen_b    = factory.GetQueen("black", "D8");
                King      king_b     = factory.GetKing("black", "E8");
                Bishop    bishop_b_2 = factory.GetBishop("black", "F8");
                Knight    knight_b_2 = factory.GetKnight("black", "G8");
                Rook      rook_b_2   = factory.GetRook("black", "H8");

                // WEISSE SPIELFIGUREN
                board.chessman.Add(pawn_w_1);
                board.chessman.Add(pawn_w_2);
                board.chessman.Add(pawn_w_3);
                board.chessman.Add(pawn_w_4);
                board.chessman.Add(pawn_w_5);
                board.chessman.Add(pawn_w_6);
                board.chessman.Add(pawn_w_7);
                board.chessman.Add(pawn_w_8);
                board.chessman.Add(rook_w_1);
                board.chessman.Add(rook_w_2);
                board.chessman.Add(bishop_w_1);
                board.chessman.Add(bishop_w_2);
                board.chessman.Add(knight_w_1);
                board.chessman.Add(knight_w_2);
                board.chessman.Add(queen_w);
                board.chessman.Add(king_w);

                // SCHWARZE SPIELFIGUREN
                board.chessman.Add(pawn_b_1);
                board.chessman.Add(pawn_b_2);
                board.chessman.Add(pawn_b_3);
                board.chessman.Add(pawn_b_4);
                board.chessman.Add(pawn_b_5);
                board.chessman.Add(pawn_b_6);
                board.chessman.Add(pawn_b_7);
                board.chessman.Add(pawn_b_8);
                board.chessman.Add(rook_b_1);
                board.chessman.Add(rook_b_2);
                board.chessman.Add(bishop_b_1);
                board.chessman.Add(bishop_b_2);
                board.chessman.Add(knight_b_1);
                board.chessman.Add(knight_b_2);
                board.chessman.Add(queen_b);
                board.chessman.Add(king_b);

                break;

            case "promotion_setup":
                // WEISSE SPIELFIGUREN
                PawnWhite promotion_pawn_w_1   = factory.GetPawnWhite("A7");
                PawnWhite promotion_pawn_w_2   = factory.GetPawnWhite("B7");
                King      promotion_king_w     = factory.GetKing("white", "E1");
                Bishop    promotion_bishop_w_2 = factory.GetBishop("white", "F1");
                Knight    promotion_knight_w_2 = factory.GetKnight("white", "G1");
                Rook      promotion_rook_w_2   = factory.GetRook("white", "H1");

                // SCHWARZE SPIELFIGUREN
                PawnBlack promotion_pawn_b_1   = factory.GetPawnBlack("A2");
                PawnBlack promotion_pawn_b_2   = factory.GetPawnBlack("B2");
                King      promotion_king_b     = factory.GetKing("black", "E8");
                Bishop    promotion_bishop_b_2 = factory.GetBishop("black", "F8");
                Knight    promotion_knight_b_2 = factory.GetKnight("black", "G8");
                Rook      promotion_rook_b_2   = factory.GetRook("black", "H8");

                // WEISSE SPIELFIGUREN
                board.chessman.Add(promotion_pawn_w_1);
                board.chessman.Add(promotion_pawn_w_2);
                board.chessman.Add(promotion_king_w);
                board.chessman.Add(promotion_knight_w_2);
                board.chessman.Add(promotion_bishop_w_2);
                board.chessman.Add(promotion_rook_w_2);

                // SCHWARZE SPIELFIGUREN
                board.chessman.Add(promotion_pawn_b_1);
                board.chessman.Add(promotion_pawn_b_2);
                board.chessman.Add(promotion_king_b);
                board.chessman.Add(promotion_knight_b_2);
                board.chessman.Add(promotion_bishop_b_2);
                board.chessman.Add(promotion_rook_b_2);

                break;

            case "en_passant_setup":

                // WEISSE SPIELFIGUREN
                PawnWhite en_passant_pawn_w_1   = factory.GetPawnWhite("A2");
                PawnWhite en_passant_pawn_w_2   = factory.GetPawnWhite("B2");
                PawnWhite en_passant_pawn_w_3   = factory.GetPawnWhite("C4");
                PawnWhite en_passant_pawn_w_4   = factory.GetPawnWhite("D2");
                PawnWhite en_passant_pawn_w_5   = factory.GetPawnWhite("E2");
                PawnWhite en_passant_pawn_w_6   = factory.GetPawnWhite("F2");
                PawnWhite en_passant_pawn_w_7   = factory.GetPawnWhite("G2");
                PawnWhite en_passant_pawn_w_8   = factory.GetPawnWhite("H2");
                Rook      en_passant_rook_w_1   = factory.GetRook("white", "A1");
                Knight    en_passant_knight_w_1 = factory.GetKnight("white", "B1");
                Bishop    en_passant_bishop_w_1 = factory.GetBishop("white", "C1");
                Queen     en_passant_queen_w    = factory.GetQueen("white", "D1");
                King      en_passant_king_w     = factory.GetKing("white", "E1");
                Bishop    en_passant_bishop_w_2 = factory.GetBishop("white", "F1");
                Knight    en_passant_knight_w_2 = factory.GetKnight("white", "G1");
                Rook      en_passant_rook_w_2   = factory.GetRook("white", "H1");

                // SCHWARZE SPIELFIGUREN
                PawnBlack en_passant_pawn_b_1   = factory.GetPawnBlack("A7");
                PawnBlack en_passant_pawn_b_2   = factory.GetPawnBlack("B7");
                PawnBlack en_passant_pawn_b_3   = factory.GetPawnBlack("C7");
                PawnBlack en_passant_pawn_b_4   = factory.GetPawnBlack("D7");
                PawnBlack en_passant_pawn_b_5   = factory.GetPawnBlack("E7");
                PawnBlack en_passant_pawn_b_6   = factory.GetPawnBlack("F5");
                PawnBlack en_passant_pawn_b_7   = factory.GetPawnBlack("G7");
                PawnBlack en_passant_pawn_b_8   = factory.GetPawnBlack("H7");
                Rook      en_passant_rook_b_1   = factory.GetRook("black", "A8");
                Knight    en_passant_knight_b_1 = factory.GetKnight("black", "B8");
                Bishop    en_passant_bishop_b_1 = factory.GetBishop("black", "C8");
                Queen     en_passant_queen_b    = factory.GetQueen("black", "D8");
                King      en_passant_king_b     = factory.GetKing("black", "E8");
                Bishop    en_passant_bishop_b_2 = factory.GetBishop("black", "F8");
                Knight    en_passant_knight_b_2 = factory.GetKnight("black", "G8");
                Rook      en_passant_rook_b_2   = factory.GetRook("black", "H8");

                // WEISSE SPIELFIGUREN
                board.chessman.Add(en_passant_pawn_w_1);
                board.chessman.Add(en_passant_pawn_w_2);
                board.chessman.Add(en_passant_pawn_w_3);
                board.chessman.Add(en_passant_pawn_w_4);
                board.chessman.Add(en_passant_pawn_w_5);
                board.chessman.Add(en_passant_pawn_w_6);
                board.chessman.Add(en_passant_pawn_w_7);
                board.chessman.Add(en_passant_pawn_w_8);
                board.chessman.Add(en_passant_rook_w_1);
                board.chessman.Add(en_passant_rook_w_2);
                board.chessman.Add(en_passant_bishop_w_1);
                board.chessman.Add(en_passant_bishop_w_2);
                board.chessman.Add(en_passant_knight_w_1);
                board.chessman.Add(en_passant_knight_w_2);
                board.chessman.Add(en_passant_queen_w);
                board.chessman.Add(en_passant_king_w);

                // SCHWARZE SPIELFIGUREN
                board.chessman.Add(en_passant_pawn_b_1);
                board.chessman.Add(en_passant_pawn_b_2);
                board.chessman.Add(en_passant_pawn_b_3);
                board.chessman.Add(en_passant_pawn_b_4);
                board.chessman.Add(en_passant_pawn_b_5);
                board.chessman.Add(en_passant_pawn_b_6);
                board.chessman.Add(en_passant_pawn_b_7);
                board.chessman.Add(en_passant_pawn_b_8);
                board.chessman.Add(en_passant_rook_b_1);
                board.chessman.Add(en_passant_rook_b_2);
                board.chessman.Add(en_passant_bishop_b_1);
                board.chessman.Add(en_passant_bishop_b_2);
                board.chessman.Add(en_passant_knight_b_1);
                board.chessman.Add(en_passant_knight_b_2);
                board.chessman.Add(en_passant_queen_b);
                board.chessman.Add(en_passant_king_b);

                break;

            case "castling_setup":

                // WEISSE SPIELFIGUREN

                Rook castling_rook_w_1 = factory.GetRook("white", "A1");
                King castling_king_w   = factory.GetKing("white", "E1");
                Rook castling_rook_w_2 = factory.GetRook("white", "H1");

                // SCHWARZE SPIELFIGUREN
                Rook castling_rook_b_1 = factory.GetRook("black", "A8");
                King castling_king_b   = factory.GetKing("black", "E8");
                Rook castling_rook_b_2 = factory.GetRook("black", "H8");

                // WEISSE SPIELFIGUREN
                board.chessman.Add(castling_rook_w_1);
                board.chessman.Add(castling_rook_w_2);
                board.chessman.Add(castling_king_w);

                // SCHWARZE SPIELFIGUREN
                board.chessman.Add(castling_rook_b_1);
                board.chessman.Add(castling_rook_b_2);
                board.chessman.Add(castling_king_b);

                break;
            }
        }
Ejemplo n.º 27
0
        private void CheckChessMove(object sender)
        {
            var startX  = (initialChessmanLocation.X - 27) / 50;
            var startY  = (initialChessmanLocation.Y - 27) / 50;
            var finishX = (CellsPositions[chessmansCellIndexRow, chessmansCellIndexColumn].X - 27) / 50;
            var finishY = (CellsPositions[chessmansCellIndexRow, chessmansCellIndexColumn].Y - 27) / 50;

            var impossibleMove = sender is Queen
                ? Queen.CheckQueenMove(startX, startY, finishX, finishY, ChessmanPresenceSign, Controls, sender, moveOrder)
                : sender is Castle
                    ? Castle.CheckCastleMove(startX, startY, finishX, finishY, ChessmanPresenceSign, Controls, sender, moveOrder)
                    : sender is Elephant
                        ? Elephant.CheckElephantMove(startX, startY, finishX, finishY, ChessmanPresenceSign, Controls, sender, moveOrder)
                        : sender is Horse
                            ? Horse.CheckHorseMove(startX, startY, finishX, finishY, ChessmanPresenceSign, Controls, sender, moveOrder)
                            : sender is King
                                ? King.CheckKingMove(startX, startY, finishX, finishY, ChessmanPresenceSign, Controls, sender, sender)
                                : Pawn.CheckPawnMove(startX, startY, finishX, finishY, ChessmanPresenceSign, Controls, sender, moveOrder);

            if (impossibleMove)
            {
                MessageBox.Show(Resources.ImpossibleMoveMessage);

                chess          = (Chessman)sender;
                chess.Location = initialChessmanLocation;
            }
            else
            {
                //Проверка возможности превращения
                if (sender is Pawn)
                {
                    if (((Pawn)sender).CanTransform)
                    {
                        var dialogWindow = new PawnTransfomDialog();
                        dialogWindow.Owner = this;

                        DialogResult dialogResult = dialogWindow.ShowDialog();
                        if (dialogResult == DialogResult.OK && !dialogWindow.RBPawn.Checked)
                        {
                            var color       = ((Pawn)sender).ChessColor;
                            var finishPoint = new Point(finishX * 50 + 27, finishY * 50 + 27);

                            ((Pawn)sender).Dispose();

                            if (dialogWindow.RBCastle.Checked)
                            {
                                var index = Castles.Count + 1;
                                Castles.Add(color + "Castel" + index, new Castle(color));
                                Controls.Add(Castles[color + "Castel" + index]);
                                Castles[color + "Castel" + index].Location = finishPoint;

                                Castles[color + "Castel" + index].MouseLeave += Chessman_MouseLeave;
                                Castles[color + "Castel" + index].MouseDown  += Chessman_MouseDown;
                                Castles[color + "Castel" + index].MouseUp    += Chessman_MouseUp;
                                Castles[color + "Castel" + index].MouseMove  += Chessman_MouseMove;

                                sender = Castles[color + "Castel" + index];
                                Chessman.CheckEnemyKingBeAttaced(startX, startY, finishX, finishY, ChessmanPresenceSign, Controls,
                                                                 sender, moveOrder);
                            }

                            if (dialogWindow.RBElephant.Checked)
                            {
                                var index = Elephants.Count + 1;
                                Elephants.Add(color + "Elephant" + index, new Elephant(color));
                                Controls.Add(Elephants[color + "Elephant" + index]);
                                Elephants[color + "Elephant" + index].Location = finishPoint;

                                Elephants[color + "Elephant" + index].MouseLeave += Chessman_MouseLeave;
                                Elephants[color + "Elephant" + index].MouseDown  += Chessman_MouseDown;
                                Elephants[color + "Elephant" + index].MouseUp    += Chessman_MouseUp;
                                Elephants[color + "Elephant" + index].MouseMove  += Chessman_MouseMove;

                                sender = Elephants[color + "Elephant" + index];
                                Chessman.CheckEnemyKingBeAttaced(startX, startY, finishX, finishY, ChessmanPresenceSign, Controls,
                                                                 sender, moveOrder);
                            }

                            if (dialogWindow.RBHorse.Checked)
                            {
                                var index = Horses.Count + 1;
                                Horses.Add(color + "Horse" + index, new Horse(color));
                                Controls.Add(Horses[color + "Horse" + index]);
                                Horses[color + "Horse" + index].Location = finishPoint;

                                Horses[color + "Horse" + index].MouseLeave += Chessman_MouseLeave;
                                Horses[color + "Horse" + index].MouseDown  += Chessman_MouseDown;
                                Horses[color + "Horse" + index].MouseUp    += Chessman_MouseUp;
                                Horses[color + "Horse" + index].MouseMove  += Chessman_MouseMove;

                                sender = Horses[color + "Horse" + index];
                                Chessman.CheckEnemyKingBeAttaced(startX, startY, finishX, finishY, ChessmanPresenceSign, Controls,
                                                                 sender, moveOrder);
                            }

                            if (dialogWindow.RBQueen.Checked)
                            {
                                var index = Queens.Count + 1;
                                Queens.Add(color + "Queen" + index, new Queen(color));
                                Controls.Add(Queens[color + "Queen" + index]);
                                Queens[color + "Queen" + index].Location = finishPoint;

                                Queens[color + "Queen" + index].MouseLeave += Chessman_MouseLeave;
                                Queens[color + "Queen" + index].MouseDown  += Chessman_MouseDown;
                                Queens[color + "Queen" + index].MouseUp    += Chessman_MouseUp;
                                Queens[color + "Queen" + index].MouseMove  += Chessman_MouseMove;

                                sender = Queens[color + "Queen" + index];
                                Chessman.CheckEnemyKingBeAttaced(startX, startY, finishX, finishY, ChessmanPresenceSign, Controls,
                                                                 sender, moveOrder);
                            }
                        }
                    }
                }

                //Проверка мата или шаха
                ChessmanPresenceSign[startY, startX]   = false;
                ChessmanPresenceSign[finishY, finishX] = true;

                moveOrder[((Chessman)sender).ChessColor] += -2;

                if (((Chessman)sender).ShahSigne && !((Chessman)sender).MateSigne)
                {
                    MessageBox.Show(Resources.ShahMessage);
                    ((Chessman)sender).ShahSigne = false;
                }

                if (((Chessman)sender).MateSigne)
                {
                    DialogResult result = MessageBox.Show(Resources.MateMessage + Environment.NewLine + Resources.restartQuestion,
                                                          Resources.GameEndMessage, MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                    if (result == DialogResult.Yes)
                    {
                        Application.Restart();
                    }
                    else
                    {
                        Application.Exit();
                    }
                }
            }
        }
Ejemplo n.º 28
0
        public static void Main(string[] args)
        {
            Board board = new Board(args[0]);

            PieceColorEnum?winner      = null;
            bool           isCheckMate = false;
            bool           isDraw      = false;
            bool           validMove   = false;

            while (!isCheckMate && !isDraw)
            {
                Console.WriteLine($"\n{board.ColorToMove}'s move");
                board.PrintBoardWithTakenPieces();
                validMove = false;

                Console.Write(" > ");
                var command = Console.ReadLine().Trim();
                if (command == "stats")
                {
                    Console.WriteLine($"black score: {board.BlackScore}, white score: {board.WhiteScore}");
                }
                if (command == "flip")
                {
                    board = board.FlipBoard();
                }
                else if (command.Contains("0-0"))
                {
                    // castle
                    CastleTypeEnum castleType = CastleTypeEnum.Queenside;
                    var            king       = board.ColorToMove == PieceColorEnum.Black ? board.BlackKing : board.WhiteKing;
                    if (command == "0-0")
                    {
                        // castle kingside
                        castleType = CastleTypeEnum.Kingside;
                    }
                    else if (command == "0-0-0")
                    {
                        // castle queenside
                        castleType = CastleTypeEnum.Queenside;
                    }

                    var canCastleResult = king.CanCastle(castleType, board);
                    if (canCastleResult.CanCastle)
                    {
                        var castleResult = king.Castle(castleType, board);
                        if (castleResult.Castled)
                        {
                            validMove = true;
                        }
                        else
                        {
                            Console.WriteLine($"can't castle {castleType}, {castleResult.Error}");
                        }
                    }
                    else
                    {
                        Console.WriteLine($"can't castle {castleType}, {canCastleResult.Error}");
                    }
                }
                else
                {
                    try
                    {
                        var commands = command.Split(' ');
                        var startPos = board.ConvertNotationForMove(commands[0]);
                        var endPos   = board.ConvertNotationForMove(commands[1]);

                        var piece = board.Instance[startPos.Key, startPos.Value];
                        if (piece != null && piece.Color == board.ColorToMove)
                        {
                            var moves = piece.AvailableMoves(board);
                            if (moves.Contains(new KeyValuePair <int, int>(endPos.Key, endPos.Value)))
                            {
                                // valid move
                                board.MovePiece(piece, endPos.Key, endPos.Value);
                                validMove = true;
                            }
                            else
                            {
                                Console.WriteLine($"piece at {commands[0]} can't move to {commands[1]}");
                            }
                        }
                        else if (piece == null)
                        {
                            Console.WriteLine($"piece at {commands[0]} not found");
                        }
                        else
                        {
                            Console.WriteLine($"piece at {commands[0]} isn't yours to move");
                        }
                    }
                    catch
                    {
                        Console.WriteLine($"invalid command");
                    }
                }

                if (validMove)
                {
                    // see if there's a resulting check or mate
                    King king          = board.ColorToMove == PieceColorEnum.Black ? board.WhiteKing : board.BlackKing;
                    var  isCheckOrMate = board.IsCheckOrMate(king);
                    if (isCheckOrMate.IsMate && isCheckOrMate.IsCheck)
                    {
                        // check & mate
                        isCheckMate = true;
                        winner      = board.ColorToMove;
                    }
                    else if (isCheckOrMate.IsCheck)
                    {
                        // check
                        Console.WriteLine("check");
                    }

                    // check if it's stalemate
                    if (board.IsStaleMate(board.ColorToMove == PieceColorEnum.Black ? PieceColorEnum.White : PieceColorEnum.Black))
                    {
                        isDraw = true;
                    }

                    board.ColorToMove = board.ColorToMove == PieceColorEnum.Black ? PieceColorEnum.White : PieceColorEnum.Black;
                }
            }

            if (winner != null)
            {
                Console.WriteLine($"check mate - game over. {winner} wins!");
            }
            else
            {
                Console.WriteLine($"draw - game over.");
            }

            Console.WriteLine($"black score: {board.BlackScore}, white score: {board.WhiteScore}");
            Console.ReadLine();
        }
Ejemplo n.º 29
0
        public int canDefend(int color)
        {
            Board  b = new Board();
            Castle castle = new Castle(color, 0, 0);
            Pawn   pawn = new Pawn(color, 0, 0);
            Knight knight = new Knight(color, 0, 0);
            Bishop bishop = new Bishop(color, 0, 0);
            Queen  queen = new Queen(color, 0, 0);
            int    i, j, m, n, f = 0;

            for (i = 0; i < 8; i++)
            {
                for (j = 0; j < 8; j++)
                {
                    b.setSquare(this.getInfo(i, j), i, j);
                }
            }
            Point p    = b.searchKing(color);
            King  king = new King(color, p.x, p.y);

            for (i = 0; i < 8; i++)
            {
                for (j = 0; j < 8; j++)
                {
                    if (b.getInfo(i, j) == 0)
                    {
                    }
                    else if (b.getInfo(i, j) == (color == 1 ? 1 : 7))
                    {
                        castle.x = i;
                        castle.y = j;
                        for (m = 0; m < 8; m++)
                        {
                            for (n = 0; n < 8; n++)
                            {
                                if (castle.move(b, m, n) == 1)
                                {
                                    f = b.getInfo(m, n);
                                    b.setSquare(0, i, j);
                                    b.setSquare((color == 1 ? 1 : 7), m, n);
                                    if (king.isChecked(b) == 0)
                                    {
                                        return(1);
                                    }
                                    b.setSquare((color == 1 ? 1 : 7), i, j);
                                    b.setSquare(f, m, n);
                                }
                            }
                        }
                    }
                    else if (b.getInfo(i, j) == (color == 1 ? 2 : 8))
                    {
                        knight.x = i;
                        knight.y = j;
                        for (m = 0; m < 8; m++)
                        {
                            for (n = 0; n < 8; n++)
                            {
                                if (knight.move(b, m, n) == 1)
                                {
                                    f = b.getInfo(m, n);
                                    b.setSquare(0, i, j);
                                    b.setSquare((color == 1 ? 2 : 8), m, n);
                                    if (king.isChecked(b) == 0)
                                    {
                                        return(1);
                                    }
                                    b.setSquare((color == 1 ? 2 : 8), i, j);
                                    b.setSquare(f, m, n);
                                }
                            }
                        }
                    }
                    else if (b.getInfo(i, j) == (color == 1 ? 3 : 9))
                    {
                        bishop.x = i;
                        bishop.y = j;
                        for (m = 0; m < 8; m++)
                        {
                            for (n = 0; n < 8; n++)
                            {
                                if (bishop.move(b, m, n) == 1)
                                {
                                    f = b.getInfo(m, n);
                                    b.setSquare(0, i, j);
                                    b.setSquare((color == 1 ? 3 : 9), m, n);
                                    if (king.isChecked(b) == 0)
                                    {
                                        return(1);
                                    }
                                    b.setSquare((color == 1 ? 3 : 9), i, j);
                                    b.setSquare(f, m, n);
                                }
                            }
                        }
                    }
                    else if (b.getInfo(i, j) == (color == 1 ? 4 : 10))
                    {
                        queen.x = i;
                        queen.y = j;
                        for (m = 0; m < 8; m++)
                        {
                            for (n = 0; n < 8; n++)
                            {
                                if (queen.move(b, m, n) == 1)
                                {
                                    f = b.getInfo(m, n);
                                    b.setSquare(0, i, j);
                                    b.setSquare((color == 1 ? 4 : 10), m, n);
                                    if (king.isChecked(b) == 0)
                                    {
                                        return(1);
                                    }
                                    b.setSquare((color == 1 ? 4 : 10), i, j);
                                    b.setSquare(f, m, n);
                                }
                            }
                        }
                    }
                    else if (b.getInfo(i, j) == (color == 1 ? 5 : 11))
                    {
                        for (m = 0; m < 8; m++)
                        {
                            for (n = 0; n < 8; n++)
                            {
                                if (king.move(b, m, n) == 1)
                                {
                                    f      = b.getInfo(m, n);
                                    king.x = m;
                                    king.y = n;
                                    b.setSquare(0, i, j);
                                    b.setSquare((color == 1 ? 5 : 11), m, n);
                                    if (king.isChecked(b) == 0)
                                    {
                                        return(1);
                                    }
                                    b.setSquare((color == 1 ? 5 : 11), i, j);
                                    b.setSquare(f, m, n);
                                    king.x = p.x;
                                    king.y = p.y;
                                }
                            }
                        }
                    }
                    else if (b.getInfo(i, j) == (color == 1 ? 6 : 12))
                    {
                        pawn.x = i;
                        pawn.y = j;
                        for (m = 0; m < 8; m++)
                        {
                            for (n = 0; n < 8; n++)
                            {
                                if (pawn.move(b, m, n) == 1)
                                {
                                    f = b.getInfo(m, n);
                                    b.setSquare(0, i, j);
                                    b.setSquare((color == 1 ? 6 : 12), m, n);
                                    if (king.isChecked(b) == 0)
                                    {
                                        return(1);
                                    }
                                    b.setSquare((color == 1 ? 6 : 12), i, j);
                                    b.setSquare(f, m, n);
                                }
                            }
                        }
                    }
                }
            }
            return(0);
        }
        public void KingShouldBeIncorrectMove()
        {
            ChessFigure figure = new King("E1");

            Assert.AreEqual(false, figure.Move("E8"));
        }
Ejemplo n.º 31
0
        static void StartPlacement(Cell[,] Field)
        {
            King WhiteKing = new King(1, '♚', 7, 4);

            WhiteKing.SetToCell(Field, 7, 4);
            WhiteKing.SetFigureColor(Field, 7, 4);
            King BlackKing = new King(0, '♚', 0, 4);

            BlackKing.SetToCell(Field, 0, 4);
            BlackKing.SetFigureColor(Field, 0, 4);
            Quinn WhiteQuinn = new Quinn(1, '♛', 7, 3);

            WhiteQuinn.SetToCell(Field, 7, 3);
            WhiteQuinn.SetFigureColor(Field, 7, 3);
            Quinn BlackQuinn = new Quinn(0, '♛', 0, 3);

            BlackQuinn.SetToCell(Field, 0, 3);
            BlackQuinn.SetFigureColor(Field, 0, 3);
            Bishop WhiteBishop1 = new Bishop(1, '♝', 7, 2);

            WhiteBishop1.SetToCell(Field, 7, 2);
            WhiteBishop1.SetFigureColor(Field, 7, 2);
            Bishop WhiteBishop2 = new Bishop(1, '♝', 7, 5);

            WhiteBishop2.SetToCell(Field, 7, 5);
            WhiteBishop2.SetFigureColor(Field, 7, 5);
            Bishop BlackBishop1 = new Bishop(0, '♝', 0, 2);

            BlackBishop1.SetToCell(Field, 0, 2);
            BlackBishop1.SetFigureColor(Field, 0, 2);
            Bishop BlackBishop2 = new Bishop(0, '♝', 0, 5);

            BlackBishop2.SetToCell(Field, 0, 5);
            BlackBishop2.SetFigureColor(Field, 0, 5);
            Knight WhiteKnight1 = new Knight(1, '♞', 7, 1);

            WhiteKnight1.SetToCell(Field, 7, 1);
            WhiteKnight1.SetFigureColor(Field, 7, 1);
            Knight WhiteKnight2 = new Knight(1, '♞', 7, 6);

            WhiteKnight2.SetToCell(Field, 7, 6);
            WhiteKnight2.SetFigureColor(Field, 7, 6);
            Knight BlackKnight1 = new Knight(0, '♞', 0, 1);

            BlackKnight1.SetToCell(Field, 0, 1);
            BlackKnight1.SetFigureColor(Field, 0, 1);
            Knight BlackKnight2 = new Knight(0, '♞', 0, 6);

            BlackKnight2.SetToCell(Field, 0, 6);
            BlackKnight2.SetFigureColor(Field, 0, 6);
            Rook WhiteRook1 = new Rook(1, '♜', 7, 0);

            WhiteRook1.SetToCell(Field, 7, 0);
            WhiteRook1.SetFigureColor(Field, 7, 0);
            Rook WhiteRook2 = new Rook(1, '♜', 7, 7);

            WhiteRook2.SetToCell(Field, 7, 7);
            WhiteRook2.SetFigureColor(Field, 7, 7);
            Rook BlackRook1 = new Rook(0, '♜', 0, 0);

            BlackRook1.SetToCell(Field, 0, 0);
            BlackRook1.SetFigureColor(Field, 0, 0);
            Rook BlackRook2 = new Rook(0, '♜', 0, 7);

            BlackRook2.SetToCell(Field, 0, 7);
            BlackRook2.SetFigureColor(Field, 0, 7);
            Pawn WhitePawn1 = new Pawn(1, '♟', 6, 0);

            WhitePawn1.SetToCell(Field, 6, 0);
            WhitePawn1.SetFigureColor(Field, 6, 0);
            Pawn WhitePawn2 = new Pawn(1, '♟', 6, 1);

            WhitePawn2.SetToCell(Field, 6, 1);
            WhitePawn2.SetFigureColor(Field, 6, 1);
            Pawn WhitePawn3 = new Pawn(1, '♟', 6, 2);

            WhitePawn3.SetToCell(Field, 6, 2);
            WhitePawn3.SetFigureColor(Field, 6, 2);
            Pawn WhitePawn4 = new Pawn(1, '♟', 6, 3);

            WhitePawn4.SetToCell(Field, 6, 3);
            WhitePawn4.SetFigureColor(Field, 6, 3);
            Pawn WhitePawn5 = new Pawn(1, '♟', 6, 4);

            WhitePawn5.SetToCell(Field, 6, 4);
            WhitePawn5.SetFigureColor(Field, 6, 4);
            Pawn WhitePawn6 = new Pawn(1, '♟', 6, 5);

            WhitePawn6.SetToCell(Field, 6, 5);
            WhitePawn6.SetFigureColor(Field, 6, 5);
            Pawn WhitePawn7 = new Pawn(1, '♟', 6, 6);

            WhitePawn7.SetToCell(Field, 6, 6);
            WhitePawn7.SetFigureColor(Field, 6, 6);
            Pawn WhitePawn8 = new Pawn(1, '♟', 6, 7);

            WhitePawn8.SetToCell(Field, 6, 7);
            WhitePawn8.SetFigureColor(Field, 6, 7);
            Pawn BlackPawn1 = new Pawn(0, '♟', 1, 0);

            BlackPawn1.SetToCell(Field, 1, 0);
            BlackPawn1.SetFigureColor(Field, 1, 0);
            Pawn BlackPawn2 = new Pawn(0, '♟', 1, 1);

            BlackPawn2.SetToCell(Field, 1, 1);
            Pawn BlackPawn3 = new Pawn(0, '♟', 1, 2);

            BlackPawn3.SetToCell(Field, 1, 2);
            Pawn BlackPawn4 = new Pawn(0, '♟', 1, 3);

            BlackPawn4.SetToCell(Field, 1, 3);
            Pawn BlackPawn5 = new Pawn(0, '♟', 1, 4);

            BlackPawn5.SetToCell(Field, 1, 4);
            Pawn BlackPawn6 = new Pawn(0, '♟', 1, 5);

            BlackPawn6.SetToCell(Field, 1, 5);
            Pawn BlackPawn7 = new Pawn(0, '♟', 1, 6);

            BlackPawn7.SetToCell(Field, 1, 6);
            Pawn BlackPawn8 = new Pawn(0, '♟', 1, 7);

            BlackPawn8.SetToCell(Field, 1, 7);
        }
Ejemplo n.º 32
0
        public int isChecked(Board brd2)
        {
            int c2, m;

            if (color == 1)
            {
                c2 = 2;
            }
            else
            {
                c2 = 1;
            }
            Board b = brd2;
            int   i, j;

            for (i = 0; i < 8; i++)
            {
                for (j = 0; j < 8; j++)
                {
                    b.setSquare(brd2.getInfo(i, j), i, j);
                }
            }
            Pawn   pawn   = new Pawn(c2, 0, 0);
            Castle castle = new Castle(c2, 0, 0);
            Knight knight = new Knight(c2, 0, 0);
            Queen  queen  = new Queen(c2, 0, 0);
            Bishop bishop = new Bishop(c2, 0, 0);
            Point  p      = brd2.searchKing(color);
            King   king   = new King(c2, 0, 0);
            int    r      = p.x;
            int    s      = p.y;

            for (i = 0; i < 8; i++)
            {
                for (j = 0; j < 8; j++)
                {
                    m = b.getInfo(i, j);
                    switch (m)
                    {
                    case 0:
                        break;

                    case 1:
                        if (c2 == 1)
                        {
                            castle.x = i;
                            castle.y = j;
                            if (castle.move(b, r, s) == 1)
                            {
                                return(1);
                            }
                        }
                        break;

                    case 2:
                        if (c2 == 1)
                        {
                            knight.x = i;
                            knight.y = j;
                            if (knight.move(b, r, s) == 1)
                            {
                                return(1);
                            }
                        }
                        break;

                    case 3:
                        if (c2 == 1)
                        {
                            bishop.x = i;
                            bishop.y = j;
                            if (bishop.move(b, r, s) == 1)
                            {
                                return(1);
                            }
                        }
                        break;

                    case 4:
                        if (c2 == 1)
                        {
                            queen.x = i;
                            queen.y = j;
                            if (queen.move(b, r, s) == 1)
                            {
                                return(1);
                            }
                        }
                        break;

                    case 5:
                        if (c2 == 2)
                        {
                            king.x = i;
                            king.y = j;
                            if (queen.move(b, r, s) == 1)
                            {
                                return(1);
                            }
                        }
                        break;

                    case 6:
                        if (c2 == 1)
                        {
                            pawn.x = i;
                            pawn.y = j;
                            if (pawn.move(b, r, s) == 1)
                            {
                                return(1);
                            }
                        }
                        break;

                    case 7:
                        if (c2 == 2)
                        {
                            castle.x = i;
                            castle.y = j;
                            if (castle.move(b, r, s) == 1)
                            {
                                return(1);
                            }
                        }
                        break;

                    case 8:
                        if (c2 == 2)
                        {
                            knight.x = i;
                            knight.y = j;
                            if (knight.move(b, r, s) == 1)
                            {
                                return(1);
                            }
                        }
                        break;

                    case 9:
                        if (c2 == 2)
                        {
                            bishop.x = i;
                            bishop.y = j;
                            if (bishop.move(b, r, s) == 1)
                            {
                                return(1);
                            }
                        }
                        break;

                    case 10:
                        if (c2 == 2)
                        {
                            queen.x = i;
                            queen.y = j;
                            if (queen.move(b, r, s) == 1)
                            {
                                return(1);
                            }
                        }
                        break;

                    case 11:
                        if (c2 == 2)
                        {
                            king.x = i;
                            king.y = j;
                            if (king.move(b, r, s) == 1)
                            {
                                return(1);
                            }
                        }
                        break;

                    case 12:
                        if (c2 == 2)
                        {
                            pawn.x = i;
                            pawn.y = j;
                            if (pawn.move(b, r, s) == 1)
                            {
                                return(1);
                            }
                        }
                        break;
                    }
                }
            }
            return(0);
        }
Ejemplo n.º 33
0
        public void placeWhiteFigures(bool isStandardChess)
        {
            Figure[] pawns            = new Figure[8];
            char     currentChar      = 'A';
            char     charPositionPawn = 'A';

            if (isStandardChess)
            {
                for (int i = 0; i < 8; i++)
                {
                    pawns[i] = new Pawn(charPositionPawn, 7, true);
                    charPositionPawn++;


                    if ((currentChar == 'A') || (currentChar == 'H'))
                    {
                        otherFigures[i] = new Rook(currentChar, 8, true);
                    }
                    else if ((currentChar == 'B') || (currentChar == 'G'))
                    {
                        otherFigures[i] = new Knight(currentChar, 8, true);
                    }
                    else if ((currentChar == 'C') || (currentChar == 'F'))
                    {
                        otherFigures[i] = new Bishop(currentChar, 8, true);
                    }
                    else if (currentChar == 'E')
                    {
                        otherFigures[i] = new King(currentChar, 8, true);
                    }
                    else
                    {
                        otherFigures[i] = new Queen(currentChar, 8, true);
                    }

                    currentChar++;
                }
                for (int i = 0; i < 8; i++)
                {
                    placeFigure(pawns[i]);
                    placeFigure(otherFigures[i]);
                }
            }
            else if (!isStandardChess)
            {
                for (int i = 0; i < 8; i++)
                {
                    pawns[i] = new Pawn(charPositionPawn, 7, true);
                    charPositionPawn++;

                    currentChar++;
                }
                do
                {
                    char[] piecePlacement = boardSpaces.OrderBy(x => random.Next()).ToArray();
                    otherFigures[0] = new Bishop(piecePlacement[0], 8, true);
                    otherFigures[1] = new Bishop(piecePlacement[1], 8, true);
                    otherFigures[2] = new Rook(piecePlacement[2], 8, true);
                    otherFigures[3] = new Rook(piecePlacement[3], 8, true);
                    otherFigures[4] = new Knight(piecePlacement[4], 8, true);
                    otherFigures[5] = new Knight(piecePlacement[5], 8, true);
                    otherFigures[6] = new Queen(piecePlacement[6], 8, true);
                    otherFigures[7] = new King(piecePlacement[7], 8, true);
                    if (!(otherFigures[0].Xpositon % 2 == otherFigures[1].Xpositon % 2) &&
                        (((otherFigures[2].Xpositon > otherFigures[7].Xpositon) && (otherFigures[3].Xpositon < otherFigures[7].Xpositon)) ||
                         ((otherFigures[2].Xpositon < otherFigures[7].Xpositon) && (otherFigures[3].Xpositon > otherFigures[7].Xpositon))))
                    {
                        valid = true;
                    }
                } while (!valid);
                for (int i = 0; i < 8; i++)
                {
                    placeFigure(pawns[i]);
                    placeFigure(otherFigures[i]);
                }
                placeBlackFigures(isStandardChess);
            }
        }
Ejemplo n.º 34
0
        public void Move(Figure f, char x, int y)

        {
            var  chessBoxStart = this.getChessBoxByCoordinates(f.Xpositon, f.Ypositon);
            var  chessBoxEnd   = this.getChessBoxByCoordinates(x, y);
            bool isFigureWhite = f.isWhite;

            if (f.getFigureType().Equals("Pawn"))
            {
                this.MovePawn(f, x, y);
            }
            else if (f.getFigureType().Equals("King") && chessBoxEnd.isFigureOn() && chessBoxEnd.Figure.getFigureType().Equals("Rook") && chessBoxStart.Figure.isWhite == chessBoxEnd.Figure.isWhite)
            {//checking for Rocade
                King king = (King)f;
                Rook rook = (Rook)chessBoxEnd.Figure;

                if (PathBetweenBoxesFree(chessBoxStart, chessBoxEnd))
                {
                    if ((!king.isMoved) && (!rook.isMoved))
                    {
                        if (rook.Xpositon == 'A')
                        {
                            chessBoxEnd.Figure.Move('D', chessBoxEnd.Ycoord);
                            this.getChessBoxByCoordinates('D', chessBoxEnd.Ycoord).Figure = chessBoxEnd.Figure;
                            chessBoxEnd.deleteFigure();

                            chessBoxStart.Figure.Move('C', chessBoxStart.Ycoord);
                            this.getChessBoxByCoordinates('C', chessBoxStart.Ycoord).Figure = chessBoxStart.Figure;
                            chessBoxStart.deleteFigure();

                            if (isInChess(f.isWhite))
                            {
                                king.BackToLastPosition();
                                rook.BackToLastPosition();
                                chessBoxEnd.Figure   = this.getChessBoxByCoordinates('D', chessBoxEnd.Ycoord).Figure;
                                chessBoxStart.Figure = this.getChessBoxByCoordinates('C', chessBoxStart.Ycoord).Figure;
                                this.getChessBoxByCoordinates('D', chessBoxEnd.Ycoord).deleteFigure();
                                this.getChessBoxByCoordinates('C', chessBoxStart.Ycoord).deleteFigure();
                                throw new KingInChessExeption("You cannot move here! You will be in chess!");
                            }
                        }
                        else
                        {
                            chessBoxEnd.Figure.Move('F', chessBoxEnd.Ycoord);
                            this.getChessBoxByCoordinates('F', chessBoxEnd.Ycoord).Figure = chessBoxEnd.Figure;
                            chessBoxEnd.deleteFigure();

                            chessBoxStart.Figure.Move('G', chessBoxStart.Ycoord);
                            this.getChessBoxByCoordinates('G', chessBoxStart.Ycoord).Figure = chessBoxStart.Figure;
                            chessBoxStart.deleteFigure();

                            if (isInChess(f.isWhite))
                            {
                                king.BackToLastPosition();
                                rook.BackToLastPosition();
                                chessBoxEnd.Figure   = this.getChessBoxByCoordinates('F', chessBoxEnd.Ycoord).Figure;
                                chessBoxStart.Figure = this.getChessBoxByCoordinates('G', chessBoxStart.Ycoord).Figure;
                                this.getChessBoxByCoordinates('F', chessBoxEnd.Ycoord).deleteFigure();
                                this.getChessBoxByCoordinates('G', chessBoxStart.Ycoord).deleteFigure();
                                throw new KingInChessExeption("You cannot move here! You will be in chess!");
                            }
                        }
                    }
                    else
                    {
                        throw new RocadeNotPossibleExeption("Rocade not possible!The figures has been moved");
                    }
                }
                else
                {
                    throw new RocadeNotPossibleExeption("Rocade not possible!There is a figure on the way!");
                }
            }
            else
            {
                if (PathBetweenBoxesFree(chessBoxStart, chessBoxEnd))
                {
                    if (chessBoxEnd.isFigureOn())
                    {
                        if (chessBoxStart.Figure.isWhite != chessBoxEnd.Figure.isWhite)
                        {
                            Figure figureToDestroy = chessBoxEnd.Figure;
                            f.Move(x, y);
                            chessBoxEnd.Figure.DestroyFigure();
                            chessBoxStart.Figure = null;
                            chessBoxEnd.Figure   = f;
                            if (isInChess(f.isWhite))
                            {
                                chessBoxStart.Figure = f;
                                f.BackToLastPosition();
                                figureToDestroy.BackToLastPosition();
                                chessBoxEnd.Figure = figureToDestroy;
                                throw new KingInChessExeption("You cannot move here! You will be in chess!");
                            }
                        }
                        else
                        {
                            throw new InvalidAttackExeption("Invalid movement! You cannot attack your figure!");
                        }
                    }
                    else
                    {
                        f.Move(x, y);
                        chessBoxStart.Figure = null;
                        chessBoxEnd.Figure   = f;
                        if (isInChess(f.isWhite))
                        {
                            f.BackToLastPosition();
                            chessBoxStart.Figure = f;
                            chessBoxEnd.Figure   = null;
                            throw new KingInChessExeption("You cannot move here! You will be in chess!");
                        }
                    }
                }
                else
                {
                    throw new PathBetweenBoxesNotFreeExeption("The path between the boxes is not free");
                }
            }

            ///if Move coordinates invalid figure.move -argument exeption
        }
Ejemplo n.º 35
0
 public void BeforeEachTest()
 {
     Target = new King();
     MovesFrom3_3 = Target.GetMovesFrom(new BoardCoordinate(3, 3));
 }