通过递归函数调用打破Python石头/剪刀/布中的平局

发布于 2025-01-11 20:09:04 字数 1405 浏览 2 评论 0原文

我还没有见过像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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

惜醉颜 2025-01-18 20:09:04
if a_c == 'paper' and b_c == 'scisssors':

在条件检查b_c 中包含额外的s。如果 a_c'paper'b_c'scissors',则所有情况都不会匹配,并且你的代码将无限循环。

相反,请使用:(

if a_c == 'paper' and b_c == 'scissors':

顺便说一句,您不需要 while 循环,因为您正在使用递归在平局上再玩一轮。)

if a_c == 'paper' and b_c == 'scisssors':

contains an extra s in the condition checking b_c. If a_c is 'paper' and b_c is 'scissors', then none of the cases will match, and your code will loop infinitely.

Instead, use:

if a_c == 'paper' and b_c == 'scissors':

(As an aside, you don't need the while loop, because you're using recursion to play another round on a tie.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文