简体   繁体   English

while循环的多个条件

[英]Multiple conditions for while loop

I'm writing a function that checks if a checkers piece can do jump moves, and stop when it can't jump, so I wrote: 我正在写一个函数,检查棋子是否可以跳动作,并在跳不动时停止,所以我写道:

def checkJump(x,y,count,incr):
player = board[x][y]
count = 0

jumpleft = (board[x+incr][y+incr] != player) and (board[x+incr][y+incr] != '') and (board[x+(incr*2)][y+(incr*2)] == '') and 0 <= x <= 7 and 0 <= y <= 7 and 0 and 0 <= x+incr <= 7 and 0 <= x+(incr*2) <= 7 and 0 <= y+(incr*2) <= 7

jumpright = (board[x+incr][y-incr] != player) and (board[x+incr][y-incr] != '') and (board[x+(incr*2)][y-(incr*2)] == '') and 0 <= x <= 7 and 0 <= y <= 7 and 0 and 0 <= x+incr <= 7 and 0 <= x+(incr*2) <= 7 and 0 <= y-(incr*2) <= 7

while jumpleft or jumpright:    
    while jumpleft:
        x += incr*2
        y += incr*2
        count += 1
        jumpleft = (board[x+incr][y+incr] != player) and (board[x+incr][y+incr] != '') and (board[x+(incr*2)][y+(incr*2)] == '') and 0 <= x <= 7 and 0 <= y <= 7 and 0 and 0 <= x+incr <= 7 and 0 <= x+(incr*2) <= 7 and 0 <= y+(incr*2) <= 7

    while jumpright:
        x += incr*2
        y -= incr*2
        count += 1
        jumpright = (board[x+incr][y-incr] != player) and (board[x+incr][y-incr] != '') and (board[x+(incr*2)][y-(incr*2)] == '') and 0 <= x <= 7 and 0 <= y <= 7 and 0 and 0 <= x+incr <= 7 and 0 <= x+(incr*2) <= 7 and 0 <= y-(incr*2) <= 7

    jumpleft = (board[x+incr][y+incr] != player) and (board[x+incr][y+incr] != '') and (board[x+(incr*2)][y+(incr*2)] == '') and 0 <= x <= 7 and 0 <= y <= 7 and 0 and 0 <= x+incr <= 7 and 0 <= x+(incr*2) <= 7 and 0 <= y+(incr*2) <= 7

    jumpright = (board[x+incr][y-incr] != player) and (board[x+incr][y-incr] != '') and (board[x+(incr*2)][y-(incr*2)] == '') and 0 <= x <= 7 and 0 <= y <= 7 and 0 and 0 <= x+incr <= 7 and 0 <= x+(incr*2) <= 7 and 0 <= y-(incr*2) <= 7
return x,y,count    

However, the conditions for checking is too redundant, multiple conditions are connected with and . 但是,检查条件过于冗长,多个条件用and连接。 Is there a better way to write multiple conditions for a while loop instead just chaining them with and ? 是否有更好的方法为while循环编写多个条件,而不是仅将它们与and链接在一起? Thanks a lot for answering 非常感谢您的回答

Maybe you can define two checking functions which return True or False: 也许您可以定义两个返回True或False的检查函数:

def jumpleft(x, y, board, incr):
    return (board[x+incr][y+incr] != player) and (board[x+incr][y+incr] != '') and (board[x+(incr*2)][y+(incr*2)] == '') and 0 <= x <= 7 and 0 <= y <= 7 and 0 and 0 <= x+incr <= 7 and 0 <= x+(incr*2) <= 7 and 0 <= y+(incr*2) <= 7

def jumpright(x, y, board, incr):
    return (board[x+incr][y-incr] != player) and (board[x+incr][y-incr] != '') and (board[x+(incr*2)][y-(incr*2)] == '') and 0 <= x <= 7 and 0 <= y <= 7 and 0 and 0 <= x+incr <= 7 and 0 <= x+(incr*2) <= 7 and 0 <= y-(incr*2) <= 7

And just call them to get the values. 只需调用它们即可获取值。 That would make your code more readable. 这将使您的代码更具可读性。

Since you're interested in game programming, i'd recommend pygame. 由于您对游戏编程感兴趣,所以我建议pygame。 If you're not interested, i'd recommend replacing a number of the "and" assignment operators with conditional "if statements". 如果您不感兴趣,建议使用条件“ if语句”替换许多“和”赋值运算符。 They serve the same purpose and are more readable. 它们具有相同的目的,并且更具可读性。

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

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