SebLague / Chess-Challenge

Create your own tiny chess bot!

Home Page:https://www.youtube.com/watch?v=Ne40a5LkK6A

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

JustinLiu2020 opened this issue · comments

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];
}

}

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.