vendredi 13 avril 2018

Questions about Python Object-Oriented Programming for Dice Game - Where do I start?

My assignment is to fill in some missing code from a dice game program my professor provided. I have some ideas as to how it might work in pseudocode and how the methods work (written as comments) but I'm unsure of how the actual syntax should look. The missing sections of code have been marked with "TODO code #". I have only included about half of the given program since the rest of the code has more to do with the look/design of the game rather than the keeping score and random value of the dice. Any help is appreciated!

# # Game Rules
# The objective is to be the first to reach 100 points. On each turn, the player will roll a die. The die value will be stored in a temporary score box (If the die value is 1, the player earns no points, and the turn goes to the next player.) Each player has an option to either roll again, or hold. If one player holds, the score in the temporary box will be added to that player's total score.
# 
# N.B. This game is designed for human players too. But for simplicity, I ignored the human part. User will only be asked to input number of computer players. And you don't have to worry about human player option. 

# In[ ]:

import random

def input_number(prompt='Please enter a number: ', minimum=0, maximum=None):
"""Read a positive number with the given prompt."""

    while True:
        try:
            number = int(input(prompt))
            if (number < minimum or (maximum is not None and number > maximum)):
                print('Number is not within range: {} to {}'.format(minimum, maximum))
            else:
                break

        except ValueError:
            print('You need to enter a number')
            continue

    return number

class RolledOneException(Exception):
    pass

class Die:
    """A die to play with."""
    '''

    Variables 
    self.value: Initialied with random value from 1 to 6.

    Methods
    roll: Returns the rolled dice, or raises RolledOneException if 1.

    '''

    def __init__(self):
        '''
        =================================
                    TODO code 1
        =================================
        '''

    def roll(self):
        """Returns the rolled dice, or raises RolledOneException if 1."""

        '''
        =================================
                    TODO code 2
        =================================
        generate random value for a dice.
        '''
        if self.value == 1:
            raise RolledOneException
        return self.value

    def __str__(self):
        return "Rolled " + str(self.value) + "."


class Box:
    """Temporary score box holder class."""
    '''
    Variables
    self.value

    '''

    def __init__(self):
        '''
        =================================
                    TODO code 3
        =================================
        '''

    def reset(self):
        self.value = 0

    def add_dice_value(self, dice_value):
        '''
        =================================
                    TODO code 4
        =================================
        adds dice value with previous value.
        '''

class Player(object):
    """Base class for different player types."""

    def __init__(self, name=None):
        self.name = name
        self.score = 0

    def add_score(self, player_score):
        """Adds player_score to total score."""
        '''
        =================================
                TODO code 5
        =================================
        adds player_score1 with previous score.
        '''

    def __str__(self):
        """Returns player name and current score."""
        return str(self.name) + ": " + str(self.score)




Aucun commentaire:

Enregistrer un commentaire