-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
60 lines (39 loc) · 1.04 KB
/
Copy pathapp.py
File metadata and controls
60 lines (39 loc) · 1.04 KB
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
from flask import Flask, render_template, url_for, request, redirect
from battleship import *
app = Flask(__name__)
app.secret_key = "some_secret"
field = Grid()
field.generate_ships()
move = 0
@app.route("/")
def index():
"""Main page."""
print(field.ships)
return render_template(
"index.html",
grid=field.initialise_grid(),
cell=Cell(),
alive_ships=len(field.alive_ships),
ships=field.ships,
move=move,
)
@app.route("/fire")
def fire():
"""Makes a shot at the coordinates of the field."""
global move
row = request.args.get("rowIndex", default=-1, type=int)
column = request.args.get("columnIndex", default=-1, type=int)
result = field.fire(row, column)
move += 1
print(move, result)
return redirect(url_for("index"))
@app.route("/restart")
def restart():
"""Restart game."""
global field, move
field = Grid()
field.generate_ships()
move = 0
return redirect(url_for("index"))
if __name__ == "__main__":
app.run()