please Help!! python; Match Coins: Create a coin toss game with two players. Eac
ID: 3833858 • Letter: P
Question
please Help!! python;
Match Coins: Create a coin toss game with two players. Each player starts with 20 coins. Rules:
each player tosses their own coin
If the coins match,
Player1 gets Player2’s coin.
If not, the Player2 gets the coin from Player1.
Whoever has the most coins wins.
In main(), create a while loop that asks to user to continue play.
In each loop, report the results … coins matched?
Total coins for each player?
The loop should toss the coins at least once.
A ‘Coin’ class is needed. The class requires:
The methods __init__(), toss(), get_sideup(), get_amount(), set_amount()
The class needs the attributes ’__amount’, initialized to 20, and ’__sideup’ initialized to one of two values, ‘Heads’ or ‘Tails’.
‘toss()’ method : generates a random value of ‘0’ or ‘1’, assigning ‘Heads’ or ‘Tails’ accordingly.
‘get_sideup()’ method : returns the current value of __sideup.
‘set_amount()’ method : passes a +1 or -1 to change __amount, depending on the results of the toss.
‘get_amount()’ method : returns the current value of __amount.
‘spend()’ method : you are in Vegas. This method ‘zero’s’ your amount and even assigns a random negative value to your amount. If the amount is zero, you get your hand slapped … just a little humor. You needn’t implement this method.
UML diagram for Coin class: (- is private, + is public)
Coin
-sideup
-amount
+__init__()
+get_amount()
+set_amount(int): should be +/-1
+get_sideup()
+toss()
Game Plan:
from coin import Coin
def main()
create two Coin objects
ask player if they want to play
while ‘y’ or ‘Y’
both players toss their coin
if both coins match
add to player1 and subtract from player2
else
add to player2 and subtract from player 1
print results of toss
ask players if they want to play
print overall results
Coin
-sideup
-amount
+__init__()
+get_amount()
+set_amount(int): should be +/-1
+get_sideup()
+toss()
Explanation / Answer
main.py
from coin import Coin
def main():
c1 = Coin()
c2 = Coin()
play = input("Do you want to play? (y/n): ")
while play == 'y' or play =='Y':
c1.toss()
c2.toss()
if c1.get_sideup() == c2.get_sideup():
c1.set_amount(1)
c2.set_amount(-1)
print("player 1 won")
else:
c1.set_amount(-1)
c2.set_amount(1)
print("player 2 won")
play = input("Do you want to play? (y/n): ")
print("After play player 1 has " + str(c1.get_amount()) + " and player 2 has " + str(c2.get_amount()))
main()
# code link for main.py https://paste.ee/p/5N53O
coin.py
from random import randint
class Coin:
def __init__(self):
self.__amount = 20
self.__sideup = "Heads"
def toss(self):
val = randint(0, 1)
if val == 0:
self.__sideup = "Heads"
else:
self.__sideup = "Tails"
def get_sideup(self):
return self.__sideup
def set_amount(self, change):
self.__amount += change
def get_amount(self):
return self.__amount
def spend(delf):
self.__amount = 0
# cod elink for coin.py: https://paste.ee/p/ZJleR
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.