2016-04-29 8 views
0

私は学習目的のためにフラスコでチックタックつま先を作っています。ボードは辞書として表され、グローバル変数です。私はボタンを空にするためにボードを "リセット"したいのですが、私のコードは動作しません(エラーなく実行されますが、ボードは同じままです)フラスコリセットグローバル辞書

htmlボタンは/リセットは実行されますが、ボードの値は変更されません。

私が間違っていることは何ですか? ありがとう!

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ボタンが実行される/リセットを呼び出し、しかしボードの値は変更されません。 TrueまたはFalseのいずれかを返す比較のために、何がしたいことはそう=演算子(代入演算子、シングル等号)、ですあなたは==を使用

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

theBoard[i] = ' '