2016-11-06 31 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

編輯:你能想出一個替代方案來解決這個問題? 請記住,它應該不斷要求一個新的輸入,如果這個地方已經被採用,那是我的目標。 –

相關問題