You are making a 4 player dice game. Each of the four players starts the game by rolling one 5 sided dice and one 8 sided dice. The total is recorded.
Player Object
Attributes:
Constructor:
Behaviours:
import random
class Player:
def __init__(self, name):
self.name = name
self.points = 0
self.type = ''
odd = (5, 7, 12)
even = (3, 9, 11)
def set_type(self, opt):
self.type = opt
def roll(self, dice_1, dice_2):
return dice_1 + dice_2
def set_points(self, point_sum):
if self.type == 'ODD' and point_sum in Player.odd:
self.points += point_sum
elif self.type == 'EVEN' and point_sum in Player.even:
self.points += point_sum
def dice(mx):
return random.randint(1, mx)
p1 = Player('Jon')
if p1.roll(dice(5),dice(8))%2 == 0:
p1.set_type('EVEN')
else:
p1.set_type('ODD')
p2 = Player('Mike')
if p2.roll(dice(5),dice(8))%2 == 0:
p2.set_type('EVEN')
else:
p2.set_type('ODD')
for i in range(3):
d1 = p1.roll(dice(5),dice(8))
p1.set_points(d1)
print('{} roll {}'.format(p1.name, d1))
d2 = p2.roll(dice(5),dice(8))
p2.set_points(d2)
print('{} roll {}'.format(p2.name, d2))
if p1.points == p2.points:
print('draw')
elif p1.points > p2.points:
print('{} wins'.format(p1.name))
else:
print('{} wins'.format(p2.name))
Comments
Leave a comment