File size: 901 Bytes
22ae33a |
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 |
from flask import Flask, jsonify, request
from tictactoe import TicTacToe
app = Flask(__name__)
game = TicTacToe()
@app.route("/make_move", methods=["POST"])
def make_move():
data = request.get_json()
move = data.get("move")
player = data.get("player")
if not move or not player:
return jsonify({"error": "Invalid input. 'move' and 'player' are required."}), 400
try:
valid_move, board_state = game.make_move(move, player)
if valid_move:
return jsonify({"board": board_state, "message": "Move successful."}), 200
else:
return jsonify({"error": "Invalid move. Try again."}), 400
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route("/get_board", methods=["GET"])
def get_board():
return jsonify({"board": game.get_board()}), 200
if __name__ == "__main__":
app.run(debug=True)
|