通过递归函数调用打破Python石头/剪刀/布中的平局
我还没有见过像Python这样的线程,特别是没有人工输入的递归解决方案。我有一个基于代理的网络结构模型,代理在网络中移动,相遇时玩 R/P/S。那部分很好。
让他们在打平的情况下再次比赛是行不通的。我的 R/P/S 函数:
def rps(a, b):
moves = ['rock','paper','scissors']
winner = 0
a_c = random.choice(moves) # choice for a
b_c = random.choice(moves) # choice for b
while winner == 0: # Attempt to keep it going until a non-tie scenario
if a_c == 'rock' and b_c == 'scissors':
winner = a
if a_c == 'rock' and b_c == 'paper':
winner = b
if a_c == 'rock' and b_c == 'rock':
winner = rps(a,b) # Attempt at recursive call
if a_c == 'scissors' and b_c == 'paper':
winner = a
if a_c == 'scissors' and b_c == 'rock':
winner = b
if a_c == 'scissors' and b_c == 'scissors':
winner = rps(a,b)
if a_c == 'paper' and b_c == 'rock':
winner = a
if a_c == 'paper' and b_c == 'scisssors':
winner = b
if a_c == 'paper' and b_c == 'paper':
winner = rps(a,b)
return winner
最初我没有 while 循环,并且将胜利者设置为 0,在这种情况下,代理不会移动。然而,从概念上讲,在该功能内强制继续游戏似乎很容易。我尝试使用以下方法进行测试:
for i in range(50):
print(rps(1,2))
但是在第一次平局时,循环被卡住了。帮助?我知道这是一个n00b问题。我没有正式的编码背景,并且正在边学习边学习。非常感谢您对我不可避免地做错的事情做出的解释。
I haven't seen a thread quite like this for python, specifically a recursive solution without human input. I have a network structure agent based model and the agents travel around the network, playing R/P/S when they meet. That part is fine.
What isn't working is having them play again upon a tie. My function for R/P/S:
def rps(a, b):
moves = ['rock','paper','scissors']
winner = 0
a_c = random.choice(moves) # choice for a
b_c = random.choice(moves) # choice for b
while winner == 0: # Attempt to keep it going until a non-tie scenario
if a_c == 'rock' and b_c == 'scissors':
winner = a
if a_c == 'rock' and b_c == 'paper':
winner = b
if a_c == 'rock' and b_c == 'rock':
winner = rps(a,b) # Attempt at recursive call
if a_c == 'scissors' and b_c == 'paper':
winner = a
if a_c == 'scissors' and b_c == 'rock':
winner = b
if a_c == 'scissors' and b_c == 'scissors':
winner = rps(a,b)
if a_c == 'paper' and b_c == 'rock':
winner = a
if a_c == 'paper' and b_c == 'scisssors':
winner = b
if a_c == 'paper' and b_c == 'paper':
winner = rps(a,b)
return winner
Originally I did not have the while loop and had ties set winner = 0, in which case the agents didn't move. However, forcing continued games within the function seemed easy conceptually. I tried testing with the following:
for i in range(50):
print(rps(1,2))
But upon the first tie, the loop is stuck. Help? I know this is a n00b question. I don't have a formal coding background and am learning as I go. Thank you very much for any explanation for what I'm inevitably doing wrong.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在条件检查
b_c
中包含额外的s
。如果a_c
是'paper'
且b_c
是'scissors'
,则所有情况都不会匹配,并且你的代码将无限循环。相反,请使用:(
顺便说一句,您不需要
while
循环,因为您正在使用递归在平局上再玩一轮。)contains an extra
s
in the condition checkingb_c
. Ifa_c
is'paper'
andb_c
is'scissors'
, then none of the cases will match, and your code will loop infinitely.Instead, use:
(As an aside, you don't need the
while
loop, because you're using recursion to play another round on a tie.)