forked from OpenBMB/ChatDev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
38 lines (38 loc) · 1.19 KB
/
game.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
'''
This file contains the Game class which manages the game logic.
'''
class Game:
def __init__(self):
self.board = [[0] * 15 for _ in range(15)]
self.current_player = 1
def make_move(self, x, y):
if self.board[y][x] == 0:
self.board[y][x] = self.current_player
self.current_player = 3 - self.current_player
return True
return False
def check_winner(self, x, y):
directions = [(1, 0), (0, 1), (1, 1), (1, -1)]
for dx, dy in directions:
count = 1
count += self.count_in_direction(x, y, dx, dy)
count += self.count_in_direction(x, y, -dx, -dy)
if count >= 5:
return True
return False
def count_in_direction(self, x, y, dx, dy):
count = 0
player = self.board[y][x]
while True:
x += dx
y += dy
if not (0 <= x < 15 and 0 <= y < 15):
break
if self.board[y][x] == player:
count += 1
else:
break
return count
def reset(self):
self.board = [[0] * 15 for _ in range(15)]
self.current_player = 1