예제 #1
0
    // public static Piece[,] EnPassantCheck(int currentPlayer, Piece[,] Board, List<Piece> allMoves)
    // {
    //     var findKing = allMoves.Find(x => x.pieceType == PieceType.King && x.player == currentPlayer);

    //     var leftRookFind = allMoves.Find(x => x.pieceType == PieceType.Rook && x.player == currentPlayer && x.x == 0);

    //     var rightRookFind = allMoves.Find(x => x.pieceType == PieceType.Rook && x.player == currentPlayer && x.x == Board.GetLength(0) - 1);


    //     bool canMoveLeft = false;
    //     bool canMoveRight = false;

    //     if (findKing == null)
    //     {
    //         if (leftRookFind == null)
    //         {
    //             canMoveLeft = true;
    //             for (int i = 1; i < 4; i++)
    //             {
    //                 if (currentPlayer == 1)
    //                     if (Board[i, Board.GetLength(1) - 1].pieceType != PieceType.None) canMoveLeft = false;
    //                 if (currentPlayer == 2)
    //                     if (Board[i, 0].pieceType != PieceType.None) canMoveLeft = false;
    //             }
    //         }
    //         if (rightRookFind == null)
    //         {
    //             canMoveRight = true;
    //             for (int i = 5; i < Board.GetLength(0) - 1; i++)
    //             {
    //                 if (currentPlayer == 1)
    //                     if (Board[i, Board.GetLength(1) - 1].pieceType != PieceType.None) canMoveRight = false;
    //                 if (currentPlayer == 2)
    //                     if (Board[i, 0].pieceType != PieceType.None) canMoveRight = false;
    //             }
    //         }
    //     }
    //     if (canMoveLeft == true)
    //     {
    //         if (currentPlayer == 1)
    //             Board[0, Board.GetLength(1) - 1].legalMove = true;
    //         if (currentPlayer == 2)
    //             Board[0, 0].legalMove = true;
    //     }
    //     if (canMoveRight == true)
    //     {
    //         if (currentPlayer == 1)
    //             Board[Board.GetLength(0) - 1, Board.GetLength(1) - 1].legalMove = true;
    //         if (currentPlayer == 2)
    //             Board[Board.GetLength(0) - 1, 0].legalMove = true;
    //     }

    //     Console.WriteLine(findKing);

    //     return Board;
    // }
    public static bool IsKingChecked(Piece[,] Board, int currentPlayer)
    {
        Piece king = null;

        for (int x = 0; x < Board.GetLength(0); x++)
        {
            for (int y = 0; y < Board.GetLength(1); y++)
            {
                if (Board[x, y].pieceType == PieceType.King && Board[x, y].player == Program.NextPlayer(currentPlayer))
                {
                    king = Board[x, y];
                }
            }
        }

        // Check if legal move is dangerous
        for (int x = 0; x < Board.GetLength(0); x++)
        {
            for (int y = 0; y < Board.GetLength(1); y++)
            {
                if (Board[x, y].player == currentPlayer && Board[x, y].pieceType != PieceType.King)
                {
                    MoveConditions.CurrentLegalMoves(Board[x, y], Board, Board[x, y].player, true);
                }
            }
        }
        return(Board[king.x, king.y].legalMove);
    }
예제 #2
0
파일: Program.cs 프로젝트: BullEKorV/Chess
    static Piece[,] MovePiece(Piece selectedPiece, Piece[,] Board, Piece newPos, int currentPlayer, List <Piece> allMoves, int round)
    {
        // Add moves to all registered moves
        allMoves.Add(new Piece(selectedPiece.player, selectedPiece.x, selectedPiece.y, selectedPiece.pieceType, selectedPiece.legalMove));
        allMoves.Add(new Piece(newPos.player, newPos.x, newPos.y, newPos.pieceType, newPos.legalMove));

        // Move player to new position
        Board[newPos.x, newPos.y].player    = selectedPiece.player;
        Board[newPos.x, newPos.y].pieceType = selectedPiece.pieceType;

        // Clear old position
        Board[selectedPiece.x, selectedPiece.y].pieceType = PieceType.None;
        Board[selectedPiece.x, selectedPiece.y].player    = 0;


        Board = MoveConditions.Promotion(newPos, currentPlayer, Board);

        return(Board);
    }
예제 #3
0
파일: Program.cs 프로젝트: BullEKorV/Chess
    static void Main(string[] args)
    {
        //Initialize board
        Piece[,] Board = new Piece[8, 8];
        Board          = GenerateBoard(Board);

        Raylib.InitWindow(1920, 1080, "Chess");
        Raylib.ToggleFullscreen();
        Raylib.SetTargetFPS(120);

        int   boardOffset   = 100;
        Piece selectedPiece = null;
        int   currentPlayer = 1;

        bool[] isChecked = { false, false };
        int    round     = 1;
        bool   gameOver  = false;

        bool hintsActivated = true;

        List <Piece> allMoves = new List <Piece>();

        Dictionary <String, Texture2D> Textures = LoadTextures();

        //Run game
        while (!Raylib.WindowShouldClose())
        {
            // Render frame
            Raylib.BeginDrawing();
            Raylib.ClearBackground(Color.WHITE);

            DrawBoard(Board, boardOffset, Textures, isChecked, round, hintsActivated, currentPlayer, gameOver);

            Raylib.EndDrawing();

            // Game logic code
            if (Raylib.IsMouseButtonPressed(MouseButton.MOUSE_LEFT_BUTTON) && gameOver == false)
            {
                // Move piece if legal move
                try
                {
                    if (CheckMousePos(Board, boardOffset) != null)
                    {
                        if (CheckMousePos(Board, boardOffset).legalMove == true)
                        {
                            isChecked[currentPlayer - 1] = false;

                            Board = MovePiece(selectedPiece, Board, CheckMousePos(Board, boardOffset), currentPlayer, allMoves, round);

                            currentPlayer = NextPlayer(currentPlayer);

                            round++;

                            isChecked[currentPlayer - 1] = MoveConditions.IsKingChecked(Board, NextPlayer(currentPlayer));
                        }
                        else
                        {
                            selectedPiece = CheckMousePos(Board, boardOffset);
                        }


                        //Check and mark legal moves
                        MoveConditions.ClearLegalMoves(Board);
                        MoveConditions.CurrentLegalMoves(selectedPiece, Board, currentPlayer, true);

                        // if (selectedPiece.pieceType == PieceType.King)
                        //     MoveConditions.EnPassantCheck(currentPlayer, Board, allMoves);
                    }
                }
                catch (System.Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }
            // Special key commands
            // if (Raylib.IsKeyPressed(KeyboardKey.KEY_A)) // Next player
            //     currentPlayer = NextPlayer(currentPlayer);
            if (Raylib.IsKeyPressed(KeyboardKey.KEY_B)) // Go back step
            {
                if (round > 1)
                {
                    Board = GoBackStep(Board, allMoves);
                    round--;
                    isChecked[currentPlayer - 1] = MoveConditions.IsKingChecked(Board, currentPlayer);
                    gameOver      = false;
                    currentPlayer = NextPlayer(currentPlayer);
                    MoveConditions.ClearLegalMoves(Board);
                }
            }
            if (Raylib.IsKeyPressed(KeyboardKey.KEY_G) && (isChecked[0] || isChecked[1])) // Give up
            {
                gameOver = true;
            }
            if (Raylib.IsKeyPressed(KeyboardKey.KEY_R)) // Reset game
            {
                Board = ResetGame(Board, ref currentPlayer, ref round, ref allMoves, ref isChecked, ref gameOver);
            }
            if (Raylib.IsKeyPressed(KeyboardKey.KEY_H)) // Activate or deactivate tips
            {
                hintsActivated = !hintsActivated;
            }
            if (Raylib.IsKeyPressed(KeyboardKey.KEY_Q)) // Quit game
            {
                Raylib.CloseWindow();
            }
        }
    }