-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
106 lines (86 loc) · 2.65 KB
/
main.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
from flask import Flask, render_template, request, redirect, url_for
from game import up, down, left, right, format_grid, add_random, check_survive, \
score_hori, score_vert, find_best_move
import random
DEBUG = False
### Flask App ###
app = Flask(__name__)
move_ls = [left, right, up, down]
score_ls = [score_hori, score_hori, score_vert, score_vert]
@app.route('/', methods=['GET', 'POST'])
def main():
global grid, move_no, score
grid = 0
grid = add_random(grid)
grid = add_random(grid)
move_no = 0
score = 0
return render_template('home.html', grid=format_grid(grid), score=score, state=True, moves=move_no)
@app.route('/gameup', methods=['GET', 'POST'])
def gameup():
global grid, move_no, score
try:
new_grid = up(grid)
except:
return redirect(url_for('main'))
score += score_vert(grid)
if new_grid != grid:
grid = add_random(new_grid)
move_no += 1
return render_template('home.html', grid=format_grid(grid), score=score, state=check_survive(grid), moves=move_no)
@app.route('/gamedown', methods=['GET', 'POST'])
def gamedown():
global grid, move_no, score
try:
new_grid = down(grid)
except:
return redirect(url_for('main'))
score += score_vert(grid)
if new_grid != grid:
grid = add_random(new_grid)
move_no += 1
return render_template('home.html', grid=format_grid(grid), score=score, state=check_survive(grid), moves=move_no)
@app.route('/gameleft', methods=['GET', 'POST'])
def gameleft():
global grid, move_no, score
try:
new_grid = left(grid)
except:
return redirect(url_for('main'))
score += score_hori(grid)
if new_grid != grid:
grid = add_random(new_grid)
move_no += 1
return render_template('home.html', grid=format_grid(grid), score=score, state=check_survive(grid), moves=move_no)
@app.route('/gameright', methods=['GET', 'POST'])
def gameright():
global grid, move_no, score
try:
new_grid = right(grid)
except:
return redirect(url_for('main'))
score += score_hori(grid)
if new_grid != grid:
grid = add_random(new_grid)
move_no += 1
return render_template('home.html', grid=format_grid(grid), score=score, state=check_survive(grid), moves=move_no)
@app.route('/bot', methods=['GET', 'POST'])
def bot():
global grid, move_no, score
try:
move = find_best_move(grid)
except:
return redirect(url_for('main'))
if move == -1:
state=False
else:
new_grid = move_ls[move](grid)
score += score_ls[move](grid)
if new_grid != grid:
grid = add_random(new_grid)
move_no += 1
state = True
# additional debug info: eval_score, depth
return render_template('home.html', grid = format_grid(grid), score=score, state=state, debug=DEBUG, moves=move_no)
if __name__ == '__main__':
app.run(debug=True)