diff --git a/app.py b/app.py index d70bbd2..8d63c81 100644 --- a/app.py +++ b/app.py @@ -1,4 +1,4 @@ -from flask import Flask, request +from flask import Flask, request, jsonify from flask_json import FlaskJSON, as_json_p from flask_cors import CORS from board import Board @@ -24,6 +24,54 @@ 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, [total, 0]), total]) + # print(sets) + 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 + 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): + + is_quad = roll[0] == roll[1] + + total_moves = roll[0] + roll[1] if not is_quad else int(roll[0]) * 4 + if is_quad: + roll = [roll[0]] * 4 + + total_moves, roll, board = tmp_name(from_board, to_board, list(roll), player, total_moves, is_quad) + return board, total_moves, roll + + @app.route('/get_board', methods=['GET']) @as_json_p def get_board(): @@ -61,16 +109,32 @@ def bot_move(): 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(',')] - str_board = ",".join([str(x) for x in (board if check_move(prev_board,board) else prev_board)]) + print(board) - print(check_move(prev_board, board)) + total_roll = int(data['total_roll']) + + return_board, total_moves, roll = calc_move_stuff(tuple(prev_board), tuple(board), tuple(roll), player) - return str_board + + + 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()