-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboard.py
113 lines (87 loc) · 3.67 KB
/
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
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
from point import Point
class Board:
def __init__(self, boardSize):
self._boardSize = boardSize
self._data = []
self._moves = 0
line = ["+"] * self._boardSize
for i in range(self._boardSize):
self._data.append(line[:])
def __str__(self):
boardString = " 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5\n"
for i in range(self._boardSize):
if i < 9:
boardString += " "
boardString += str(i + 1) + " "
for j in range(self._boardSize):
boardString += str(self._data[i][j]) + " "
boardString += "\n"
return boardString
@staticmethod
def pieceFromPlayerCharacter(character):
if character == "b":
return "●"
if character == "w":
return "○"
@staticmethod
def playerFromPiece(piece):
if piece == "●":
return "Black"
if piece == "○":
return "White"
def makeMove(self, point, player):
if 1 <= point.getX() <= self._boardSize and 1 <= point.getY() <= self._boardSize:
if self._data[point.getX() - 1][point.getY() - 1] == "+":
if player == "b" or player == "w":
self._data[point.getX() - 1][point.getY() - 1] = self.pieceFromPlayerCharacter(player)
self._moves += 1
else:
raise ValueError("Invalid character")
else:
raise ValueError("Invalid move. The point is already used")
else:
raise ValueError("Invalid move. Wrong coordinates")
def removeMove(self, point):
self._data[point.getX() - 1][point.getY() - 1] = "+"
self._moves -= 1
def freePoints(self):
return [Point(i + 1, j + 1) for i in range(0, self._boardSize)
for j in range(0, self._boardSize) if self._data[i][j] == "+"]
def isTheGameWon(self, numberInARow):
"""horizontal check"""
for i in range(self._boardSize):
for j in range(self._boardSize - (numberInARow - 1)):
row = self._data[i][j: j + numberInARow]
if len(set(row)) == 1 and row[0] != "+":
return True, row[0]
"""vertical check"""
for i in range(self._boardSize):
fullColumn = [row[i] for row in self._data]
for j in range(len(fullColumn) - (numberInARow - 1)):
column = fullColumn[j: j + numberInARow]
if len(set(column)) == 1 and column[0] != "+":
return True, column[0]
"""diagonal check"""
"""first diagonal and its parallels"""
for i in range(self._boardSize - (numberInARow - 1)):
for j in range(self._boardSize - (numberInARow - 1)):
diagonal = [self._data[i + k][j + k] for k in range(numberInARow)]
if len(set(diagonal)) == 1 and diagonal[0] != "+":
return True, diagonal[0]
"""second diagonal and its parallels"""
for i in range(self._boardSize - (numberInARow - 1)):
for j in range(numberInARow - 1, self._boardSize):
diagonal = [self._data[i + k][j - k] for k in range(numberInARow)]
if len(set(diagonal)) == 1 and diagonal[0] != "+":
return True, diagonal[0]
return False, ""
def longestSequence(self, numberInARow):
for i in range(numberInARow, 0, -1):
if self.isTheGameWon(i)[0] is True:
return i
def getBoardSize(self):
return self._boardSize
def getMoves(self):
return self._moves
def getData(self):
return self._data