-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathConnect_Four.py
More file actions
160 lines (145 loc) · 4.28 KB
/
Copy pathConnect_Four.py
File metadata and controls
160 lines (145 loc) · 4.28 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
"""
A game of connect-4 Versus the computer
Author: Oscar A. Nieves
Updated: August 9, 2021
"""
from random import randint
# Class stack (for each column)
class Stack:
def __init__(self):
self._list = []
def __len__(self):
return len(self._list)
def push(self, element):
if len(self._list) <= 6:
self._list.append(element)
else:
return
def peek(self):
return self._list[-1]
# Initialize board (empty)
def initBoard():
# empty board
rows = ['a','b','c','d','e','f']
board = []
for i in range(0,len(rows)):
board.append([' '] * 7)
return board
# Initialize Stacks (empty
def initStacks():
S = [ Stack(), Stack(), Stack(),
Stack(), Stack(), Stack(), Stack() ]
return S
# print board
def printBoard(board):
rows = ['a','b','c','d','e','f']
top = ' 1 2 3 4 5 6 7 '
row = [[n] for n in range(0,7)]
row[0][0] = 'f | '
row[1][0] = 'e | '
row[2][0] = 'd | '
row[3][0] = 'c | '
row[4][0] = 'b | '
row[5][0] = 'a | '
print('')
print(' ' + '-'*(len(top)-3))
for j in range(0,len(rows)):
for i in range(1,8):
row[j][0] = row[j][0] + str(board[j][i-1]) + ' | '
print(row[j][0])
print(' ' + '-'*((len(row[j][0])-3)))
print(top)
print('')
# Move
def move(piece, board, Stacks, computer):
Set0 = {'1','2','3','4','5','6','7'}
if piece == computer:
pos = randint(1,7)
if len(Stacks[pos-1]) < 6:
Stacks[pos-1].push(piece)
board[6-len(Stacks[pos-1])][pos-1] = \
Stacks[pos-1].peek()
else:
move(piece, board, Stacks, computer)
else:
pos = str(input('Your move: '))
if (pos in Set0) == False:
print('Input must be integer between 1 and 7')
move(piece, board, Stacks, computer)
else:
pos = int(pos)
if len(Stacks[pos-1]) < 6:
Stacks[pos-1].push(piece)
board[6-len(Stacks[pos-1])][pos-1] = \
Stacks[pos-1].peek()
else:
print('Column full, try again...')
move(piece, board, Stacks, computer)
return board, Stacks
# Check wins
def checkWin(S,board):
game = False
# Horizontal checker
for j in range(0,6):
for i in range(3,7):
if (board[j][i]==board[j][i-1]==\
board[j][i-2]==board[j][i-3]==S):
game = True
else:
continue
# Vertical checker
for i in range(0,7):
for j in range(3,6):
if (board[j][i]==board[j-1][i]==\
board[j-2][i]==board[j-3][i]==S):
game = True
else:
continue
# Diagonal checker
for i in range(0,4):
for j in range(0,3):
if (board[j][i]==board[j+1][i+1]==\
board[j+2][i+2]==board[j+3][i+3]==S or
board[j+3][i]==board[j+2][i+1]==\
board[j+1][i+2]==board[j][i+3]==S):
game = True
else:
continue
if game == True:
print(S + ' wins!')
return game
# Main program
def main():
# Prompt player input
player1 = str( input('Choose X or O: ') )
if player1 != 'X' and player1 != 'O':
player1 = str( input('Choose X or O: ') )
if player1 == 'X':
computer1 = 'O'
else:
computer1 = 'X'
# Print board
board = initBoard()
Stacks = initStacks()
printBoard(board)
print('To play: enter an integer between 1 to 7 ' + \
'corresponding to each column in the board. ' + \
'Whoever stacks 4 pieces next to each other, ' + \
'either horizontally, vertically or diagonally wins.')
game = False
while game == False:
# X player
board, Stacks = move('X',board,Stacks,computer1)
printBoard(board)
game = checkWin('X',board)
if game == True:
break
# O player
board, Stacks = move('O',board,Stacks,computer1)
printBoard(board)
game = checkWin('O',board)
if game == True:
break
print('Good game.')
if __name__ == '__main__':
main()