-
Notifications
You must be signed in to change notification settings - Fork 0
/
chessgame.py
54 lines (41 loc) · 1.38 KB
/
chessgame.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import os
import sys
# Ensure that we're running Python version 3
if sys.version_info[0] < 3:
raise Exception("Must be using Python 3")
from chessboard import ChessBoard
from chessboard import CBNothingToMoveE, CBSameColorE, CBInvalidMove, CBInvalidColor
commands = {"m": "move", "q": "quit"}
# TODO: fix issue with invalid color printed
def print_commands():
for command in commands.keys():
print('%s => %s' % (command, commands[command]))
def cmd_is_valid(key):
if key in commands.keys():
return 1
else:
return 0
def main():
b = ChessBoard()
while True:
os.system('cls' if os.name == 'nt' else 'clear')
print(b)
print_commands()
_c = input('next command: ')
if cmd_is_valid(_c):
if _c == 'q':
break
elif _c == 'm':
_from = input('move: ').strip()
_to = input('to: ').strip()
try:
b.move(_from, _to)
except (CBNothingToMoveE, CBSameColorE, CBInvalidMove, CBInvalidColor) as e:
input('%s, press ENTER' % e.msg)
except NameError as e:
print('Error: invalid coordinates, %s, press ENTER' % e)
input()
else:
input('received invalid command, press ENTER')
if __name__ == '__main__':
main()