from flask import Flask, request, jsonify 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() def calc_move_sets(from_board, roll, player): board = from_board sets = [] total = 0 for r in roll: # print("Value of r:", r) sets.append([Board.calculate_legal_states(board, player, [r, 0]), r]) total += r sets.append([Board.calculate_legal_states(board, player, roll), total]) 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 print("To board:\n",to_board) print("All sets:\n",sets) 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]: # print("To board:", to_board) # print(board_set[0]) # print(board_set[1]) 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 # print("Return board!:\n",return_board) return total_moves, roll, return_board def calc_move_stuff(from_board, to_board, roll, player, total_roll, is_quad): total_moves, roll, board = tmp_name(from_board, to_board, list(roll), player, total_roll, is_quad) return board, total_moves, roll @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) # TODO: Fix hardcoded player player = -1 board = [int(x) for x in data['board'].split(',')] prev_board = [int(x) for x in data['prev_board'].split(',')] roll = [int(x) for x in data['roll'].split(',')] quad = data['quad'] == "true" # print(board) total_roll = int(data['total_roll']) 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) str_board = ",".join([str(x) for x in return_board]) str_roll = ",".join([str(x) for x in roll]) return_string = str_board + "#" + str(total_moves) + "#" + str_roll print(return_string) return return_string if __name__ == '__main__': app.run()