-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBoard.py
More file actions
115 lines (107 loc) · 3.11 KB
/
Board.py
File metadata and controls
115 lines (107 loc) · 3.11 KB
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
class Board:
def __init__(self, boardClone = None):
self._board = [[" " for x in range(0,6)] for x in range(0,7)]
if (boardClone != None):
for x in range(0,7):
for y in range(0,6):
self._board[x][y] = boardClone.GetSpace(x,y)
def GetSpace(self, x, y):
return self._board[x][y]
def DropToken(self, column, token):
if (self._board[column][0] != " "): # If column is full
return False
lowest = 0
for y in range(1,6):
if (self._board[column][y] == " "):
lowest = y
self._board[column][lowest] = token
return True
def IsFull(self):
isfull = True
for x in range(0,7):
for y in range(0,6):
if (self._board[x][y] == " "):
isfull = False
break
return isfull
def GetWinner(self):
# Check columns
for x in range(0,7):
lastFound = ""
foundLength = 1
for y in range(0,6):
if (self._board[x][y] == " "):
lastFound = ""
foundLength = 1
continue
if (self._board[x][y] == lastFound):
foundLength += 1
if (foundLength == 4):
return lastFound
else:
lastFound = ""
foundLength = 1
lastFound = self._board[x][y]
# Check rows
for y in range(0,6):
lastFound = ""
foundLength = 1
for x in range(0,7):
if (self._board[x][y] == " "):
lastFound = ""
foundLength = 1
continue
if (self._board[x][y] == lastFound):
foundLength += 1
if (foundLength == 4):
return lastFound
else:
lastFound = ""
foundLength = 1
lastFound = self._board[x][y]
# Check \ diagonals
for xStart in range(0,4):
for yStart in range(0,3):
lastFound = ""
foundLength = 1
y = yStart - 1
for x in range(xStart, 7):
y += 1
if (y == 6):
break
if (self._board[x][y] == " "):
lastFound = ""
foundLength = 1
continue
if (self._board[x][y] == lastFound):
foundLength += 1
if (foundLength == 4):
return lastFound
else:
lastFound = ""
foundLength = 1
lastFound = self._board[x][y]
# Check / diagonals
for xStart in range(4,7):
for yStart in range(0,3):
lastFound = ""
foundLength = 1
y = yStart - 1
for x in range(xStart, 0, -1):
y += 1
if (y == 6):
break
if (self._board[x][y] == " "):
lastFound = ""
foundLength = 1
continue
if (self._board[x][y] == lastFound):
foundLength += 1
if (foundLength == 4):
return lastFound
else:
lastFound = ""
foundLength = 1
lastFound = self._board[x][y]
# If no matches found, return ""
return ""