-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboard.py
102 lines (91 loc) · 3.55 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
class Board :
# Handles the connect 4 board. The board
# is represented by a 2d list with 6 rows and 7 columns.
#
# each function is self-explanatory
BLANK = '-'
def __init__(self):
self.data = []
for i in range(6):
self.data.append([self.BLANK for x in range(7)])
self.currentHeight = [5, 5, 5, 5, 5, 5, 5]
self.previousMoves = []
def printBoard(self):
for x in self.data:
print(x)
def printBackend(self):
print("Blank character:", self.BLANK)
print("Current column heights:", self.currentHeight)
print("Move Order:", self.previousMoves)
def playMove(self, player, column):
if self.currentHeight[column] == -1:
return False
self.data[self.currentHeight[column]][column] = player
self.currentHeight[column] -= 1
self.previousMoves.append(column)
return True
def undoMove(self):
if len(self.previousMoves) == 0:
return
column = self.previousMoves.pop()
self.data[self.currentHeight[column] + 1][column] = self.BLANK
self.currentHeight[column] += 1
# Loops through every placed piece to see if there are 3 of the
# same type piece in the five directions below it.
def checkWin(self):
for col in range(7):
if self.currentHeight[col] == 5:
continue
for row in range(self.currentHeight[col] + 1, 6):
status = self.inARow(row, col)
if status != False:
return status
return False
# Checks left, right, downleft, down, and downright for a
# four in a row starting at (rowStart, colStart)
# Returns the player if someone has won and False if there isn't a
# winner yet.
def inARow(self, rowStart, colStart):
directions = ((1, 0), (-1, 0), (1, 1), (0, 1), (-1, 1))
for k, (dx, dy) in enumerate(directions):
found = True
player = self.data[rowStart][colStart]
for i in range(1, 4):
if (colStart + (dx * i) < 0 or colStart + (dx * i) > 6
or rowStart + (dy * i) < 0 or rowStart + (dy * i) > 5):
found = False
break
if (self.data[rowStart + (dy*i)][colStart + (dx*i)] != player):
found = False
break
if found:
return player
return False
def winningMove(self, column):
colStart = column
rowStart = self.currentHeight[column]
if rowStart == -1:
return False
directions = ((1, 1), (1, 0), (1, -1), (0, -1), (0, 1), (-1, 1), (-1, -1))
for dx, dy in directions:
found = True
for i in range(1, 4):
if (colStart + (dx * i) < 0 or colStart + (dx * i) > 6
or rowStart + (dy * i) < 0 or rowStart + (dy * i) > 5):
found = False
break
player = self.data[rowStart + dy][colStart + dx]
if player == self.BLANK:
found = False
break
if (self.data[rowStart + (dy*i)][colStart + (dx*i)] != player):
found = False
break
if found:
return player
return False
def isFull(self):
fullBoard = [-1, -1, -1, -1, -1, -1, -1]
return self.currentHeight == fullBoard
def copyBoard(self):
return self.data.copy()