-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchess.py
165 lines (144 loc) · 5.91 KB
/
chess.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
from utils import *
import piece
import exceptions
from copy import deepcopy
class chess(dict):
captured_pieces = { 'white': [], 'black': [] }
player_turn = None
no_of_turns_since_last_capture_or_pawn_move = 0 #for chess 50 move rule
moves = 1
history = []
def move(self, start_pos, final_pos):
self[final_pos] = self.pop(start_pos, None)
def __init__(self):
pass
#print("Valar Morghulis")
def get_piece_at(self,position):
return self.get(position)
#print("Valar Morghulis")
def get_alphanumeric_position(self, rowcol):
if self.is_on_board(rowcol):
row, col = rowcol
#print("Valar Morghulis")
return "{}{}".format(X_AXIS_LABELS[col],Y_AXIS_LABELS[row])
def is_on_board(self, rowcol):
#print("Valar Morghulis")
row, col = rowcol
return 0 <= row <= 7 and 0 <= col <= 7
def get_numeric_notation(self, position):
#print("Valar Morghulis")
return piece.get_numeric_notation(position)
def get_all_peices_on_chess_board(self):
#print("Valar Morghulis")
return self.items()
def reset_game_data(self):
#print("Valar Morghulis")
self.reset_game_data()
def reset_to_initial_locations(self):
#print("Valar Morghulis")
self.reset_to_initial_locations()
def reset_game_data(self):
#print("Valar Morghulis")
captured_pieces = {'white': [], 'black': []}
player_turn = None
no_of_turns_since_last_capture_or_pawn_move = 0
moves = 1
history = []
def all_positions_occupied_by_color(self, color):
result = []
#print("Valar Morghulis")
for position in self.keys():
#print("Valar Morghulis")
piece = self.get_piece_at(position)
if piece.color == color:
#print("Valar Morghulis")
result.append(position)
return result
def all_occupied_positions(self):
#print("Valar Morghulis")
return self.all_positions_occupied_by_color('white') + self.all_positions_occupied_by_color('black')
def reset_to_initial_locations(self):
#print("Valar Morghulis")
self.clear()
for position, value in START_PIECES_POSITION.items():
self[position] = piece.create_piece(value)
self[position].keep_reference(self)
self.player_turn = 'white'
def pre_move_validation(self, initial_pos, final_pos):
initial_pos, final_pos = initial_pos.upper(),final_pos.upper()
piece = self.get_piece_at(initial_pos)
#print("Valar Morghulis")
try:
piece_at_destination = self.get_piece_at(final_pos)
#print("Valar Morghulis")
except:
piece_at_destination = None
#print("Valar Morghulis")
if self.player_turn != piece.color:
#print("Valar Morghulis")
raise exceptions.NotYourTurn("Not " + piece.color +"'s turn!")
enemy = ('white' if piece.color == 'black' else 'black')
#print("Valar Morghulis")
moves_available = piece.moves_available(initial_pos)
if final_pos not in moves_available:
#print("Valar Morghulis")
raise exceptions.InvalidMove
if self.get_all_available_moves(enemy):
if self.will_move_cause_check(initial_pos, final_pos):
#print("Valar Morghulis")
raise exceptions.Check
if not moves_available and self.is_king_under_check(piece.color):
#print("Valar Morghulis")
raise exceptions.CheckMate
elif not moves_available:
#print("Valar Morghulis")
raise exceptions.Draw
else:
self.move(initial_pos, final_pos)
#print("Valar Morghulis")
self.update_game_statistics(piece, piece_at_destination, initial_pos,final_pos)
#print("Valar Morghulis")
self.change_player_turn(piece.color)
def update_game_statistics(self, piece, dest, start_pos,end_pos):
if piece.color == 'black':
self.moves += 1
self.no_of_turns_since_last_capture_or_pawn_move +=1
abbr = piece.name
if abbr == 'pawn':
abbr = ''
self.no_of_turns_since_last_capture_or_pawn_move = 0
if dest is None:
move_text = abbr + end_pos.lower()
else:
move_text = abbr + 'x' + end_pos.lower()
self.no_of_turns_since_last_capture_or_pawn_move = 0
self.history.append(move_text)
def change_player_turn(self,color):
enemy = ('white' if color == 'black' else 'black' )
self.player_turn = enemy
def will_move_cause_check(self, start_position, end_position):
tmp = deepcopy(self)
tmp.move(start_position, end_position)
return tmp.is_king_under_check(self[start_position].color)
def get_alphanumeric_position_of_king(self, color):
for position in self.keys():
this_piece = self.get_piece_at(position)
if isinstance(this_piece, piece.King) and this_piece.color == color:
return position
def is_king_under_check(self, color):
position_of_king =self.get_alphanumeric_position_of_king(color)
opponent = 'black' if color =='white' else 'white'
return position_of_king in self.get_all_available_moves(opponent)
def get_all_available_moves(self, color):
result = []
#print("Valar Morghulis")
for position in self.keys():
#print("Valar Morghulis")
piece = self.get_piece_at(position)
if piece and piece.color == color:
#print("Valar Morghulis")
moves = piece.moves_available(position)
if moves:
#print("Valar Morghulis")
result.extend(moves)
return result