SebLague / Chess-Challenge

Create your own tiny chess bot!
https://www.youtube.com/watch?v=Ne40a5LkK6A
MIT License
1.78k stars 1.07k forks source link

Add helper method to get a human-readable string that represents the board #405

Closed ernestoyaquello closed 11 months ago

ernestoyaquello commented 11 months ago

What is this about?

This PR adds a very simple method to the Board class that can be called to get a human-readable string representing the board state.

Method signature

/// <summary>
/// Returns a human-readable string that represents the chess board on its current state.
/// The capital letters are the white pieces, while the lowercase letters are the black ones.
/// NOTE: To distinguish kings from knights, kings are represented by K/k and knights by N/n.
/// </summary>
string ToHumanReadableString(bool includeFen = true, bool includeZobristKey = true, Square? highlightedSquare = null)

Method output

board.ToHumanReadableString()

+---+---+---+---+---+---+---+---+
| r |   |   | q | k |   |   | r | 8
+---+---+---+---+---+---+---+---+
|   | b | p |   |   | p | p |   | 7
+---+---+---+---+---+---+---+---+
| p |   |   | p |   | n |   | p | 6
+---+---+---+---+---+---+---+---+
|   | p |   |   | p | P |   |   | 5
+---+---+---+---+---+---+---+---+
|   |   |   | b | P |   |   |   | 4
+---+---+---+---+---+---+---+---+
|   | B | N | P |   |   |   |   | 3
+---+---+---+---+---+---+---+---+
| P | P | P | B | Q |   | P | P | 2
+---+---+---+---+---+---+---+---+
| R |   |   | K |   |   |   | R | 1
+---+---+---+---+---+---+---+---+
  a   b   c   d   e   f   g   h  

Fen         : r2qk2r/1bp2pp1/p2p1n1p/1p2pP2/3bP3/1BNP4/PPPBQ1PP/R2K3R b kq - 1 12
Zobrist Key : 527159420195030830
board.ToHumanReadableString(includeFen: false, includeZobristKey: false, highlightedSquare: new Square("d2"))

+---+---+---+---+---+---+---+---+
| r |   |   | q | k |   |   | r | 8
+---+---+---+---+---+---+---+---+
|   | b | p |   |   | p | p |   | 7
+---+---+---+---+---+---+---+---+
| p |   |   | p |   | n |   | p | 6
+---+---+---+---+---+---+---+---+
|   | p |   |   | p | P |   |   | 5
+---+---+---+---+---+---+---+---+
|   |   |   | b | P |   |   |   | 4
+---+---+---+---+---+---+---+---+
|   | B | N | P |   |   |   |   | 3
+---+---+---+---+---+---+---+---+
| P | P | P |(B)| Q |   | P | P | 2
+---+---+---+---+---+---+---+---+
| R |   |   | K |   |   |   | R | 1
+---+---+---+---+---+---+---+---+
  a   b   c   d   e   f   g   h  

P.S. Not sure if there is a rule about spaces and tabs, I see them all mixed in the file! 😅

SebLague commented 11 months ago

Great idea, thanks for this! I've just added a parameter for drawing from black's perspective, and have renamed to CreateDiagram().