-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.py
More file actions
167 lines (141 loc) · 6.63 KB
/
Copy pathGame.py
File metadata and controls
167 lines (141 loc) · 6.63 KB
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
166
167
from constants import *
import chess
import board
import neopixel
from cali_mux import find_changes, copy_board
class Game:
def __init__(self, chessboard):
self.chessboard = chessboard # updates after every legal move
self.interimboard = copy_board() # updates after pick up, put down
self.game_state = Pickup_State(True, self) # White player first
self.pixels = neopixel.NeoPixel(board.D18, 8, auto_write=False) # D16 is physical/GPIO.BOARD pin 23
self.legal_squares = []
self.from_square = -1
# find all legal moves from a given square
def find_squares(self, square):
self.legal_squares = []
for move in self.chessboard.legal_moves:
if move.from_square == square:
self.legal_squares.append(chess.square_name(move.to_square))
return self.legal_squares
# just calls the state function
def calc(self, square, incoming_board_data):
self.game_state.piece_change(square, incoming_board_data)
def show_legal_squares(self):
for square in self.legal_squares:
## convert to row and col index
ahh = chess.parse_square(square)
row = chess.square_file(ahh)
col = chess.square_rank(ahh)
# print("ahh: " + ahh)
print(f"ahh: {ahh}")
print(f"square: {square}")
print(f"row: {row}")
print(f"col {col}")
if (row == 0):
self.pixels[col] = (0, 255, 0)
print(f"self.pixels: {self.pixels}")
self.pixels.show()
# lights up all the given squares
def lightup_squares(self, squares):
for square in squares:
## convert to row and col index
ahh = chess.parse_square(square)
row = chess.square_file(ahh)
col = chess.square_rank(ahh)
# print("ahh: " + ahh)
print(f"ahh: {ahh}")
print(f"square: {square}")
print(f"row: {row}")
print(f"col {col}")
if (row == 0):
self.pixels[col] = (0, 255, 0)
print(f"self.pixels: {self.pixels}")
self.pixels.show()
# Lights up all lights with the colour red (chatgpt said that double brackets work
# i believe in it)
def error_lightup(self):
self.pixels.fill((255, 0, 0))
self.pixels.show()
# turn all lights off
def revert_lights(self):
self.pixels.fill((0, 0, 0))
self.pixels.show()
self.curr_lights = []
# just to change state of the game
def change_state(self, game_state):
self.game_state = game_state
def is_legal_square(self, square):
for sq in self.legal_squares:
if (square == sq):
return True
return False
class Game_State:
def __init__(self, colour, game):
self.colour = colour
self.game = game
def same_colour(self, square):
index = chess.parse_square(square)
colour = self.game.chessboard.color_at(index)
return colour == self.colour
def piece_change(self, changes, chessboard):
pass
class Pickup_State(Game_State):
def piece_change(self, changes, chessboard):
print(f"is colour same:{self.game.chessboard.color_at(chess.parse_square(changes[0]['square'])) == self.colour } ")
print(f"is piece removed: {changes[0]['action'] == 'removed'}")
if self.game.chessboard.color_at(chess.parse_square(changes[0]['square'])) == self.colour and changes[0]['action'] == 'removed':
squares = self.game.find_squares(chess.parse_square(changes[0]['square']))
self.game.from_square = chess.parse_square(changes[0]['square'])
self.game.lightup_squares(squares)
print(f"light up: {squares}")
self.game.interimboard = copy_board()
self.game.change_state(Putdown_State(self.colour, self.game))
else:
self.game.change_state(Error_State(self.colour, self.game, self))
self.game.error_lightup()
print(f"Panik_DOWN: {self.colour}")
print(f"piece colour: {self.game.chessboard.color_at(chess.parse_square(changes[0]['square']))}")
# print(f", Symbol: {self.game.chessboard.piece_at(chess.parse_square(changes[0]['square'])).symbol()}")
class Putdown_State(Game_State):
def __init__(self, colour, game):
super().__init__(colour, game)
self.capture = False
self.capture_square = -1
def piece_change(self, changes, chessboard):
# if change is put down, check if chosen square is valid and has not captured already
if self.game.is_legal_square(changes[0]['square']) and changes[0]['action'] == 'placed' and self.capture == False:
self.finalise_move(chess.parse_square(changes[0]['square']))
# if change is a pick up, check if it is a valid pick up
# 1. the square is part of legal_squares
# 2. the piece is of the opposite colour
elif self.game.is_legal_square(changes[0]['square']) and changes[0]['action'] == 'removed' and self.capture == False and not self.same_colour(changes[0]['square']):
self.capture = True
self.capture_square = chess.parse_square(changes[0]['square'])
# if change is put down and a piece has been captured already,
# must make sure the put down square is the same
elif self.capture_square == chess.parse_square(changes[0]['square']) and changes[0]['action'] == 'placed':
self.finalise_move(chess.parse_square(changes[0]['square']))
else:
self.game.change_state(Error_State(self.colour, self.game, self))
self.game.error_lightup()
print(f"Panik_UP: {self.colour}")
# print(f", Symbol: {self.game.chessboard.piece_at(chess.parse_square(changes[0]['square'])).symbol()}")
def finalise_move(self, to_square):
self.game.chessboard.push(chess.Move(self.game.from_square, to_square))
self.game.revert_lights()
self.game.interimboard = copy_board()
self.game.from_square = -1
self.game.change_state(Pickup_State(not self.colour, self.game))
class Error_State(Game_State):
def __init__(self, colour, game, prev_state):
super().__init__(colour, game)
self.prev_state = prev_state
def piece_change(self, changes, chessboard):
diff = find_changes(self.game.interimboard, chessboard)
if diff:
self.game.error_lightup()
else:
self.game.revert_lights()
self.game.show_legal_squares()
self.game.game_state = self.prev_state