我写了一个简单的石头剪刀布程序,但我想从三个中选出最好的,并且不知道如何将其写入我现有的代码中



就像标题所暗示的那样,我是新手,正在努力磨练我的技能,但我不知道该怎么做......任何帮助将不胜感激。

import random
playerOneWin = ((1,3), (3,2), (2,1))
playerTwoWin = ((3,1), (2,3), (1,2))
tie = ((1,1), (2,2), (3,3))

while True:
roll = random.randint(1,3), random.randint(1,3)
if roll in tie:
print('Tie')
break
elif roll in playerOneWin:
print('Player 1 Wins')
break
elif roll in playerTwoWin:
print('Player 2 Wins')
break

比如我做一个函数?我是否将其写入我的 while 循环?

如果我这样做,那怎么办?我该怎么做呢?

关于解决问题的一般建议:

  • 想想一般的解决方案是什么,并细化为算法,而不是列出所有可能的结果。

要实现"3 局两胜",即 2 次执行后 2-0 停止,请检查任一玩家的得分为 2 并在那里结束。

当然,可以有进一步的优化,但这引入了一些概念。即:函数、字典、if 语句、f 字符串和 *args 作为函数参数。

我决定在底部显示一些额外的信息,包括玩过的游戏总数和平局的数量。

import random
def winner(p1, p2):
return (3 + p1 - p2) % 3
def score_round():
roll = random.randint(1,3), random.randint(1,3)
w = winner(*roll)
if w == 0:
scores[w] += 1
print('Tie')
return
scores[w] += 1
print(f"Player {w} Wins")
return
num_games = 3
games_to_win = num_games // 2 + 1
games_played = 0
scores = {0:0, 1:0, 2:0}
while True:
score_round()
games_played += 1
if scores[1] == games_to_win or scores[2] == games_to_win:
break
if scores[1] > scores[2]: print(f"Player 1 wins: {scores[1]} - {scores[2]}")
else: print(f"Player 2 wins: {scores[2]} - {scores[1]}")
print(f"Games played: {games_played}, Ties: {scores[0]}")

您可以初始化 2 个计数器变量,这些变量将跟踪每个玩家的获胜次数。当每个玩家赢得一轮时,将他们各自的获胜次数增加 1。领带不应递增任一计数器。

而不是做while True:,你可以使用类似的东西

while p1Wins < 2 or p2Wins < 2:
# rest of your code

当然,在while循环中使用适当的条件,您还可以删除这些break语句。

import random
def best_of(x):
playerOneWin = ((1,3), (3,2), (2,1))
playerTwoWin = ((3,1), (2,3), (1,2))
tie = ((1,1), (2,2), (3,3))

i=0
while True:
roll = random.randint(1,3), random.randint(1,3)
if roll in tie:
print('Tie')
elif roll in playerOneWin:
print('Player 1 Wins')
elif roll in playerTwoWin:
print('Player 2 Wins')
i=i+1
if i==x:
break

以 best_of(3( 或 best_of(5( ......

查看标题中建议的代码和要求, 您可以在现有代码中进行更改以作为函数和以下代码工作:

def rps(best_of_n=3):
player1_win = 0
player2_win = 0
while best_of_n>0:
best_of_n = best_of_n - 1
roll = random.randint(1,3), random.randint(1,3)
if roll in tie:
print('Tie')
elif roll in playerOneWin:
player1_win = player1_win + 1
print('Player 1 Wins')
elif roll in playerTwoWin:
player2_win = player2_win + 1
print('Player 2 Wins')
if player2_win > player1_win:
print('player 2 win the game')
else:
print('player 1 win the game.')

或者,如果您想修改 while 循环,则只需执行以下操作:

n = 3
while n>0:
n = n - 1
roll = random.randint(1,3), random.randint(1,3)
if roll in tie:
print('Tie')
elif roll in playerOneWin:
player1_win = player1_win + 1
print('Player 1 Wins')
elif roll in playerTwoWin:
player2_win = player2_win + 1
print('Player 2 Wins')
if player2_win > player1_win:
print('player 2 win the game')
else:
print('player 1 win the game.')
import random
bestof3 = []
playerOneWin = [[1,3], [3,2], [2,1]]
playerTwoWin = [[3,1], [2,3], [1,2]]
tie = [[1,1], [2,2], [3,3]]
rolllist = []
counter = 3
while counter > 0:
rolllist.append(random.randint(1,3))
rolllist.append(random.randint(1,3))
if rolllist in tie:
print('Tie')
elif rolllist in playerOneWin:
print('Player 1 Wins')
counter -= 1
bestof3.append('P1')
elif rolllist in playerTwoWin:
print('Player 2 Wins')
counter -= 1
bestof3.append('P2')
rollist.clear()
p1 = bestof3.count('P1')
p2 = bestof3.count('P2')
if p1 > p2: print('Player one wins the game')
else: print('Player 2 wins the game')

我试图保持代码简单。这会列出玩家 1 和 2 赢得的(bestof3(,并根据获胜者不断附加'P1''P2'。最后,对列表进行评估,你会得到答案。

player1 = 0 
player2 = 0 
if player1 >= 2:
print('Player 1 Wins best out of 3')
break
elif player2 >= 2:
print('Player 2 Wins best out of 3')
break

相关内容

最新更新