Answer to Question #202461 in Python for Elliot

Question #202461


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.

  • If the player rolls an even number, they become an "EVEN" player.
  • If they roll an odd, then the player becomes an "ODD" player


  • If the Player is an "EVEN" Player, then he wins the total if that total is a 3 or 9 or 11
  • If the Player is an "ODD" Player, then he wins the total if that total is a 5 or 7 or 12



Player Object

Attributes:

  • The name
  • The total points
  • The type of the player "EVEN" or "ODD"

Constructor:

  • Accept name of the player as the parameter
  • Set the points accumulated to 0

Behaviours:

  • Set Type Function:
  • Accept a string either "EVEN" or "ODD"
  • Set the player type attribute


  • Roll Function:
  • Accept two dice objects as parameters
  • Return the sum of two dice rolls


  • Set Points Function:
  • Accept a value representing the sum of two dice
  • Return the number of points the player should earn based on that sum
1
Expert's answer
2021-06-03T02:28:04-0400
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))

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS