简体   繁体   English

在连接四个python游戏中的玩家之间交替

[英]Alternating between players in a connect four python game

I'm currently writing a function to play Connect Four in Python3. 我目前正在编写一个函数来在Python3中玩Connect Four。 I've progressed well through much of it but am having trouble alternating between two players. 我在很多方面都取得了不错的进展,但是在两名球员之间交替时遇到了麻烦。

What I'm trying to do is run a function that will place a chip in the appropriate location as a function of the current player, playturn(curr). 我想做的是运行一个函数,该函数会将筹码放置在当前玩家的适当位置,即playturn(curr)。 So what I'm trying to say is that while there's no tie and while there's no winner, the game will continue and alternate between the two players. 所以我想说的是,虽然没有平局,没有赢家,但这场比赛将继续进行,并在两名玩家之间交替进行。

If it's Player 1's turn, curr=1 , and if it's Player 2's turn, curr=2 . 如果是玩家1的回合,则curr=1 ;如果是玩家2的回合,则curr=2

My current code isn't working, as it won't allow me to switch between players after each turn if there's no tie or no winner. 我当前的代码不起作用,因为如果没有平局或没有获胜者,则不允许我在每个回合之后在玩家之间切换。 My logic here was that if curr=1 is initially 1, then I'll have to set curr=2 after the first move. 我的逻辑是,如果curr=1最初为1,那么在第一步之后我将必须设置curr=2 Then, when curr=2 , I have to switch curr equal back to 1. In the following code, checkforwinner and checkfortie are two functions that will return False if there is no winner and if there is no tie. 然后,当curr=2 ,我必须将curr equal切换回1。在下面的代码中, checkforwinnercheckfortie是两个函数,如果没有获胜者并且没有平局,它们将返回False。 playturn(curr) will place a chip in the correct column depending on the column chosen by either Player1 or Player2. playturn(curr)会将筹码放置在正确的列中,具体取决于Player1或Player2选择的列。

curr=1
while checkforwinner==False and checkfortie==False:
    if curr==1:
        curr==2
        print(playturn(curr))
    if curr==2:
        curr==1
        print(playturn(curr))

Can someone explain why this code is not working and what can be done to fix it? 有人可以解释为什么此代码不起作用以及如何解决该问题吗?

curr==2 is a comparison. curr==2是一个比较。 You probably want curr=2 . 您可能需要curr=2 The second if should be an elif . 第二个if应该是elif

There are a couple ways to make this nicer! 有几种方法可以使这个更好!

To make your original code work, you should use jspcal's recommendation to turn the comparison operators ( == ) to assignment operators ( = ). 为了使原始代码正常工作,应使用jspcal的建议将比较运算符( == )转换为赋值运算符( = )。

You also need to use an elif for the second comparison, or every single loop will switch the player twice. 您还需要使用elif进行第二次比较,否则每个循环将播放器切换两次。

curr=1
while not (checkforwinner() or checkfortie()):
    if curr==1:
        curr=2
        print(playturn(curr))
    elif curr==2:
        curr=1
        print(playturn(curr))

You can also clean up the code a little: 您还可以清理一下代码:

def switch_player(current_player):
    if current_player == 1:
        return 2
    elif current_player == 2:
        return 1

while not (checkforwinner() or checkfortie()):
    print(playerturn(curr))
    curr = switch_player(curr)

The last version you might go with is the shortest, but is a little harder to read: 您可能使用的最新版本是最短的,但较难阅读:

while not (checkforwinner() or checkfortie()):
    print(playerturn(curr))
    curr = 1 if curr == 2 else 2

If checkforwinner and checkfortie are functions, you need parenthesis after them: 如果checkforwinnercheckfortie是函数,则需要在它们后面加上括号:

while checkforwinner()==False and checkfortie()==False:

Also, as @jspcal pointed out, you want to assign values with a single '=' and only use '==' for boolean comparison. 另外,正如@jspcal所指出的那样,您希望使用单个“ =”分配值,并且仅将“ ==”用于布尔比较。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM