- 剪子剪布
- 布包石头
- 石头砸蜥蜴
- 蜥蜴毒死斯波克
- 斯波克踩碎剪子
- 剪子斩首蜥蜴
- 蜥蜴吃掉布
- 论文(布,英语中与论文同词)证明斯波克不存在
- 斯波克融化石头
- 石头敲坏剪子
- 剪子剪布
选择 |
石头 |
布 |
剪子 |
蜥蜴 |
史波克 |
石头 |
0 |
-1 |
1 |
1 |
-1 |
布 |
1 |
0 |
-1 |
-1 |
1 |
剪子 |
-1 |
1 |
0 |
1 |
-1 |
蜥蜴 |
-1 |
1 |
-1 |
0 |
1 |
史波克 |
1 |
-1 |
1 |
-1 |
0 |
class Participant:
def __init__(self, name):
self.name = name
self.points = 0
self.choice = ""
def choose(self):
self.choice = input("{name}, select rock, paper, scissor, lizard or spock: ".format(name=self.name))
print("{name} selects {choice}".format(name=self.name, choice=self.choice))
def toNumericalChoice(self):
switcher = {
"rock": 0,
"paper": 1,
"scissor": 2,
"lizard": 3,
"spock": 4
}
return switcher[self.choice]
def incrementPoint(self):
self.points += 1
class GameRound:
def __init__(self, p1, p2):
self.rules = [
[0, -1, 1, 1, -1],
[1, 0, -1, -1, 1],
[-1, 1, 0, 1, -1],
[-1, 1, -1, 0, 1],
[1, -1, 1, -1, 0]
]
p1.choose()
p2.choose()
result = self.compareChoices(p1, p2)
print("Round resulted in a {result}".format(result=self.getResultAsString(result)))
if result > 0:
p1.incrementPoint()
elif result < 0:
p2.incrementPoint()
def awardPoints(self):
print("implement")
def compareChoices(self, p1, p2):
return self.rules[p1.toNumericalChoice()][p2.toNumericalChoice()]
def getResultAsString(self, result):
res = {
0: "draw",
1: "win",
-1: "loss"
}
return res[result]
class Game:
def __init__(self):
self.endGame = False
self.participant = Participant("Spock")
self.secondParticipant = Participant("Kirk")
def start(self):
while not self.endGame:
GameRound(self.participant, self.secondParticipant)
self.checkEndCondition()
def checkEndCondition(self):
answer = input("Continue game y/n: ")
if answer == 'y':
GameRound(self.participant, self.secondParticipant)
self.checkEndCondition()
else:
print("Game ended, {p1name} has {p1points}, and {p2name} has {p2points}".format(p1name=self.participant.name, p1points=self.participant.points, p2name=self.secondParticipant.name, p2points=self.secondParticipant.points))
self.determineWinner()
self.endGame = True
def determineWinner(self):
resultString = "It's a Draw"
if self.participant.points > self.secondParticipant.points:
resultString = "Winner is {name}".format(name=self.participant.name)
elif self.participant.points < self.secondParticipant.points:
resultString = "Winner is {name}".format(name=self.secondParticipant.name)
print(resultString)
game = Game()
game.start()
- 转载请保留原文链接:Python实现石头剪子布蜥蜴斯波克 https://aboss.top/post/151/
- 本站所有资源文章出自互联网收集整理,本站不参与制作,如果侵犯了您的合法权益,请联系本站我们会及时删除。
- 本站发布资源来源于互联网,可能存在水印或者引流等信息,请用户擦亮眼睛自行鉴别,做一个有主见和判断力的用户。
- 本站资源仅供研究、学习交流之用,若使用商业用途,请购买正版授权,否则产生的一切后果将由下载用户自行承担。
- 联系方式(#替换成@):mail#aboss.top
评论