PyTFall > PyTFall: Bugs and Game balancing

Code review

<< < (2/8) > >>

rudistoned:
You are correct. Number should be anything between 1 and 100. That way, 0 will be False, 1 will have 1% chance for True, 99 will have 99% for True and 100 will always be True.

I would prefer to write the function this way:

--- Code: ---def throw_dice(value, limit=100, show=True):
        '''Returns True if value is <= a random integer between 1 and limit.

        If limit is 100, this means that a value of 60 has a 60% chance for True
        '''
        number = random.randint(1, limit)
        if number <= value:
            result = True
        else:
            result = False
        if config.developer and show:
            notify(u"Resulted in %d from %d, limit - %d, and result - %s." % (number, limit, value, result))
        return result

--- End code ---
randint is simpler than randrange and does exactly what we want. limit is a better keyword than max because it does not overwrite a stdlib function name. rolldice or roll_dice are better function names as those names represent the activity of the function better. Documentation on how to use the function should be in the docstring rather than in comments.

Xela:

--- Quote from: rudistoned on June 23, 2013, 04:15:57 AM ---You are correct. Number should be anything between 1 and 100. That way, 0 will be False, 1 will have 1% chance for True, 99 will have 99% for True and 100 will always be True.

I would prefer to write the function this way:

--- Code: ---def throw_dice(value, limit=100, show=True):
        '''Returns True if value is <= a random integer between 1 and limit.

        If limit is 100, this means that a value of 60 has a 60% chance for True
        '''
        number = random.randint(1, limit)
        if number <= value:
            result = True
        else:
            result = False
        if config.developer and show:
            notify(u"Resulted in %d from %d, limit - %d, and result - %s." % (number, limit, value, result))
        return result

--- End code ---
randint is simpler than randrange and does exactly what we want. limit is a better keyword than max because it does not overwrite a stdlib function name. rolldice or roll_dice are better function names as those names represent the activity of the function better. Documentation on how to use the function should be in the docstring rather than in comments.

--- End quote ---

Great, thanks for your input, I like the name, just don't like the range error.

mijh:
I was going to rewrite and rename the dice function to something like this:

--- Code: ---def chance(value):
    """Randomly generated percentage chance to return True"""
    return (value / 100.0) > random.random()

--- End code ---

Given that the extra arguments are NEVER used anywhere, that's all it needs to be.

Neither does a chance out of 100 have anything to do with dice...

Xela:

--- Quote from: mijh on June 23, 2013, 09:51:16 AM ---I was going to rewrite and rename the dice function to something like this:

--- Code: ---def chance(value):
    """Randomly generated percentage chance to return True"""
    return (value / 100.0) > random.random()

--- End code ---

Given that the extra arguments are NEVER used anywhere, that's all it needs to be.

Neither does a chance out of 100 have anything to do with dice...

--- End quote ---

Hey, actually it can be useful if you want to throw out of 1k (very small chance). Leave it as it is now.

mijh:
Like....
--- Code: ---chance(0.1)
--- End code ---
?

A dice() function should return the number of a dice throw, not True or False, anyway. People don't know how to name functions :S

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version