2016-11-06 78 views
1
def move(list, wins_1, wins_2): 
    global turn 
    if turn % 2 == 0: 
     sign = "| x " 
    else: 
     sign = "| o " 
y_1 = int(input("Type the value of y: ")) 
x_1 = int(input("Type the value of x: ")) 

if list[y_1 - 1][x_1 - 1] == "| x " or list[y_1 - 1][x_1 - 1] == "| o ": 
    print("The place is already filled by %s |" % list[y_1 - 1][x_1 - 1]) 
    move(list, wins_1, wins_2) 
else: 
    list[y_1 - 1][x_1 - 1] = sign 

print_board(list) # 

wins_1, wins_2 = check_winner(sign, wins_1, wins_2) 

turn += 1 
return wins_1, wins_2 

如果用户输入一个列表的[x] [y]并且其已经被ax或o(它的一个tic tac toe游戏)取得,它应该打印(“这个地方是已经填充“),然后让用户键入另一组x和y来放置他的x/o。调用其中的函数

这让我觉得,也许我可以简单地调用函数并重复它自己。它运行良好,没有错误。 但由于某种原因,它多次印制我的纸板两次(或多次按“x”和“y”以填充已充满的地方)。

有人可以解释当你在函数中调用函数时会发生什么。我的代码究竟发生了什么?

注:我的代码很长,这只是它的一小部分。如果需要更多的代码,请告诉我。

以下是输出示例:请注意,在将x/o放在已填充的位置之后,它将打印板2次。

Type the name of player 1: 1 
type the name of player 2: 1 
____________________________________________________________ 
player_1: 1  X  wins: 0 
player_2: 1  O  wins: 0 
____________________________________________________________ 
Type the value of y: 1 
Type the value of x: 1 
------------- 
| x | | | 
------------- 
| | | | 
------------- 
| | | | 
------------- 
____________________________________________________________ 
player_1: 1  X  wins: 0 
player_2: 1  O  wins: 0 
____________________________________________________________ 
Type the value of y: 1 
Type the value of x: 1 
The place is already filled by | x | 
Type the value of y: 1 
Type the value of x: 2 
------------- 
| x | o | | 
------------- 
| | | | 
------------- 
| | | | 
------------- 
------------- 
| x | o | | 
------------- 
| | | | 
------------- 
| | | | 
------------- 
____________________________________________________________ 
player_1: 1  X  wins: 0 
player_2: 1  O  wins: 0 
____________________________________________________________ 
Type the value of y: 

整个代码:

wins_1 = 0 
wins_2 = 0 
turn = 0 
board = [ 
     ['| ', '| ', '| ', '| '], 
     ['| ', '| ', '| ', '| '], 
     ['| ', '| ', '| ', '| '] 
    ] 
def print_board(board_list): 

    for i in range(len(board_list)): 
     print(" -------------\n %s" % "".join(board_list[i])) # .join binder 2 items sammen. 
    print(" -------------") 

player_1 = input("Type the name of player 1: ") 
player_2 = input("type the name of player 2: ") 
def game_info(player_1, player_2, wins_1, wins_2): # prints layout 
    print("_" * 60) 
    print("player_1: %s  X  wins: %s" 
      "\nplayer_2: %s  O  wins: %s" 
      % (str(player_1), str(wins_1), str(player_2), str(wins_2))) 
    print("_" * 60) 
# ovenstående viser output af wins og navn 



def print_winner(sign, wins_1, wins_2): 

    if sign == "| x ": 
     print("%s got 3 in a row, %s wins!" % (player_1, player_1)) 
     wins_1 += 1 
    else: 
     print("%s got 3 in a row, %s wins!" % (player_2, player_2)) 
     wins_2 += 1 
    return wins_1, wins_2 


def check_winner(sign, wins_1, wins_2): 

    # check vertical: | 
    for x in range(0,3): 
     if board[0][x] == sign and board[1][x] == sign and board[2][x] == sign: 
      wins_1, wins_2 = print_winner(sign, wins_1, wins_2) 

    # check horizontal: - 
    for x in range(0, 3): 
     if board[x][0] == sign and board[x][1] == sign and board[x][2] == sign: 
      wins_1, wins_2 = print_winner(sign, wins_1, wins_2) 

    # check diagonal: \ 
    if board[0][0] == sign and board[1][1] == sign and board[2][2] == sign: 
     wins_1, wins_2 = print_winner(sign, wins_1, wins_2) 
    elif board[0][2] == sign and board[1][1] == sign and board[2][0] == sign: 
     wins_1, wins_2 = print_winner(sign, wins_1, wins_2) 

    return wins_1, wins_2 


def move(list, wins_1, wins_2): 
    global turn 
    if turn % 2 == 0: 
     sign = "| x " 
    else: 
     sign = "| o " 

    y_1 = int(input("Type the value of y: ")) 
    x_1 = int(input("Type the value of x: ")) 

    if list[y_1 - 1][x_1 - 1] == "| x " or list[y_1 - 1][x_1 - 1] == "| o ": 
     print("The place is already filled by %s |" % list[y_1 - 1][x_1 - 1]) 
     move(list, wins_1, wins_2) 
    else: 
     list[y_1 - 1][x_1 - 1] = sign 

    print_board(list) # 

    wins_1, wins_2 = check_winner(sign, wins_1, wins_2) 

    turn += 1 
    return wins_1, wins_2 





while True: 
    game_info(player_1, player_2, wins_1, wins_2) 
    wins_1, wins_2 = move(board, wins_1, wins_2) # move() sætter et 'tegn' og returner win1/win2 

    if wins_1 or wins_2 == 1: 
     break 
print("*****************************************************\nGame Over. \n IT WORKED!") 
+1

intendation是可疑的。这是最后的“移动”方法吗?或者其他一些方法?方法在出现输入问题时不应该自行调用。 –

+0

你好,塞巴斯蒂安,你可以请包括整个代码?如果你想要的话,你可以复制和粘贴,我会将它改为代码格式,然后我可以看看整个代码并正确运行。 –

+0

https://gist.github.com/sebastian3495/6c856c6d7f93f629f91496893c2b5cfb 看看整个代码。 –

回答

0

下面是答案:

def move(list, wins_1, wins_2): 
    printed = False 
    global turn 
    if turn % 2 == 0: 
     sign = "| x " 
    else: 
     sign = "| o " 

    y_1 = int(input("Type the value of y: ")) 
    x_1 = int(input("Type the value of x: ")) 

    if list[y_1 - 1][x_1 - 1] == "| x " or list[y_1 - 1][x_1 - 1] == "| o ": 
     print("The place is already filled by %s |" % list[y_1 - 1][x_1 - 1]) 
     move(list, wins_1, wins_2) 
     printed = True 
    else: 
     list[y_1 - 1][x_1 - 1] = sign 

    if printed == False: 
     print_board(list) 

    wins_1, wins_2 = check_winner(sign, wins_1, wins_2) 

    turn += 1 
    return wins_1, wins_2 

我有增加了一个名为printed所以只打印变量一旦

然后,这可以让你的程序只打印如果尚未打印

希望这有助于:)

+0

编辑:nvm,我忽略了你的代码改变了我的东西。谢谢您的帮助。 –

+0

没关系!很高兴我能帮上忙。如果你有机会+1我的答案,这也将真正帮助我! –

+0

我会但不幸的是我无法这样做。因为我是新用户。 –

0

你进入无限循环。你第一次穿过move。然后你第二次启动move。假设第二个move结束,那么你的程序将在第一个叫做第二个的move中停止。只要if list为真,您就会实例化move的另一个实例。

你想要达到的是一个简单的例子是recursion

def factorial(n): 
    if n == 1: 
     return 1 
    else: 
     return n * factorial(n-1) 

您可以跟踪功能如何运作通过增加两个print()函数以前的功能定义:

def factorial(n): 
    print("factorial has been called with n = " + str(n)) 
    if n == 1: 
     return 1 
    else: 
     res = n * factorial(n-1) 
     print("intermediate result for ", n, " * factorial(" ,n-1, "): ",res) 
     return res 

print(factorial(5)) 

输出:

factorial has been called with n = 5 
factorial has been called with n = 4 
factorial has been called with n = 3 
factorial has been called with n = 2 
factorial has been called with n = 1 
intermediate result for 2 * factorial(1): 2 
intermediate result for 3 * factorial(2): 6 
intermediate result for 4 * factorial(3): 24 
intermediate result for 5 * factorial(4): 120 
120 
+0

对不起,但我没有完全得到,我该如何在我的代码中实现? –

+0

首先我开始“移动”功能,然后再次调用“移动”功能。 第一个“移动”功能然后芬兰语,然后第二个功能开始。或者我的第二个函数首次运行,因为我调用它,当结束时,我的“移动”函数将运行? 这是一个有点cunfusing .. –

+0

编辑:你能想出一个替代方案来解决这个问题? 请记住,它应该不断要求一个新的输入,如果这个地方已经被采用,那是我的目标。 –