-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconnect4.py
More file actions
107 lines (88 loc) · 2.92 KB
/
connect4.py
File metadata and controls
107 lines (88 loc) · 2.92 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
class Connect4():
def __init__(self):
self.board = [[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0]]
self.xturn = True
def render(self):
chars = {0: ' ', 1: 'x', 2: 'o'}
print
if self.xturn:
print "x's turn"
else:
print "o's turn"
print
print '1 2 3 4 5 6 7'
for row in self.board:
for space in row:
print chars[space],
print
print '-------------'
def getUserInput(self):
column = raw_input('What column would you like to drop a piece into? ')
if column not in ['1','2','3','4','5','6','7']:
print 'invalid input!'
return self.getUserInput()
else:
return int(column) - 1
def get(self, i, j):
if i > 5 or i < 0 or j > 6 or j < 0:
raise IndexError('')
else:
return self.board[i][j]
def dropPiece(self, column):
pieceType = 1 if self.xturn else 2
if self.board[0][column]:
raise ValueError('That column is full! Try another column.')
for i in range(5, -1, -1):
if not self.board[i][column]:
self.board[i][column] = pieceType
return i, column
def boardIsFull(self):
for row in self.board:
for piece in row:
if piece == 0:
return False
return True
def connected_four(self,i,j):
pieceType = 1 if self.xturn else 2
for di, dj in [(1,0), (1,1), (0,1), (-1,1)]:
for offset in range(-3,1):
try:
ret = True
for k in range(4):
if self.get(di*(offset+k) + i, dj*(offset+k) + j) != pieceType:
ret = False
break
if ret:
return True
except:
pass
return False
def play(self):
print 'Welcome to Connect 4!\n'
while(True):
self.render()
if self.boardIsFull():
print 'Game Over: tie'
break
while(True):
column = self.getUserInput()
try:
i, j = self.dropPiece(column)
break
except ValueError as e:
print
print e
print
if self.connected_four(i,j):
self.render()
print 'Game Over:', ('x' if self.xturn else 'o'), 'wins'
break
self.xturn = not self.xturn
if __name__ == '__main__':
game = Connect4()
game.play()