-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame copy.py
194 lines (171 loc) · 7.72 KB
/
game copy.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
from abc import ABC, abstractmethod
from copy import deepcopy
from enum import Enum
import numpy as np
# Rules on PDF and https://cdn.1j1ju.com/medias/a8/5e/26-quixo-rulebook.pdf
class Move(Enum):
'''
Selects where you want to place the taken piece. The rest of the pieces are shifted
'''
TOP = 0
BOTTOM = 1
LEFT = 2
RIGHT = 3
class Player(ABC):
def __init__(self) -> None:
'''You can change this for your player if you need to handle state/have memory'''
pass
@abstractmethod
def make_move(self, game: 'Game') -> tuple[tuple[int, int], Move]:
'''
The game accepts coordinates of the type (X, Y). X goes from left to right, while Y goes from top to bottom, as in 2D graphics.
Thus, the coordinates that this method returns shall be in the (X, Y) format.
game: the Quixo game. You can use it to override the current game with yours, but everything is evaluated by the main game
return values: this method shall return a tuple of X,Y positions and a move among TOP, BOTTOM, LEFT and RIGHT
'''
pass
class Game(object):
def __init__(self) -> None:
self._board = np.full((5, 5), -1, dtype=np.int8)
self.current_player_idx = 1
def get_board(self) -> np.ndarray:
'''
Returns the board
'''
return deepcopy(self._board)
def set_board(self, board: np.ndarray) -> None:
'''
Sets the board
'''
self._board = deepcopy(board)
def get_current_player(self) -> int:
'''
Returns the current player
'''
return deepcopy(self.current_player_idx)
def print(self):
'''Prints the board. -1 are neutral pieces, 0 are pieces of player 0, 1 pieces of player 1'''
print(self._board)
def is_game_over(self):
return self.check_winner() != -1
def check_winner(self) -> int:
'''Check the winner. Returns the player ID of the winner if any, otherwise returns -1'''
# for each row
player = self.get_current_player()
winner = -1
for x in range(self._board.shape[0]):
# if a player has completed an entire row
if self._board[x, 0] != -1 and all(self._board[x, :] == self._board[x, 0]):
# return winner is this guy
winner = self._board[x, 0]
if winner > -1 and winner != self.get_current_player():
return winner
# for each column
for y in range(self._board.shape[1]):
# if a player has completed an entire column
if self._board[0, y] != -1 and all(self._board[:, y] == self._board[0, y]):
# return the relative id
winner = self._board[0, y]
if winner > -1 and winner != self.get_current_player():
return winner
# if a player has completed the principal diagonal
if self._board[0, 0] != -1 and all(
[self._board[x, x]
for x in range(self._board.shape[0])] == self._board[0, 0]
):
# return the relative id
winner = self._board[0, 0]
if winner > -1 and winner != self.get_current_player():
return winner
# if a player has completed the secondary diagonal
if self._board[0, -1] != -1 and all(
[self._board[x, -(x + 1)]
for x in range(self._board.shape[0])] == self._board[0, -1]
):
# return the relative id
winner = self._board[0, -1]
return winner
def play(self, player1: Player, player2: Player) -> int:
'''Play the game. Returns the winning player'''
players = [player1, player2]
winner = -1
while winner < 0:
self.current_player_idx += 1
self.current_player_idx %= len(players)
ok = False
while not ok:
from_pos, slide = players[self.current_player_idx].make_move(
self)
ok = self.move(from_pos, slide, self.current_player_idx)
winner = self.check_winner()
return winner
def move(self, from_pos: tuple[int, int], slide: Move, player_id: int) -> bool:
'''Perform a move'''
if player_id not in (0, 1):
return False
prev_value = deepcopy(self._board[(from_pos[1], from_pos[0])])
acceptable = self.__take((from_pos[1], from_pos[0]), player_id)
if acceptable:
acceptable = self.__slide((from_pos[1], from_pos[0]), slide)
if not acceptable: # restore previous
self._board[(from_pos[1], from_pos[0])] = deepcopy(prev_value)
return acceptable
def __take(self, from_pos: tuple[int, int], player_id: int) -> bool:
"""Checks that {from_pos} is in the border and marks the cell with {player_id}"""
row, col = from_pos
from_border = row in (0, 4) or col in (0, 4)
if not from_border:
return False # the cell is not in the border
if self._board[from_pos] != player_id and self._board[from_pos] != -1:
return False # the cell belongs to the opponent
self._board[from_pos] = player_id
return True
@staticmethod
def acceptable_slides(from_position: tuple[int, int]):
"""When taking a piece from {from_position} returns the possible moves (slides)"""
acceptable_slides = [Move.BOTTOM, Move.TOP, Move.LEFT, Move.RIGHT]
axis_0 = from_position[0] # axis_0 = 0 means uppermost row
axis_1 = from_position[1] # axis_1 = 0 means leftmost column
if axis_0 == 0: # can't move upwards if in the top row...
acceptable_slides.remove(Move.TOP)
elif axis_0 == 4:
acceptable_slides.remove(Move.BOTTOM)
if axis_1 == 0:
acceptable_slides.remove(Move.LEFT)
elif axis_1 == 4:
acceptable_slides.remove(Move.RIGHT)
return acceptable_slides
def __slide(self, from_pos: tuple[int, int], slide: Move) -> bool:
'''Slide the other pieces'''
if slide not in self.acceptable_slides(from_pos):
return False # consider raise ValueError('Invalid argument value')
axis_0, axis_1 = from_pos
if slide == Move.RIGHT:
# pick the cube without face or with player's face and put it on the far right of the same row and push the other cubes on the left
self._board[axis_0] = np.concatenate(
(self._board[axis_0, :axis_1], self._board[axis_0,
axis_1 + 1:], self._board[axis_0, axis_1]),
axis=None,
)
elif slide == Move.LEFT:
# pick the cube without face or with player's face and put it on the far left of the same row and push the other cubes on the right
self._board[axis_0] = np.concatenate(
(self._board[axis_0, axis_1], self._board[axis_0,
:axis_1], self._board[axis_0, axis_1 + 1:]),
axis=None,
)
elif slide == Move.BOTTOM:
# pick the cube without face or with player's face and put it at the bottom on the same column and push the other cubes upwards
self._board[:, axis_1] = np.concatenate(
(self._board[:axis_0, axis_1], self._board[axis_0 +
1:, axis_1], self._board[axis_0, axis_1]),
axis=None,
)
elif slide == Move.TOP:
# pick the cube without face or with player's face and put it at the top on the same column and push the other cubes downwards
self._board[:, axis_1] = np.concatenate(
(self._board[axis_0, axis_1], self._board[:axis_0,
axis_1], self._board[axis_0 + 1:, axis_1]),
axis=None,
)
return True