Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- TIE-02100 Introduction to programming
- Mölkky
- """
- class Player:
- def __init__(self, pelaaja):
- self.__pelaaja = pelaaja
- self.__pisteet = 0
- self.__heittoja = []
- def get_name(self):
- return self.__pelaaja
- def get_points(self):
- return self.__pisteet
- def has_won(self):
- if self.__pisteet == 50:
- return True
- else:
- return False
- def add_points(self, pisteet):
- self.__heittoja.append(pisteet)
- summa = 0
- for heitto in self.__heittoja:
- summa = summa + heitto
- if pisteet > summa / len(self.__heittoja):
- print("Cheers {}!".format(self.__pelaaja))
- self.__pisteet = self.__pisteet = pisteet
- if 40 <= self.__pisteet < 50:
- print("{:s} needs only {:d} points. It's better to avoid \n"
- "knocking down the pins with higher \n"
- "points.".format(self.__pelaaja, 50 - self.__pisteet))
- if self.__pisteet > 50:
- self.__pisteet = 25
- print("{} gets penalty points!".format(self.__pelaaja))
- def osuma_prosentti(self):
- osumat = 0
- for heitto in self.__heittoja:
- if heitto != 0:
- osumat += 1
- if len(self.__heittoja) != 0:
- prosentti = ("hit percentage {:.1f}".format(osumat/len(self.__heittoja)*100))
- else:
- prosentti = "hit percentage 0.0"
- return prosentti
- def main():
- # Here we define two variables which are the objects initiated from the
- # class Player. This is how the constructor of the class Player
- # (the method that is named __init__) is called!
- player1 = Player("Matti")
- player2 = Player("Teppo")
- throw = 1
- while True:
- if throw % 2 == 0:
- in_turn = player1
- else:
- in_turn = player2
- pts = int(input("Enter the score of player " + in_turn.get_name() +
- " of throw " + str(throw) + ": "))
- in_turn.add_points(pts)
- if in_turn.has_won():
- print("Game over! The winner is " + in_turn.get_name() + "!")
- return
- print("")
- print("Scoreboard after throw " + str(throw) + ":")
- print(player1.get_name() + ":", player1.get_points(), "p" + ",", player1.osuma_prosentti())
- print(player2.get_name() + ":", player2.get_points(), "p" + ",", player2.osuma_prosentti())
- print("")
- throw += 1
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement