2018-05-19 20:01:55 +00:00
|
|
|
from flask import Flask, request, jsonify
|
2018-05-19 12:12:13 +00:00
|
|
|
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()
|
|
|
|
|
|
|
|
|
2018-05-19 20:01:55 +00:00
|
|
|
def calc_move_sets(from_board, roll, player):
|
|
|
|
board = from_board
|
|
|
|
sets = []
|
|
|
|
total = 0
|
|
|
|
for r in roll:
|
2018-05-19 22:38:13 +00:00
|
|
|
# print("Value of r:", r)
|
2018-05-19 20:01:55 +00:00
|
|
|
sets.append([Board.calculate_legal_states(board, player, [r, 0]), r])
|
|
|
|
total += r
|
2018-05-20 17:43:28 +00:00
|
|
|
sets.append([Board.calculate_legal_states(board, player, roll), total])
|
2018-05-19 20:01:55 +00:00
|
|
|
return sets
|
|
|
|
|
|
|
|
|
|
|
|
def tmp_name(from_board, to_board, roll, player, total_moves, is_quad=False):
|
|
|
|
sets = calc_move_sets(from_board, roll, player)
|
|
|
|
return_board = from_board
|
2018-05-20 17:43:28 +00:00
|
|
|
print("To board:\n",to_board)
|
|
|
|
print("All sets:\n",sets)
|
2018-05-19 20:01:55 +00:00
|
|
|
for idx, board_set in enumerate(sets):
|
|
|
|
board_set[0] = list(board_set[0])
|
|
|
|
# print(to_board)
|
|
|
|
# print(board_set)
|
|
|
|
if to_board in board_set[0]:
|
2018-05-19 22:38:13 +00:00
|
|
|
# print("To board:", to_board)
|
|
|
|
# print(board_set[0])
|
|
|
|
# print(board_set[1])
|
2018-05-19 20:01:55 +00:00
|
|
|
total_moves -= board_set[1]
|
|
|
|
# if it's not the sum of the moves
|
|
|
|
if idx < (4 if is_quad else 2):
|
|
|
|
roll[idx] = 0
|
|
|
|
else:
|
|
|
|
roll = [0, 0]
|
|
|
|
return_board = to_board
|
|
|
|
break
|
|
|
|
|
2018-05-19 22:38:13 +00:00
|
|
|
# print("Return board!:\n",return_board)
|
2018-05-19 20:01:55 +00:00
|
|
|
return total_moves, roll, return_board
|
|
|
|
|
2018-05-19 22:38:13 +00:00
|
|
|
def calc_move_stuff(from_board, to_board, roll, player, total_roll, is_quad):
|
2018-05-19 20:01:55 +00:00
|
|
|
|
2018-05-19 22:38:13 +00:00
|
|
|
total_moves, roll, board = tmp_name(from_board, to_board, list(roll), player, total_roll, is_quad)
|
2018-05-19 20:01:55 +00:00
|
|
|
return board, total_moves, roll
|
|
|
|
|
|
|
|
|
2018-05-19 12:12:13 +00:00
|
|
|
@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))
|
2018-05-19 22:38:13 +00:00
|
|
|
# print(roll)
|
2018-05-19 12:12:13 +00:00
|
|
|
board, _ = network.make_move(tuple(board), roll, 1)
|
2018-05-19 22:38:13 +00:00
|
|
|
# print("Boards!:",board)
|
2018-05-19 12:12:13 +00:00
|
|
|
|
|
|
|
return ",".join([str(x) for x in list(board)])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/post_board', methods=['POST'])
|
|
|
|
def post_board():
|
|
|
|
data = request.get_json(force=True)
|
|
|
|
|
2018-05-19 20:01:55 +00:00
|
|
|
# TODO: Fix hardcoded player
|
|
|
|
player = -1
|
|
|
|
|
2018-05-19 12:12:13 +00:00
|
|
|
board = [int(x) for x in data['board'].split(',')]
|
|
|
|
prev_board = [int(x) for x in data['prev_board'].split(',')]
|
2018-05-19 20:01:55 +00:00
|
|
|
roll = [int(x) for x in data['roll'].split(',')]
|
2018-05-19 22:38:13 +00:00
|
|
|
quad = data['quad'] == "true"
|
2018-05-19 20:01:55 +00:00
|
|
|
|
|
|
|
|
2018-05-19 22:38:13 +00:00
|
|
|
# print(board)
|
2018-05-19 20:01:55 +00:00
|
|
|
|
|
|
|
total_roll = int(data['total_roll'])
|
2018-05-19 22:38:13 +00:00
|
|
|
print("total roll is:", total_roll)
|
|
|
|
return_board, total_moves, roll = calc_move_stuff(tuple(prev_board), tuple(board), tuple(roll), player, total_roll, quad)
|
2018-05-19 20:01:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2018-05-19 12:12:13 +00:00
|
|
|
|
2018-05-19 20:01:55 +00:00
|
|
|
str_board = ",".join([str(x) for x in return_board])
|
|
|
|
str_roll = ",".join([str(x) for x in roll])
|
2018-05-19 12:12:13 +00:00
|
|
|
|
|
|
|
|
2018-05-19 20:01:55 +00:00
|
|
|
return_string = str_board + "#" + str(total_moves) + "#" + str_roll
|
2018-05-19 12:12:13 +00:00
|
|
|
|
2018-05-19 20:01:55 +00:00
|
|
|
print(return_string)
|
2018-05-19 12:12:13 +00:00
|
|
|
|
2018-05-19 20:01:55 +00:00
|
|
|
return return_string
|
2018-05-19 12:12:13 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
app.run()
|