backgammon/app.py

77 lines
1.7 KiB
Python
Raw Normal View History

2018-05-19 12:12:13 +00:00
from flask import Flask, request
from flask_json import FlaskJSON, as_json_p
from flask_cors import CORS
from board import Board
import main
import random
from network import Network
app = Flask(__name__)
app.config['JSON_ADD_STATUS'] = False
app.config['JSON_JSONP_OPTIONAL'] = False
json = FlaskJSON(app)
CORS(app)
config = main.config.copy()
config['model'] = "player_testings"
config['ply'] = "1"
config['board_representation'] = 'quack-fat'
network = Network(config, config['model'])
network.restore_model()
@app.route('/get_board', methods=['GET'])
@as_json_p
def get_board():
return {'board':'0, 2, 0, 0, 0, 0, -5, 0, -3, 0, 0, 0, 5, -5, 0, 0, 0, 3, 0, 5, 0, 0, 0, 0, -2, 0'}
def check_move(prev, curr):
# TODO: Decide on player system and implement roll properly
legal_states = Board.calculate_legal_states(tuple(prev), -1, [1,2])
truth_list = [list(curr) == list(ele) for ele in legal_states]
return any(truth_list)
@app.route('/bot_move', methods=['POST'])
def bot_move():
data = request.get_json(force=True)
board = [int(x) for x in data['board'].split(',')]
roll = (random.randrange(1,7), random.randrange(1,7))
print(roll)
board, _ = network.make_move(tuple(board), roll, 1)
print("Boards!:",board)
return ",".join([str(x) for x in list(board)])
@app.route('/post_board', methods=['POST'])
def post_board():
data = request.get_json(force=True)
board = [int(x) for x in data['board'].split(',')]
prev_board = [int(x) for x in data['prev_board'].split(',')]
str_board = ",".join([str(x) for x in (board if check_move(prev_board,board) else prev_board)])
print(check_move(prev_board, board))
return str_board
if __name__ == '__main__':
app.run()