-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAPI.py
101 lines (86 loc) · 3.47 KB
/
API.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
from flask import Flask,request,render_template
from flask_restful import Resource, Api
from sudoku import setPuzzle,decodeSudoku,solvePuzzle,isValidBoard,setNewUniquePuzzle
import configparser
import multiprocessing
import time
config = configparser.ConfigParser()
config.read('config.ini')
boardSize = int(config['DEFAULT']['boardSize'])
app = Flask(__name__, static_url_path='')
api = Api(app)
'''
Load html page for SuDoKu board
'''
@app.route("/")
def index():
return render_template('game.html')
@app.route("/tutorial")
def tutorial():
return render_template('tutorial.html')
'''
Creates a brand new puzzle when GET requested
http://127.0.0.1:5000/new
http://127.0.0.1:5000/new?removeCells=40
removeCells declares how many max cells can be removed
'''
class setAPuzzle(Resource):
def get(self):
board = [[0 for x in range(boardSize)] for y in range(boardSize)]
setPuzzle(board,0 ,0, True)
# user input is super evil so we default to a puzzle instead of humouring them :P
try:
removeCells = int(request.args.get('removeCells'))
if(removeCells != None and (removeCells > 0) and removeCells < (boardSize*boardSize-1)):
setNewUniquePuzzle(board,removeCells)
else:
setNewUniquePuzzle(board,40)
except:
setNewUniquePuzzle(board,40)
#if somehow the board managed to be generated invalid (which should never happen but never hurts to be extra careful)
while isValidBoard(board):
setNewUniquePuzzle(board,40)
return {'board': board}
api.add_resource(setAPuzzle, '/new')
'''
solves the puzzle GET revolsed with one
Why GET and not POST? because javascript is evil.
Since I didn't know if some of the zeros would be cut off I used dots instead of 0's
http://127.0.0.1:5000/solve?puzzle=....8.9716.34.7852798..134643....5....71..43.5.2349.1.3...1...4..59..163..6.3....
'''
class solveAPuzzle(Resource):
def get(self):
board = [[0 for x in range(boardSize)] for y in range(boardSize)]
puzzle = request.args.get('puzzle')
if(puzzle != None):
outputBoard = decodeSudoku(puzzle)
if(outputBoard == None):
return {'message': 'Undefined input'}
else:
result,outputBoard = (solvePuzzle(outputBoard,0,0))
if(result == None):
result = "False"
if(outputBoard == None):
outputBoard = "False"
return {'isSolvable': result,'board': outputBoard}
api.add_resource(solveAPuzzle, '/solve')
'''
If user has submitted a puzzle we want to know whether it's valid or not this function check and responds with
isValid: true
isValid: false
http://127.0.0.1:5000/valid?puzzle=254683971613497852798251346431768529967125438582349617329516784845972163176834295
http://127.0.0.1:5000/valid?puzzle=154683971613497852798251346431768529967125438582349617329516784845972163176834295
'''
class isAValidPuzzle(Resource):
def get(self):
board = [[0 for x in range(boardSize)] for y in range(boardSize)]
puzzle = request.args.get('puzzle')
if(puzzle != None):
outputBoard = decodeSudoku(puzzle)
if(outputBoard == None):
return {'message': 'Undefined input'}
else:
return {'isValid': isValidBoard(outputBoard)}
api.add_resource(isAValidPuzzle, '/valid')
if __name__ == '__main__':
app.run(debug=False)