Python Forum
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Match coins game
#1
I have completed every step of this program and have gotten it to run, but my brain can not wrap around how to solve this problem.

Here is the instructions:

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.


I have most of this working but I can not figure out how to add coins to one instance and subract from the next.
(I put self.get_amount as filler in the function im having problems with)

Here is my code:

import random


class Coin (object):
    def __init__ (self, __amount = 20, __sideup = "Heads"):

        self.__amount = __amount
        self.__sideup = __sideup

    def toss (self):
        val = random.randint(0,1)
        if val == 0:
            self.__sideup = "Heads"
        else:
            self.__sideup = "Tails"

    def get_sideup (self):
        return self.__sideup

    def get_amount(self,):
        return self.__amount

    def set_amount (self, __amount):
        self.get_amount

        



c1 = Coin()
c2 = Coin()
play = input ("Do you want to play? (Y/N): ")
while play == "Y" or "y":
    c1.toss()
    c2.toss()
    if c1.get_sideup() == c2.get_sideup():
        c1.set_amount(+1)
        c2.set_amount(-1)
        print("player 1 wins")
    else:
        c1.set_amount(-1)
        c2.set_amount(+1)
        print("player 2 wins")

    print(" player1: " + str(c1.get_amount()) + " and player2: " + str(c2.get_amount()))
    play = input("Do you want to play? (y/n): ")
Its my set_amount that I am having troubles with. Thank you for any help
Reply
#2
Change method set_amount,then it should work:
def set_amount (self, value):
    self.__amount += value
The  getters/setters and __(which is not private),could be removed.
Double underscores __ is used for name mangling,nothing is really private in Python.
Not your fault but your teacher who comes from a Java/C++ background,
and do it in a same as those languages in Python.
So could look like this:
import random

class Coin:
    def __init__ (self, amount=20, sideup="Heads"):
        self.sideup = sideup
        self.amount = amount

    def toss(self):
        val = random.randint(0,1)
        if val == 0:
            self.sideup = "Heads"
        else:
            self.sideup = "Tails"

c1 = Coin()
c2 = Coin()
play = input('Do you want to play? (Y/N): ')
while play.lower() == 'y':
    c1.toss()
    c2.toss()
    if c1.sideup == c2.sideup:
        c1.amount += 5
        c2.amount -= 5
        print("player 1 wins")
    else:
         c1.amount -= 5
         c2.amount += 5
         print("player 2 wins")
    print(f'player1: {c1.amount} and player2: {c2.amount}')
    play = input("Do you want to play? (y/n): ")
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Adding and Removing coins to match Coin Bag Total infinite times Strayfe 8 4,866 Sep-11-2018, 07:30 PM
Last Post: gruntfutuk

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020