-
Notifications
You must be signed in to change notification settings - Fork 0
/
connectFour.py
158 lines (136 loc) · 5.65 KB
/
connectFour.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
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
# Author: Brendan Cook
# Date: 06/08/2023
# Description: Defines the class for the connect 4 game.
class connectFour:
"""Defines a class for the game Connect 4"""
def __init__(self):
self._board = [[" "] * 7 for _ in range(5)]
self._numMoves = 0
def setBoard(self, board, numMoves):
"""Updates the board to match the other player's board."""
self._board = board
self._numMoves = numMoves
def getBoard(self):
"""Returns the board and number of moves."""
return self._board, self._numMoves
def display(self):
"""Print out the current board"""
for row in range(len(self._board)):
if row> 0:
print('|')
for space in self._board[row]:
print('|', space, end='')
print('|')
print('--------------------------')
def xMove(self):
"""Defines the characteristics of a player adding a blue peice to the board
and updates the board. Then calls display to show the new board."""
columns = [0, 1, 2, 3, 4, 5, 6]
while True:
print("Your move, enter a column number 0 through 6 > ", end="")
column = input()
if column.isdigit():
column = int(column)
elif column == '/q':
return '/q'
else:
print("Please enter a valid column number!")
continue
if column not in columns:
print("Please enter a valid column number!")
continue
else:
break
if self._board[0][column] != " ":
return "bad move"
num_rows = len(self._board)
for row in range(num_rows):
if self._board[row][column] == " " and row < num_rows - 1:
continue
elif self._board[row][column] == " " and row == num_rows - 1:
self._board[row][column] = 'X'
else:
self._board[row - 1][column] = 'X'
self._numMoves += 1
if self._numMoves > 6:
win = self.checkWin("X")
if win:
self.display()
print("X wins")
return "win"
else:
self.display()
else:
self.display()
return "continue"
def oMove(self):
"""Defines the characteristics of a player adding a red peice to the board
and updates the board. Then calls display to show the new board."""
columns = [0, 1, 2, 3, 4, 5, 6]
while True:
print("Your move, enter a column number 0 through 6 > ", end="")
column = input()
if column.isdigit():
column = int(column)
elif column == '/q':
return '/q'
else:
print("Please enter a valid column number!")
continue
if column not in columns:
print("Please enter a valid column number!")
continue
else:
break
if self._board[0][column] != " ":
return "bad move"
num_rows = len(self._board)
for row in range(num_rows):
if self._board[row][column] == " " and row < num_rows - 1:
continue
elif self._board[row][column] == " " and row == num_rows - 1:
self._board[row][column] = 'O'
else:
self._board[row - 1][column] = 'O'
self._numMoves += 1
if self._numMoves > 6:
win = self.checkWin("O")
if win:
self.display()
print("O wins")
return "win"
else:
self.display()
else:
self.display()
return "continue"
def checkWin(self, char):
"""Checks to see if a player has won after 7 moves"""
# Check rows
for row in range(len(self._board)):
for column in range(len(self._board[0]) - 3):
if self._board[row][column] == char and self._board[row][column+1] == char \
and self._board[row][column+2] == char and self._board[row][column+3] == char:
return True
# Check columns
for row in range(len(self._board) - 3):
for column in range(len(self._board[0])):
if self._board[row][column] == char and self._board[row+1][column] == char \
and self._board[row+2][column] == char and self._board[row+3][column] == char:
return True
# Check diagonals (positive slope)
for row in range(len(self._board) - 3):
for col in range(len(self._board[0]) - 3):
if self._board[row][col] == char and self._board[row+1][col+1] == char \
and self._board[row+2][col+2] == char and self._board[row+3][col+3] == char:
return True
# Check diagonals (negative slope)
for row in range(len(self._board) - 3):
for col in range(3, len(self._board[0])):
if self._board[row][col] == char and self._board[row+1][col-1] == char \
and self._board[row+2][col-2] == char and self._board[row+3][col-3] == char:
return True
def welcome(self):
"""Prints welcome message and rules"""
message = "Welcome to Connect 4. Enter the column number you would like to drop a peice into.\nThe first player to connect 4 peices in a row, column or diagonally wins! Type /q to quit. Good Luck!"
print(message)