Esempio n. 1
0
        void DisplayChessboard(ChessPiece[,] chessboard)
        {
            string[] colLetters = { "  ", "A ", "B ", "C ", "D ", "E ", "F ", "G ", "H " };

            for (int row = 0; row < chessboard.GetLength(0) + 1; row++)
            {
                for (int col = 0; col < chessboard.GetLength(1) + 1; col++)
                {
                    if (row == 0)
                    {
                        Console.Write("{0} ", colLetters[col]);
                        continue;
                    }
                    else if (col == 0)
                    {
                        Console.Write("{0} ", row);
                        continue;
                    }

                    if ((row + col) % 2 == 0)
                    {
                        Console.BackgroundColor = ConsoleColor.DarkYellow;
                    }
                    else
                    {
                        Console.BackgroundColor = ConsoleColor.Gray;
                    }

                    DisplayChessPiece(chessboard[row - 1, col - 1]);

                    Console.ResetColor();
                }
                Console.WriteLine();
            }
        }
Esempio n. 2
0
 private void LoadPieceCache()
 {
     lock (pieceCacheLocked)
     {
         whitePieceCache = new List <ChessPiece>();
         blackPieceCache = new List <ChessPiece>();
         for (int x = 0; x < pieces.GetLength(0); x++)
         {
             for (int y = 0; y < pieces.GetLength(1); y++)
             {
                 var pieceAt = pieces[x, y];
                 if (pieceAt == null)
                 {
                     continue;
                 }
                 if (pieceAt.Color == PieceColor.White)
                 {
                     whitePieceCache.Add(pieceAt);
                 }
                 else
                 {
                     blackPieceCache.Add(pieceAt);
                 }
             }
         }
         cacheSet = true;
     }
 }
Esempio n. 3
0
        public static Player[,] MapToEngineBoard(ChessPiece[,] source, Player blackPlayer)
        {
            var b = new Player[source.GetLength(0), source.GetLength(1)];

            for (int i = 0; i < b.GetLength(0); i++)
            {
                for (int j = 0; j < b.GetLength(1); j++)
                {
                    ref var d = ref b[i, j];
                    switch (source[i, j])
                    {
                    case ChessPiece.Empty:
                        d = Player.Empty;
                        break;

                    case ChessPiece.Black:
                        d = blackPlayer;
                        break;

                    case ChessPiece.White:
                        d = blackPlayer.OppositePlayer();
                        break;
                    }
                }
            }
Esempio n. 4
0
        void DisplayChessBoard(ChessPiece[,] chessboard)
        {
            Console.WriteLine("   A  B  C  D  E  F  G  H");
            for (int r = 0; r < chessboard.GetLength(1); r++)
            {
                Console.Write($"{r+1} ");
                for (int k = 0; k < chessboard.GetLength(1); k++)
                {
                    if ((r + k) % 2 == 0)
                    {
                        Console.BackgroundColor = ConsoleColor.DarkYellow;
                    }
                    else
                    {
                        Console.BackgroundColor = ConsoleColor.Gray;
                    }

                    DisplayChessPiece(chessboard[r, k]);
                    if (k + 1 == chessboard.GetLength(1))
                    {
                        k = 0;
                        Console.WriteLine();
                        break;
                    }
                }

                Console.ResetColor();
            }
        }
        void PutChessPieces(ChessPiece[,] chessboard)
        {
            ChessPieceType[] order = { ChessPieceType.Rook, ChessPieceType.Knight, ChessPieceType.Bishop, ChessPieceType.King, ChessPieceType.Queen, ChessPieceType.Bishop, ChessPieceType.Knight, ChessPieceType.Rook };

            for (int row = 0; row < chessboard.GetLength(0); row++)
            {
                for (int col = 0; col < chessboard.GetLength(1); col++)
                {
                    // Set type
                    if (row == 1 || row == 6)
                    {
                        chessboard[row, col]      = new ChessPiece();
                        chessboard[row, col].type = ChessPieceType.Pawn;
                    }
                    else if (row == 0 || row == 7)
                    {
                        chessboard[row, col] = new ChessPiece();

                        chessboard[row, col].type = order[col];
                    }

                    // Set color
                    if (row == 0 || row == 1)
                    {
                        chessboard[row, col].color = ChessPieceColor.White;
                    }

                    else if (row == 6 || row == 7)
                    {
                        chessboard[row, col].color = ChessPieceColor.Black;
                    }
                }
            }
        }
Esempio n. 6
0
        void DisplayChessboard(ChessPiece[,] chessboard)
        {
            Console.Write("    A  B  C  D  E  F  G  H");
            Console.WriteLine();

            int count = 0;

            for (int i = 0; i < chessboard.GetLength(0); i++)
            {
                for (int j = 0; j < chessboard.GetLength(1); j++)
                {
                    if (j == 0)
                    {
                        count++;
                        Console.Write("{0}  ", count);
                    }
                    if ((i + j) % 2 == 0)
                    {
                        Console.BackgroundColor = ConsoleColor.DarkYellow;
                    }
                    else
                    {
                        Console.BackgroundColor = ConsoleColor.Gray;
                    }

                    DisplayChessPiece(chessboard[i, j]);
                    Console.ResetColor();
                }
                Console.WriteLine();
            }
        }
Esempio n. 7
0
        void PutChessPieces(ChessPiece[,] chessboard)
        {
            ChessPieceType[] order = { ChessPieceType.Rook, ChessPieceType.Knight, ChessPieceType.Bishop, ChessPieceType.King, ChessPieceType.Queen, ChessPieceType.Bishop, ChessPieceType.Knight, ChessPieceType.Rook };

            for (int i = 0; i < chessboard.GetLength(0); i++)
            {
                for (int j = 0; j < chessboard.GetLength(1); j++)
                {
                    if (i == 1 || i == 6)
                    {
                        chessboard[i, j]      = new ChessPiece();
                        chessboard[i, j].Type = ChessPieceType.Pawn;
                    }
                    else if (i == 0 || i == 7)
                    {
                        chessboard[i, j]      = new ChessPiece();
                        chessboard[i, j].Type = order[j];
                    }


                    if (i == 0 || i == 1)
                    {
                        chessboard[i, j].Color = ChessPieceColor.White;
                    }
                    else if (i == 6 || i == 7)
                    {
                        chessboard[i, j].Color = ChessPieceColor.Black;
                    }
                }
            }
        }
Esempio n. 8
0
        public static void PrintBoard(ChessPiece[,] pieces, bool[,] possibleMoves)
        {
            ConsoleColor originalBackground = Console.BackgroundColor;
            ConsoleColor greyBackground     = ConsoleColor.DarkGray;

            for (int i = 0; i < pieces.GetLength(0); i++)
            {
                Console.Write(8 - i + " ");
                for (int j = 0; j < pieces.GetLength(1); j++)
                {
                    if (possibleMoves[i, j])
                    {
                        Console.BackgroundColor = greyBackground;
                        PrintPiece(pieces[i, j]);
                        Console.BackgroundColor = originalBackground;
                    }
                    else
                    {
                        PrintPiece(pieces[i, j]);
                    }
                }
                Console.WriteLine();
            }
            Console.WriteLine("  a b c d e f g h");
            Console.BackgroundColor = originalBackground;
        }
Esempio n. 9
0
        void PutChessPieces(ChessPiece[,] chessboard)
        {
            ChessPieceType[] order = { ChessPieceType.Rook,  ChessPieceType.Knight, ChessPieceType.Bishop, ChessPieceType.King,
                                       ChessPieceType.Queen, ChessPieceType.Bishop, ChessPieceType.Knight, ChessPieceType.Rook };

            for (int c = 0; c < chessboard.GetLength(1); c++)
            {
                chessboard[1, c] = new  ChessPiece {
                    colour = ChessPieceColour.White, type = ChessPieceType.Pawn
                };
                chessboard[6, c] = new ChessPiece {
                    colour = ChessPieceColour.Black, type = ChessPieceType.Pawn
                };
            }

            for (int c = 0; c < chessboard.GetLength(1); c++)
            {
                chessboard[0, c] = new ChessPiece {
                    colour = ChessPieceColour.White, type = order[c]
                };
                chessboard[7, c] = new ChessPiece {
                    colour = ChessPieceColour.Black, type = order[c]
                };
            }
        }
Esempio n. 10
0
 public void CheckMove(ChessPiece[,] chessboard, Position from, Position to, Turn turn)
 {
     if (chessboard[from.row, from.col] == null)
     {
         throw new Exception("No chesspiece at current 'from' coordinates");
     }
     else if (!(chessboard[to.row, to.col] == null))
     {
         if (chessboard[from.row, from.col].colour == chessboard[to.row, to.col].colour)
         {
             throw new Exception("A chesspiece is already at the currently chosen 'to' coordinates");
         }
         else
         {
             Console.ForegroundColor = ConsoleColor.Red;
             Console.WriteLine($"{chessboard[to.row, to.col].colour} {chessboard[to.row, to.col].type} has been eliminated!");
             Console.ResetColor();
             DeadPieces(chessboard[to.row, to.col]);
         }
     }
     else if (!ValidMove(chessboard, chessboard[from.row, from.col], from, to))
     {
         throw new Exception("Move is invalid for this chesspiece");
     }
     else if (from.col >= chessboard.GetLength(1) || from.row >= chessboard.GetLength(0) || to.col >= chessboard.GetLength(1) || to.row >= chessboard.GetLength(0))
     {
         throw new Exception("Coordinates out of bounds");
     }
     else if ((turn == Turn.Black && chessboard[from.row, from.col].colour == ChessPieceColour.White) || (turn == Turn.White && chessboard[from.row, from.col].colour == ChessPieceColour.Black))
     {
         throw new Exception($"The {turn} player can only move pieces of colour {turn}");
     }
 }
Esempio n. 11
0
        void PutChessPieces(ref ChessPiece[,] chessboard)
        {
            ChessPieceType[] order = { ChessPieceType.Rook,  ChessPieceType.Knight, ChessPieceType.Bishop, ChessPieceType.King,
                                       ChessPieceType.Queen, ChessPieceType.Bishop, ChessPieceType.Knight, ChessPieceType.Rook };

            for (int r = 0; r < chessboard.GetLength(0); r++)
            {
                for (int c = 0; c < chessboard.GetLength(1); c++)
                {
                    if (r == 0)
                    {
                        chessboard[r, c] = new ChessPiece(ChessPieceColor.White, order[c]);
                    }
                    else if (r == 1)
                    {
                        chessboard[r, c] = new ChessPiece(ChessPieceColor.White, ChessPieceType.Pawn);
                    }
                    else if (r == 6)
                    {
                        chessboard[r, c] = new ChessPiece(ChessPieceColor.Black, ChessPieceType.Pawn);
                    }
                    else if (r == 7)
                    {
                        chessboard[r, c] = new ChessPiece(ChessPieceColor.Black, order[c]);
                    }
                }
            }
        }
Esempio n. 12
0
        public bool GameCheck(ChessPiece[,] chessboard, int move)
        {
            int kings = 0;

            for (int r = 0; r < chessboard.GetLength(0); r++)
            {
                for (int c = 0; c < chessboard.GetLength(1); c++)
                {
                    if (chessboard[r, c] != null && (chessboard[r, c].type == ChessPieceType.King))
                    {
                        kings++;
                    }
                }
            }
            if (kings == 2)
            {
                return(true);
            }
            else
            {
                Turn winner = DetermineTurn(move);
                if (winner == Turn.White)
                {
                    Console.BackgroundColor = ConsoleColor.White;
                    Console.ForegroundColor = ConsoleColor.Black;
                }
                else
                {
                    Console.BackgroundColor = ConsoleColor.Gray;
                    Console.ForegroundColor = ConsoleColor.White;
                }
                Console.WriteLine($"GAME OVER   -   {winner} wins");
                return(false);
            }
        }
Esempio n. 13
0
        public override List <Point> GetValidMoves()
        {
            ChessPiece[,] board = ChessBoard.GetBoard;
            List <Point> validMoves = new List <Point>();

            for (int i = 0; i < board.GetLength(0); i++)
            {
                for (int j = 0; j < board.GetLength(1); j++)
                {
                    if (!(j == Coordinates.Y && i == Coordinates.X) &&
                        (j == Coordinates.Y + 2 && i == Coordinates.X + 1 ||
                         j == Coordinates.Y + 2 && i == Coordinates.X - 1 ||
                         j == Coordinates.Y - 2 && i == Coordinates.X + 1 ||
                         j == Coordinates.Y - 2 && i == Coordinates.X - 1 ||
                         j == Coordinates.Y + 1 && i == Coordinates.X + 2 ||
                         j == Coordinates.Y + 1 && i == Coordinates.X - 2 ||
                         j == Coordinates.Y - 1 && i == Coordinates.X + 2 ||
                         j == Coordinates.Y - 1 && i == Coordinates.X - 2))
                    {
                        validMoves.Add(new Point(i, j));
                    }
                }
            }

            validMoves = RemoveInvalidMoves(validMoves);

            return(validMoves);
        }
Esempio n. 14
0
        private bool IsCheckMate(Color color)
        {
            ChessPiece[,] board = GetBoard;
            King king = GetKing(color);

            for (int i = 0; i < board.GetLength(0); i++)
            {
                for (int j = 0; j < board.GetLength(1); j++)
                {
                    ChessPiece chessPiece = board[i, j];

                    if (chessPiece != null)
                    {
                        if (chessPiece.GetColor == king.GetColor)
                        {
                            foreach (Point move in chessPiece.GetValidMoves())
                            {
                                if (chessPiece.MoveResolvesCheck(color, move))
                                {
                                    return(false);
                                }
                            }
                        }
                    }
                }
            }

            return(true);
        }
Esempio n. 15
0
        void DisplayChessBoard(ChessPiece[,] chessboard)
        {
            Console.WriteLine("   A  B  C  D  E  F  G  H  ");

            for (int row = 0; row < chessboard.GetLength(0); row++)
            {
                Console.Write(row + 1 + " ");
                for (int col = 0; col < chessboard.GetLength(1); col++)
                {
                    if ((row + col) % 2 == 0)
                    {
                        Console.BackgroundColor = ConsoleColor.DarkYellow;
                    }
                    else
                    {
                        Console.BackgroundColor = ConsoleColor.Gray;
                    }

                    DisplayChessPiece(chessboard[row, col]);
                    Console.ResetColor();
                }
                Console.WriteLine();
            }
            Console.WriteLine();
        }
Esempio n. 16
0
 void ClearPieces()
 {
     for (int x = 0; x < Pieces.GetLength(0); x++)
     {
         for (int y = 0; y < Pieces.GetLength(1); y++)
         {
             Pieces[x, y] = null;
         }
     }
 }
Esempio n. 17
0
 void InitChessboard(ChessPiece[,] chessboard)
 {
     for (int i = 0; i < chessboard.GetLength(0); i++)
     {
         for (int j = 0; j < chessboard.GetLength(1); j++)
         {
             chessboard[i, j] = null;
         }
         PutChessPieces(chessboard);
     }
 }
Esempio n. 18
0
 void InitChessboard(ChessPiece[,] chessboard)
 {
     for (int row = 0; row < chessboard.GetLength(0); row++)
     {
         for (int col = 0; col < chessboard.GetLength(1); col++)
         {
             chessboard[row, col] = null;
         }
     }
     PutChessPieces(chessboard);
 }
Esempio n. 19
0
 public void InitChessboard(ChessPiece[,] chessboard)
 {
     for (int r = 0; r < chessboard.GetLength(0); r++)
     {
         for (int c = 0; c < chessboard.GetLength(1); c++)
         {
             chessboard[r, c] = null;
         }
     }
     PutChessPieces(chessboard);
 }
Esempio n. 20
0
 /// <summary>
 /// 清空棋盘,重新开始
 /// </summary>
 public void Clear()
 {
     for (int i = 0; i < chessboard.GetLength(0); i++)
     {
         for (int j = 0; j < chessboard.GetLength(1); j++)
         {
             chessboard[i, j] = ChessPiece.EMPTY;
         }
     }
     turn = ChessPiece.CROSS;
 }
Esempio n. 21
0
 void InitChessBoard(ChessPiece[,] chessboard)
 {
     for (int r = 0; r < chessboard.GetLength(0); r++)
     {
         for (int k = 0; k < chessboard.GetLength(1); k++)
         {
             chessboard[r, k] = null;
         }
     }
     PutChessPieces(chessboard);
 }
 private void InitChessboard(ChessPiece[,] cb)
 {
     for (int r = 0; r < cb.GetLength(0); r++)
     {
         for (int c = 0; c < cb.GetLength(1); c++)
         {
             cb[r, c] = null;
         }
     }
     PutChessPieces(cb);
 }
        private void DisplayChessboard(ChessPiece[,] cb)
        {
            Console.Write("   " + " A " + " B " + " C " + " D " + " E " + " F " + " G " + " H " + "\n");

            for (int r = 0; r < chessboard.GetLength(0); r++)
            {
                Console.Write(r + 1 + "  ");
                for (int c = 0; c < chessboard.GetLength(1); c++)
                {
                    if ((r + c) % 2 == 0)
                    {
                        Console.BackgroundColor = ConsoleColor.DarkYellow;
                    }
                    else
                    {
                        Console.BackgroundColor = ConsoleColor.Gray;
                    }
                    ChessPiece cp = chessboard[r, c];
                    DisplayChessPiece(cp);
                    Console.ResetColor();
                }
                Console.WriteLine();
            }

            /* for (int c = 0; c < cb.GetLength(0);c++)
             * {
             *   for(int r = -1; r < cb.GetLength(1); r++)
             *   {
             *       if (r == -1)
             *       {
             *           Console.Write((c + 1) + "  ");
             *       } else
             *       {
             *           if ((r+c)%2==0)
             *           {
             *               Console.BackgroundColor = ConsoleColor.DarkYellow;
             *           }
             *           else
             *           {
             *               Console.BackgroundColor = ConsoleColor.Gray;
             *           }
             *       }
             *
             *
             *       if(r >= 0)
             *       {
             *           DisplayChessPiece(cb[r, c]);
             *       }
             *   }
             *   Console.WriteLine();
             *   Console.ResetColor();
             * } */
        }
Esempio n. 24
0
 public void InitChessboard()
 {
     for (int row = 0; row < chessboard.GetLength(0); row++)
     {
         for (int col = 0; col < chessboard.GetLength(1); col++)
         {
             chessboard[row, col] = null;
         }
     }
     PutChessPieces();
     turn = ChessPieceColor.White;
 }
Esempio n. 25
0
 public static void PrintBoard(ChessPiece[,] chessPieces, bool[,] possibleMoves)
 {
     for (int i = 0; i < chessPieces.GetLength(0); i++)
     {
         Console.Write(8 - i + " ");
         for (int j = 0; j < chessPieces.GetLength(1); j++)
         {
             PrintPiece(chessPieces[i, j], possibleMoves[i, j]);
         }
         Console.WriteLine();
     }
     Console.WriteLine("  a b c d e f g h");
 }
Esempio n. 26
0
        private static ChessPiece[,] TurnBoard(ChessPiece[,] source)
        {
            var b = new ChessPiece[source.GetLength(0), source.GetLength(1)];

            for (int i = 0; i < b.GetLength(0); i++)
            {
                for (int j = 0; j < b.GetLength(1); j++)
                {
                    b[i, j] = source[b.GetUpperBound(0) - j, i];
                }
            }
            return(b);
        }
Esempio n. 27
0
 public static void PrintBoard(ChessPiece[,] pieces)
 {
     for (int i = 0; i < pieces.GetLength(0); i++)
     {
         Console.Write(8 - i + " ");
         for (int j = 0; j < pieces.GetLength(1); j++)
         {
             PrintPiece(pieces[i, j]);
         }
         Console.WriteLine();
     }
     Console.WriteLine("  a b c d e f g h");
 }
    private bool CheckMate()
    {
        var totalPossibleMoves = 0;

        for (var rowIndex = 0; rowIndex < _chessBoard.GetLength(0); rowIndex++)
        {
            for (var columnIndex = 0; columnIndex < _chessBoard.GetLength(1); columnIndex++)
            {
                var chessPiece = _chessBoard[rowIndex, columnIndex];

                //Check for piece - is cell occupied?
                if (chessPiece.type == ChessPiece.PieceType.Empty)
                {
                    break;
                }

                var position = new Vector2(columnIndex, rowIndex);

                //Get all legal moves for piece
                var legalMoves = GetLegalMoves(chessPiece, position);
                Debug.Log(chessPiece.color + " " + chessPiece.type + ": " + legalMoves.Count);
                totalPossibleMoves += legalMoves.Count;

//                foreach (var move in legalMoves)
//                {
//                    //Make each move one by one
//                    chessBoard[rowIndex, columnIndex] = new ChessPiece(ChessPiece.PieceType.Empty, ChessPiece.PieceColor.None, new Vector2(rowIndex, columnIndex));
//                    chessBoard[(int)move.x, (int)move.y] = chessPiece;
//
//                    //if the move does not result in the king being checked
//                    if (!KingInCheck(chessPiece)) break;
//
//                    //Get all legal moves for the king under attack
//                    var kingsMoves = GetLegalMoves(attackedPiece, attackedPiece.position);
//
//                    //if king cannot escape
//                    if (!KingCanEscape(kingsMoves, chessPiece, position))
//                    {
//                        //TODO: Check if other pieces can block the piece(s) that have King in check, break if true
//
//                        //If we haven't returned from any of the above checks on a given move, King must be in checkmate!
//                        return true;
//                    }
//
//                    //TODO: Undo last move, reset board to original state
//                }
            }
        }
        Debug.Log("Total possible legal moves: " + totalPossibleMoves);
        return(false);
    }
Esempio n. 29
0
        public void Init()
        {
            chessBoard = new ChessPiece[8, 8];

            for (int x = 0; x < chessBoard.GetLength(0); x++)
            {
                for (int y = 0; y < chessBoard.GetLength(1); y++)
                {
                    chessBoard[x, y] = null;
                }
            }

            ChessPieceHelper.InitChessPieces(ref chessBoard);

            whoIsActive = ChessPieceColor.White;
            winner      = ChessPieceColor.None;
        }
Esempio n. 30
0
 private Board(List <Move> moveHistory, ChessPiece[,] pieces)
 {
     ChessPiece[,] piecesCopy = new ChessPiece[pieces.GetLength(0), pieces.GetLength(1)];
     for (int x = 0; x < pieces.GetLength(0); x++)
     {
         for (int y = 0; y < pieces.GetLength(1); y++)
         {
             var pieceAtSpot = pieces[x, y];
             if (pieceAtSpot != null)
             {
                 piecesCopy[x, y] = pieceAtSpot.MoveTo(pieceAtSpot.Position);
             }
         }
     }
     this.pieces      = piecesCopy;
     this.moveHistory = new List <Move>(moveHistory);
 }