Answered step by step
Verified Expert Solution
Question
1 Approved Answer
What did I do wrong with my classes? When I run the program it says that TicTacToe object has no attribute run. Here is my
What did I do wrong with my classes? When I run the program it says that TicTacToe object has no attribute run. Here is my code.
class Player: def __init__(self, name): self.name = name self.loss = 0 self.win = 0 self.draw = 0 def calculateWinPercent(self): total = self.loss + self.win + self.draw if total == 0: return 0 else: percent = round((self.win * 100) / total, 2) return percent def getName(self): return self.name def addStatistic(self, loss, win, draw): self.loss += loss self.win += win self.draw += draw def resetScore(self): self.loss = 0 self.win = 0 self.draw = 0 class TicTacToe: def __init__(self): self.board = [["_", "_", "_"], ["_", "_", "_"], ["_", "_", "_"]] self.listOfPlayers = [] def leaderboard(self): topPlayers = sorted(self.listOfPlayers, key=lambda x: x.calculateWinPercent(), reverse=True) if len(topPlayers) > 10: for i in topPlayers[:10]: print("Player name : ", i.name, "\tPlayer win percentage : ", i.calculateWinPercent()) else: for i in topPlayers: print("Player name : ", i.name, "\tPlayer win percentage : ", i.calculateWinPercent()) def displayBoard(self): for i in range(len(self.board)): print(" ") for j in range(len(self.board)): print(self.board[j][-i-1], end=" ") def isGameComplete(self): for i in self.board: for j in i: if j == "_": return False return True def addScore(self, playerName, lose, won, draw): for i in self.listOfPlayers: if i.getName() == playerName: i.addStatistic(lose, won, draw) return def isWin(self, symbol): for i in range(len(self.board)): isSame = True for j in range(len(self.board)): if self.board[i][j] != symbol: isSame = False break if isSame == True: print("Won by the row") return True for i in range(len(self.board)): isSame = True for j in range(len(self.board)): if self.board[j][i] != symbol: isSame = False break if isSame == True: print("Won by the column") return True for i in range(len(self.board)): isSame = True if self.board[i][i] != symbol: isSame = False break if isSame == True: print("Won by the main diagonal") return True for i in range(len(self.board)): isSame = True if self.board[i][(len(self.board) - i) - 1] != symbol: isSame = False break if isSame == True: print("Won by the second diagonal") return True class Swap: def swapPlayer(self, currentPlayer, player1, player2): if currentPlayer == player1: currentPlayer = player2 symbol = "X" else: currentPlayer = player1 symbol = "O" return currentPlayer, symbol class Start: def run(self): game = self namesOfPlayers = [] while True: print(" ---------------------------------------------") print(" MENU : Please select one of the following ") print("---------------------------------------------") print("0. Exit program") print("1. Play new game") print("2. Change size of board") print("3. Add new player") print("4. Display leaderboard") print("5. Print rules & instructions") print("6. Reset all player scores") option = int(input(">>>> ")) if option == 0: break elif option == 1: player1 = input("Enter the name of first player: ") if player1 not in namesOfPlayers: print("Invalid player. Add the player first then start the game.") continue player2 = input("Enter the name of second player: ") if player2 not in namesOfPlayers: print("Invalid player. Add the player first then start the game.") continue if player1 == player2: print("Player 1 and player 2 cannot be the same.") continue currentPlayer = player2 symbol = "X" samePlayer = False game.displayBoard() while not game.isGameComplete(): if not samePlayer: currentPlayer, symbol = game.swapPlayer(currentPlayer, player1, player2) samePlayer = False message = " " + currentPlayer + " can move: " move = input(message).split(" ") i = ord(move[0].lower()) - ord("a") j = int(move[1]) - 1 if i < len(game.board) and j >= 0 and j < len(game.board): if game.board[i][j] != "_": print("Invalid entry. Try again") samePlayer = True continue else: game.board[i][j] = symbol if game.isWin(symbol): print("Congrats!", currentPlayer, "you won!") game.displayBoard() game.board = [["_", "_", "_"], ["_", "_", "_"], ["_", "_", "_"]] if currentPlayer == player1: game.addScore(player1, 0, 1, 0) game.addScore(player2, 1, 0, 0) elif currentPlayer == player2: game.addScore(player1, 1, 0, 0) game.addScore(player2, 0, 1, 0) break elif game.isGameComplete(): game.addScore(player1, 0, 0, 1) game.addScore(player2, 0, 0, 1) else: game.displayBoard() else: print("Invalid entry. try again") samePlayer = True continue elif option == 2: game.board = [] n = int(input("Enter the new size: ")) for i in range(n): li = [] for j in range(n): li.append("_") game.board.append(li) elif option == 3: name = input("Enter the name of the new player, must be at least two characters: ") if len(name) < 2: print("Error. Needs more than one character. ") continue if name not in namesOfPlayers: game.listOfPlayers.append(Player(name)) namesOfPlayers.append(name) print("Successfully added the player") else: print("player already exists") elif option == 4: game.leaderboard() elif option == 5: print("Instructions: ") print("First create new players. Start game. Enter player names. To make a move, " "first enter the letter of the row, then space, and then the number of the column." " The board is as follows. Rows start from lowercase a. Columns start from 1.") elif option == 6: for player in game.listOfPlayers: player.resetScore() print(" Thank you for playing") ttt = TicTacToe() ttt.run()
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started