Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Using Python, create the game Mastermind: Here is the code I've worked on so far (I have used images since the post is limited by
Using Python, create the game "Mastermind":
Here is the code I've worked on so far (I have used images since the post is limited by how much code is involved):
Director:
Board:
Player:
Guess:
Console:
Roster:
It needs to use at least 6 classes, and use enumeration in creating the puzzle.
Thanks in advance!
1. The code is a randomly generated, four digit number between 1000 and 9999. 2. The players take turns registering themselves by entering their name. 3. The players take turns guessing the secret code based on the hint that is offered. An x means a correct number in a correct position. An o means a correct number in an incorrect position. An *means an incorrect number (see interface section). 4. If the guess is correct, the current player wins and the game is over. 5. If the guess is incorrect, a new hint is generated and play continues. Interface Enter a name for player 1: Matt Enter a name for player 2: John **** Player Matt: Player John: **** Matt's turn: What is your guess? 1111 Player Matt: 1111, xooo Player John: **** John's turn: What is your guess? 4356 Player Matt: 1111, Xooo Player John: 4356, 00** Matt's turn: What is your guess? 1234 Matt won! > 5 board.py e director.py C:\...\mastermind game guess.py e director.py C:\...\Master... X C: > Users > philn > OneDrive > Documents > CSE 210 Programming with Classes > mastermind > Master > game > e director.py > Director > _init_ 1 from game. board import Board 2 from game.console import Console 3 from game. guess import Guess 4 from game.player import Player from game.roster import Roster 6 7 class Director: 8 """A code template for a person who directs the game. The responsibility of 9 this class of objects is to control the sequence of play. 10 11 Stereotype: 12 Controller 13 14 Attributes: 15 board (Hunter): An instance of the class of objects known as Board. 16 console (Console): An instance of the class of objects known as Console. 17 keep_playing (boolean): whether or not the game can continue. 18 guess: An instance of the class of objects known as Guess. 19 roster (Roster): An instance of the class of objects known as Roster. 20 21 22 def _init__(self): 23 "The class constructor. 24 25 Args: 26 self (Director): an instance of Director. 27 28 self. board = Board() 29 self. console = Console() 30 self._keep_playing = True 31 self._guess = None self._roster = Roster() 33 34 def start game(self): 35 ***"Starts the game loop to control the sequence of play. 36 37 Args: 38 self (Director): an instance of Director. 39 40 self._prepare_game() 41 while self. keep_playing: 42 self._get_inputs() 43 self._do_updates) 44 self._do_outputs() 45 46 def _prepare_game(self): 47 ***Prepares the game before it begins. In this case, that means getting the player names and adding them to 32 48 Args: self (Director): An instance of Director. 49 50 51 52 53 54 55 56 57 #n = input("How many players are playing? ") #n = int[n] for n in range(2) : name = self._console.read(f"Enter a name for player {n + 1}: ") player = Player(name) self._roster.add_player(player) 58 32 board.py. director.py C:\...\mastermind game guess.py director.py C:\...\Master... X C: > Users > ph1ln > OneDrive > Documents > CSE 210 Programming with Classes > mastermind > Master > game> direct #n = input( HOW many players are playing: in ) 53 #n = int[n] 54 for n in range(2): 55 name = self._console.read(f"Enter a name for player {n + 1}: ") 56 player = player(name) 57 self._roster.add_player(player) 58 59 def _get_inputs(self): 60 ***"Gets the inputs at the beginning of each round of play. In this case, 61 that means getting the move from the current player. 62 63 Args: 64 self (Director): An instance of Director. 65 66 # display the game board 67 #board = self._board.to_string() 68 #self._console.Write(board) 69 # get next player's guess 70 player = self._roster.get_current() 71 self._console.Write(f"{player.get_name()}'s turn:") 72 attempt = self._console.read_number("What is your guess? ") 73 guess = Guess(attempt) ) 74 player.set_guess(guess) 75 76 def _do_outputs(self): 77 "**"Outputs the important game information for each round of play. In 78 this case, that means checking if a guess is spot on and declaring the winner. 79 80 Args: 81 self (Director): An instance of Director.' TE IT TE 82 83 84 85 86 87 88 89 if self._board.is_empty(): winner = self._roster.get_current() name = winner.get_name() print(f" {name} won!") self._keep_playing = False self._roster.next_player() board.py e director.py C:\...\mastermind\game guess.py e director.py C:\...\Master... C: > Users > ph1ln > OneDrive > Documents > CSE 210 Programming with Classes > mastermind > game > board.py > ... 1 import random 2 3 class Board: 4 """A designated playing surface. The responsibility of Board is to keep track of the pieces in play. 5 6 Stereotype: 7 Information Holder 8 9 Attributes: 10 _piles (list): The number of piles of stones. 11 12 def _init__(self): 13 ""The class constructor. 14 15 Args: 16 self (Board): an instance of Board. 17 18 self._attempt = [] 19 self._prepare() 20 21 def encode (correct, guess): 22 23 output_arr = [''] * len(correct) 24 25 for i, (correct_num, guess_num) in enumerate(self._correct, guess)): 26 output_arr[i] = 'X' if guess_num == correct_num else 'o' if guess_num in correct else'-' 27 28 return ".join(output_arr) 29 30 def _prepare(self): 31 "*"Sets up the board with the numbers randomly generated 32 33 Args: 34 self (Board): an instance of Board. 35 36 number_of_numbers = 10 37 code_length = safe_int_input("How long is the code to be guessed? (4-10): ", 4, 10) 38 numbers = '1234567890'[:number_of_numbers] 39 code = ..join(random.choices(numbers, k=code_length)) 40 guesses = [] C: > Users > ph1ln > OneDrive > Documents > CSE 210 Programming with Classes > mastermind > Master > game > eo player.py > Player class Player: 2 ***"A person taking part in a game. The responsibility of player is to keep track of their identity and last move 1 3 4 5 Stereotype: Information Holder 6 7 8 Attributes: _name (string): The player's name. move (Move): The player's last move. 9 def _init__(self, name): ""The class constructor. Args: self (player): an instance of player. self. name = name self. move = None def get_move(self): "Returns the player's last move (an instance of Move). If the player asn't moved yet this method returns None. Args: self (player): an instance of player. 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 return self._move def get_name(self): """Returns the player's name. Args: self (Player): an instance of player. return self._name def set_move(self, move): ""Sets the player's last move to the given instance of Move. Args: self (player): an instance of player. move (Move): an instance of Move self._move = move _init_ C: > Users > ph1ln > OneDrive > Documents > CSE 210 Programming with Classes > mastermind > game > guess.py > egy Guess > 1 class Guess: 2 """A maneuver in the game. The responsibility of Guess is to keep track of the guesses. 3 4. Stereotype: 5 Information Holder 6 7 Attributes: 8 _attempt (integer): The attempt or guess from a player 9 def 10 11 _init_(self, attempt): "The class constructor. 12 13 Args: self (Board): an instance of Board. 14 15 16 self._attempt = attempt 17 18 def get_attempt(self): " "Returns the guess 19 20 21 22 23 Args: self (Guess): an instance of Guess. ERIRE 24 return self._attempt 25 roster.py e guess.py e director.py board.py e console.py X player.py e director.py C:\...\mastermind game C: > Users > ph1ln > OneDrive > Documents > CSE 210 Programming with Classes > mastermind > Master > game > e console.py > ... 1 import random 2 3 class Console: 4 **"A code template for a computer console. The responsibility of this 5 class of objects is to get text or numerical input and display text output. 6 7 Stereotype: Service Provider, Interfacer 9 Attributes: 11 prompt (string): The prompt to display on each line. 10 12 13 #def numPlayer() 14 15 16 17 18 def read(self, prompt): ***"Gets text input from the user through the screen. 19 Args: self (Screen): An instance of Screen. prompt (string): The prompt to display to the user. 20 21 22 23 24 Returns: string: The user's input as text. 25 return input(prompt) 26 27 def read_number(self, prompt): ***"Gets numerical input from the user through the screen. 28 29 30 31 Args: self (Screen): An instance of Screen. prompt (string): The prompt to display to the user. 32 33 34 35 Returns: integer: The user's input as an integer. 36 return int(input(prompt)) 37 38 39 def write(self, text): "*"Displays the given text on the screen. 40 41 42 43 Args: self (screen): An instance of Screen. text (string): The text to display. 44 45 46 print(text) e director.pl e board.py e console.py e player.py e roster.py X e guess.py e director.py C:\...\mastermind game C: > Users > ph1ln > OneDrive > Documents > CSE 210 Programming with Classes > mastermind > Master > game > roster.py > Pe Roster 17 self.current = -1 18 self.players = [] 19 20 def add_player(self, player): "**"'Adds the given player to the roster 21 22 23 24 Args: self (Roster): An instance of Roster. player (Player): The player object to add. 25 26 27 if player not in self.players: self.players.append(player) 28 29 30 def get_current(self): "" "Gets the current player object. 31 32 33 34 35 Args: self (Roster): An instance of Roster. 36 Returns: Player: The current player. 37 38 39 return self.players[self.current] 40 41 def next_player(self): ""Advances the turn to the next player. 42 43 44 Args: self (Roster): An instance of Roster. 45 46 47 self.current = (self.current + 1) % len(self.players) 1. The code is a randomly generated, four digit number between 1000 and 9999. 2. The players take turns registering themselves by entering their name. 3. The players take turns guessing the secret code based on the hint that is offered. An x means a correct number in a correct position. An o means a correct number in an incorrect position. An *means an incorrect number (see interface section). 4. If the guess is correct, the current player wins and the game is over. 5. If the guess is incorrect, a new hint is generated and play continues. Interface Enter a name for player 1: Matt Enter a name for player 2: John **** Player Matt: Player John: **** Matt's turn: What is your guess? 1111 Player Matt: 1111, xooo Player John: **** John's turn: What is your guess? 4356 Player Matt: 1111, Xooo Player John: 4356, 00** Matt's turn: What is your guess? 1234 Matt won! > 5 board.py e director.py C:\...\mastermind game guess.py e director.py C:\...\Master... X C: > Users > philn > OneDrive > Documents > CSE 210 Programming with Classes > mastermind > Master > game > e director.py > Director > _init_ 1 from game. board import Board 2 from game.console import Console 3 from game. guess import Guess 4 from game.player import Player from game.roster import Roster 6 7 class Director: 8 """A code template for a person who directs the game. The responsibility of 9 this class of objects is to control the sequence of play. 10 11 Stereotype: 12 Controller 13 14 Attributes: 15 board (Hunter): An instance of the class of objects known as Board. 16 console (Console): An instance of the class of objects known as Console. 17 keep_playing (boolean): whether or not the game can continue. 18 guess: An instance of the class of objects known as Guess. 19 roster (Roster): An instance of the class of objects known as Roster. 20 21 22 def _init__(self): 23 "The class constructor. 24 25 Args: 26 self (Director): an instance of Director. 27 28 self. board = Board() 29 self. console = Console() 30 self._keep_playing = True 31 self._guess = None self._roster = Roster() 33 34 def start game(self): 35 ***"Starts the game loop to control the sequence of play. 36 37 Args: 38 self (Director): an instance of Director. 39 40 self._prepare_game() 41 while self. keep_playing: 42 self._get_inputs() 43 self._do_updates) 44 self._do_outputs() 45 46 def _prepare_game(self): 47 ***Prepares the game before it begins. In this case, that means getting the player names and adding them to 32 48 Args: self (Director): An instance of Director. 49 50 51 52 53 54 55 56 57 #n = input("How many players are playing? ") #n = int[n] for n in range(2) : name = self._console.read(f"Enter a name for player {n + 1}: ") player = Player(name) self._roster.add_player(player) 58 32 board.py. director.py C:\...\mastermind game guess.py director.py C:\...\Master... X C: > Users > ph1ln > OneDrive > Documents > CSE 210 Programming with Classes > mastermind > Master > game> direct #n = input( HOW many players are playing: in ) 53 #n = int[n] 54 for n in range(2): 55 name = self._console.read(f"Enter a name for player {n + 1}: ") 56 player = player(name) 57 self._roster.add_player(player) 58 59 def _get_inputs(self): 60 ***"Gets the inputs at the beginning of each round of play. In this case, 61 that means getting the move from the current player. 62 63 Args: 64 self (Director): An instance of Director. 65 66 # display the game board 67 #board = self._board.to_string() 68 #self._console.Write(board) 69 # get next player's guess 70 player = self._roster.get_current() 71 self._console.Write(f"{player.get_name()}'s turn:") 72 attempt = self._console.read_number("What is your guess? ") 73 guess = Guess(attempt) ) 74 player.set_guess(guess) 75 76 def _do_outputs(self): 77 "**"Outputs the important game information for each round of play. In 78 this case, that means checking if a guess is spot on and declaring the winner. 79 80 Args: 81 self (Director): An instance of Director.' TE IT TE 82 83 84 85 86 87 88 89 if self._board.is_empty(): winner = self._roster.get_current() name = winner.get_name() print(f" {name} won!") self._keep_playing = False self._roster.next_player() board.py e director.py C:\...\mastermind\game guess.py e director.py C:\...\Master... C: > Users > ph1ln > OneDrive > Documents > CSE 210 Programming with Classes > mastermind > game > board.py > ... 1 import random 2 3 class Board: 4 """A designated playing surface. The responsibility of Board is to keep track of the pieces in play. 5 6 Stereotype: 7 Information Holder 8 9 Attributes: 10 _piles (list): The number of piles of stones. 11 12 def _init__(self): 13 ""The class constructor. 14 15 Args: 16 self (Board): an instance of Board. 17 18 self._attempt = [] 19 self._prepare() 20 21 def encode (correct, guess): 22 23 output_arr = [''] * len(correct) 24 25 for i, (correct_num, guess_num) in enumerate(self._correct, guess)): 26 output_arr[i] = 'X' if guess_num == correct_num else 'o' if guess_num in correct else'-' 27 28 return ".join(output_arr) 29 30 def _prepare(self): 31 "*"Sets up the board with the numbers randomly generated 32 33 Args: 34 self (Board): an instance of Board. 35 36 number_of_numbers = 10 37 code_length = safe_int_input("How long is the code to be guessed? (4-10): ", 4, 10) 38 numbers = '1234567890'[:number_of_numbers] 39 code = ..join(random.choices(numbers, k=code_length)) 40 guesses = [] C: > Users > ph1ln > OneDrive > Documents > CSE 210 Programming with Classes > mastermind > Master > game > eo player.py > Player class Player: 2 ***"A person taking part in a game. The responsibility of player is to keep track of their identity and last move 1 3 4 5 Stereotype: Information Holder 6 7 8 Attributes: _name (string): The player's name. move (Move): The player's last move. 9 def _init__(self, name): ""The class constructor. Args: self (player): an instance of player. self. name = name self. move = None def get_move(self): "Returns the player's last move (an instance of Move). If the player asn't moved yet this method returns None. Args: self (player): an instance of player. 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 return self._move def get_name(self): """Returns the player's name. Args: self (Player): an instance of player. return self._name def set_move(self, move): ""Sets the player's last move to the given instance of Move. Args: self (player): an instance of player. move (Move): an instance of Move self._move = move _init_ C: > Users > ph1ln > OneDrive > Documents > CSE 210 Programming with Classes > mastermind > game > guess.py > egy Guess > 1 class Guess: 2 """A maneuver in the game. The responsibility of Guess is to keep track of the guesses. 3 4. Stereotype: 5 Information Holder 6 7 Attributes: 8 _attempt (integer): The attempt or guess from a player 9 def 10 11 _init_(self, attempt): "The class constructor. 12 13 Args: self (Board): an instance of Board. 14 15 16 self._attempt = attempt 17 18 def get_attempt(self): " "Returns the guess 19 20 21 22 23 Args: self (Guess): an instance of Guess. ERIRE 24 return self._attempt 25 roster.py e guess.py e director.py board.py e console.py X player.py e director.py C:\...\mastermind game C: > Users > ph1ln > OneDrive > Documents > CSE 210 Programming with Classes > mastermind > Master > game > e console.py > ... 1 import random 2 3 class Console: 4 **"A code template for a computer console. The responsibility of this 5 class of objects is to get text or numerical input and display text output. 6 7 Stereotype: Service Provider, Interfacer 9 Attributes: 11 prompt (string): The prompt to display on each line. 10 12 13 #def numPlayer() 14 15 16 17 18 def read(self, prompt): ***"Gets text input from the user through the screen. 19 Args: self (Screen): An instance of Screen. prompt (string): The prompt to display to the user. 20 21 22 23 24 Returns: string: The user's input as text. 25 return input(prompt) 26 27 def read_number(self, prompt): ***"Gets numerical input from the user through the screen. 28 29 30 31 Args: self (Screen): An instance of Screen. prompt (string): The prompt to display to the user. 32 33 34 35 Returns: integer: The user's input as an integer. 36 return int(input(prompt)) 37 38 39 def write(self, text): "*"Displays the given text on the screen. 40 41 42 43 Args: self (screen): An instance of Screen. text (string): The text to display. 44 45 46 print(text) e director.pl e board.py e console.py e player.py e roster.py X e guess.py e director.py C:\...\mastermind game C: > Users > ph1ln > OneDrive > Documents > CSE 210 Programming with Classes > mastermind > Master > game > roster.py > Pe Roster 17 self.current = -1 18 self.players = [] 19 20 def add_player(self, player): "**"'Adds the given player to the roster 21 22 23 24 Args: self (Roster): An instance of Roster. player (Player): The player object to add. 25 26 27 if player not in self.players: self.players.append(player) 28 29 30 def get_current(self): "" "Gets the current player object. 31 32 33 34 35 Args: self (Roster): An instance of Roster. 36 Returns: Player: The current player. 37 38 39 return self.players[self.current] 40 41 def next_player(self): ""Advances the turn to the next player. 42 43 44 Args: self (Roster): An instance of Roster. 45 46 47 self.current = (self.current + 1) % len(self.players)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