-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnect4logic.py
More file actions
143 lines (119 loc) · 4.35 KB
/
connect4logic.py
File metadata and controls
143 lines (119 loc) · 4.35 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
class Board():
"""Represents a connect 4 board"""
def __init__(self):
board = []
for i in range(7):
board.append([None,None, None,None,None,None])
self._board = board
def __str__(self):
returnString = ""
for i in range(5,-1,-1):
line = ""
b = self.getBoard()
for column in b:
line += str(column[i]) + ", "
returnString += line + "\n"
return returnString
def getPiece(self, x, y):
if not self.onBoard(x,y):
raise ValueError("Given coords not on board")
return self._board[x][y]
def getBoard(self):
return self._board.copy()
def fullColumn(self, column):
"""Checks if given column is full"""
if type(column) != int:
raise TypeError("Column must be an int")
elif column > 6 or column < 0:
raise ValueError("Column must be between 0-6 inclusive")
column = self._board[column]
if column[5] == None:
return False
else:
return True
def onBoard(self, x, y=0):
"""Given x and y coord returns true if its a valid coordinate"""
if type(x) != int:
raise TypeError("x must be an int")
if type(y) != int:
raise TypeError("y must be an int")
if x < 0 or x > 6:
return False
if y < 0 or y > 5:
return False
return True
def placePiece(self, columnVal, colour):
"""Places piece on board, returns coords piece landed at"""
if type(columnVal) != int:
raise TypeError("Column must be an int")
elif not self.onBoard(columnVal):
raise ValueError("Invalid column number")
if type(colour) != str:
raise TypeError("Colour must be a string")
colour = colour.lower()
if colour != "red" and colour != "yellow":
raise ValueError("Colour must be 'red' or 'yellow'")
if self.fullColumn(columnVal):
raise RuntimeError("Tried to add piece to full column")
else:
column = self._board[columnVal]
for i in range(len(column)):
if column[i] == None:
column[i] = colour
rowVal = i
break
return (columnVal, rowVal)
def checkWin(self, coords):
"""Given x and y coord, check if it involved in a win, coord is tuple"""
x = coords[0]
y = coords[1]
if not self.onBoard(x,y):
raise ValueError("Given coord was not on board")
currentPiece = self.getPiece(x,y)
if currentPiece == None:
return False
#first value in tuple = left direction, second is right
horizontal = ((-1,0),(1,0))
diag1 = ((-1,+1),(1,-1))
vertical = ((0,1),(0,-1))
diag2 = ((1,-1),(-1,1))
directions = (horizontal, vertical, diag1, diag2)
for direction in directions:
left = direction[0]
right = direction[1]
inRow = 1
for i in range(1,4):
if left != False:
lxcoord = x+(left[0]*i)
lycoord = y+(left[1]*i)
if self.onBoard(lxcoord, lycoord):
lpiece = self.getPiece(lxcoord,lycoord)
if currentPiece == lpiece:
inRow += 1
else:
left = False
else:
left = False
if right != False:
rxcoord = x+(right[0]*i)
rycoord = y+(right[1]*i)
if self.onBoard(rxcoord,rycoord):
rpiece = self.getPiece(rxcoord,rycoord)
if currentPiece == rpiece:
inRow += 1
else:
right = False
else:
right = False
if inRow >= 4:
return True
return False
if __name__ == "__main__":
board = Board()
while True:
b = board.getBoard()
print(b)
x = input("X: ")
colour = input("Colour: ")
x, y = board.placePiece(int(x),colour)
print(board.checkWin(x,y))