Advertisement
pyryc

Untitled

Jul 18th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 KB | None | 0 0
  1. """
  2. TIE-02100 Introduction to programming
  3. Mölkky
  4. """
  5. class Player:
  6.  
  7. def __init__(self, pelaaja):
  8. self.__pelaaja = pelaaja
  9. self.__pisteet = 0
  10. self.__heittoja = []
  11.  
  12. def get_name(self):
  13. return self.__pelaaja
  14.  
  15. def get_points(self):
  16. return self.__pisteet
  17.  
  18. def has_won(self):
  19. if self.__pisteet == 50:
  20. return True
  21. else:
  22. return False
  23.  
  24. def add_points(self, pisteet):
  25. self.__heittoja.append(pisteet)
  26. summa = 0
  27. for heitto in self.__heittoja:
  28. summa = summa + heitto
  29.  
  30. if pisteet > summa / len(self.__heittoja):
  31. print("Cheers {}!".format(self.__pelaaja))
  32.  
  33. self.__pisteet = self.__pisteet = pisteet
  34. if 40 <= self.__pisteet < 50:
  35. print("{:s} needs only {:d} points. It's better to avoid \n"
  36. "knocking down the pins with higher \n"
  37. "points.".format(self.__pelaaja, 50 - self.__pisteet))
  38.  
  39. if self.__pisteet > 50:
  40. self.__pisteet = 25
  41. print("{} gets penalty points!".format(self.__pelaaja))
  42.  
  43. def osuma_prosentti(self):
  44. osumat = 0
  45. for heitto in self.__heittoja:
  46. if heitto != 0:
  47. osumat += 1
  48.  
  49. if len(self.__heittoja) != 0:
  50. prosentti = ("hit percentage {:.1f}".format(osumat/len(self.__heittoja)*100))
  51.  
  52. else:
  53. prosentti = "hit percentage 0.0"
  54.  
  55. return prosentti
  56.  
  57. def main():
  58.  
  59. # Here we define two variables which are the objects initiated from the
  60. # class Player. This is how the constructor of the class Player
  61. # (the method that is named __init__) is called!
  62. player1 = Player("Matti")
  63. player2 = Player("Teppo")
  64.  
  65. throw = 1
  66. while True:
  67. if throw % 2 == 0:
  68. in_turn = player1
  69. else:
  70. in_turn = player2
  71.  
  72. pts = int(input("Enter the score of player " + in_turn.get_name() +
  73. " of throw " + str(throw) + ": "))
  74. in_turn.add_points(pts)
  75. if in_turn.has_won():
  76. print("Game over! The winner is " + in_turn.get_name() + "!")
  77. return
  78.  
  79. print("")
  80. print("Scoreboard after throw " + str(throw) + ":")
  81. print(player1.get_name() + ":", player1.get_points(), "p" + ",", player1.osuma_prosentti())
  82. print(player2.get_name() + ":", player2.get_points(), "p" + ",", player2.osuma_prosentti())
  83. print("")
  84.  
  85. throw += 1
  86.  
  87. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement