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!")
intendation是可疑的。這是最後的「移動」方法嗎?或者其他一些方法?方法在出現輸入問題時不應該自行調用。 –
你好,塞巴斯蒂安,你可以請包括整個代碼?如果你想要的話,你可以複製和粘貼,我會將它改爲代碼格式,然後我可以看看整個代碼並正確運行。 –
https://gist.github.com/sebastian3495/6c856c6d7f93f629f91496893c2b5cfb 看看整個代碼。 –