Skip to content

Commit 11f2880

Browse files
committed
Add command line support for the unicode console GUI
1 parent 4f17241 commit 11f2880

File tree

2 files changed

+81
-16
lines changed

2 files changed

+81
-16
lines changed

chesslib/gui_console.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# -*- encoding: utf-8 -*-
2+
import board
3+
import os
4+
5+
UNICODE_PIECES = {
6+
'r': u'♜', 'n': u'♞', 'b': u'♝', 'q': u'♛',
7+
'k': u'♚', 'p': u'♟', 'R': u'♖', 'N': u'♘',
8+
'B': u'♗', 'Q': u'♕', 'K': u'♔', 'P': u'♙',
9+
None: ' '
10+
}
11+
12+
class BoardGuiConsole(object):
13+
'''
14+
Print a text-mode chessboard using the unicode chess pieces
15+
'''
16+
error = ''
17+
18+
def __init__(self, chessboard):
19+
self.board = chessboard
20+
21+
def move(self):
22+
os.system("clear")
23+
self.unicode_representation()
24+
print "\n", self.error
25+
print "State a move in chess notation (e.g. A2A3). Type \"exit\" to leave:\n", ">>>",
26+
self.error = ''
27+
coord = raw_input()
28+
if coord == "exit":
29+
print "Bye."
30+
exit(0)
31+
try:
32+
if len(coord) != 4: raise board.InvalidCoord
33+
self.board.move(coord[0:2], coord[2:4])
34+
os.system("clear")
35+
except board.ChessError as error:
36+
self.error = "Error: %s" % error.__class__.__name__
37+
38+
self.move()
39+
40+
def unicode_representation(self):
41+
print "\n", ("%s's turn\n" % self.board.player_turn.capitalize()).center(28)
42+
for number in self.board.axis_x[::-1]:
43+
print " " + str(number) + " ",
44+
for letter in self.board.axis_y:
45+
piece = self.board[letter+str(number)]
46+
if piece is not None:
47+
print UNICODE_PIECES[piece.abbriviation] + ' ',
48+
else: print ' ',
49+
print "\n"
50+
print " " + " ".join(self.board.axis_y)
51+
52+
53+
def display(board):
54+
try:
55+
gui = BoardGuiConsole(board)
56+
gui.move()
57+
except (KeyboardInterrupt, EOFError):
58+
os.system("clear")
59+
exit(0)
60+

game.py

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,32 @@
11
#!/usr/bin/env python
22

33
from chesslib import board
4-
from chesslib import boardgui
4+
55
import os
6+
import sys
7+
8+
# Load a save if it exists
69

710
if os.path.exists("state.fen"):
811
with open("state.fen") as save:
9-
b = board.Board(save.read())
12+
game = board.Board(save.read())
1013
else:
11-
b = board.Board()
12-
boardgui.display(b)
14+
game = board.Board()
1315

14-
## Text Mode
16+
# Choose display method
17+
if len(sys.argv) > 1:
18+
if sys.argv[1] in ('--console', '-c'):
19+
from chesslib.gui_console import display
20+
display(game)
21+
exit(0)
22+
elif sys.argv[1] in ('--help', '-h'):
23+
print '''Usage: game.py [OPTION]\n\n\tPlay a game of chess\n\n\tOptions:\n\t -c, --console\tplay in console mode\n\n'''
24+
exit(0)
1525

16-
#os.system("clear")
17-
#board.init()
18-
#board.unicode_representation()
19-
20-
def move(coord):
21-
board.move(coord[0:2], coord[2:4])
22-
os.system("clear")
23-
board.unicode_representation()
26+
try:
27+
from chesslib.gui_tkinter import display
28+
except ImportError:
29+
from chesslib.gui_console import display
30+
else:
31+
display(game)
2432

25-
def render():
26-
os.system("clear")
27-
board.unicode_representation()

0 commit comments

Comments
 (0)