class Board(): def display(self): for i in range(self.__rows): for x in self.__board[i]: print('|' + str(x), end='') print('|') def __checkVertical(self): for c in range(self.__columns): for r in range(self.__rows - 3): if self.__board[r][c] != 0 and self.__board[r][c] == self.__board[r+1][c] and self.__board[r+1][c] == self.__board[r+2][c] and self.__board[r+2][c] == self.__board[r+3][c]: return self.__board[r][c] return 0 def __checkHorizontal(self): for r in range(self.__rows): for c in range(self.__columns - 3): if self.__board[r][c] != 0 and self.__board[r][c] == self.__board[r][c+1] and self.__board[r][c+1] == self.__board[r][c+2] and self.__board[r][c+2] == self.__board[r][c+3]: return self.__board[r][c] return 0 def __checkRightDiagonal(self): for r in range(self.__rows - 3): for c in range(self.__columns - 3): if self.__board[r][c] != 0 and self.__board[r][c] == self.__board[r+1][c+1] and self.__board[r+1][c+1] == self.__board[r+2][c+2] and self.__board[r+2][c+2] == self.__board[r+3][c+3]: return self.__board[r][c] return 0 def __checkLeftDiagonal(self): for r in range(self.__rows - 3): for c in range(self.__columns - 3): if self.__board[r][(self.__columns - 1) - c] != 0 and self.__board[r][(self.__columns - 1) - c] == self.__board[r+1][(self.__columns - 2) - c] and self.__board[r+1][(self.__columns - 2) - c] == self.__board[r+2][(self.__columns - 3) - c] and self.__board[r+2][(self.__columns - 3) - c] == self.__board[r+3][(self.__columns - 4) - c]: return self.__board[r][(self.__columns - 1) - c] return 0 class Player(): def main(): widthSet = False heightSet = False while not widthSet: try: width = int(input("Enter the width of your game board (4-10):")) if width >= 4 and width <= 10: widthSet = True else: print("The width must be an integer from 4-10. Please try again.") except: print("The width must be an integer from 4-10. Please try again.") while not heightSet: try: height = int(input("Enter the height of your game board (4-10):")) if height >= 4 and height <= 10: heightSet = True else: print("The height must be an integer from 4-10. Please try again.") except: print("The height must be an integer from 4-10. Please try again.") board = Board(width, height) allPlayersAdded = False players = [] while not allPlayersAdded: validName = False while not validName: name = input("Enter this player's name:") validName = True for player in players: if player.getName() == name: validName = False print("There is already a player called " + name + ". Please try again.") if name == "Nobody": validName = False print("You cannot use the name \"Nobody\". Please try again.") newPlayer = Player(name, len(players) + 1) players.append(newPlayer) print("Player " + str(newPlayer.getNumber()) + " is: " + name) if len(players) > 1 and len(players) < 4: addMore = input("Keep adding players (y/n)") if addMore.lower() != "y" and addMore.lower() != "yes": allPlayersAdded = True elif len(players) == 4: allPlayersAdded = True print() print("This game has " + str(len(players)) + " players:") for player in players: print(player.getName()) gameOver = False winner = "Nobody" while not gameOver: for player in players: print() print(player.getName() + ", it's your turn:") board.display() player.makeMove(board) winner = player.checkWinner(board) if winner != "Nobody" or board.boardFull(): gameOver = True break board.display() input(winner + " wins the game!") if __name__ == '__main__': main()