コード例 #1
0
def main():
    print("The Dice Roller program")

    #print an image of every possible roll
    die = Die()
    for i in range(1, 7):
        die.value = i

        print(die.image)

    print()

    # get number of dice from user
    count = int(input("Enter the number of dice to roll: "))

    # create Die objects and add to Dice object
    dice = Dice()
    for i in range(count):
        die = Die()
        dice.addDie(die)

    while True:
        # roll the dice
        dice.rollAll()
        print("YOUR ROLL: ")
        for die in dice:
            print(die)
        print("\nTotal:\t" + str(dice.getTotal()))
        print("\n")

        choice = input("Roll again? (y/n): ")
        if choice != "y":
            print("Bye!")
            break
コード例 #2
0
class TestDiceMethods(unittest.TestCase):
    '''Testing module Dice'''
    def setUp(self):
        '''Setting up'''
        random.seed(42)
        self.die = Die(range(1, 7))
        self.cup = Cup(2, 6)
        self.yahtzee = Cup(5, 6)

    def test_die_roll(self):
        '''Testing die roll method'''
        self.assertEqual(self.die.roll(), 6)

    def test_die_get_value(self):
        '''Testing value getter'''
        self.assertEqual(self.die.value, 6)

    def test_die_set_value(self):
        '''Testing value setter'''
        with self.assertRaises(ValueError):
            self.die.value = 1

    def test_die_str(self):
        '''Testing die.__str__method'''
        with patch('sys.stdout', new=StringIO()) as output:
            print(self.die)
        self.assertEqual(output.getvalue().strip(), '6')

    def test_cup_shake(self):
        '''Testing shake method'''
        self.cup.shake()
        self.assertEqual(str(self.cup), '[6, 1]')

    def test_cup_add(self):
        '''Testing add method'''
        self.cup.add(Die(range(1, 7)))
        self.assertEqual(str(self.cup), '[1, 1, 6]')

    def test_cup_remove(self):
        '''Testing remove method'''
        self.cup.remove(0)
        self.assertEqual(str(self.cup), '[1]')

    def test_cup_roll(self):
        '''Testing cup roll method'''
        self.assertEqual(str(self.yahtzee), '[6, 3, 2, 2, 2]')
        self.yahtzee.shake()
        self.assertEqual(str(self.yahtzee), '[6, 1, 6, 6, 5]')
        self.yahtzee.roll(1, 3, 5)
        self.assertEqual(str(self.yahtzee), '[1, 1, 5, 6, 4]')

    def test_cup_str(self):
        '''Testing cup.__str__method'''
        with patch('sys.stdout', new=StringIO()) as output:
            print(self.cup)
        self.assertEqual(output.getvalue().strip(), '[1, 1]')
コード例 #3
0
        print("Please enter a number.")

while True:
    try:
        num_sides = int(input("How many sides should each die have (max 20)? "))
        if num_sides > 20:
            print("Pick a number less than 20.")
            continue
        break
    except ValueError:
        print("Please enter a number.")

# Creates die object, rolls it, and adds the value to a list
rolls = []
for num in range(num_die):
    die = Die(num_sides)
    roll = die.roll()
    rolls.append(roll)


# Counts each value in rolls & adds to list
frequencies = []
for value in range(1, num_sides+1):
    frequency = rolls.count(value)
    frequencies.append(frequency)

print(frequencies)

# Creates a formatted bar chart
x_values = list(range(1, num_sides+1))
plt.ylim(0, max(frequencies)+(max(frequencies)*0.1))
コード例 #4
0
 def test_cup_add(self):
     '''Testing add method'''
     self.cup.add(Die(range(1, 7)))
     self.assertEqual(str(self.cup), '[1, 1, 6]')
コード例 #5
0
 def setUp(self):
     '''Setting up'''
     random.seed(42)
     self.die = Die(range(1, 7))
     self.cup = Cup(2, 6)
     self.yahtzee = Cup(5, 6)
コード例 #6
0
ファイル: CrapsGame.py プロジェクト: omegaroku/CS106SP17CRAPS
    def throw(self):
        """

        :rtype: object
        """
        Dice1 = Die(6)
        Dice2 = Die(6)
        Dice1.roll()
        Dice2.roll()
        firstThrow = Dice1.getValue() + Dice2.getValue()

        if firstThrow is 7 or firstThrow is 11:
            print("You Win!")
            self.setWinCount(self.getWinCount() + 1)
            self.currentBank = self.getCurrentBank() + self.getCurrentBet()
        elif firstThrow is 2 or firstThrow is 3 or firstThrow is 12:
            print("You Lose!")
            self.setLossCount(self.getLossCount() + 1)
            self.currentBank = self.getCurrentBank() - self.getCurrentBet()
        else:
            Dice1.roll()
            Dice2.roll()
            if Dice1.getValue() + Dice2.getValue() is firstThrow:
                print("You Win!")
                self.setWinCount(self.getWinCount() + 1)
                self.currentBank = self.getCurrentBank() + self.getCurrentBet()
            else:
                print("You Lose!")
                self.setLossCount(self.getLossCount() + 1)
                self.currentBank = self.getCurrentBank() - self.getCurrentBet()
        pass
コード例 #7
0
from Dice import Die
die_1 = Die()
die_2 = Die(10)

results = []
for roll_num in range(5000):
    result = die_1.roll() + die_2.roll()
    results.append(result)
print(results)

frequencies = []
max_result = die_1.num_sides + die_2.num_sides
for value in range(2, max_result + 1):
    frequency = results.count(value)
    frequencies.append(frequency)
print(frequencies)

import pygal

hist = pygal.Bar()
hist.title = "Results of rolling a D6 and a D10 50,000 times."
hist.x_labels = [
    '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15',
    '16'
]
hist.x_title = "Result"
hist.y_title = "Frequency of Result"
hist.add('D6 + D6', frequencies)
hist.render_to_file('die_visual.svg')
コード例 #8
0
ファイル: test_dice.py プロジェクト: PhumlaniKubeka/OOP
def test_negative():
    with pytest.raises(Exception):
        error = Exception('negative probabilities not allowed')
        rand_instance = Die(6, [1, -1, 1, 1, 1, 1])
        assert rand_instance.value == error
コード例 #9
0
ファイル: test_dice.py プロジェクト: PhumlaniKubeka/OOP
def test_roll():
    rand_instance = Die(6)
    assert rand_instance.value == True
コード例 #10
0
ファイル: test_dice.py プロジェクト: PhumlaniKubeka/OOP
def test_no_values():
    with pytest.raises(Exception):
        error = Exception("only integer values allowed")
        rand_instance = Die(7, [0, 2, 3.2, 5, 0.6, 5])
        assert rand_instance.value == error
コード例 #11
0
ファイル: test_dice.py プロジェクト: PhumlaniKubeka/OOP
def test_greater_than():
    with pytest.raises(Exception):
        error = Exception("probability sum must be greater than 0")
        rand_instance = Die(6, [0, 0, 0, 0, 0, 0])
        assert rand_instance.value == error