1
+ from chessai import RandomPlayer
1
2
from chesslogic import *
2
3
import random
3
4
import argparse
@@ -40,50 +41,67 @@ def ask_promote(board, file_moves):
40
41
return CHAR_PROMOTE [choice ]
41
42
42
43
44
+ def end_game (game_status ):
45
+ return (game_status == BoardStatus .Checkmate or
46
+ game_status == BoardStatus .Stalemate
47
+ )
48
+
49
+
43
50
if __name__ == "__main__" :
44
51
parser = argparse .ArgumentParser ()
45
52
parser .add_argument ("-t" , "--test" , help = "path to test file" )
46
53
parser .add_argument ("-ai" , "--ai" , help = "play against AI: r - random" )
47
- # TODO add argument to choose color
48
- args = parser .parse_args ()
54
+ parser .add_argument ("-c" , "--color" ,
55
+ help = "color of human player: b - black, w - white"
56
+ )
49
57
50
- board = Board ()
51
58
print (INTRO )
52
59
print ("move: 'e2 e4' or 'a7 a8 Q' revert: 'r' exit: '0'" )
53
- print ("help: 'python chess.py -h'\n " )
60
+ print ("help: 'python chess.py -h'" )
61
+ args = parser .parse_args ()
62
+
63
+ board = Board ()
54
64
print (board )
55
65
56
- file_moves = []
66
+ computer_turn = False
67
+ ai_player = None
68
+ if args .ai == "r" :
69
+ # TODO change to composition
70
+ ai_player = RandomPlayer (board , False )
71
+ # if args.color == "b":
72
+ # computer_turn = True
73
+
74
+
75
+ moves_input = []
57
76
if args .test :
58
77
with open (args .test ) as f :
59
78
for line in f :
60
79
print (line )
61
- file_moves .append (line .strip ())
62
-
63
- ai_enabled = False
64
- if args .ai == "r" :
65
- ai_enabled = True
80
+ moves_input .append (line .strip ())
66
81
67
- # TODO remove this - take turn only from board itself
68
- white_turn = True
69
- computer_turn = False
70
82
# TODO refactor this
71
83
while True :
72
- if computer_turn == white_turn and ai_enabled :
73
- moves = board .generate_moves ()
74
- move = random .choice (moves )
75
- # move = minmax(depth = 5)
76
- board .move_piece (move [0 ], move [1 ], move [2 ])
77
- print ("computer's move" )
78
- print (board )
79
- white_turn = not white_turn
84
+ if ai_player and ai_player .is_white == board .white_turn :
85
+ # TODO change so AI plays and returns status and move
86
+ move = ai_player .get_ai_move ()
87
+ try :
88
+ origin , target , promotion = move
89
+ game_status = board .move_piece (origin , target , promotion )
90
+ print ("computer's move" )
91
+ print (board )
92
+ if end_game (game_status ):
93
+ break
94
+ except Exception as e :
95
+ print (e )
96
+ continue
80
97
else :
81
98
print ("enter move" )
82
- move_input = file_moves .pop (0 ) if len (file_moves ) > 0 else input ()
99
+ move_input = moves_input .pop (0 ) if len (moves_input ) > 0 else input ()
83
100
if move_input == 'r' :
84
101
try :
85
102
board .revert_last_move ()
86
- if ai_enabled :
103
+ # TODO differnetiate if black or white
104
+ if ai_player :
87
105
board .revert_last_move ()
88
106
except Exception as e :
89
107
print (e )
@@ -103,24 +121,17 @@ def ask_promote(board, file_moves):
103
121
origin = text_to_coords (start )
104
122
target = text_to_coords (end )
105
123
promotion = text_to_promotion (promote_choice )
106
- move_return = board .move_piece (origin , target , promotion )
124
+ game_status = board .move_piece (origin , target , promotion )
107
125
print (board )
108
- if (move_return == BoardStatus .Checkmate or
109
- move_return == BoardStatus .Stalemate
110
- ):
111
- break
112
- white_turn = not white_turn
126
+ if end_game (game_status ):
127
+ break
113
128
except MissingPromotionChoice as e :
114
- promotion = ask_promote (board , file_moves )
129
+ promotion = ask_promote (board , moves_input )
115
130
if promotion == None :
116
131
break
117
- move_return = board .move_piece (origin , target , promotion )
132
+ game_status = board .move_piece (origin , target , promotion )
118
133
print (board )
119
- if (move_return == BoardStatus .Checkmate or
120
- move_return == BoardStatus .Stalemate
121
- ):
122
- break
123
- white_turn = not white_turn
124
-
134
+ if end_game (game_status ):
135
+ break
125
136
except ValueError as e :
126
137
print (e )
0 commit comments