python-chess icon indicating copy to clipboard operation
python-chess copied to clipboard

Feature/grid labels

Open R3dan opened this issue 1 year ago • 3 comments

Grid Labels

Explanation:

This push request adds labels to the __str__ method of chess.Board as mentioned in #971. You can set the label when calling __init__() of chess.Board or __call__() of chess.BaseBoard. By default, labels are displayed on the bottom and left. You can display them with either a list, dictionary or bool:

  • list: enter the first letter of the side (l, b, r, u) of the sides you want to display on
  • dictionary: for every side (as key) enter a bool: True = display and False = don't display
  • bool: True: Display on default sides (bottom and left), False : Don't display any

Examples

Currently:

r n b q k b n r
p p p p p p p p
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
P P P P P P P P
R N B Q K B N R

Default:

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

All sides:

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

R3dan avatar Dec 31 '23 17:12 R3dan

Also if anyone knows what's generating these errors (in my code) I will happily fix, but 5 of these are http requests not found. And the others are "errors" in the code but I don't know where and have tried and tested it.

R3dan avatar Dec 31 '23 18:12 R3dan

Fixed the orders of the numbers

See commit

R3dan avatar Dec 31 '23 19:12 R3dan

Add str options

This allows the program (running the game) to look at the game without the grids. You have to call __str__ and disable them:

board = chess.Board
#no grid labels
print(board.__str__(False)
#with grid labels set earlier
print(board)

Legal move list

I also added __getitem__ to LegalMoveGenerator which turns a list of legal moves (similar to __repr__)

See commit

R3dan avatar Jan 01 '24 16:01 R3dan

Sorry to reject this. In the current design, chess.Board instances should carry no more information than is required to describe the position and its history (in particular no orientation and formatting information, so that __eq__ makes sense).

Even as a normal methods, ASCII/Unicode formatting tends to approach a massively configurable mess, because there a so many possible ways to do it, some of which existed in the history of this library. By now I think it's probably best to provide something very simple, and leave customization as an exercise to the reader.

niklasf avatar Apr 18 '24 15:04 niklasf