2016-04-29 73 views
0

我在燒瓶中製作一個tic tac toe用於學習目的。董事會被表示爲字典,是一個全局變量。我想有一個「復位」板的按鈕,使其可以再次播放,但是我的代碼不起作用重置哪個執行,但板值不會改變。燒瓶重置全球詞典

任何想法我做錯了什麼? 非常感謝!

theBoard = {1:' ', 2:' ', 3:' ', 4: ' ', 5:' ', 6: ' ', 7:' ', 8:' ', 9:' '} 

@app.route('/reset', methods=["GET", "POST"]) 
def reset(): 
    for i in range (1,9): 
     theBoard[i] == ' ' 
    return render_template("test.html", theBoard=theBoard) 

@app.route('/play', methods=["GET","POST"]) 
def test1(): 
    return render_template("test.html", theBoard=theBoard) 

@app.route('/play1', methods=["GET", "POST"]) 
def test(): 
    if gameover(theBoard): 
     True 
     return 'the game is over1' 
    else: 
     x = request.form['move'] 
     move = int(x) 
     valid_moves = [1,2,3,4,5,6,7,8,9] 
    if move not in valid_moves: 
     return 'you did not specify a valid move, please try again!' 
    elif theBoard[move] != ' ': 
     return 'you can not play that space, it is taken' 
    else: 
     theBoard[move] = 'X' 
     if gameover(theBoard): 
      True 
      return 'the game is over2' 
     if winning_X(theBoard): 
     <and much more code - this part works> 

在HTML:

<!DOCTYPE html> 
<html> 
<head> 
<title>Test</title> 
</head> 

<body> 

<div class="enter name"> 
    <form action="/play1" method="POST"> 
    <lable>Please specify your move (1,2,3,4,5,6,7,8,9)</lable> 
    <input type="number" name="move" value""> 
    <input type="submit" value="Make your move!"> 
    </form> 
</div> 
<div> 
<table border="1"> 
<tr id="row1"> 



    {% if theBoard[1]!=' ' %} 
    <td><h1>{{ theBoard[1] }} </h1></td> 
    {% else %} 
    <td><h1>&nbsp;1&nbsp;</h1></td> 
    {% endif %} 

    {% if theBoard[2]!=' ' %} 
    <td><h1>{{ theBoard[2] }} </h1></td> 
    {% else %} 
    <td><h1>&nbsp;2&nbsp;</h1></td> 
    {% endif %} 

    {% if theBoard[3]!=' ' %} 
    <td><h1>{{ theBoard[3] }} </h1></td> 
    {% else %} 
    <td><h1>&nbsp;3&nbsp;</h1></td> 
    {% endif %} 

<tr id="row2"> 
    {% if theBoard[4]!=' ' %} 
    <td><h1>{{ theBoard[4] }} </h1></td> 
    {% else %} 
    <td><h1>&nbsp;4&nbsp;</h1></td> 
    {% endif %} 
    {% if theBoard[5]!=' ' %} 
    <td><h1>{{ theBoard[5] }} </h1></td> 
    {% else %} 
    <td><h1>&nbsp;5&nbsp;</h1></td> 
    {% endif %} 

    {% if theBoard[6]!=' ' %} 
    <td><h1>{{ theBoard[6] }} </h1></td> 
    {% else %} 
    <td><h1>&nbsp;6&nbsp;</h1></td> 
    {% endif %} 

<tr id="row3"> 
    {% if theBoard[7]!=' ' %} 
    <td><h1>{{ theBoard[7] }} </h1></td> 
    {% else %} 
    <td><h1>&nbsp;7&nbsp;</h1></td> 
    {% endif %} 
    {% if theBoard[8]!=' ' %} 
    <td><h1>{{ theBoard[8] }} </h1></td> 
    {% else %} 
    <td><h1>&nbsp;8&nbsp;</h1></td> 
    {% endif %} 

    {% if theBoard[9]!=' ' %} 
    <td><h1>{{ theBoard[9] }} </h1></td> 
    {% else %} 
    <td><h1>&nbsp;9&nbsp;</h1></td> 
    {% endif %} 
</table> 
</div> 

<div class="reset"> 
<form action="/reset" method="GET"> 
    <lable>Do you wanna play again?</lable> 
    <button>Play!</button> 
</form> 
</div> 

</body> 
</html> 
{% endblock %} 
+0

您還可以分享您用於發佈主板的標記的相關代碼片段嗎? – joemurphy

+0

@joemurphy我認爲你的意思是HTML? (抱歉,我仍在學習)。上面加了 –

回答

1

下面的代碼是導致從我可以看到的錯誤:

theBoard[i] == ' ' 

以上實際上執行的比較不是將其更改爲:

theBoard[i] = ' ' 
+0

對不起還沒有看到你的答案,一會兒回來! –

2

HTML按鈕調用其上執行/復位,但是板的值不改變。

def reset(): 
    for i in range (1,9): 
     theBoard[i] == ' ' # <--- This line 
    return render_template("test.html", theBoard=theBoard) 

您使用==這是比較返回要麼TrueFalse,你想要什麼=運算符(賦值運算符,一個等號),所以:theBoard[i] = ' '