-
Notifications
You must be signed in to change notification settings - Fork 1
/
reversi.py
430 lines (394 loc) · 15.8 KB
/
reversi.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
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
# import the pygame module, so you can use it
import os
import pygame
import pygame.locals as pl
import pygame_menu
import pygame_textinput
from pygame_menu.locals import *
from matrix_client.client import MatrixClient, Room, User
fps = 30
clock = pygame.time.Clock()
def change_room(room, index):
pass
def set_room(room, index):
global room_id
global room_menu
global start_game
room_menu.disable()
print(str(room[0][1]))
start_game = True
room_id = room[0][1]
def event_handler(event):
if event["type"] == "reverstrix":
global state
state = event["content"]
def validate_move(board, x, y, col):
dirs = [
(-1, 1),
(0, 1),
(1, 1),
(-1, 0),
(1, 0),
(-1, -1),
(0, -1),
(1, -1)
]
if board[y][x] != -1 or not 7 >= x >= 0 or not 7 >= y >= 0:
return False
flipped = []
for dir in dirs:
go = True
temp_flip = []
pos = [x, y]
while go:
pos = list(pos)
pos[0] += dir[0]
pos[1] += dir[1]
if not 7 >= pos[0] >= 0 or not 7 >= pos[1] >= 0 or board[pos[1]][pos[0]] == -1:
go = False
elif board[pos[1]][pos[0]] != col:
temp_flip.append(pos)
else:
for flip in temp_flip:
flipped.append(flip)
go = False
if len(flipped) == 0:
return False
else:
return flipped
def update_state(state):
global room
room.send_state_event("reverstrix", state)
def draw_board(surface, size, cell_size, xi, xf, yi, yf, board):
for x in range(0, size, int(cell_size)):
x = x + xi
pygame.draw.line(surface, (0, 0, 0), (x, yi), (x, yf))
for y in range(0, size, int(cell_size)):
y = y + yi
pygame.draw.line(surface, (0, 0, 0), (xi, y), (xf, y))
for piece in state["pieces"]:
if piece["player"] == 0:
piece_color = (255, 0, 0)
else:
piece_color = (0, 0, 255)
pos = (
int(xi + piece["x"] * cell_size + (cell_size / 2)),
int(yi + piece["y"] * cell_size + (cell_size / 2))
)
board[piece["y"]][piece["x"]] = piece["player"]
pygame.draw.circle(surface, piece_color, pos, int(cell_size / 2))
def get_score(state):
scores = [0, 0]
for piece in state["pieces"]:
scores[piece["player"]] += 1
return scores
# define a main function
def main():
global client
global room_menu
global start_game
global room_id
global state
global game_running
global game_ended
global last_state_id
global room
global cursor_x
global cursor_y
start_game = False
game_running = False
game_ended = False
last_state_id = "0"
# initialize the pygame module
pygame.init()
os.environ['SDL_VIDEO_CENTERED'] = '1'
# load and set the logo
# logo = pygame.image.load("logo32x32.png")
# pygame.display.set_icon(logo)
pygame.display.set_caption("Reverstrix")
cursor_x = 0
cursor_y = 0
# create a surface on screen that has the size of 240 x 180
swidth = 960
sheight = 540
surface: pygame.Surface = pygame.display.set_mode((swidth, sheight))
logging_in = True
passwording = False
textinput = pygame_textinput.TextInput(
antialias=True,
text_color=(255, 255, 255),
cursor_color=(255, 255, 255),
font_family="Arial",
)
passwordinput = pygame_textinput.TextInput(
antialias=True,
text_color=(255, 255, 255),
cursor_color=(255, 255, 255),
font_family="Arial",
replace_with="*"
)
menu_theme = pygame_menu.themes.Theme(widget_font=pygame_menu.font.FONT_HELVETICA,
background_color=(30, 50, 107),
title_background_color=(120, 45, 30)
)
room_menu = pygame_menu.Menu("Reverstrix",
swidth,
sheight,
theme = menu_theme,
enabled=False
)
rooms = [
]
# define a variable to control the main loop
running = True
font_family = "Arial"
if not os.path.isfile(font_family):
font_family = pygame.font.match_font(font_family)
title_font = pygame.font.Font(font_family, 50)
header_font = pygame.font.Font(font_family, 35)
username = ""
password = ""
client = None
# main loop
while running:
# event handling, gets all event from the event queue
events = pygame.event.get()
if room_menu.is_enabled():
room_menu.mainloop(surface)
elif logging_in:
surface.fill((0, 0, 0))
if not textinput.update(events):
title_render = title_font.render("MATRIX LOGIN", True, (32, 32, 255))
login_prompt_render = header_font.render("Login:", True, (255, 255, 255))
surface.blit(title_render, (0, 0))
surface.blit(login_prompt_render, (0, 50))
surface.blit(textinput.get_surface(), (35, 100))
else:
username = textinput.get_text()
logging_in = False
passwording = True
elif passwording:
surface.fill((0, 0, 0))
if not passwordinput.update(events):
title_render = title_font.render("MATRIX LOGIN", True, (32, 32, 255))
login_prompt_render = header_font.render("Login:", True, (255, 255, 255))
login_username_render = header_font.render(username, True, (255, 255, 255))
password_prompt_render = header_font.render("Password", True, (255, 255, 255))
surface.blit(title_render, (0, 0))
surface.blit(login_prompt_render, (0, 50))
surface.blit(login_username_render, (35, 100))
surface.blit(password_prompt_render, (0, 150))
surface.blit(passwordinput.get_surface(), (35, 200))
else:
password = passwordinput.get_text()
passwording = False
user_details = username.split(":")
user = user_details[0].replace("@","")
domain = user_details[1]
client = MatrixClient("https://" + domain)
token = client.login(username, password, sync=True)
rooms = []
for room_id in client.rooms:
room = client.rooms[room_id]
rooms.append(
(room.display_name + " " + room_id[0:5], room_id)
)
room_menu.enable()
room_menu.add.selector("Room",
rooms,
onchange=change_room,
onreturn=set_room
)
elif start_game:
surface.fill((48, 149, 48))
room = client.rooms[room_id]
room.add_state_listener(event_handler, "reverstrix")
client.start_listener_thread()
# room_events = room.get_events()
members = room.get_joined_members()
opponent = "0"
for member in members:
member: User
if member.user_id != client.user_id:
opponent = member.user_id
states = client.api.get_room_state(room.room_id)
found = False
for room_state in states:
if room_state["type"] == "reverstrix" and room_state["content"]["status"] == "running":
found = True
last_state_id = room_state["event_id"]
state = room_state["content"]
if not found:
# Create reversetrix
state = {
"pieces": [
{
"x": 3,
"y": 3,
"player": 0
},
{
"x": 4,
"y": 3,
"player": 1
},
{
"x": 3,
"y": 4,
"player": 1
},
{
"x": 4,
"y": 4,
"player": 0
}
],
"turn": 0,
"players": [
client.user_id,
opponent
],
"status": "running"
}
room.send_state_event("reverstrix", state)
print("Started")
if state:
start_game = False
game_running = True
attempt_place = False
for event in events:
# only do something if the event is of type QUIT
if event.type == pygame.KEYDOWN:
if event.key == pl.K_UP:
cursor_y = max(cursor_y - 1, 0)
elif event.key == pl.K_DOWN:
cursor_y = min(cursor_y + 1, 7)
elif event.key == pl.K_LEFT:
cursor_x = max(cursor_x - 1, 0)
elif event.key == pl.K_RIGHT:
cursor_x = min(cursor_x + 1, 7)
elif event.key == pl.K_SPACE or event.key == pl.K_KP_ENTER:
attempt_place = True
elif event.type == pygame.QUIT:
# change the value to False, to exit the main loop
running = False
game_running = False
if game_running:
turn_mod = state["turn"] % 2
surface.fill((48, 149, 48))
board = []
for y in range(8):
board.append([-1, -1, -1, -1, -1, -1, -1, -1])
size = 500
cell_size = 500/8
xi = ((swidth - size) / 2)
xf = swidth - ((swidth - size) / 2)
yi = ((sheight - size) / 2)
yf = sheight - ((sheight - size) / 2)
draw_board(surface, size, cell_size, xi, xf, yi, yf, board)
player_turn = state["players"][turn_mod]
if turn_mod == 0:
turn_color = (255, 0, 0)
else:
turn_color = (0, 0, 255)
if player_turn == client.user_id:
turn_render = header_font.render("Your Turn", True, turn_color)
else:
turn_render = header_font.render(player_turn + "'s Turn", True, turn_color)
surface.blit(turn_render, (0, 0))
scores = get_score(state)
red_score_name = header_font.render(state["players"][0].split(":")[0], True, (255, 0, 0))
red_score = header_font.render(str(scores[0]), True, (255, 0, 0))
blue_score_name = header_font.render(state["players"][1].split(":")[0], True, (0, 0, 255))
blue_score = header_font.render(str(scores[1]), True, (0, 0, 255))
surface.blit(red_score_name, (0, 40))
surface.blit(red_score, (0, 75))
surface.blit(blue_score_name, (0, 110))
surface.blit(blue_score, (0, 145))
if player_turn == client.user_id:
available = []
for y in range(8):
for x in range(8):
result = (x, y, validate_move(board, x, y, turn_mod))
if result[2]:
available.append(result)
for available_move in available:
if turn_mod == 0:
indicator_color = (255, 0, 0)
else:
indicator_color = (0, 0, 255)
pos = (
int(xi + available_move[0] * cell_size)+5,
int(yi + available_move[1] * cell_size)+10
)
turn_render = header_font.render(str(len(available_move[2])), True, indicator_color)
surface.blit(turn_render, pos)
if len(available) == 0:
state["turn"] += 1
turn_mod = state["turn"] % 2
for y in range(8):
for x in range(8):
result = (x, y, validate_move(board, x, y, turn_mod))
if result[2]:
available.append(result)
if len(available) == 0:
state["status"] = "ended"
game_running = False
game_ended = True
update_state(state)
if attempt_place:
result = validate_move(board, cursor_x, cursor_y, turn_mod)
if result:
print("Result of move: " + str(result))
state["pieces"].append({
"x": cursor_x,
"y": cursor_y,
"player": turn_mod
})
for piece in state["pieces"]:
for taken in result:
if piece["x"] == taken[0] and piece["y"] == taken[1]:
piece["player"] = turn_mod
state["turn"] += 1
update_state(state)
indicator_xi = int(cursor_x * cell_size) + xi
indicator_yi = int(cursor_y * cell_size) + yi
indicator_xf = indicator_xi + int(cell_size)
indicator_yf = indicator_yi + int(cell_size)
highlight_col = (128, 128, 128)
if player_turn == client.user_id:
highlight_col = (255, 255, 255)
pygame.draw.line(surface, highlight_col, (indicator_xi, indicator_yi), (indicator_xf, indicator_yi))
pygame.draw.line(surface, highlight_col, (indicator_xf, indicator_yi), (indicator_xf, indicator_yf))
pygame.draw.line(surface, highlight_col, (indicator_xf, indicator_yf), (indicator_xi, indicator_yf))
pygame.draw.line(surface, highlight_col, (indicator_xi, indicator_yi), (indicator_xi, indicator_yf))
# room_events = room.get_events()
# print(str(room_events) + " //// " + str(state))
elif game_ended:
surface.fill((48, 149, 48))
board = []
for y in range(8):
board.append([-1, -1, -1, -1, -1, -1, -1, -1])
size = 500
cell_size = 500 / 8
xi = ((swidth - size) / 2)
xf = swidth - ((swidth - size) / 2)
yi = ((sheight - size) / 2)
yf = sheight - ((sheight - size) / 2)
draw_board(surface, size, cell_size, xi, xf, yi, yf, board)
scores = get_score(state)
red_score_name = header_font.render(state["players"][0].split(":")[0], True, (255, 0, 0))
red_score = header_font.render(str(scores[0]), True, (255, 0, 0))
blue_score_name = header_font.render(state["players"][1].split(":")[0], True, (0, 0, 255))
blue_score = header_font.render(str(scores[1]), True, (0, 0, 255))
surface.blit(red_score_name, (0, 40))
surface.blit(red_score, (0, 75))
surface.blit(blue_score_name, (0, 110))
surface.blit(blue_score, (0, 145))
pygame.display.update()
clock.tick(fps)
# run the main function only if this module is executed as the main script
# (if you import this as a module then nothing is executed)
if __name__ == "__main__":
# call the main function
main()