Skip to content

Commit

Permalink
Replace termcolor with colored module (#40)
Browse files Browse the repository at this point in the history
Add new option to the CLI for checkered board
Use colored module for printing the board
Update README
  • Loading branch information
GauthamBellamkonda authored Oct 7, 2022
1 parent cc1b5b4 commit e0903dc
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 13 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
termcolor
colored
mypy>=0.981
mypy-extensions>=0.4.3
tomli>=2.0.1
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
],
},
install_requires=[
'termcolor',
'colored',
],
keywords=['chess', 'game'],
classifiers=["Programming Language :: Python :: 3"],
Expand Down
22 changes: 17 additions & 5 deletions src/Board.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -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)),
Expand Down Expand Up @@ -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()
Expand Down
14 changes: 11 additions & 3 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,22 +231,30 @@ def main() -> None:
'-w',
'--white',
action='store',
default="blue",
default='white',
metavar='W',
help="color for white player"
)
parser.add_argument(
'-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)
Expand Down

0 comments on commit e0903dc

Please sign in to comment.