Write a program to play the traditional version of Connect Four. It should take place on a 6 x 7 game board.
On each turn, the game will print out the state of the board, then alert the current player to make a move. The player's move will consist of a single number, giving the column to place a piece. If the move is invalid, the player will be given the option to select another move.
After each move is made, the game will determine if the current player has won, or if the game was a draw. To simplify the game, we will not check for diagonal wins.
The program should be built using MVC architecture.
Image Credit: Giphy
Use MVC!
class ConnectModel:
@property
def board(self):
return self.__board
@property
def current_player(self):
return self.__current_player
# more code here
class ConnectModel:
@property
def board(self):
return self.__board
@property
def current_player(self):
return self.__current_player
# more code here
def __init__(self, m, n):
if m < 4 or n < 5:
raise ValueError("The board must
be at least 4 x 5")
self.__board = []
for i in range(0, m):
self.__board.append([])
for j in range(0, n):
self.__board[i].append(0)
self.__current_player = 1
def load_game(self, board, current_player):
if len(board) < 4:
raise ValueError("Board ...")
if len(board[0]) < 5:
raise ValueError("Board ...")
if current_player != 1 and
current_player != 2:
raise ValueError("Current ...")
self.__board = board
self.__current_player = current_player
def make_move(self, y):
def make_move(self, y):
if y < 0 or y >= len(self.board[0]):
raise ValueError("Invalid column")
def make_move(self, y):
if y < 0 or y >= len(self.board[0]):
raise ValueError("Invalid column")
if self.board[0][y] != 0:
raise ValueError("Column full")
def make_move(self, y):
if y < 0 or y >= len(self.board[0]):
raise ValueError("Invalid column")
if self.board[0][y] != 0:
raise ValueError("Column full")
row = len(self.board) - 1
def make_move(self, y):
if y < 0 or y >= len(self.board[0]):
raise ValueError("Invalid column")
if self.board[0][y] != 0:
raise ValueError("Column full")
row = len(self.board) - 1
while self.board[row][y] != 0:
row -= 1
def make_move(self, y):
if y < 0 or y >= len(self.board[0]):
raise ValueError("Invalid column")
if self.board[0][y] != 0:
raise ValueError("Column full")
row = len(self.board) - 1
while self.board[row][y] != 0:
row -= 1
self.__board[row][y] = self.current_player
return True
def check_win(self):
def check_win(self):
# check rows
for i in range(0, len(self.board)):
for j in range(0, len(self.board[0])):
def check_win(self):
# check rows
for i in range(0, len(self.board)):
count = 0
for j in range(0, len(self.board[0])):
def check_win(self):
# check rows
for i in range(0, len(self.board)):
count = 0
for j in range(0, len(self.board[0])):
if self.board[i][j] == self.current_player:
count += 1
else:
count = 0
def check_win(self):
# check rows
for i in range(0, len(self.board)):
count = 0
for j in range(0, len(self.board[0])):
if self.board[i][j] == self.current_player:
count += 1
else:
count = 0
if count >= 4:
return True
def check_win(self):
# check rows
for i in range(0, len(self.board)):
count = 0
for j in range(0, len(self.board[0])):
if self.board[i][j] == self.current_player:
count += 1
else:
count = 0
if count >= 4:
return True
# check columns
for j in range(0, len(self.board[0])):
count = 0
for i in range(0, len(self.board)):
if self.board[i][j] == self.current_player:
count += 1
else:
count = 0
if count >= 4:
return True
def check_win(self):
# check rows
for i in range(0, len(self.board)):
count = 0
for j in range(0, len(self.board[0])):
if self.board[i][j] == self.current_player:
count += 1
else:
count = 0
if count >= 4:
return True
# check columns
for j in range(0, len(self.board[0])):
count = 0
for i in range(0, len(self.board)):
if self.board[i][j] == self.current_player:
count += 1
else:
count = 0
if count >= 4:
return True
return False
def check_win(self):
# check rows
for i in range(0, len(self.board)):
count = 0
for j in range(0, len(self.board[0])):
if self.board[i][j] == self.current_player:
count += 1
else:
count = 0
if count >= 4:
return True
# check columns
for j in range(0, len(self.board[0])):
count = 0
for i in range(0, len(self.board)):
if self.board[i][j] == self.current_player:
count += 1
else:
count = 0
if count >= 4:
return True
return False
def check_draw(self):
for j in range(0, len(self.board[0])):
if self.board[0][j] == 0:
return False
return True
def next_player(self):
if self.current_player == 1:
self.__current_player = 2
else:
self.__current_player = 1
import sys
class ConnectView:
def __init__(self):
# default constructor
pass
# more code here
def show_board(self, board):
for row in board:
for square in row:
print("[{}]".format(square), end="")
print("")
def show_board(self, board):
for row in board:
for square in row:
print("[{}]".format(square), end="")
print("")
[1][2][1][2][1]
[1][2][1][2][1]
[1][2][1][2][1]
[1][2][1][2][1]
[1][2][1][2][1]
def show_menu(self):
while(True):
try:
print("Which column would you like to place a token in?")
inp = sys.stdin.readline().strip()
out = int(inp)
return out
except ValueError:
print("Invalid input! Please enter a number")
except Exception:
print("Unable to read input")
return -1
def show_menu(self):
while(True):
try:
print("Which column would you like to place a token in?")
inp = sys.stdin.readline().strip()
out = int(inp)
return out
except ValueError:
print("Invalid input! Please enter a number")
except Exception:
print("Unable to read input")
return -1
def show_menu(self):
while(True):
try:
print("Which column would you like to place a token in?")
inp = sys.stdin.readline().strip()
out = int(inp)
return out
except ValueError:
print("Invalid input! Please enter a number")
except Exception:
print("Unable to read input")
return -1
def show_menu(self):
while(True):
try:
print("Which column would you like to place a token in?")
inp = sys.stdin.readline().strip()
out = int(inp)
return out
except ValueError:
print("Invalid input! Please enter a number")
except Exception:
print("Unable to read input")
return -1
def show_menu(self):
while(True):
try:
print("Which column would you like to place a token in?")
inp = sys.stdin.readline().strip()
out = int(inp)
return out
except ValueError:
print("Invalid input! Please enter a number")
except Exception:
print("Unable to read input")
return -1
def show_menu(self):
while(True):
try:
print("Which column would you like to place a token in?")
inp = sys.stdin.readline().strip()
out = int(inp)
return out
except ValueError:
print("Invalid input! Please enter a number")
except Exception:
print("Unable to read input")
return -1
def show_turn(self, player):
print("It is player {}'s turn!".format(player))
def show_end_game(self, winner):
if winner == 0:
print("The game is a draw!")
else:
print("Player {} wins!".format(winner))
def show_error(self, error):
print("Error: {}".format(error))
from ConnectModel import *
from ConnectView import *
import sys
class ConnectController:
@staticmethod
def main(args):
# main guard
if __name__ == "__main__":
ConnectController.main(sys.argv)
from ConnectModel import *
from ConnectView import *
import sys
class ConnectController:
@staticmethod
def main(args):
model = ConnectModel(6, 7)
view = ConnectView()
# main guard
if __name__ == "__main__":
ConnectController.main(sys.argv)
from ConnectModel import *
from ConnectView import *
import sys
class ConnectController:
@staticmethod
def main(args):
model = ConnectModel(6, 7)
view = ConnectView()
while True:
view.show_board(model.board)
view.show_turn(model.current_player)
try:
except Exception as e:
view.show_error(str(e))
# main guard
if __name__ == "__main__":
ConnectController.main(sys.argv)
from ConnectModel import *
from ConnectView import *
import sys
class ConnectController:
@staticmethod
def main(args):
model = ConnectModel(6, 7)
view = ConnectView()
while True:
view.show_board(model.board)
view.show_turn(model.current_player)
try:
if model.make_move(view.show_menu()):
except Exception as e:
view.show_error(str(e))
# main guard
if __name__ == "__main__":
ConnectController.main(sys.argv)
from ConnectModel import *
from ConnectView import *
import sys
class ConnectController:
@staticmethod
def main(args):
model = ConnectModel(6, 7)
view = ConnectView()
while True:
view.show_board(model.board)
view.show_turn(model.current_player)
try:
if model.make_move(view.show_menu()):
if model.check_win() or model.check_draw():
break
model.next_player()
except Exception as e:
view.show_error(str(e))
# main guard
if __name__ == "__main__":
ConnectController.main(sys.argv)
from ConnectModel import *
from ConnectView import *
import sys
class ConnectController:
@staticmethod
def main(args):
model = ConnectModel(6, 7)
view = ConnectView()
while True:
view.show_board(model.board)
view.show_turn(model.current_player)
try:
if model.make_move(view.show_menu()):
if model.check_win() or model.check_draw():
break
model.next_player()
except Exception as e:
view.show_error(str(e))
if model.check_win():
view.show_end_game(model.current_player)
else:
view.show_end_game(0)
# main guard
if __name__ == "__main__":
ConnectController.main(sys.argv)