Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do I check if it's white's turn? #448

Open
JustinLiu2020 opened this issue Aug 12, 2023 · 1 comment
Open

How do I check if it's white's turn? #448

JustinLiu2020 opened this issue Aug 12, 2023 · 1 comment

Comments

@JustinLiu2020
Copy link

How do I check if it's white's turn? Here's the code :

using ChessChallenge.API;
using System;

public class MyBot : IChessBot
{
//The idea is to check for checks, captures, and threats, and evaluate every possible one to check if it's a good move
//If it is, then it's added to a list of good moves, and then the best one is chosen from that list
//If there aren't any checks, captures, or threats, then it just uses a minimax algorithm to find the best move
//The minimax algorithm is a bit slow, so I'm going to try to make it faster by using alpha-beta pruning

int[] pieceValues = { 0, 100, 300, 350, 500, 900, 10000};

public bool IsCheckmate(Board board, Move move)
{
    board.MakeMove(move);
    bool isMate = board.IsInCheckmate();
    board.UndoMove(move);
    return isMate;
}

public int Evaluate(Board board)
{
    int score = 0;
    for (int i = 0; i < 64; i++)
    {
        Square square = (Square)i;
        Piece piece = board.GetPiece(square);
        if (piece != null)
        {
            if (piece.IsWhite)
            {
                score += pieceValues[(int)piece.PieceType];
            }
            else
            {
                score -= pieceValues[(int)piece.PieceType];
            }
        }
    }
    
}

public int Minimax(int depth, Board board, int alpha, int beta)
{
    if (depth == 0)
    {
        return Evaluate(board);
    }

    Move[] moves = board.GetLegalMoves();

    if (IsWhiteToMove(board))
    {
        int maxEval = -999999;
        foreach (Move move in moves)
        {
            board.MakeMove(move);
            int eval = Minimax(depth - 1, board, alpha, beta);
            board.UndoMove(move);
            maxEval = Math.Max(maxEval, eval); 
            alpha = Math.Max(alpha, eval);
            if (beta <= alpha)
            {
                break;
            }
        }
        return maxEval;
    }
    else
    {
        int minEval = 999999;
        foreach (Move move in moves)
        {
            board.MakeMove(move);
            int eval = Minimax(depth - 1, board, alpha, beta);
            board.UndoMove(move);
            minEval = Math.Min(minEval, eval);
            beta = Math.Min(beta, eval);
            if (beta <= alpha)
            {
                break;
            }
        }
        return minEval;
    }
}

public Move Think(Board board, Timer timer)
{
    Move[] moves = board.GetLegalMoves();
    Minimax(5, board, -999999, 999999);
    return moves[0];
}

}

@ryanheath
Copy link

You mean board.IsWhiteToMove ?
For example, before you make a move board.IsWhiteToMove returns true for white.
Then when you call board.MakeMove(move), board.IsWhiteToMove will return false for white, it is black's turn.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants