Пример #1
0
        private void ArrangeFigures()
        {
            ChessBoard       = new object[8, 8];
            ChessBoard[0, 0] = new BlackRook(new Point(0, 0), this);
            ChessBoard[1, 0] = new BlackKnight(new Point(1, 0), this);
            ChessBoard[2, 0] = new BlackBishop(new Point(2, 0), this);
            ChessBoard[3, 0] = new BlackQueen(new Point(3, 0), this);
            ChessBoard[4, 0] = new BlackKing(new Point(4, 0), this, new Point(0, 0), new Point(7, 0), new Point(2, 0), new Point(6, 0));
            ChessBoard[5, 0] = new BlackBishop(new Point(5, 0), this);
            ChessBoard[6, 0] = new BlackKnight(new Point(6, 0), this);
            ChessBoard[7, 0] = new BlackRook(new Point(7, 0), this);
            for (int i = 0; i < 8; i++)
            {
                ChessBoard[i, 1] = new BlackPawn(new Point(i, 1), this, new Point(0, 1));
            }

            for (int i = 0; i < 8; i++)
            {
                ChessBoard[i, 6] = new WhitePawn(new Point(i, 6), this, new Point(0, -1));
            }
            ChessBoard[0, 7] = new WhiteRook(new Point(0, 7), this);
            ChessBoard[1, 7] = new WhiteKnight(new Point(1, 7), this);
            ChessBoard[2, 7] = new WhiteBishop(new Point(2, 7), this);
            ChessBoard[3, 7] = new WhiteQueen(new Point(3, 7), this);
            ChessBoard[4, 7] = new WhiteKing(new Point(4, 7), this, new Point(0, 7), new Point(7, 7), new Point(2, 7), new Point(6, 7));
            ChessBoard[5, 7] = new WhiteBishop(new Point(5, 7), this);
            ChessBoard[6, 7] = new WhiteKnight(new Point(6, 7), this);
            ChessBoard[7, 7] = new WhiteRook(new Point(7, 7), this);
        }
Пример #2
0
        private void CheckForCheckmateOrTie()
        {
            if (WhiteKing.IsAtacked(this) && BlackKing.IsAtacked(this))
            {
                throw new ApplicationException("What the f**k just happened?");
            }

            if (WhiteKing.IsAtacked(this) || BlackKing.IsAtacked(this))
            {
                if (WhiteKing.IsAtacked(this))
                {
                    if (WhiteFigures().All(fig => fig.GetPossibleMoves(this).Count == 0))
                    {
                        OnMate?.Invoke(FigureSide.Black);
                    }
                }

                if (BlackKing.IsAtacked(this))
                {
                    if (BlackFigures().All(fig => fig.GetPossibleMoves(this).Count == 0))
                    {
                        OnMate?.Invoke(FigureSide.White);
                    }
                }
            }
            else
            if (WhiteFigures().All(fig => fig.GetPossibleMoves(this).Count == 0) ||
                BlackFigures().All(fig => fig.GetPossibleMoves(this).Count == 0))
            {
                OnTie?.Invoke();
            }
        }
Пример #3
0
 private static void BlackMove(ref ChessBoard gameboard, ref string notation, List <string> gamestory, ref string move, ref int NumberOfMoves)
 {
     Console.SetCursorPosition(0, 12);
     Console.Write("Black, your move. Enter \"help\" if you don't know what to do");
     Console.WriteLine();
     notation = Console.ReadLine();
     if (notation == "help")
     {
         Draw.PrintHelp();
         Console.ReadLine();
         return;
     }
     DefaultInfo.BlackEnPassantEndangered = false;
     try
     {
         gameboard = FIDEnotation.PerformBlackMove(gameboard, notation);
         NumberOfMoves++;
         move = move + " " + notation;
         if (!WhiteKing.IsSafe(gameboard))
         {
             move += "+";
         }
         gamestory.Add(move);
         move = "";
         DefaultInfo.IsWhiteMove = !DefaultInfo.IsWhiteMove;
     }
     catch (ArgumentException)
     {
         // Console.WriteLine(e.Message);
         // BlackMove(ref gameboard, ref notation, gamestory, ref move, ref NumberOfMoves);
         return;
     }
 }
Пример #4
0
        public void TestWhiteKingIsSafe()
        {
            ChessBoard board = new ChessBoard();

            board['e', 8] = (sbyte)DefaultPieces.WhiteKing;
            board['e', 1] = (sbyte)DefaultPieces.BlackKing;
            Assert.IsTrue(WhiteKing.IsSafe(board));
            board['e', 2] = (sbyte)DefaultPieces.BlackQueen;
            Assert.IsTrue(!WhiteKing.IsSafe(board));
            board['e', 7] = 1;
            Assert.IsTrue(WhiteKing.IsSafe(board));
            board['c', 6] = (sbyte)DefaultPieces.BlackQueen;
            Assert.IsTrue(!WhiteKing.IsSafe(board));
            board['d', 7] = -1;
            board.DebugConsoleSimpleDraw();
            Assert.IsTrue(WhiteKing.IsSafe(board));
            board['a', 8] = (sbyte)DefaultPieces.BlackRook;
            Assert.IsTrue(!WhiteKing.IsSafe(board));
            board['b', 8] = (sbyte)DefaultPieces.BlackkNight;
            Assert.IsTrue(WhiteKing.IsSafe(board));
            board['c', 8] = (sbyte)DefaultPieces.BlackQueen;
            Assert.IsFalse(WhiteKing.IsSafe(board));
            board['d', 8] = (sbyte)DefaultPieces.WhiteQueen;
            Assert.IsTrue(WhiteKing.IsSafe(board));
        }
Пример #5
0
        public byte GetInstanceNumber(Piece piece, SquareFlag square)
        {
            if (piece.Colour == Colour.White)
            {
                if (piece.Type == PieceType.Pawn)
                {
                    return(WhitePawns.GetInstanceNumber(square));
                }
                if (piece.Type == PieceType.Rook)
                {
                    return(WhiteRooks.GetInstanceNumber(square));
                }
                if (piece.Type == PieceType.Knight)
                {
                    return(WhiteKnights.GetInstanceNumber(square));
                }
                if (piece.Type == PieceType.Bishop)
                {
                    return(WhiteBishops.GetInstanceNumber(square));
                }
                if (piece.Type == PieceType.Queen)
                {
                    return(WhiteQueens.GetInstanceNumber(square));
                }
                if (piece.Type == PieceType.King)
                {
                    return(WhiteKing.GetInstanceNumber(square));
                }
            }
            else
            {
                if (piece.Type == PieceType.Pawn)
                {
                    return(BlackPawns.GetInstanceNumber(square));
                }
                if (piece.Type == PieceType.Rook)
                {
                    return(BlackRooks.GetInstanceNumber(square));
                }
                if (piece.Type == PieceType.Knight)
                {
                    return(BlackKnights.GetInstanceNumber(square));
                }
                if (piece.Type == PieceType.Bishop)
                {
                    return(BlackBishops.GetInstanceNumber(square));
                }
                if (piece.Type == PieceType.Queen)
                {
                    return(BlackQueens.GetInstanceNumber(square));
                }
                if (piece.Type == PieceType.King)
                {
                    return(BlackKing.GetInstanceNumber(square));
                }
            }

            return(0);
        }
Пример #6
0
 public ChessMatch(ChessMatchState owner)
 {
     m_owner         = owner;
     m_board         = new Chessboard();
     m_whiteKing     = null;
     m_blackKing     = null;
     m_winningPlayer = null;
 }
Пример #7
0
        public void SetupGame()
        {
            m_board.Clean();

            //White pieces
            m_board[ChessboardTile.A2] = new WhitePawn(m_owner, m_board);
            m_board[ChessboardTile.B2] = new WhitePawn(m_owner, m_board);
            m_board[ChessboardTile.C2] = new WhitePawn(m_owner, m_board);
            m_board[ChessboardTile.D2] = new WhitePawn(m_owner, m_board);
            m_board[ChessboardTile.E2] = new WhitePawn(m_owner, m_board);
            m_board[ChessboardTile.F2] = new WhitePawn(m_owner, m_board);
            m_board[ChessboardTile.G2] = new WhitePawn(m_owner, m_board);
            m_board[ChessboardTile.H2] = new WhitePawn(m_owner, m_board);

            m_board[ChessboardTile.A1] = new WhiteRook(m_owner, m_board);
            m_board[ChessboardTile.H1] = new WhiteRook(m_owner, m_board);

            m_board[ChessboardTile.B1] = new WhiteKnight(m_owner, m_board);
            m_board[ChessboardTile.G1] = new WhiteKnight(m_owner, m_board);

            m_board[ChessboardTile.C1] = new WhiteBishop(m_owner, m_board);
            m_board[ChessboardTile.F1] = new WhiteBishop(m_owner, m_board);

            m_board[ChessboardTile.D1] = new WhiteQueen(m_owner, m_board);

            m_whiteKing = new WhiteKing(m_owner, m_board);
            m_board[ChessboardTile.E1] = m_whiteKing;

            //Black pieces
            m_board[ChessboardTile.A7] = new BlackPawn(m_owner, m_board);
            m_board[ChessboardTile.B7] = new BlackPawn(m_owner, m_board);
            m_board[ChessboardTile.C7] = new BlackPawn(m_owner, m_board);
            m_board[ChessboardTile.D7] = new BlackPawn(m_owner, m_board);
            m_board[ChessboardTile.E7] = new BlackPawn(m_owner, m_board);
            m_board[ChessboardTile.F7] = new BlackPawn(m_owner, m_board);
            m_board[ChessboardTile.G7] = new BlackPawn(m_owner, m_board);
            m_board[ChessboardTile.H7] = new BlackPawn(m_owner, m_board);

            m_board[ChessboardTile.A8] = new BlackRook(m_owner, m_board);
            m_board[ChessboardTile.H8] = new BlackRook(m_owner, m_board);

            m_board[ChessboardTile.B8] = new BlackKnight(m_owner, m_board);
            m_board[ChessboardTile.G8] = new BlackKnight(m_owner, m_board);

            m_board[ChessboardTile.C8] = new BlackBishop(m_owner, m_board);
            m_board[ChessboardTile.F8] = new BlackBishop(m_owner, m_board);

            m_board[ChessboardTile.D8] = new BlackQueen(m_owner, m_board);

            m_blackKing = new BlackKing(m_owner, m_board);
            m_board[ChessboardTile.E8] = m_blackKing;

            //Set no winner
            m_winningPlayer = null;
        }
Пример #8
0
 public void SetUp()
 {
     _chessBoard  = new ChessBoard();
     _blackBishop = new Bishop(PieceColor.Black, _chessBoard);
     _whiteRook   = new Rook(PieceColor.White, _chessBoard);
     _whitePawn   = new WhitePawn(_chessBoard);
     _blackKing   = new BlackKing(_chessBoard);
     _whiteKing   = new WhiteKing(_chessBoard);
     _blackPawn   = new BlackPawn(_chessBoard);
     _chessBoard.Add(_blackKing, 4, 7);
     _chessBoard.Add(_whiteKing, 4, 0);
 }
Пример #9
0
 public void SetUp()
 {
     _chessBoard  = new ChessBoard();
     _blackBishop = new Bishop(PieceColor.Black, _chessBoard);
     _whiteQueen  = new Queen(PieceColor.White, _chessBoard);
     _whiteKnight = new Knight(PieceColor.White, _chessBoard);
     _blackPawn   = new BlackPawn(_chessBoard);
     _blackKing   = new BlackKing(_chessBoard);
     _whiteKing   = new WhiteKing(_chessBoard);
     _chessBoard.Add(_blackKing, 4, 7);
     _chessBoard.Add(_whiteKing, 4, 0);
 }
Пример #10
0
 public int AnalyzeKingStatus()
 {
     if (this.WhiteToMove)
     {
         WhiteCombos = new List <AttackCombo>(WhiteKing.AnalyzeStatus());
         return(WhiteCombos.Count);
     }
     else
     {
         BlackCombos = new List <AttackCombo>(BlackKing.AnalyzeStatus());
         return(BlackCombos.Count);
     }
 }
Пример #11
0
 public void TryCheckKings()
 {
     for (int i = 0; i < 8; i++)
     {
         for (int j = 0; j < 8; j++)
         {
             if (!plField.CheckEmpty(i, j))
             {
                 WhiteKing.TryCheckBy(plField[i, j], whiteKingY, whiteKingX);
                 BlackKing.TryCheckBy(plField[i, j], blackKingY, blackKingX);
             }
         }
     }
 }
Пример #12
0
 public void UpdateGameStatus()
 {
     WhiteKing.UpdateState();
     BlackKing.UpdateState();
     if (
         WhiteKing.IsCheckmated ||
         WhiteKing.IsStalemated ||
         BlackKing.IsCheckmated ||
         BlackKing.IsStalemated ||
         InsufficientMatingMaterial())
     {
         GameState = GameState.Ended;
     }
 }
Пример #13
0
        /// <summary>
        /// Gets a board in the starting position.
        /// </summary>
        /// <returns></returns>
        public static Board GetStartingBoard()
        {
            var squares = new Piece[SquareNo];
            var status  = new BoardStatus();

            squares[A8] = new BlackRook();
            squares[B8] = new BlackKnight();
            squares[C8] = new BlackBishop();
            squares[D8] = new BlackQueen();
            squares[E8] = new BlackKing();
            squares[F8] = new BlackBishop();
            squares[G8] = new BlackKnight();
            squares[H8] = new BlackRook();

            for (var sqIndex = SideSquareNo; sqIndex < SideSquareNo * 2; sqIndex++)
            {
                squares[sqIndex] = new BlackPawn();
            }

            for (var sqIndex = SideSquareNo * (SideSquareNo - 2); sqIndex < SideSquareNo * (SideSquareNo - 1); sqIndex++)
            {
                squares[sqIndex] = new WhitePawn();
            }

            squares[A1] = new WhiteRook();
            squares[B1] = new WhiteKnight();
            squares[C1] = new WhiteBishop();
            squares[D1] = new WhiteQueen();
            squares[E1] = new WhiteKing();
            squares[F1] = new WhiteBishop();
            squares[G1] = new WhiteKnight();
            squares[H1] = new WhiteRook();

            status.WhiteTurn            = true;
            status.WhiteCouldCastleLong = status.WhiteCouldCastleShort = status.BlackCouldCastleLong = status.BlackCouldCastleShort = true;
            status.EnPassantTarget      = null;
            status.Ply   = 0;
            status.Moves = 1;

            return(new Board(squares, status));
        }
Пример #14
0
 public void ResetKingsChecks()
 {
     WhiteKing.ResetCheck();
     BlackKing.ResetCheck();
 }
Пример #15
0
        //TO DO drawns

        static void Main(string[] args)
        {
            ChessBoard gameboard = new ChessBoard();

            gameboard.SetDefaultChessBoard();
            DefaultInfo.SetDefaultValues();
            string        notation  = "";
            List <string> gamestory = new List <string>();
            string        move      = "";
            // Code Review: Назва локальної змінної повинна починатися з малої літери.
            int NumberOfMoves = 1;
            int rows          = 20;
            int rowwidth      = 20;

            //Draw.DrawChessBoard(gameboard);
            Console.Title = "Dan's True Chess Game";
            do
            {
                DrawGameField(gameboard, gamestory, ref move, NumberOfMoves, ref rows, rowwidth);
                if (DefaultInfo.IsWhiteMove)
                {
                    if (!FIDEnotation.CheckIfArePossibleMoves(gameboard, true))
                    {
                        if (!WhiteKing.IsSafe(gameboard))
                        {
                            DefaultInfo.BlackWin = true;
                        }
                        break;
                    }
                    WhiteMove(ref gameboard, ref notation, ref move);
                }
                else
                {
                    if (!FIDEnotation.CheckIfArePossibleMoves(gameboard, false))
                    {
                        if (!BlackKing.IsSafe(gameboard))
                        {
                            DefaultInfo.WhiteWin = true;
                        }

                        break;
                    }
                    BlackMove(ref gameboard, ref notation, gamestory, ref move, ref NumberOfMoves);
                }
            }while (true);

            if (DefaultInfo.WhiteWin)
            {
                gamestory[gamestory.Count] += "+";
                move = " ";
                DrawGameField(gameboard, gamestory, ref move, NumberOfMoves, ref rows, rowwidth);
                Console.WriteLine("White win!");
            }
            if (DefaultInfo.BlackWin)
            {
                gamestory[gamestory.Count - 1] += "+";
                move = " ";
                DrawGameField(gameboard, gamestory, ref move, NumberOfMoves, ref rows, rowwidth);
                Console.WriteLine("Black win!");
            }
            else
            {
                gamestory[gamestory.Count] += "=";
                move = " ";
                DrawGameField(gameboard, gamestory, ref move, NumberOfMoves, ref rows, rowwidth);
                Console.WriteLine("Draw");
            }
        }