From f1ed65c219f5e5cc35b5f1b12472b5a9d5345201 Mon Sep 17 00:00:00 2001 From: Sebastian Lague Date: Tue, 25 Jul 2023 22:02:44 +0200 Subject: [PATCH] Refactor bitboard visualization --- Chess-Challenge/src/API/BitboardHelper.cs | 16 ++++++++++++---- .../Helpers/API Helpers/BitboardDebugState.cs | 8 ++++++++ .../src/Framework/Application/UI/BoardUI.cs | 8 ++++---- 3 files changed, 24 insertions(+), 8 deletions(-) create mode 100644 Chess-Challenge/src/Framework/Application/Helpers/API Helpers/BitboardDebugState.cs diff --git a/Chess-Challenge/src/API/BitboardHelper.cs b/Chess-Challenge/src/API/BitboardHelper.cs index 2fc60f1..019478d 100644 --- a/Chess-Challenge/src/API/BitboardHelper.cs +++ b/Chess-Challenge/src/API/BitboardHelper.cs @@ -1,7 +1,7 @@ namespace ChessChallenge.API { - using ChessChallenge.Application; + using ChessChallenge.Application.APIHelpers; using ChessChallenge.Chess; /// @@ -101,13 +101,21 @@ namespace ChessChallenge.API return isWhite ? Bits.WhitePawnAttacks[square.Index] : Bits.BlackPawnAttacks[square.Index]; } + /// + /// 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. + /// public static void VisualizeBitboard(ulong bitboard) { - BoardUI.VisualizeBitboardEnabled = true; - BoardUI.BitboardToVisualize = bitboard; + BitboardDebugState.BitboardDebugVisualizationRequested = true; + BitboardDebugState.BitboardToVisualize = bitboard; } - public static void StopVisualizingBitboard() => BoardUI.VisualizeBitboardEnabled = false; + /// + /// Clears the bitboard debug visualization + /// + public static void StopVisualizingBitboard() => BitboardDebugState.BitboardDebugVisualizationRequested = false; static ulong GetRookAttacks(Square square, ulong blockers) { diff --git a/Chess-Challenge/src/Framework/Application/Helpers/API Helpers/BitboardDebugState.cs b/Chess-Challenge/src/Framework/Application/Helpers/API Helpers/BitboardDebugState.cs new file mode 100644 index 0000000..73248da --- /dev/null +++ b/Chess-Challenge/src/Framework/Application/Helpers/API Helpers/BitboardDebugState.cs @@ -0,0 +1,8 @@ +namespace ChessChallenge.Application.APIHelpers +{ + public static class BitboardDebugState + { + public static bool BitboardDebugVisualizationRequested { get; set; } + public static ulong BitboardToVisualize {get; set;} + } +} diff --git a/Chess-Challenge/src/Framework/Application/UI/BoardUI.cs b/Chess-Challenge/src/Framework/Application/UI/BoardUI.cs index c0ef9d0..288a767 100644 --- a/Chess-Challenge/src/Framework/Application/UI/BoardUI.cs +++ b/Chess-Challenge/src/Framework/Application/UI/BoardUI.cs @@ -5,6 +5,7 @@ using System.Collections.Generic; using System.Numerics; using System.IO; using static ChessChallenge.Application.UIHelper; +using ChessChallenge.Application.APIHelpers; namespace ChessChallenge.Application { @@ -22,8 +23,6 @@ namespace ChessChallenge.Application static readonly Color nameCol = new(67, 204, 101, 255); // 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 bitboardColONE = new(252, 43, 92, 200); @@ -210,7 +209,7 @@ namespace ChessChallenge.Application UpdateMoveAnimation(animT); } - if (VisualizeBitboardEnabled) + if (BitboardDebugState.BitboardDebugVisualizationRequested) { ForEachSquare(DrawBitboardDebugOverlaySquare); } @@ -364,7 +363,8 @@ namespace ChessChallenge.Application 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; Vector2 squarePos = GetSquarePos(file, rank, whitePerspective);