|
|
|
from flask import Flask, request, jsonify |
|
from flask_cors import CORS |
|
import numpy as np |
|
from tensorflow import keras |
|
import json |
|
import os |
|
|
|
app = Flask(__name__) |
|
CORS(app) |
|
|
|
class TicTacToeAI: |
|
def __init__(self, model_path='model/model.keras'): |
|
self.model_path = model_path |
|
self.model = None |
|
self.load_model() |
|
|
|
def load_model(self): |
|
"""Wczytuje model z pliku""" |
|
if os.path.exists(self.model_path): |
|
self.model = keras.models.load_model(self.model_path) |
|
return True |
|
return False |
|
|
|
def get_move(self, board): |
|
"""Zwraca najlepszy ruch dla danego stanu planszy""" |
|
if self.model is None: |
|
raise ValueError("Model nie został wczytany") |
|
|
|
board_array = np.array(board) |
|
predictions = self.model.predict(board_array.reshape(1, -1), verbose=0)[0] |
|
valid_moves = np.where(board_array == 0)[0] |
|
|
|
if len(valid_moves) == 0: |
|
raise ValueError("Brak dostępnych ruchów") |
|
|
|
valid_predictions = [(i, pred) for i, pred in enumerate(predictions) if i in valid_moves] |
|
return int(max(valid_predictions, key=lambda x: x[1])[0]) |
|
|
|
|
|
ai = TicTacToeAI() |
|
|
|
@app.route('/api/move', methods=['POST']) |
|
def get_move(): |
|
""" |
|
Endpoint do wykonania ruchu AI |
|
|
|
Przykładowe żądanie: |
|
{ |
|
"board": [0, 0, 0, 0, 1, 0, 0, 0, -1] |
|
} |
|
""" |
|
try: |
|
data = request.get_json() |
|
|
|
if 'board' not in data: |
|
return jsonify({'error': 'Brak planszy w żądaniu'}), 400 |
|
|
|
board = data['board'] |
|
|
|
if len(board) != 9: |
|
return jsonify({'error': 'Nieprawidłowy rozmiar planszy'}), 400 |
|
|
|
if not all(x in [0, 1, -1] for x in board): |
|
return jsonify({'error': 'Nieprawidłowe wartości na planszy'}), 400 |
|
|
|
move = ai.get_move(board) |
|
|
|
return jsonify({ |
|
'status': 'success', |
|
'move': move |
|
}) |
|
|
|
except Exception as e: |
|
return jsonify({'error': str(e)}), 500 |
|
|
|
|
|
|
|
@app.route('/api/game', methods=['POST']) |
|
def add_game(): |
|
""" |
|
Endpoint do dodania gry do pliku |
|
|
|
Przykładowe żądanie: |
|
{ |
|
"board": [0, 0, 0, 0, 1, 0, 0, 0, -1], |
|
"win": true |
|
} |
|
""" |
|
try: |
|
data = request.get_json() |
|
|
|
if 'board' not in data: |
|
return jsonify({'error': 'Brak planszy w żądaniu'}), 400 |
|
|
|
if 'win' not in data: |
|
return jsonify({'error': 'Brak informacji o wygranej w żądaniu'}), 400 |
|
|
|
board = data['board'] |
|
win = data['win'] |
|
|
|
if len(board) != 9: |
|
return jsonify({'error': 'Nieprawidłowy rozmiar planszy'}), 400 |
|
|
|
if not all(x in [0, 1, -1] for x in board): |
|
return jsonify({'error': 'Nieprawidłowe wartości na planszy'}), 400 |
|
|
|
with open('gry.json', 'r') as file: |
|
games_data = json.load(file) |
|
|
|
games_data.append({ |
|
'board': board, |
|
'win': win |
|
}) |
|
|
|
with open('gry.json', 'w') as file: |
|
json.dump(games_data, file) |
|
|
|
return jsonify({ |
|
'status': 'success' |
|
}) |
|
|
|
except Exception as e: |
|
return jsonify({'error': str(e)}), 500 |
|
|
|
|
|
|
|
@app.route('/api/status', methods=['GET']) |
|
def get_status(): |
|
"""Sprawdza status modelu""" |
|
return jsonify({ |
|
'status': 'success', |
|
'model_loaded': ai.model is not None, |
|
'model_path': ai.model_path |
|
}) |
|
|
|
if __name__ == '__main__': |
|
app.run(host='0.0.0.0', port=5001) |
|
|