-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
272 lines (205 loc) · 8.46 KB
/
main.py
File metadata and controls
272 lines (205 loc) · 8.46 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
from chess_utils import *
from teams import *
import timer
import protocol
import screen
import opening_screen
import exceptions
import bot
import os
import threading
import socket
import pygame
import random
import string
import logging
SOUNDS_PATH = 'sounds'
team_got_turn = Team(is_white_team=True)
team_doesnt_got_turn = Team(is_white_team=False)
turn_number = 0
username = "default_name"
my_socket = None
def update_screen():
while True:
pygame.display.flip()
def redraw_scoreboard():
white_team, black_team = get_teams_colors(team_got_turn, team_doesnt_got_turn)
while True:
screen.draw_scoreboard(team_got_turn, team_doesnt_got_turn)
check_timers_out_of_time(white_team, black_team)
def get_square_clicked():
mouse_pos = pygame.mouse.get_pos()
for line in screen.squares:
for square in line:
if square.rect.collidepoint(mouse_pos):
return square
def switch_turn(white_team, black_team):
global team_got_turn
global team_doesnt_got_turn
team_got_turn = team_doesnt_got_turn
if team_got_turn is white_team:
team_doesnt_got_turn = black_team
else:
team_doesnt_got_turn = white_team
timer.switch_timers(team_got_turn, team_doesnt_got_turn)
def remove_eaten_pieces(white_team, black_team):
for piece in white_team.pieces + black_team.pieces:
piece_team = white_team if piece.team.is_white_team else black_team
if piece.is_eaten:
piece_team.pieces.remove(piece)
def update_eaten_pieces(white_team: Team, black_team: Team):
for piece in white_team.pieces:
if piece.is_eaten and piece not in white_team.eaten_pieces:
white_team.eaten_pieces.append(piece)
return
for piece in black_team.pieces:
if piece.is_eaten and piece not in black_team.eaten_pieces:
black_team.eaten_pieces.append(piece)
def update_game_after_move(piece_clicked, black_team, white_team):
switch_turn(white_team, black_team)
screen.draw_board()
update_eaten_pieces(white_team, black_team)
screen.draw_eaten_pieces(white_team, black_team)
global turn_number
pygame.mixer.Sound(os.path.join(SOUNDS_PATH, 'pong.wav')).play()
logging.debug(f"piece moved is {piece_clicked}")
if is_checkmated(team_got_turn, team_doesnt_got_turn):
screen.draw_winner(team_doesnt_got_turn)
raise exceptions.Checkmated
if is_tie(team_got_turn, team_doesnt_got_turn):
screen.draw_tie()
raise exceptions.Tie
piece_clicked.move_counter += 1
remove_eaten_pieces(white_team, black_team)
turn_number += 1
logging.info(f"turn number: {turn_number}")
# For debugging
def print_board(white_team, black_team):
print(white_team)
white_team.print_pieces()
print(black_team)
black_team.print_pieces()
def check_timers_out_of_time(white_team, black_team):
try:
# If white team is out of time exception would raise and black team would win
team_won = black_team
white_team.timer.update_timer()
# If black team is out of time exception would raise and white team would win
team_won = white_team
black_team.timer.update_timer()
except exceptions.RunOutOfTime:
screen.draw_winner(team_won)
raise exceptions.GameEnd
def random_word(length):
return ''.join(random.choice(string.ascii_letters) for x in range(length))
def event_handler(event, piece_clicked, game_type):
white_team, black_team = get_teams_colors(team_got_turn, team_doesnt_got_turn)
if event.type == pygame.QUIT:
raise exceptions.UserExitGame
if event.type != pygame.MOUSEBUTTONDOWN:
return piece_clicked
# User clikced on something.
clicked_square = get_square_clicked()
if clicked_square is None:
# User click on something out of board.
return piece_clicked
if piece_clicked is None:
if clicked_square.current_piece in team_got_turn.pieces:
piece_clicked = clicked_square.current_piece
piece_clicked.color_next_step()
screen.draw_board() # Draw the colored squares.
return piece_clicked
# If user already clicked on a piece,
# we try to move the piece to the square the user clicked on.
try:
starting_square = str(piece_clicked.square.line_cord) + str(piece_clicked.square.tur_cord)
destination_square = str(clicked_square.line_cord) + str(clicked_square.tur_cord)
try_to_move(piece_clicked, clicked_square, team_got_turn, team_doesnt_got_turn)
# Move have finished successfully.
if game_type == opening_screen.ONLINE_GAME_TYPE:
move = starting_square + destination_square
request = protocol.Request(username, protocol.REGULAR_MOVE, move).set_request_to_server()
my_socket.send(request)
update_game_after_move(piece_clicked, black_team, white_team)
return None
except exceptions.MoveError:
# The move wasn't valid.
pygame.mixer.Sound(os.path.join(SOUNDS_PATH, 'error.wav')).play()
# Print all the squares in their original colors.
screen.draw_board()
return None
def game_loop(game_type, my_team, bot_depth=0):
white_team, black_team = get_teams_colors(team_got_turn, team_doesnt_got_turn)
white_team.timer.resume()
piece_clicked = None
screen.draw_bg(team_got_turn, team_doesnt_got_turn)
while True:
if team_got_turn is not my_team and game_type == opening_screen.BOT_GAME_TYPE:
# bot turn.
piece_moved = bot.move(team_doesnt_got_turn, team_got_turn, bot_depth)
update_game_after_move(piece_moved, black_team, white_team)
elif team_got_turn is not my_team and game_type == opening_screen.ONLINE_GAME_TYPE:
game_status = my_socket.recv(1)
logging.debug(f"game status: {game_status}")
if game_status == protocol.ERROR_MESSAGE:
logging.info("opponent player quit")
screen.draw_winner(my_team)
raise exceptions.GameEnd
start_square = screen.squares[int(my_socket.recv(1))][int(my_socket.recv(1))]
destination_square = screen.squares[int(my_socket.recv(1))][int(my_socket.recv(1))]
piece_moved = start_square.current_piece
piece_moved.move(destination_square)
update_game_after_move(piece_moved, black_team, white_team)
else:
for event in pygame.event.get():
piece_clicked = event_handler(event, piece_clicked, game_type)
def quit_from_sever(client_socket: socket.socket):
quit_request = protocol.Request(opening_screen.username, protocol.QUIT).set_request_to_server()
opening_screen.my_socket.send(quit_request)
client_socket.close()
def main():
global my_socket
global username
logging.basicConfig(level=logging.INFO)
try:
# Get game data from user.
# The return values is in the global vars of opening screen module.
opening_screen.starting_screen()
except exceptions.FinishStartingScreen:
game_type = opening_screen.game_type
game_length = opening_screen.game_length
bot_depth = opening_screen.level
is_player_white = opening_screen.is_white
my_socket = opening_screen.my_socket
username = opening_screen.username
except exceptions.UserExitGame:
if opening_screen.my_socket is not None:
quit_from_sever(opening_screen.my_socket)
return
timer.set_game_length(game_length)
screen.add_squares_to_board()
white_team, black_team = get_teams_colors(team_got_turn, team_doesnt_got_turn)
place_pieces(white_team, black_team)
my_team = white_team if is_player_white else black_team
screen.draw_eaten_pieces(white_team, black_team)
# Start remote threads.
scoreboard_thread = threading.Thread(target=redraw_scoreboard, daemon=True)
scoreboard_thread.start()
board_thread = threading.Thread(target=update_screen, daemon=True)
board_thread.start()
try:
game_loop(game_type, my_team, bot_depth)
except exceptions.UserExitGame:
if my_socket is not None:
# Already connected to server
quit_from_sever(my_socket)
return
except exceptions.GameEnd:
logging.info("Game ended.")
timer.sleep(10)
if my_socket is not None:
# Already connected to server
quit_from_sever(my_socket)
return
if __name__ == '__main__':
main()