예제 #1
0
        public void CheckMove(ChessPiece[,] chessboard, Position from, Position to)
        {
            if (chessboard[from.field, from.column] == null)
            {
                Console.WriteLine("No chesspiece at from position!");
                return;
            }

            if (chessboard[to.field, to.column] != null)
            {
                Console.WriteLine("There is a pice on the to position.");
                return;
            }

            // check if valid

            bool isValid = ValidMove(chessboard[from.field, from.column], from, to);

            if (isValid)
            {
                DoMove(chessboard, from, to);
            }
            else
            {
                Console.WriteLine("Invalid move for chesspiece {0}", chessboard[from.field, from.column].type);
            }
        }
예제 #2
0
        // h(x) = pairs of queens that are attacking each other (directly or indirectly)
        public int Heuristic(ChessPiece[,] board, int boardSize)
        {
            int result       = 0;
            int queenToCount = boardSize;

            for (int x = 0; x < boardSize; x++)
            {
                for (int y = 0; y < boardSize; y++)
                {
                    if (queenToCount == 0)
                    {
                        break;
                    }

                    if (board[x, y] == ChessPiece.Queen)
                    {
                        queenToCount--;

                        result += HeuristicRight(board, boardSize, x, y);
                        result += HeuristicDown(board, boardSize, x, y);
                        result += HeuristicRightDown(board, boardSize, x, y);
                        result += HeuristicRightUp(board, boardSize, x, y);
                    }
                }
            }

            return(result);
        }
예제 #3
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();
            }
        }
예제 #4
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;
                    }
                }
            }
    private void SpawnAllChessPieces()
    {
        activeChessPiece = new List <GameObject>();
        chessPieces      = new ChessPiece[7, 7];
        //Blue Team

        //King Spawn
        SpawnChessPiece(0, 3, 6);

        //Archer Spawn
        SpawnChessPiece(1, 2, 6);
        SpawnChessPiece(1, 4, 6);

        //Pawn Spawn
        SpawnChessPiece(2, 0, 6);
        SpawnChessPiece(2, 6, 6);
        SpawnChessPiece(2, 1, 6);
        SpawnChessPiece(2, 5, 6);

        //Red Team

        //King Spawn
        SpawnChessPiece(3, 3, 0);

        //Archer Spawn
        SpawnChessPiece(4, 2, 0);
        SpawnChessPiece(4, 4, 0);

        //Pawn Spawn
        SpawnChessPiece(5, 0, 0);
        SpawnChessPiece(5, 6, 0);
        SpawnChessPiece(5, 1, 0);
        SpawnChessPiece(5, 5, 0);
    }
예제 #6
0
        protected virtual List <ChessPosition> Vertical(ChessPosition pos_start, ChessPiece[,] Map_Chess)
        {
            List <ChessPosition> list  = new List <ChessPosition>();
            ChessColor           color = Map_Chess[pos_start.X, pos_start.Y].Color;
            ChessEnum            type  = Map_Chess[pos_start.X, pos_start.Y].ChessType;

            if (color == ChessColor.Die)
            {
                return(list);
            }
            for (int index = 0; index < 2; index++)
            {
                int val = maps_vertical_n_horizontal[index];
                while (pos_start.Y + val >= 0 && pos_start.Y + val <= 7)
                {
                    if (Map_Chess[pos_start.X, pos_start.Y + val].Color == color)
                    {
                        break;                                                          // if ally -> can't move
                    }
                    else
                    {
                        list.Add(new ChessPosition()
                        {
                            X = pos_start.X, Y = pos_start.Y + val
                        });
                        if (Map_Chess[pos_start.X, pos_start.Y + val].Color != ChessColor.Die)
                        {
                            break;                                                                    // if enemy -> eat
                        }
                    }
                    val = val + maps_vertical_n_horizontal[index];
                }
            }
            return(list);
        }
예제 #7
0
        /// <summary>
        /// Find out if a square is vulnerable to attacks by another player.
        /// </summary>
        /// <param name="player">The vulnerable player</param>
        /// <param name="boardArray">An optional substitute board for validating moves</param>
        /// <returns>True if square can be attacked</returns>
        public bool CheckSquareVulnerable(int squareX, int squareY, int player, ChessPiece[,] boardArray = null)
        {
            if (boardArray == null)
            {
                boardArray = this.boardArray;
            }

            for (int x = 0; x < boardArray.GetLength(0); x++)
            {
                for (int y = 0; y < boardArray.GetLength(1); y++)
                {
                    if (boardArray[x, y] != null && boardArray[x, y].Player != player)
                    {
                        foreach (Point point in PieceActions(x, y, true, true, false, boardArray))
                        {
                            if (point.x == squareX && point.y == squareY)
                            {
                                return(true);
                            }
                        }
                    }
                }
            }
            return(false);
        }
 private void CheckMove(ChessPiece[,] cb, Position from, Position to)
 {
     /*
      * -the from-position contains a chess piece;
      * -the to-position does not contain a chess piece;
      * -the move is valid for the specific chess piece (at the from-position); to check this, call method ValidMove, see next;
      */
     try
     {
         if (cb[from.column, from.row] != null)
         {
             if (cb[to.column, to.row] == null)
             {
                 ValidMove(cb[from.column, from.row], from, to);
             }
         }
         DoMove(chessboard, from, to);
     }
     catch (Exception e)
     {
         if (e != null)
         {
             Console.WriteLine("Invalid move");
             Console.WriteLine("From: " + from.column + " " + from.row);
             Console.WriteLine("To:   " + to.column + " " + to.row);
         }
         Console.ReadKey();
     }
 }
예제 #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 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;
                    }
                }
            }
        }
예제 #10
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);
        }
예제 #11
0
 public Utility()
 {
     _boardSquares = new ChessPiece[8,8];
     startMove = new int[2];
     endMove = new int[2];
     SetBoard();
 }
예제 #12
0
 public override bool IsValidMove(Move move, ChessPiece[,] board)
 {
     if (move != null && board != null && (Math.Abs(move.From.X - move.Target.X) == Math.Abs(move.From.Y - move.Target.Y)))
     {
         //left-up
         if (move.From.X > move.Target.X && move.From.Y > move.Target.Y)
         {
             return(isValidDiagonalUpLeftMovement(move, board));
         }
         //right-up
         else if (move.From.X > move.Target.X && move.From.Y < move.Target.Y)
         {
             return(isValidDiagonalUpRightMovement(move, board));
         }
         //left-down
         else if (move.From.X < move.Target.X && move.From.Y > move.Target.Y)
         {
             return(isValidDiagonalDownLeftMovement(move, board));
         }
         //right-down
         else if (move.From.X < move.Target.X && move.From.Y < move.Target.Y)
         {
             return(isValidDiagonalDownRightMovement(move, board));
         }
     }
     return(false);
 }
예제 #13
0
 public override bool IsValidMove(Move move, ChessPiece[,] board)
 {
     //vertical
     if (move.From.X == move.Target.X)
     {
         if (move.From.Y > move.Target.Y)//left
         {
             return(leftMovement(move, board));
         }
         else if (move.From.Y < move.Target.Y)//right
         {
             return(rightMovement(move, board));
         }
     }
     //portrait
     if (move.From.Y == move.Target.Y)
     {
         if (move.From.X > move.Target.X)//up
         {
             return(upMovement(move, board));
         }
         else if (move.From.X < move.Target.X)//down
         {
             return(downMovement(move, board));
         }
     }
     return(false);
 }
예제 #14
0
        Move Turn(bool isWhiteTurn)
        {
            Location from = null, target = null;
            Move     ret = new Move();

            try
            {
                from = inputRecieve(isWhiteTurn, true);
                if (from != null)
                {
                    target = inputRecieve(isWhiteTurn, false);
                    ret    = new Move(from, target);
                }
                ChessPiece[,] hypotheticBoard = applyingMoveOnHypotheticBoardIfLegal(board, ret, !isWhiteTurn);
                if (hypotheticBoard != null && isValidMoveNoCheck(hypotheticBoard, !isWhiteTurn))
                {
                    return(ret);
                }
                throw new IllegalMoveException();
            }
            catch (TieRequestException) { throw; }
            catch (CastleException) { throw; }
            catch (IllegalMoveException) { throw; }
            catch (CheckException) { throw; }
        }
예제 #15
0
 bool isMate(bool isWhiteThreatning)
 {
     for (int i = 0; i < 8; i++)
     {
         for (int j = 0; j < 8; j++)
         {
             Location from = new Location(i, j);
             if (!board[from.X, from.Y].IsEmpty && board[from.X, from.Y].IsWhite == !isWhiteThreatning)
             {
                 for (int k = 0; k < 8; k++)
                 {
                     for (int l = 0; l < 8; l++)
                     {
                         Location target = new Location(k, l);
                         Move     move   = new Move(from, target);
                         ChessPiece[,] hypotheticBoard = applyingMoveOnHypotheticBoardIfLegal(board, move, isWhiteThreatning);
                         Location kingLocation = findKing(hypotheticBoard, !isWhiteThreatning);
                         if (isValidMoveNoCheck(hypotheticBoard, isWhiteThreatning))
                         {
                             return(false);
                         }
                     }
                 }
             }
         }
     }
     return(true);
 }
예제 #16
0
        public bool IsValidMove(Location from, Location target, ChessPiece[,] board, bool isWhiteTurn)
        {
            int upOrDownPawnDirection = IsWhite ? -1 : 1;

            //one step forward
            if (board[target.X, target.Y].IsEmpty && from.Y == target.Y && from.X == target.X + upOrDownPawnDirection)
            {
                return(true);
            }
            //two steps forward
            else if (board[target.X, target.Y].IsEmpty && from.Y == target.Y &&
                     from.X == target.X + 2 * upOrDownPawnDirection && (isWhiteTurn ? from.X == 1 : from.X == 6))
            {
                IsPawnEnPassantThreat = true;
                return(true);
            }
            //capturing
            else if (!board[target.X, target.Y].IsEmpty && (board[target.X, target.Y].IsWhite == !isWhiteTurn) &&
                     from.X == target.X + upOrDownPawnDirection && (from.Y == target.Y + 1 || from.Y == target.Y - 1))
            {
                return(true);
            }
            //en passant
            else if (board[from.X, target.Y].IsPawnEnPassantThreat && (from.Y == target.Y + 1 || from.Y == target.Y - 1) &&
                     from.X == target.X + upOrDownPawnDirection && ((isWhiteTurn && from.X == 4) || (!isWhiteTurn && from.X == 3)))
            {
                return(true);
            }
            return(false);
        }
예제 #17
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();
            }
        }
예제 #18
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();
        }
예제 #19
0
        public ChessBoard(ChessPiece[,] state)
        {
            if (state == null)
                throw new ArgumentNullException("state");

            _boardState = state;
        }
예제 #20
0
        private ChessBoard SetupBoard()
        {
            boardArray = new ChessPiece[COLUMNS, ROWS];
            string[] playerPeices =
            {
                "Rook", "Knight", "Bishop", "Queen",
                "King", "Bishop", "Knight", "Rook",
                "Pawn", "Pawn",   "Pawn",   "Pawn",
                "Pawn", "Pawn",   "Pawn",   "Pawn"
            };

            for (int i = 0; i < COLUMNS; i++)
            {
                // Player 0 pieces
                boardArray[i, 0] = (ChessPiece)Activator.CreateInstance(
                    Type.GetType("Chess." + playerPeices[i]));
                boardArray[i, 1] = (ChessPiece)Activator.CreateInstance(
                    Type.GetType("Chess." + playerPeices[i + COLUMNS]));
                // Player 1 pieces
                boardArray[i, ROWS - 1] = (ChessPiece)Activator.CreateInstance(
                    Type.GetType("Chess." + playerPeices[i]), new object[] { 1 });
                boardArray[i, ROWS - 2] = (ChessPiece)Activator.CreateInstance(
                    Type.GetType("Chess." + playerPeices[i + COLUMNS]), new object[] { 1 });
            }
            return(this);
        }
예제 #21
0
        public bool MoveResolvesCheck(Color color, Point move)
        {
            ChessPiece[,] board = ChessBoard.GetBoard;

            int originX = (int)Coordinates.X;
            int originY = (int)Coordinates.Y;

            int moveX = (int)move.X;
            int moveY = (int)move.Y;

            ChessPiece chessPieceAtMove = board[moveX, moveY];

            bool moveResolvesCheck = false;

            if ((chessPieceAtMove != null && chessPieceAtMove.GetType().Name != "King") || chessPieceAtMove == null)
            {
                board[moveX, moveY]     = board[originX, originY];
                board[originX, originY] = null;

                board[moveX, moveY].SetCoordinates = move;

                if (!ChessBoard.KingIsChecked(color))
                {
                    moveResolvesCheck = true;
                }

                board[originX, originY] = board[moveX, moveY];
                board[moveX, moveY]     = chessPieceAtMove;

                board[originX, originY].SetCoordinates = new Point(originX, originY);
            }
            return(moveResolvesCheck);
        }
예제 #22
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;
                    }
                }
            }
        }
예제 #23
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;
        }
예제 #24
0
 public bool isCheckMate(int colorSel, ChessPiece[] pcs, ChessPiece[,] board, int c) //can be used for both checkmate and stalemate
 {
     int[][] pMoves;                                                                 //the moves for the piece being checked
     for (int i = 0; i < pcs.Length; i++)
     {
         if (pcs[i].getPColor() == c)
         {
             if (pcs[i].getLocation()[0] != -1)
             {
                 pMoves = pcs[i].moves(board, colorSel);
                 for (int j = 0; j < pMoves.Length; j++)
                 {
                     if (pMoves[j] != null)
                     {
                         if (isMoveSafe(colorSel, pcs, i, pMoves[j]))                                //if there is a safe move to be made it returns false
                         {
                             return(false);
                         }
                     }
                 }
             }
         }
     }
     return(true);           //return true if no safe moves to be made
 }
예제 #25
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]);
                    }
                }
            }
        }
예제 #26
0
        protected override List <Point> RemoveInvalidMoves(List <Point> validMoves)
        {
            ChessPiece[,] board = ChessBoard.GetBoard;

            IEnumerable <Point> obstacles = validMoves
                                            .Where(point => board[(int)point.X, (int)point.Y] != null);

            foreach (Point obstacle in obstacles)
            {
                if (board[(int)obstacle.X, (int)obstacle.Y].GetColor == this.Color)
                {
                    validMoves = validMoves
                                 .Where(move => !(move.X == obstacle.X && move.Y == obstacle.Y))
                                 .ToList();
                }
                if (obstacle.X < this.Coordinates.X && obstacle.Y < this.Coordinates.Y)
                {
                    validMoves = validMoves.Where(move => !(move.X < obstacle.X && move.Y < obstacle.Y)).ToList();
                }
                else if (obstacle.X < this.Coordinates.X && obstacle.Y > this.Coordinates.Y)
                {
                    validMoves = validMoves.Where(move => !(move.X <obstacle.X && move.Y> obstacle.Y)).ToList();
                }
                else if (obstacle.X > this.Coordinates.X && obstacle.Y < this.Coordinates.Y)
                {
                    validMoves = validMoves.Where(move => !(move.X > obstacle.X && move.Y < obstacle.Y)).ToList();
                }
                else if (obstacle.X > this.Coordinates.X && obstacle.Y > this.Coordinates.Y)
                {
                    validMoves = validMoves.Where(move => !(move.X > obstacle.X && move.Y > obstacle.Y)).ToList();
                }
            }

            return(validMoves);
        }
예제 #27
0
        void CheckMove(ChessPiece[,] chessboard, Position from, Position to)
        {
            if (chessboard[from.row, from.column] == null)
            {
                Console.WriteLine("no chess piece at from - position");
                return;
            }

            if (chessboard[to.row, to.column] != null)
            {
                Console.WriteLine("Piece existing at position");
                return;
            }

            ChessPiece chess = chessboard[from.row, from.column];
            bool       Moves = ValidMove(chess, from, to);

            if (Moves)
            {
                DoMove(chessboard, from, to);
            }
            else
            {
                Console.WriteLine("Invalid move for chess piece {0}", chess.Type);
            }
        }
 public State()
 {
     Board = new ChessPiece[2, 3] {
         { ChessPiece.King, ChessPiece.Bishop, ChessPiece.Bishop },
         { ChessPiece.Rook, ChessPiece.Rook, ChessPiece.Empty }
     };
 }
예제 #29
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();
            }
        }
예제 #30
0
파일: Knight.cs 프로젝트: KhoiNg/ChessGame
        public static List <ChessPosition> knightmove(ChessPosition pos_start, ChessPiece[,] Map_Chess)
        {
            List <ChessPosition> list  = new List <ChessPosition>();
            ChessColor           color = Map_Chess[pos_start.X, pos_start.Y].Color;

            if (color == ChessColor.Die)
            {
                return(list);
            }
            ChessEnum type = Map_Chess[pos_start.X, pos_start.Y].ChessType;

            for (int val = 0; val < 8; val++)
            {
                int _x = pos_start.X + maps_knight[val, 0];
                int _y = pos_start.Y + maps_knight[val, 1];
                if (_x < 0 | _x > 7 | _y < 0 | _y > 7)
                {
                    continue;                                    //outside
                }
                if (Map_Chess[_x, _y].Color == color)
                {
                    continue;                                   // if ally -> can't move
                }
                else
                {
                    list.Add(new ChessPosition()
                    {
                        X = _x, Y = _y
                    });
                }
            }
            return(list);
        }
예제 #31
0
파일: Knight.cs 프로젝트: KhoiNg/ChessGame
        public override List <ChessPosition> ListCanMove(ChessPosition pos_start, ChessPiece[,] Map_Chess)
        {
            List <ChessPosition> list = new List <ChessPosition>();

            list.AddRange(knightmove(pos_start, Map_Chess));
            return(list);
        }
예제 #32
0
    //public ChessBoardSim(ChessPiece[,] boardLayout, int turnNumber, ChessPiece piece, int[]moveLoc)
    public ChessBoardSim(ChessPiece[,] boardLayout, int turnNumber)
    {
        this.boardLayout = (ChessPiece[, ])boardLayout.Clone();
        this.turnNumber  = turnNumber;
        ChessPiece temp;

        for (int i = 0; i < 8; i++)
        {
            for (int j = 0; j < 8; j++)
            {
                temp = boardLayout[i, j];
                if (temp != null)
                {
                    if (temp.isWhite)
                    {
                        whitePiecesRemaining.Add(temp);
                    }
                    else
                    {
                        blackPiecesRemaining.Add(temp);
                    }
                }
            }
        }
    }
예제 #33
0
        //Instantierar objekt som AI-spelaren behöver för att fungera
        public override void setupAI(ChessBoard chessboard, GameView gw)
        {
            this.chessboard = chessboard;
            this.rules = new RulesEngine(chessboard);
            this.board = chessboard.get();
            this.gw = gw;

        }
예제 #34
0
        //AI-spelaren gör ett drag
        public async override void movePiece()
        {
            await Task.Delay(500);

            this.board = chessboard.get();
            updateLists();
            Move bestMove = findBestMove();
            if (bestMove != null)
            {
                gw.makeMove(bestMove.getfromX(), bestMove.getfromY(), bestMove.gettoX(), bestMove.gettoY());
            }
         
        }
예제 #35
0
 public void setBoard(ChessPiece[,] board)
 {
     this.board = board;
 }
 private ChessGamePlayer()
 {
     gameStatus = GAME_STATUS_NONE;
     positions = new ChessPiece[8, 8];
 }
        // Make a new board state by applying a new move to an already existing board state.
        public BoardState(BoardState previousState, ChessMove newMove, bool lookForCheck = true)
        {
            BoardGrid = new ChessPiece[8, 8];
            if (!newMove.KingsideCastle && !newMove.QueensideCastle)
            {
                HashSet<ChessMove> previousPossibleMoves = previousState.GetPossibleMoves(newMove.From);
                if (!previousPossibleMoves.Contains(newMove))
                {
                    throw new BoardStateException("Illegal move.");
                }
            }
            // Copy elements.
            for (int row = 0; row < 8; row++)
            {
                for (int column = 0; column < 8; column++)
                {
                    Coordinate coordinate = new Coordinate(row, column);
                    SetPieceAtCoordinate(previousState.GetPieceAtCoordinate(coordinate), coordinate);
                }
            }
            // Copy other board state values.
            BlackKingsideCastling = previousState.BlackKingsideCastling;
            BlackQueensideCastling = previousState.BlackQueensideCastling;
            WhiteKingsideCastling = previousState.WhiteKingsideCastling;
            WhiteQueensideCastling = previousState.WhiteQueensideCastling;
            // Turn color will be flipped and fullmove/halfmove will be incremented after move is applied.
            TurnColor = previousState.TurnColor;
            FullMove = previousState.FullMove;
            HalfMove = previousState.HalfMove;
            // Reset En Passant.
            EnPassantTarget = new Coordinate(-1, -1);
            // Castling special case.
            if (newMove.KingsideCastle || newMove.QueensideCastle)
            {
                int row = TurnColor == ChessPieceColor.White ? FirstRowWhite : FirstRowBlack;
                int rookStartColumn = newMove.KingsideCastle ? KingsideRookColumn : QueensideRookColumn;
                int kingEndColumn = newMove.KingsideCastle ? KingsideCastledKingColumn : QueensideCastledKingColumn;
                int rookEndColumn = newMove.KingsideCastle ? KingsideCastledRookColumn : QueensideCastledRookColumn;
                var kingStart = new Coordinate(row, KingStartColumn);
                var kingEnd = new Coordinate(row, kingEndColumn);
                var rookStart = new Coordinate(row, rookStartColumn);
                var rookEnd = new Coordinate(row, rookEndColumn);
                SetPieceAtCoordinate(new ChessPiece(ChessPieceColor.None, ChessPieceType.None), kingStart);
                SetPieceAtCoordinate(new ChessPiece(ChessPieceColor.None, ChessPieceType.None), rookStart);
                SetPieceAtCoordinate(new ChessPiece(TurnColor, ChessPieceType.King), kingEnd);
                SetPieceAtCoordinate(new ChessPiece(TurnColor, ChessPieceType.Rook), rookEnd);
                if (TurnColor == ChessPieceColor.White)
                {
                    WhiteKingsideCastling = false;
                    WhiteQueensideCastling = false;
                }
                else
                {
                    BlackKingsideCastling = false;
                    BlackQueensideCastling = false;
                }
            }
            // All other move types.
            else
            {
                // If en passant
                if (newMove.PieceType == ChessPieceType.Pawn)
                {
                    if (previousState.EnPassantTarget.Equals(newMove.To))
                    {
                        if (TurnColor == ChessPieceColor.White)
                        {
                            SetPieceAtCoordinate(new ChessPiece(ChessPieceColor.None, ChessPieceType.None), new Coordinate(newMove.To, -1, 0));
                        }
                        else
                        {
                            SetPieceAtCoordinate(new ChessPiece(ChessPieceColor.None, ChessPieceType.None), new Coordinate(newMove.To, 1, 0));
                        }
                    }
                    // Mark if the new move triggers the possibilty of an En Passant from the following turn.

                    int pawnDoubleFromRow = TurnColor == ChessPieceColor.White ? 1 : 6;
                    int pawnDoubleToRow = TurnColor == ChessPieceColor.White ? 3 : 4;
                    int enPassantTargetTargetRow = TurnColor == ChessPieceColor.White ? 2 : 5;
                    if (newMove.From.Row == pawnDoubleFromRow && newMove.To.Row == pawnDoubleToRow)
                    {
                        EnPassantTarget = new Coordinate(enPassantTargetTargetRow, newMove.From.Column);
                    }
                }
                // King movements disable castling.
                else if (newMove.PieceType == ChessPieceType.King)
                {
                    if (TurnColor == ChessPieceColor.White)
                    {
                        WhiteKingsideCastling = false;
                        WhiteQueensideCastling = false;
                    }
                    else
                    {
                        BlackKingsideCastling = false;
                        BlackQueensideCastling = false;
                    }
                }
                // Rook movements disable on their side.
                else if (newMove.PieceType == ChessPieceType.Rook)
                {
                    if (TurnColor == ChessPieceColor.White)
                    {
                        if (newMove.From.Equals(new Coordinate(FirstRowWhite, KingsideRookColumn)))
                        {
                            WhiteKingsideCastling = false;
                        }
                        else if (newMove.From.Equals(new Coordinate(FirstRowWhite, QueensideRookColumn)))
                        {
                            WhiteQueensideCastling = false;
                        }
                    }
                    else
                    {
                        if (newMove.From.Equals(new Coordinate(FirstRowBlack, KingsideRookColumn)))
                        {
                            BlackKingsideCastling = false;
                        }
                        else if (newMove.From.Equals(new Coordinate(FirstRowBlack, QueensideRookColumn)))
                        {
                            BlackQueensideCastling = false;
                        }
                    }
                }
                // Set square that the piece is moving from to empty, and moving to to have the piece.
                SetPieceAtCoordinate(new ChessPiece(ChessPieceColor.None, ChessPieceType.None), newMove.From);
                SetPieceAtCoordinate(new ChessPiece(TurnColor, newMove.IsPromotionToQueen ? ChessPieceType.Queen : newMove.PieceType), newMove.To);
            }

            // Reset or increment halfMove.
            if (newMove.IsCapture || newMove.PieceType == ChessPieceType.Pawn)
            {
                HalfMove = 0;
            }
            else
            {
                HalfMove++;
            }

            // Set applied move to be the previous move.
            PreviousMove = newMove;

            // Increment fullMove after blacks turn;
            if (TurnColor == ChessPieceColor.Black)
            {
                FullMove++;
            }

            // Switch turns.
            TurnColor = previousState.TurnColor == ChessPieceColor.White ? ChessPieceColor.Black : ChessPieceColor.White;

            bool isCheck = false;
            bool isCheckMate = false;
            if (lookForCheck)
            {
                new BoardState(this, out isCheck, out isCheckMate);
                PreviousMove = new ChessMove(PreviousMove.From, PreviousMove.To, PreviousMove.PieceType, PreviousMove.IsCapture, PreviousMove.IsPromotionToQueen, PreviousMove.DrawOfferExtended, isCheck, isCheckMate, PreviousMove.KingsideCastle, PreviousMove.QueensideCastle);
            }
            // Finally, determine the list of legal moves.
            InitializeAllPossibleMoves();
        }
예제 #38
0
        public ChessBoard(DataStorage dataS)
        {
            ds = dataS;
            //Om det finns ett sparat spel, läs in det
            if (File.Exists(@".\chessdata\chessdata.xml"))
            {
                this.board = ds.LoadData();
                ds.SaveData(board);
            }
            else
            {
                //Skapar vita pjäser
                ChessPiece whiteKing = new King("white", 4, 0);
                ChessPiece whiteQueen = new Queen("white", 3, 0);
                ChessPiece whiteRunner1 = new Runner("white", 2, 0);
                ChessPiece whiteRunner2 = new Runner("white", 5, 0);
                ChessPiece whiteHorse1 = new Horse("white", 1, 0);
                ChessPiece whiteHorse2 = new Horse("white", 6, 0);
                ChessPiece whiteTower1 = new Tower("white", 0, 0);
                ChessPiece whiteTower2 = new Tower("white", 7, 0);
                ChessPiece whiteFarmer1 = new Farmer("white", 0, 1);
                ChessPiece whiteFarmer2 = new Farmer("white", 1, 1);
                ChessPiece whiteFarmer3 = new Farmer("white", 2, 1);
                ChessPiece whiteFarmer4 = new Farmer("white", 3, 1);
                ChessPiece whiteFarmer5 = new Farmer("white", 4, 1);
                ChessPiece whiteFarmer6 = new Farmer("white", 5, 1);
                ChessPiece whiteFarmer7 = new Farmer("white", 6, 1);
                ChessPiece whiteFarmer8 = new Farmer("white", 7, 1);

                //Skapar svarta pjäser
                ChessPiece blackKing = new King("black", 4, 7);
                ChessPiece blackQueen = new Queen("black", 3, 7);
                ChessPiece blackRunner1 = new Runner("black", 2, 7);
                ChessPiece blackRunner2 = new Runner("black", 5, 7);
                ChessPiece blackHorse1 = new Horse("black", 1, 7);
                ChessPiece blackHorse2 = new Horse("black", 6, 7);
                ChessPiece blackTower1 = new Tower("black", 0, 7);
                ChessPiece blackTower2 = new Tower("black", 7, 7);
                ChessPiece blackFarmer1 = new Farmer("black", 0, 6);
                ChessPiece blackFarmer2 = new Farmer("black", 1, 6);
                ChessPiece blackFarmer3 = new Farmer("black", 2, 6);
                ChessPiece blackFarmer4 = new Farmer("black", 3, 6);
                ChessPiece blackFarmer5 = new Farmer("black", 4, 6);
                ChessPiece blackFarmer6 = new Farmer("black", 5, 6);
                ChessPiece blackFarmer7 = new Farmer("black", 6, 6);
                ChessPiece blackFarmer8 = new Farmer("black", 7, 6);

                //Lägg till pjäser i tvådimensionell array som representerar schackbrädet
                board[4, 0] = whiteKing;
                board[3, 0] = whiteQueen;
                board[2, 0] = whiteRunner1;
                board[5, 0] = whiteRunner2;
                board[1, 0] = whiteHorse1;
                board[6, 0] = whiteHorse2;
                board[0, 0] = whiteTower1;
                board[7, 0] = whiteTower2;
                board[0, 1] = whiteFarmer1;
                board[1, 1] = whiteFarmer2;
                board[2, 1] = whiteFarmer3;
                board[3, 1] = whiteFarmer4;
                board[4, 1] = whiteFarmer5;
                board[5, 1] = whiteFarmer6;
                board[6, 1] = whiteFarmer7;
                board[7, 1] = whiteFarmer8;

                board[4, 7] = blackKing;
                board[3, 7] = blackQueen;
                board[2, 7] = blackRunner1;
                board[5, 7] = blackRunner2;
                board[1, 7] = blackHorse1;
                board[6, 7] = blackHorse2;
                board[0, 7] = blackTower1;
                board[7, 7] = blackTower2;
                board[0, 6] = blackFarmer1;
                board[1, 6] = blackFarmer2;
                board[2, 6] = blackFarmer3;
                board[3, 6] = blackFarmer4;
                board[4, 6] = blackFarmer5;
                board[5, 6] = blackFarmer6;
                board[6, 6] = blackFarmer7;
                board[7, 6] = blackFarmer8;

                ds.SaveData(board);
            }
        }
 // Take the current state of the board in Forsyth-Edwards Notation and the Long Algebraic Notation
 // of the most recent move (the FEN should already reflect the move).
 public BoardState(string forsythEdwardsNotation, string algebraicNotation)
 {
     BoardGrid = new ChessPiece[8, 8];
     // Default No color or type (i.e. an empty square)
     for (int row = 0; row < 8; row++)
     {
         for (int column = 0; column < 8; column++)
         {
             this.BoardGrid[row, column] = new ChessPiece(ChessPieceColor.None, ChessPieceType.None);
         }
     }
     ParseFEN(forsythEdwardsNotation);
     ParseLongAlgebraicNotation(algebraicNotation);
     InitializeAllPossibleMoves();
 }
예제 #40
0
 public GameState(GameState gs)
 {
     chessboard = (ChessPiece[,])gs.chessboard.Clone();
     turn = gs.turn;
     last_modify = (int[])gs.last_modify.Clone();
 }
예제 #41
0
 public void setBoard(ChessPiece[,] brd)
 {
     board = brd;
 }
        // Hypothetical board state where the turn color has not changed
        private BoardState(BoardState previousState, out bool isCheck, out bool isCheckMate)
        {
            BoardGrid = new ChessPiece[8, 8];
            // Copy elements.
            for (int row = 0; row < 8; row++)
            {
                for (int column = 0; column < 8; column++)
                {
                    Coordinate coordinate = new Coordinate(row, column);
                    SetPieceAtCoordinate(previousState.GetPieceAtCoordinate(coordinate), coordinate);
                }
            }

            // Copy other board state values.
            BlackKingsideCastling = previousState.BlackKingsideCastling;
            BlackKingsideCastling = previousState.BlackQueensideCastling;
            WhiteKingsideCastling = previousState.WhiteKingsideCastling;
            WhiteQueensideCastling = previousState.WhiteQueensideCastling;
            TurnColor = previousState.TurnColor == ChessPieceColor.White ? ChessPieceColor.Black : ChessPieceColor.White; ;
            FullMove = previousState.FullMove;
            HalfMove = previousState.HalfMove;
            EnPassantTarget = new Coordinate(-1, -1);
            ParseLongAlgebraicNotation("");

            InitializeAllPossibleMoves();

            isCheck = KingIsCapturable();

            isCheckMate = false;
            if (isCheck)
            {
                this.TurnColor = TurnColor == ChessPieceColor.White ? ChessPieceColor.Black : ChessPieceColor.White;

                InitializeAllPossibleMoves();

                isCheckMate = true;
                for (int row = 0; row < 8 && isCheckMate; row++)
                {
                    for (int column = 0; column < 8 && isCheckMate; column++)
                    {
                        var moves = PossibleMovesGrid[row, column];
                        if (moves != null)
                        {
                            foreach (var move in moves)
                            {
                                if (!new BoardState(this, move, false).KingIsCapturable())
                                {
                                    isCheckMate = false;
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
예제 #43
0
 public ChessBoard()
 {
     pieces = new ChessPiece[8, 8];
     highlightedSquares = new Dictionary<SquareCoordinates, Color>();
     turn = Turn.NOT_IN_SESSION;
 }
예제 #44
0
 public GameState()
 {
     chessboard = new ChessPiece[Settings.BOARD_SIZE, Settings.BOARD_SIZE];
     turn = ChessPiece.CROSS;
     last_modify = new int[2] { -1, -1 };
 }
예제 #45
0
파일: ChessBoard.cs 프로젝트: ashu-22/Chess
 private ChessBoard()
 {
     board = new ChessPiece[8, 8];
 }