可以在函数中指定某些参数吗?

时间:2017-04-17 20:19:07

标签: python python-3.x

在我的player_player函数中,我想调用check_win函数,以便我可以在这个连接四游戏中声明一个胜利者。我觉得我设置代码的方式只有在输入某个参数时才有效。我想知道是否有可能只在我的代码中输入变量 token 的值。 (对不起,如果这是一个多余的问题,我是python的初学者)

def check_win(token, i, j):
    if i-3 in range(0, 6) and j+3 in range(0,5):
        if board[i][j] == token and board[i-1][j+1] == token and board[i-2][j+2] == token and board[i-3][j+3] == token:
            return True
    if i-3 in range(0, 6) and j-3 in range(0,5):
        if board[i][j] == token and board[i-1][j-1] == token and board[i-2][j-2] == token and board[i-3][j-3] == token:
            return True
    if j+3 in range(0, 5):
        if board[i][j] == token and board[i][j+1] == token and board[i][j+2] == token and board[i][j+3] == token:
            return True
    if i-3 in range(0, 6):
        if board[i][j] == token and board[i-1][j] == token and board[i-2][j] == token and board[i-3][j] == token:
            return True
    else:
        return False

2 个答案:

答案 0 :(得分:1)

使用三个参数定义函数check_win,但是用一个参数调用它。正如评论中已经提到的那样,我猜你正在寻找的东西是i和j in for循环而不是参数。也许是这样的:

    def check_win(token):
        for i in range(0, 6):
            for j in range(0, 6):
                if i-3 in range(0, 6) and j+3 in range(0,6):
                    if board[i][j] == token and board[i-1][j+1] == token and board[i-2][j+2] == token and board[i-3][j+3] == token:
                        return True
                if i-3 in range(0, 6) and j-3 in range(0,6):
                    if board[i][j] == token and board[i-1][j-1] == token and board[i-2][j-2] == token and board[i-3][j-3] == token:
                        return True
                if j+3 in range(0, 6):
                    if board[i][j] == token and board[i][j+1] == token and board[i][j+2] == token and board[i][j+3] == token:
                        return True
                if i-3 in range(0, 6):
                    if board[i][j] == token and board[i-1][j] == token and board[i-2][j] == token and board[i-3][j] == token:
                        return True
        else:
            return False

答案 1 :(得分:0)

这是我的check_win版本:

def check_win(token):
    token = [token]*4
    l1 = lambda i, j:[ board[i-p][j+p] for p in range(4)]
    l2 = lambda i, j:[ board[i-p][j-p] for p in range(4)]
    for i in range(5,-1,-1):
        c = 0
        for k in range(4):
            if board[i][k:k+4] == token:
                return True
        if i > 2:
            for k in range(6):
                if [sub[k] for sub in board[i-3:i+1]] == token:
                    return True
            for k in range(4):
                if l1(i,k) == token:
                    return True
                if l2(i,k+3) == token:
                    return True
    return False

样板:

board = [['.', '.', '☼', '.', '.', '.', '.'],
         ['.', '○', '☼', '○', '○', '.', '.'],
         ['.', '○', '☼', '○', '○', '.', '.'],
         ['○', '○', '○', '○', '☼', '.', '.'],
         ['○', '☼', '○', '☼', '○', '○', '☼'],
         ['○', '☼', '○', '☼', '○', '☼', '☼']]