forked from KeepCoding/Connecta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinear_board.py
62 lines (47 loc) · 1.48 KB
/
linear_board.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
from list_utils import find_streak
from settings import BOARD_LENGTH, VICTORY_STRIKE
class LinearBoard():
"""
Clase que representa un tablero de una sola columna
x un jugador
o otro jugador
None un espacio vacío
"""
@classmethod
def fromList(cls, list):
board = cls()
board._column = list
return board
def __init__(self):
"""
Una lista de None
"""
self._column = [None for _ in range(BOARD_LENGTH)]
def __eq__(self, other):
if not isinstance(other, self.__class__):
return False
else:
return self._column == other._column
def __hash__(self):
return hash(self._column)
def __repr__(self):
return f'<{self.__class__}: {self._column}>'
def add(self, char):
"""
Juega en la primera posición disponible
"""
# siempre y cunado no esté lleno...
if not self.is_full():
# buscamos la primera posición libre (None)
i = self._column.index(None)
# lo sustituimos por char
self._column[i] = char
def is_full(self):
return self._column[-1] != None
def is_victory(self, char):
return find_streak(self._column, char, VICTORY_STRIKE)
def is_tie(self, char1, char2):
"""
no hay victoria ni de char1 ni de char 2
"""
return (self.is_victory('x') == False) and (self.is_victory('o') == False)