forked from thepalbi/connect-4-ai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LogicalBoard.py
85 lines (72 loc) · 2.87 KB
/
LogicalBoard.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
from constants import *
class LogicalBoard:
def __init__(self, columns, rows, c, p):
self.c = c
self.p = p
self.columns = columns
self.rows = rows
self.reset()
def __getitem__(self, index):
return self.board[index]
def getLowestEmptySpace(self, column):
# Return the row number of the lowest empty row in the given column.
for y in range(self.rows - 1, -1, -1):
if self.board[column][y] == EMPTY:
return y
return -1
def isValidMove(self, column):
# if there is no more pieces to play, is not a valid move
if not self.availableMoves():
return False
# Returns True if there is an empty space in the given column.
# Otherwise returns False.
if column < 0 or column >= (self.columns) or self.board[column][0] != EMPTY:
return False
return True
def makeMove(self, player, column):
lowest = self.getLowestEmptySpace(column)
if lowest != -1:
self.board[column][lowest] = player
self.madeMoves += 1
def undoMove(self, column):
lowest = self.getLowestEmptySpace(column) + 1
if lowest < self.rows:
self.board[column][lowest] = EMPTY
self.madeMoves -= 1
def isBoardFull(self):
# Returns True if there are no empty spaces anywhere on the board.
for x in range(self.columns):
for y in range(self.rows):
if self.board[x][y] == EMPTY:
return False
return True
def availableMoves(self):
return self.madeMoves < 2 * self.p
# TODO: mirar que sea correcto el self.c - 1 en todos los ranges
def isWinner(self, tile):
# check horizontal spaces
for x in range(self.columns - self.c + 1):
for y in range(self.rows):
if all([self.board[x + i][y] == tile for i in range(self.c)]):
return True
# check vertical spaces
for x in range(self.columns):
for y in range(self.rows - self.c + 1):
if all([self.board[x][y + i] == tile for i in range(self.c)]):
return True
# check / diagonal spaces
for x in range(self.columns - self.c + 1):
for y in range(self.c - 1, self.rows):
if all([self.board[x + i][y - i] == tile for i in range(self.c)]):
return True
# check \ diagonal spaces
for x in range(self.columns - self.c + 1):
for y in range(self.rows - self.c + 1):
if all([self.board[x + i][y + i] == tile for i in range(self.c)]):
return True
return False
def reset(self):
self.madeMoves = 0
self.board = []
for x in range(self.columns):
self.board.append([EMPTY] * self.rows)