Ejemplo n.º 1
0
class Director:
    """A code template for a person who directs the game. The responsibility of 
    this class of objects is to control the sequence of play.
    
    Stereotype:
        Controller

    Attributes:
        board (Hunter): An instance of the class of objects known as Board.
        console (Console): An instance of the class of objects known as Console.
        keep_playing (boolean): Whether or not the game can continue.
        move (Rabbit): An instance of the class of objects known as Move.
        roster (Roster): An instance of the class of objects known as Roster.
    """

    def __init__(self):
        """The class constructor.
        
        Args:
            self (Director): an instance of Director.
        """
        self._board = Board()
        self._console = Console()
        self._keep_playing = True
        self._move = None
        self._roster = Roster()
        
    def start_game(self):
        """Starts the game loop to control the sequence of play.
        
        Args:
            self (Director): an instance of Director.
        """
        self._prepare_game()
        while self._keep_playing:
            self._get_inputs()
            self._do_updates()
            self._do_outputs()

    def _prepare_game(self):
        """Prepares the game before it begins. In this case, that means getting the player names and adding them to the roster.
        
        Args:
            self (Director): An instance of Director.
        """
        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)
    
    def _get_inputs(self):
        """Gets the inputs at the beginning of each round of play. In this case,
        that means getting the move from the current player.

        Args:
            self (Director): An instance of Director.
        """
        # display the game board
        board = self._board.to_string()
        self._console.write(board)
        # get next player's move
        player = self._roster.get_current()
        self._console.write(f"{player.get_name()}'s turn:")
        pile = self._console.read_number("What pile to remove from? ")
        stones = self._console.read_number("How many stones to remove? ")
        move = Move(stones, pile)
        player.set_move(move)

    def _do_updates(self):
        """Updates the important game information for each round of play. In 
        this case, that means updating the board with the current move.

        Args:
            self (Director): An instance of Director.
        """
        player = self._roster.get_current()
        move = player.get_move()
        self._board.apply(move)
 
    def _do_outputs(self):
        """Outputs the important game information for each round of play. In 
        this case, that means checking if there are stones left and declaring the winner.

        Args:
            self (Director): An instance of Director.
        """
        if self._board.is_empty():
            winner = self._roster.get_current()
            name = winner.get_name()
            print(f"\n{name} won!")
            self._keep_playing = False
        self._roster.next_player()
Ejemplo n.º 2
0
class Director:
    """A code template for a person who directs the game. The responsibility of 
    this class of objects is to control the sequence of play.
    
    Stereotype:
        Controller

    Attributes:
        _board (Board): An instance of the class of objects known as Board.
        _console (Console): An instance of the class of objects known as Console.
        keep_playing (boolean): Whether or not the game can continue.
        code: the code for the game
        _moves (Move): Instances of the class of objects known as Move.
        _roster (Roster): An instance of the class of objects known as Roster.
    """

    def __init__(self):
        """The class constructor.
        
        Args:
            self (Director): an instance of Director.
        """
        self._board = Board()
        self._console = Console()
        self.keep_playing = True
        
        self.code = self._board.numbers_to_guess

        self._moves = [Move(self.code, '----'), Move(self.code, '----')]
        self._roster = Roster()

    def start_game(self):
        """Starts the game loop to control the sequence of play.
        
        Args:
            self (Director): an instance of Director.
        """
        self._prepare_game()
        while self.keep_playing:
            self.print_board()
            self.turn()

    def _prepare_game(self):
        """Prepares the game before it begins. In this case, that means getting the player names and adding them to the roster.
        
        Args:
            self (Director): An instance of Director.
        """
        player = [None, None]
        for n in range(2):
            name = self._console.get_name()
            player[n] = Player(name)
        self._roster.add_player(player[0], player[1])
    
    def print_board(self):
        """Outputs the important game information for each round of play.

        Args:
            self (Director): An instance of Director.
        """
        # display the game board
        board = self._board.to_string(self._roster, self._moves[0], self._moves[1])
        self._console.write(board)


    def turn(self):
        """Gets input from player, applies input, checks to see if player won, selects next player

        Args:
            self (Director): An instance of Director.
        """
        # get player's move
        player = self._roster.get_current()
        self._console.write(f"{player.get_name()}'s turn:")
        guess = self._console.read_number("What is your guess? ")
        self._moves[self._roster.current].update_guess(guess)
        player.set_move(self._moves[self._roster.current])

        # check for victory
        if str(guess) == str(self.code):
            self._console.write(f'\n{player.get_name()} won!')
            self.keep_playing = False

        # next player
        self._roster.next_player()
Ejemplo n.º 3
0
class Director:
    """A code template for a person who directs the game. The responsibility of 
    this class of objects is to control the sequence of play.
  
    Attributes:
        console (Console): An instance of the class of objects known as Console.
        roster (Roster): An instance of the class of objects known as Roster.
    """

    def __init__(self):
        """The class constructor.
        
        Args:
            self (Director): an instance of Director.
        """
        self._console = Console()
        self._keep_playing = True
        self._move = Move()
        self._roster = Roster()
        self._logic = Logic()
        
    def start_game(self):
        """Starts the game loop to control the sequence of play.
        
        Args:
            self (Director): an instance of Director.
        """
        self._get_name()
        while self._keep_playing:
            self._get_inputs()
            self._do_updates()
            self._do_outputs()
            

    def _get_name(self):
        """Prepares the game before it begins. In this case, that means getting the player names and adding them to the roster.
        
        Args:
            self (Director): An instance of Director.
        """
        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)
            
            

    
    def _get_inputs(self):
        """Gets the inputs at the beginning of each round of play. In this case,
        that means getting the move from the current player.

        Args:
            self (Director): An instance of Director.
        """
        # display the game board
        self._console._print_board(self._roster._the_roster[0],self._roster._the_roster[1])
        # get next player's move
        player = self._roster.get_current()
        self._console.write(f"{player.player_name}'s turn:")

        guess = self._console.read_number("What is your next guess? ")
        #could send to player or logic. Who controls the numbers?
        self._roster.get_current().guess = guess

    def _do_updates(self):
        """Updates the important game information for each round of play. In 
        this case, that means updating the logic/roster with the current move.

        Args:
            self (Director): An instance of Director.
        """
        player = self._roster.get_current()
        self._logic.check_number(str(player.guess), player)
        self._roster.get_current().hint = "".join(self._logic.result)
        self._move.as_string(str(self._roster.get_current().guess),str(self._roster.get_current().hint))
 
    def _do_outputs(self):
        """Outputs the important game information for each round of play. In 
        this case, that means checking if there are stones left and declaring the winner.

        Args:
            self (Director): An instance of Director.
        """
        if self._roster.get_current().win:
            winner = self._roster.get_current().player_name
            print(f"\n{winner} won!")
            self._keep_playing = False
        self._roster.next_player()
Ejemplo n.º 4
0
class Director:
    """A code template for a person who directs the game. The responsibility of 
    this class of objects is to control the sequence of play.
    
    Stereotype:
        Controller

    Attributes:
        _board (Board): An instance of the class of objects known as Board.
        _console (Console): An instance of the class of objects known as Console.
        _keep_playing (boolean): Whether or not the game can continue.
        _move (Move): An instance of the class of objects known as Move.
        _roster (Roster): An instance of the class of objects known as Roster.
    """
    def __init__(self):
        """The class constructor.
        
        Args:
            self (Director): an instance of Director.
        """
        self._board = Board()
        self._console = Console()
        self._keep_playing = True
        self._roster = Roster()
        self.num_players = 0

    def start_game(self):
        """Starts the game loop to control sequence of play.

        Args:
            self(Director): an instance of Director.
        """
        self._prepare_game()
        while self._keep_playing:
            self._get_inputs()
            self._do_updates()
            self._do_outputs()

    def _prepare_game(self):
        """Prepares game. That means getting player names and adding them
        to the roster.

        Args:
            self (Director): An instance of Director.
        """
        prompt = "How many players? (2-6): "
        self.num_players = self._console.read_number(prompt)
        while True:
            if (self.num_players > 6):
                self._console.write("Please choose less than 7 players.")
                self.num_players = self._console.read_number(prompt)
            elif (self.num_players < 2):
                self._console.write("Please choose more than 1 player.")
                self.num_players = self._console.read_number(prompt)
            else:
                break
        for i in range(self.num_players):
            p_name = self._console.read(
                f'Player {(i+1)}, please enter your name: ')
            p_code = self._board.get_code()
            player = Player(p_name, p_code)
            self._roster.add_player(player)

    def _get_inputs(self):
        """Gets the inputs at the beginning of each round of play. For this
        game, gets move from current player.

        Args:
            self(Director): An instance of Director.
        """
        player = self._roster.get_current()
        self._console.write(f"{player.get_name()}'s turn")
        prompt = "Please enter your guess (1000-9999): "
        guess = self._console.read_number(prompt)
        while True:
            if (guess < 1000):
                self._console.write("Please enter a guess over 1000.")
                guess = self._console.read_number(prompt)
            elif (guess > 9999):
                self._console.write("Please enter a guess under 9999.")
                guess = self._console.read_number(prompt)
            else:
                break
        move = Move(guess)
        player.set_move(move.get_guess())

    def _do_updates(self):
        """Updates important game information for each round of play. For
        this game, the board is updated with the current guess.

        Args:
            self(Director): An instance of Director.
        """
        player = self._roster.get_current()
        move = player.get_move()
        code = player.get_code()
        self._board.apply(move, code)
        player.set_hint(self._board.get_hint())

    def _do_outputs(self):
        """Outputs the important game information for each round of play.
        For this game, a hint is printed from the board. If the code
        matches the guess exactly, a winner is declared.

        Args:
            self(Director): An instance of Director.
        """
        if self._board.matches_code():
            winner = self._roster.get_current()
            name = winner.get_name()
            self._console.write(f'\n{name} won!')
            self._keep_playing = False
        print()
        for _ in range(self.num_players):
            self._roster.next_player()
            move = self._roster.get_current().get_move()
            hint = self._roster.get_current().get_hint()
            name = self._roster.get_current().get_name()
            text = (f"Player {name}: {move}, {hint}")
            self._console.write(text)
        self._roster.next_player()
Ejemplo n.º 5
0
class Driver:
    """A code template for a person who directs the game. The responsibility of 
    this class of objects is to control the sequence of play.
    
    Stereotype:
        Controller

    Attributes:
        _console(Console): reads and writes needed data
        _keep_playing(boolean): Determines whether to stay in the game play loop
        _roster(Roster): holds and keeps track of players
        _board(Board): Keeps track of hints and comparison - manages game-play variables
        _check(Check): Validates input and victory
        guessCounter(int): Helps determine the next player
    """
    def __init__(self):
        """The class constructor.
        
        Args:
            self (Director): an instance of Director.
        """
        self._console = Console()
        self._display = Display()
        self._keep_playing = True
        self._roster = Roster()
        self._board = Board()
        self._check = Check()
        self.guessCounter = 1

    def start_game(self):
        """Starts the game loop to control the sequence of play.
        
        Args:
            self (Director): an instance of Director.
        """
        self._prepare_game()
        while self._keep_playing:
            self._get_inputs()
            self._do_updates()
            self._do_outputs()

    def _prepare_game(self):
        """Prepares the game before it begins. In this case, that means getting the player names and adding them to the roster.
        
        Args:
            self (Director): An instance of Director.
        """

        for n in range(2):
            name = self._console.read(f"Enter a name for player {n + 1}: ")
            if n == 0:
                self._roster.player1 = name
            else:
                self._roster.player2 = name

    def _get_inputs(self):
        """Gets the inputs at the beginning of each round of play. In this case,
        that means getting the move from the current player.

        Args:
            self (Director): An instance of Director.
        """
        # display the game board
        # board = self._board.to_string(self._roster) #passes the players list to to_string
        display = self._display.displayMain(self._roster, self._board)
        self._console.write(display)

        # get next player's move
        player = self._roster.get_current()

        self._console.write(f"\n{player}'s turn:")
        guess = self._console.read("What is your guess? ")

        self._check.checkGuess(guess)

        while self._check._validGuess == False:
            self._console.write("Please only enter 4 numbers!")
            guess = self._console.read("What is your guess? ")
            self._check.checkGuess(guess)

        #insert data validation bit here **guess is a string**

        self.guessCounter = self.guessCounter + 1  #starts at 1 and goes to 2 before passing once. The % 2 of 2 is 0 so it works
        self._board._create_hint(
            guess, self.guessCounter)  # update hint and guess arrays in board
        # player.set_move(move) don't think we need this

    def _do_updates(self):
        """Updates the important game information for each round of play. In 
        this case, that means updating the board with the current move.

        Args:
            self (Director): An instance of Director.
        """
        player = self._roster.get_current()
        self._check.checkVictory(self._board)

    def _do_outputs(self):
        """Outputs the important game information for each round of play. In 
        this case, that means checking if there are stones left and declaring the winner.

        Args:
            self (Director): An instance of Director.
        """

        if self._check.player1VictoryCount == 4:
            displayWinner = self._roster.player1
        elif self._check.player2VictoryCount == 4:
            displayWinner = self._roster.player2
        else:
            displayWinner = ""

        if displayWinner == "":
            pass
        else:
            displayText = self._display.displayWinner(displayWinner)
            self._console.write(displayText)
            self._keep_playing = False
        self._roster.next_player()
Ejemplo n.º 6
0
class Director:
    """
    A code template for a person who directs the game. The responsibility of 
    this class of objects is to keep track of the score and control the 
    sequence of play.
    
    Attributes:
        _console = An instance of the Console class
        _logic = An instance of the Logic class
        _roster = An instance of the Roseter class
        _keep_playing (boolean) = Defines whether or not the game loop should continue
        _player_guess (string) = Holds the most recent player's guess
        _passcode (string) = The code the players are trying to guess. Since this is a static attribute, 
                             it is convient to define it here and use it throughout the program instead of 
                             calling the on the _logic class every time. 
    """
    def __init__(self):

        self._passcode = ''
        self._player_guess = ''
        self._keep_playing = True
        self._console = Console()
        self._roster = Roster()
        self._logic = Logic()

    def start_game(self):
        """
        Starts the game loop
        """

        self._prepare_game()
        while self._keep_playing == True:
            self._get_input()
            self._do_updates()
            self._do_output()

    def _prepare_game(self):
        """Prepares the game before it begins. 
           This entails :
                Adding each play to the board
                Creating and displaying the intial board
                Setting the passcode

        Args:
        self (Director): An instance of Director.
        """

        # A simple loop to identify each player and add them to the roster
        self._console.write("Welcome to the H.A.C.K.")
        for n in range(2):
            name = self._console.read(f"Enter a name for player {n + 1}: ")
            player = Player(name)
            if n == 0:
                self.player1 = player
            else:
                self.player2 = player
            self._roster.add_player(player)

        # Creates the board class with the two new players
        self._board = Board(self.player1, self.player2)

        #Creates the first board and displays it to the terminal
        board = self._board.create_board_string()
        self._console.write(board)

        #Uses the logic class to set the passcode that will be used for the game
        self._logic.set_passcode()

    def _get_input(self):
        """
        Asks the user for their guess each round, also switches the turns before any further actions
        """
        #Begins the turn system
        self._roster.next_player()

        #Retrieves and displays whoever's turn it is
        self.current_player = self._roster.get_current()
        self._console.write(f"{self.current_player.get_name()}'s guess:")
        Thread(target=self._console.timer_for_turn).start()
        Thread(target=self._console.read_for_turn).start()
        time.sleep(self._console._countdown)

        self._player_guess = self._console._answer

    def _do_updates(self):
        """
        An in depth "if" statement that updates key game information based on the user's input and current player

        """
        # If the player is number 1, then the board is updated according to what they entered. The same is true for player 2.
        if self._roster.current == 0:

            self.player1.set_guess(self._player_guess)

            player1_hint = self._logic.get_hint(self._logic.get_passcode(),
                                                self.player1.get_guess())
            self.player1.set_hint(player1_hint)

            board = self._board.update_board(self.player1.get_guess(),
                                             self.player1.get_hint(),
                                             self.player2.get_guess(),
                                             self.player2.get_hint())
            self._console.write(board)

        elif self._roster.current == 1:

            self.player2.set_guess(self._player_guess)

            player2_hint = self._logic.get_hint(self._logic.get_passcode(),
                                                self.player2.get_guess())
            self.player2.set_hint(player2_hint)

            board = self._board.update_board(self.player1.get_guess(),
                                             self.player1.get_hint(),
                                             self.player2.get_guess(),
                                             self.player2.get_hint())
            self._console.write(board)

    def _do_output(self):
        """
        Determines if the game will continue or end
        """

        if self._logic.is_correct(self._player_guess) == True:
            self._console.write(
                f'{self._roster.get_current().get_name()} Wins!!')
            self._console.write("""
___________________ __________    _____________________________________________________________
\__    ___/\_____  \ ______   \  /   _____/\_   _____/\_   ___ \______   \_   _____/\__    ___/
  |    |    /   |   \|     ___/  \_____  \  |    __)_ /    \  \/|       _/|    __)_   |    |   
  |    |   /    |    \    |      /        \ |        \|     \___|    |   \|        \  |    |   
  |____|   \_______  /____|     /_______  //_______  / \______  /____|_  /_______  /  |____|   
                   \/                   \/         \/         \/       \/        \/            
   _____ _____________________   _____     .____________                                       
  /  _  \ |_____   \_   _____/  /  _  \    |   ____/_   |                                      
 /  /_\  \|       _/|    __)_  /  /_\  \   |____  \ |   |                                      
/    |    \    |   \|        \/    |    \  /       \|   |                                      
\____|__  /____|_  /_______  /\____|__  / /______  /|___|                                      
        \/       \/        \/         \/         \/                   
            You're in...""")
            self._keep_playing = False

        elif self._logic.is_correct(self._player_guess) == False:
            self._keep_playing == True