-
Notifications
You must be signed in to change notification settings - Fork 6
/
battleship_web_app_template.py
58 lines (43 loc) · 1.41 KB
/
battleship_web_app_template.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
from flask import *
from battleship import *
import random
from pymongo import *
app = Flask(__name__)
client = MongoClient('127.0.0.1', 27017)
db = client.ship_database
coordinates = db.coordinates
def RandomShip():
global ship_X, ship_Y, won
ship_X = random.randint(0,4)
ship_Y = random.randint(0,5)
won = False
print("ship_X: ",ship_X)
print("ship_Y: ",ship_Y)
@app.route('/') #decorator
def root():
global grid
################
#your code here#
################
RandomShip()
grid = initialiseGrid()
coordinates.remove({})
return render_template('main.html', grid=grid)
@app.route('/calculate', methods = ["POST"])
def calculate():
global won
data = request.form
################
#your code here#
################
X = data['X']
Y = data['Y']
if validateRow(X) and validateCol(Y):
xy_coord = { "X": X, "Y": Y }
coordinates.insert_one(xy_coord)
won = checkResult(grid, int(X), int(Y), ship_X, ship_Y, won)
if won:
return render_template('won.html', coords=list(coordinates.find({})))
return render_template('main.html', grid=grid)
if __name__ == '__main__':
app.run(port = 6789, debug = True)