From e0903dc409ded4ab2215777d7c583d6ee646fd7d Mon Sep 17 00:00:00 2001 From: Gautham Bellamkonda <73273020+GauthamBellamkonda@users.noreply.github.com> Date: Sat, 8 Oct 2022 00:34:08 +0530 Subject: [PATCH] Replace termcolor with colored module (#40) Add new option to the CLI for checkered board Use colored module for printing the board Update README --- README.md | 8 +++++--- requirements.txt | 2 +- setup.py | 2 +- src/Board.py | 22 +++++++++++++++++----- src/main.py | 14 +++++++++++--- 5 files changed, 35 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index aa4233b..8fe4039 100644 --- a/README.md +++ b/README.md @@ -63,17 +63,19 @@ pip install . chess -h # to see all possible options ``` ``` -usage: chess [-h] [-t] [-w W] [-b B] +usage: chess [-h] [-t] [-w W] [-b B] [-c] A python program to play chess against an AI in the terminal. optional arguments: -h, --help show this help message and exit -t, --two to play a 2-player game (default: False) - -w W, --white W color for white player (default: blue) - -b B, --black B color for black player (default: red) + -w W, --white W color for white player (default: white) + -b B, --black B color for black player (default: black) + -c, --checkered use checkered theme for the chess board (default: False) Enjoy the game! + ``` ## Contributing diff --git a/requirements.txt b/requirements.txt index 0e919c6..cd8248a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,4 @@ -termcolor +colored mypy>=0.981 mypy-extensions>=0.4.3 tomli>=2.0.1 diff --git a/setup.py b/setup.py index 3021f1b..51b4d4a 100644 --- a/setup.py +++ b/setup.py @@ -15,7 +15,7 @@ ], }, install_requires=[ - 'termcolor', + 'colored', ], keywords=['chess', 'game'], classifiers=["Programming Language :: Python :: 3"], diff --git a/src/Board.py b/src/Board.py index 15031a2..4b4709d 100644 --- a/src/Board.py +++ b/src/Board.py @@ -13,10 +13,16 @@ from src.Piece import Piece from src.Queen import Queen from src.Rook import Rook +import colored +from colored import fg, bg, attr WHITE = True BLACK = False +TILES = { + 0:'#769656', + 1:'#EEEED2', +} class Board: @@ -28,8 +34,13 @@ def __init__(self, mateInOne: bool = False, castleBoard: bool = False, self.currentSide = WHITE self.movesMade = 0 self.checkmate = False - self.whiteColor = 'blue' - self.blackColor = 'red' + self.whiteColor = 'white' + self.blackColor = 'black' + self.isCheckered = False + self.tileColors = { + 0:'#769656', + 1:'#BACA44', + } if not mateInOne and not castleBoard and not passant and not promotion: self.pieces.extend([Rook(self, BLACK, C(0, 7)), @@ -201,12 +212,13 @@ def makeUnicodeStringRep(self, pieces: list[Piece]) -> str: if p.position == C(x, y): piece = p break - on_color = 'on_cyan' if y % 2 == x % 2 else 'on_yellow' - pieceRep = colored(' ', on_color=on_color) + bg_color = bg(self.tileColors[(x+y)%2]) if self.isCheckered else '' + pieceRep = bg_color + ' ' + attr(0) if piece: side = piece.side color = self.whiteColor if side == WHITE else self.blackColor - pieceRep = colored(DISPLAY_LOOKUP[piece.stringRep] + ' ', color=color, on_color=on_color) + fg_color = fg(color) + pieceRep = fg_color + bg_color + DISPLAY_LOOKUP[piece.stringRep] + ' ' + attr(0) stringRep += pieceRep stringRep += '\n' return stringRep.rstrip() diff --git a/src/main.py b/src/main.py index 44bf3fb..18c0911 100644 --- a/src/main.py +++ b/src/main.py @@ -231,7 +231,7 @@ def main() -> None: '-w', '--white', action='store', - default="blue", + default='white', metavar='W', help="color for white player" ) @@ -239,14 +239,22 @@ def main() -> None: '-b', '--black', action='store', - default="red", + default='black', metavar='B', help="color for black player" ) + parser.add_argument( + '-c', + '--checkered', + action='store_true', + default=False, + help="use checkered theme for the chess board", + ) + args = parser.parse_args() board.whiteColor = args.white board.blackColor = args.black - + board.isCheckered = args.checkered try: if args.two: twoPlayerGame(board)