-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
65 lines (53 loc) · 2.24 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from flask import Flask, render_template, request, redirect
from logic import Game, GameStats
app = Flask(__name__)
game = Game()
game_stats = GameStats()
@app.route("/play", methods=['POST','GET'])
def play():
global game
# make a play
message = None
play_button_pressed = ('Play' in request.form.getlist('button'))
if request.method == 'POST' and play_button_pressed:
selected_gobbler = request.form['gobbler_size']
board_position = request.form['board_position']
select_success = game.select_gobbler(selected_gobbler)
place_success, winner = game.place_selected_gobbler(board_position)
if not select_success or not place_success:
message = 'Invalid selection!'
else:
game_stats.record_move(selected_gobbler, board_position)
if winner is not None:
game_stats.save(winner)
# start a new game
new_game_button_pressed = ('New Game' in request.form.getlist('button'))
if request.method == 'POST' and new_game_button_pressed:
return redirect('/')
# view stats
stats_button_pressed = ('View Stats' in request.form.getlist('button'))
if request.method == 'POST' and stats_button_pressed:
return redirect('/stats')
return render_template('play.html', game=game, message=message)
@app.route("/", methods=['POST','GET'])
def index():
global game
button_pressed = ('Start Game' in request.form.getlist('button'))
if request.method == 'POST' and button_pressed:
player_names = request.form.getlist('name')
success, response = game.set_player_names(player_names)
if success:
return redirect('/play')
elif request.method == 'GET':
# start a new game when the index page is loaded
game = Game()
return render_template('index.html')
@app.route("/stats", methods=['POST','GET'])
def stats():
global game_stats
new_game_button_pressed = ('New Game' in request.form.getlist('button'))
if request.method == 'POST' and new_game_button_pressed:
return redirect('/')
return render_template('stats.html')
if __name__ == "__main__":
app.run(debug=True, port=8000)