井字遊戲模式有問題? (Having problem in Tic-Tac-Toe game pattern?)


問題描述

井字遊戲模式有問題? (Having problem in Tic‑Tac‑Toe game pattern?)

當我運行此代碼時,它只為 j=1 返回 d['a'] 我應該怎麼做才能增加 j 的值?

def pattern():
    d = {'a': '   |   |   ', 'b': '‑‑‑ ‑‑‑ ‑‑‑'}
    j = 1
    while j <= 11:
        if j not in [4,8]:
            return d['a']
        else:
            return d['b']
        j+=1


參考解法

方法 1:

I see that you are trying to get the pattern one by one every time look executes. One alternative would be to put all the results in a array and then return the array.

def pattern():
    d = {'a': '   |   |   ', 'b': '‑‑‑ ‑‑‑ ‑‑‑'}
    j = 1
    result_pattern = []
    while j <= 11:
        if j not in [4,8]:
            result_pattern.append(d['a'])
        else:
            result_pattern.append(d['b'])
        j+=1

    # return your array and loop over it after function call.
    return result_pattern 

You will use your function something like this:

p = pattern()
for item in p:
    # do something with your result.

(by Dhruv pratapAjay Rathore)

參考文件

  1. Having problem in Tic‑Tac‑Toe game pattern? (CC BY‑SA 2.5/3.0/4.0)

#tic-tac-toe #while-loop #python-3.x






相關問題

為什麼我的遞歸沒有返回但最終導致堆棧溢出? (Why does my recursion not return but end up in a stack overflow?)

重新開始遊戲 (Restarting a game)

沒有人工智能的井字遊戲 (Tic-Tac-Toe without AI)

Tictactoe 遊戲意外結束輸入期待 IF MySQL (Tictactoe game Unexpected end of input expecting IF MySQL)

如何在第一次和第二次觸摸時執行不同的事件(TicTacToe for Android) (how to do different event on first and second touch (TicTacToe for Android))

TicTacToe Python 檢查獲勝者 (TicTacToe Python Check for Winner)

使用 Java 可定制的井字遊戲板 (Customizable TicTacToe game board with Java)

我的井字遊戲理論上是個大問題,但對我來說一切似乎都很好 (Theoretically big problem with my Tic tac toe game , but for me all seems good)

遊戲線程問題 (Issues with threads in game)

我在使用 Turtle 圖形檢查井字遊戲中的獲勝者時遇到了一些問題 (I am having some problems checking the winner in a tic tac toe game using Turtle graphics)

為什麼我選擇第一個位置後立即收到勝利信息 (Why do i get the victory message right after choosing the first position)

如何修復此井字遊戲的未捕獲參考錯誤 (How do I fix an uncaught reference error for this TicTacToe)







留言討論