|
| 1 | +class Participant: |
| 2 | + def __init__(self, name): |
| 3 | + self.name = name |
| 4 | + self.points = 0 |
| 5 | + self.choice = "" |
| 6 | + def choose(self,choice): |
| 7 | + self.choice = choice |
| 8 | + return "{name} selects {choice}".format(name=self.name, choice=self.choice) |
| 9 | + |
| 10 | + def toNumericalChoice(self): |
| 11 | + switcher = { |
| 12 | + "rock": 0, |
| 13 | + "paper": 1, |
| 14 | + "scissor": 2, |
| 15 | + "lizard": 3, |
| 16 | + "spock": 4 |
| 17 | + } |
| 18 | + return switcher[self.choice] |
| 19 | + def incrementPoint(self): |
| 20 | + self.points += 1 |
| 21 | + |
| 22 | +class GameRound: |
| 23 | + def __init__(self): |
| 24 | + self.rules = [ |
| 25 | + [0, -1, 1, 1, -1], |
| 26 | + [1, 0, -1, -1, 1], |
| 27 | + [-1, 1, 0, 1, -1], |
| 28 | + [-1, 1, -1, 0, 1], |
| 29 | + [1, -1, 1, -1, 0] |
| 30 | + ] |
| 31 | + def compareChoices(self, p1, p2): |
| 32 | + result = self.rules[p1.toNumericalChoice()][p2.toNumericalChoice()] |
| 33 | + |
| 34 | + if result > 0: |
| 35 | + p1.incrementPoint() |
| 36 | + elif result < 0: |
| 37 | + p2.incrementPoint() |
| 38 | + |
| 39 | + return "Round resulted in a {result}".format(result=self.getResultAsString(result)) |
| 40 | + def getResultAsString(self, result): |
| 41 | + res = { |
| 42 | + 0: "draw", |
| 43 | + 1: "win", |
| 44 | + -1: "loss" |
| 45 | + } |
| 46 | + return res[result] |
| 47 | + |
| 48 | +class Game: |
| 49 | + def __init__(self): |
| 50 | + self.endGame = False |
| 51 | + self.participant = Participant("Spock") |
| 52 | + self.secondParticipant = Participant("Kirk") |
| 53 | + def start(self): |
| 54 | + while not self.endGame: |
| 55 | + GameRound(self.participant, self.secondParticipant) |
| 56 | + self.checkEndCondition() |
| 57 | + def checkEndCondition(self): |
| 58 | + answer = input("Continue game y/n: ") |
| 59 | + if answer == 'y': |
| 60 | + GameRound(self.participant, self.secondParticipant) |
| 61 | + self.checkEndCondition() |
| 62 | + else: |
| 63 | + print("Game ended, {p1name} has {p1points}, and {p2name} had {p2points}".format(p1name=self.participant.name, p1points=self.participant.points, p2name=self.secondParticipant.name, p2points=self.secondParticipant.points)) |
| 64 | + self.determineWinner() |
| 65 | + self.endGame = True |
| 66 | + def determineWinner(self): |
| 67 | + resultString = "It's a Draw" |
| 68 | + if self.participant.points > self.secondParticipant.points: |
| 69 | + resultString = "Winner is {name}".format(name=self.participant.name) |
| 70 | + elif self.participant.points < self.secondParticipant.points: |
| 71 | + resultString = "Winner is {name}".format(name=self.secondParticipant.name) |
| 72 | + return resultString |
0 commit comments