Refactor bitboard visualization

This commit is contained in:
Sebastian Lague 2023-07-25 22:02:44 +02:00
parent 6ae5aa695f
commit f1ed65c219
3 changed files with 24 additions and 8 deletions

View file

@ -1,7 +1,7 @@
namespace ChessChallenge.API namespace ChessChallenge.API
{ {
using ChessChallenge.Application; using ChessChallenge.Application.APIHelpers;
using ChessChallenge.Chess; using ChessChallenge.Chess;
/// <summary> /// <summary>
@ -101,13 +101,21 @@ namespace ChessChallenge.API
return isWhite ? Bits.WhitePawnAttacks[square.Index] : Bits.BlackPawnAttacks[square.Index]; return isWhite ? Bits.WhitePawnAttacks[square.Index] : Bits.BlackPawnAttacks[square.Index];
} }
/// <summary>
/// A debug function for visualizing bitboards.
/// Highlights the squares that are set to 1 in the given bitboard with a red colour.
/// Highlights the squares that are set to 0 in the given bitboard with a blue colour.
/// </summary>
public static void VisualizeBitboard(ulong bitboard) public static void VisualizeBitboard(ulong bitboard)
{ {
BoardUI.VisualizeBitboardEnabled = true; BitboardDebugState.BitboardDebugVisualizationRequested = true;
BoardUI.BitboardToVisualize = bitboard; BitboardDebugState.BitboardToVisualize = bitboard;
} }
public static void StopVisualizingBitboard() => BoardUI.VisualizeBitboardEnabled = false; /// <summary>
/// Clears the bitboard debug visualization
/// </summary>
public static void StopVisualizingBitboard() => BitboardDebugState.BitboardDebugVisualizationRequested = false;
static ulong GetRookAttacks(Square square, ulong blockers) static ulong GetRookAttacks(Square square, ulong blockers)
{ {

View file

@ -0,0 +1,8 @@
namespace ChessChallenge.Application.APIHelpers
{
public static class BitboardDebugState
{
public static bool BitboardDebugVisualizationRequested { get; set; }
public static ulong BitboardToVisualize {get; set;}
}
}

View file

@ -5,6 +5,7 @@ using System.Collections.Generic;
using System.Numerics; using System.Numerics;
using System.IO; using System.IO;
using static ChessChallenge.Application.UIHelper; using static ChessChallenge.Application.UIHelper;
using ChessChallenge.Application.APIHelpers;
namespace ChessChallenge.Application namespace ChessChallenge.Application
{ {
@ -22,8 +23,6 @@ namespace ChessChallenge.Application
static readonly Color nameCol = new(67, 204, 101, 255); static readonly Color nameCol = new(67, 204, 101, 255);
// Bitboard debug mode // Bitboard debug mode
public static bool VisualizeBitboardEnabled { get; set; }
public static ulong BitboardToVisualize { get; set; }
static readonly Color bitboardColZERO = new(61, 121, 217, 200); static readonly Color bitboardColZERO = new(61, 121, 217, 200);
static readonly Color bitboardColONE = new(252, 43, 92, 200); static readonly Color bitboardColONE = new(252, 43, 92, 200);
@ -210,7 +209,7 @@ namespace ChessChallenge.Application
UpdateMoveAnimation(animT); UpdateMoveAnimation(animT);
} }
if (VisualizeBitboardEnabled) if (BitboardDebugState.BitboardDebugVisualizationRequested)
{ {
ForEachSquare(DrawBitboardDebugOverlaySquare); ForEachSquare(DrawBitboardDebugOverlaySquare);
} }
@ -364,7 +363,8 @@ namespace ChessChallenge.Application
void DrawBitboardDebugOverlaySquare(int file, int rank) void DrawBitboardDebugOverlaySquare(int file, int rank)
{ {
bool isSet = BitBoardUtility.ContainsSquare(BitboardToVisualize, new Coord(file,rank).SquareIndex); ulong bitboard = BitboardDebugState.BitboardToVisualize;
bool isSet = BitBoardUtility.ContainsSquare(bitboard, new Coord(file,rank).SquareIndex);
Color col = isSet ? bitboardColONE : bitboardColZERO; Color col = isSet ? bitboardColONE : bitboardColZERO;
Vector2 squarePos = GetSquarePos(file, rank, whitePerspective); Vector2 squarePos = GetSquarePos(file, rank, whitePerspective);