在tictactoe遊戲中重複輸入錯誤 (Error in repeating input in a tictactoe game)


問題描述

在tictactoe遊戲中重複輸入錯誤 (Error in repeating input in a tictactoe game)

我試圖阻止用戶輸入一個已經被標記的方塊,但是 for 循環繼續到下一個玩家的輸入,而不將 i 的值減一,所以玩家 1 可以重複他的輸入。我該如何解決這個問題?


arr = [[0,0,0],[0,0,0],[0,0,0]]
grid = grid(arr)
grid.print_grid()

for i in range(9):
    row = int(input("Enter the row name: "))
    col = int(input("Enter the column name: "))
    if(arr[row][col] == 0):
        if(i%2):
            arr[row][col] = 1
        else:
            arr[row][col] = 2
    else:
        print("\nThat square has already been marked! Please select another square")
        i = i‑1
        continue
    grid.print_grid()
    res = grid.grid_checker()
    if (res == 1):
        print("\nPlayer 1 wins the game!")
        break
    elif(res == 2):
        print("\nPlayer 2 wins the game!")
        break
    elif(i == 8):
        print("\nThe game has ended in a draw!")

參考解法

方法 1:

You need to store another variable to keep track of whose turn it is. You cannot modify the variable you are looping on while you are in the loop body. This means that i cannot be manipulated while you are running in the loop. Here's how I would change it.

turn = 0
while True:
    row = int(input("Enter the row name: "))
    col = int(input("Enter the column name: "))
    if(arr[row][col] == 0):
        if(i%2):
            arr[row][col] = 1
            turn = turn + 1
        else:
            arr[row][col] = 2
            turn = turn + 1
    else:
        print("\nThat square has already been marked! Please select another square")
        continue
    grid.print_grid()
    res = grid.grid_checker()
    if (res == 1):
        print("\nPlayer 1 wins the game!")
        break
    elif(res == 2):
        print("\nPlayer 2 wins the game!")
        break
    elif(turn == 8):
        print("\nThe game has ended in a draw!")

Here we're saving the turn number in the variable turn and only incrementing the variable when we can confirm a player has successfully completed their turn.

Why you cannot modify i: For optimisations, loops are often expanded by python before they are converted to assembly instructions. For example a simple loop like this:

for i in range(9):
    print(i)

May be expanded to something like this:

i = 0
print(i)
i = 1
print(i)
# and so on, upto
i = 9
print(i)

This is done to avoid having to jump around in memory. So as you can see here, i is reassigned for every iteration of the loop. Therefore, even if you change the value of i in the body of the loop, it will simply be reassigned before the next iteration.

(by Reuben GeorgeShubham Vasaikar)

參考文件

  1. Error in repeating input in a tictactoe game (CC BY‑SA 2.5/3.0/4.0)

#continue #Python #loops






相關問題

skrip shell: loop bersarang dan lanjutkan (shell script : nested loop and continue)

syntaxError:“繼續”在循環中不正確 (syntaxError: 'continue' not properly in loop)

php continue - 替代方式? (php continue - alternative way?)

Python 中的歐拉 #4 (Euler #4 in Python)

if-continue vs 嵌套 if (if-continue vs nested if)

如何返回上一個 while 循環? (How do I return to the previous while loop?)

php foreach 繼續 (php foreach continue)

在while循環中繼續的奇怪行為 (weird behaviour of continue in while loop)

是否可以使用 'yield' 來生成 'Iterator' 而不是 Scala 中的列表? (Is it possible to use 'yield' to generate 'Iterator' instead of a list in Scala?)

BASH:在 for 循環中使用 continue (BASH: Using a continue in a for loop)

rand() 函數後繼續指令的問題 (Problem with continue instruction after rand() function)

在tictactoe遊戲中重複輸入錯誤 (Error in repeating input in a tictactoe game)







留言討論