게시판인베이더 게임으로 배우는 휴리스틱과 AI 브리지예제 소스

예제 소스

인베이더 게임으로 배우는 휴리스틱과 AI 브리지 · 파일 17개

mainwindow.h

#pragma once

#include "gameengine.h"

#include <QElapsedTimer>
#include <QMainWindow>
#include <QPointer>

class GameCanvas;
class QComboBox;
class QLabel;
class QLineEdit;
class QPushButton;
class QSpinBox;
class QThread;
class QTimer;
class QShowEvent;

namespace TcpVlmClient {
class Worker;
}

class MainWindow final : public QMainWindow {
    Q_OBJECT
public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow() override;

    void selectPlayMode(Invader::PlayMode mode);

protected:
    void showEvent(QShowEvent *event) override;
    void keyPressEvent(QKeyEvent *event) override;

private slots:
    void onGameTick();
    void onModeChanged(int index);
    void onNewGame();
    void onTogglePause();
    void onRestart();
    void onPingServer();
    void onCancelVlm();
    void onVlmMoveReady(bool success, int action, int unusedCol, QString errorMessage);
    void onVlmPingReady(bool success, QString errorMessage);
    void onVlmConnectionStateChanged(bool connected, QString endpoint);
    void onVlmTickProgress();

private:
    void pushView();
    void updateStatus();
    void restartMatch();
    void setVlmUiVisible(bool visible);
    void startVlmTcpRequest();
    void finishVlmCleanup();
    void playAiFallbackForVlm();
    void logStepToServer(Invader::PlayerAction a, int actorKind);
    void maybeAppendLocalTrainJsonl(Invader::PlayerAction a, int actorKind);

    Invader::PlayerAction clampAction(int raw) const;

    Invader::GameEngine m_game;
    GameCanvas *m_canvas = nullptr;
    QLabel *m_statusLabel = nullptr;
    QComboBox *m_modeCombo = nullptr;
    QPushButton *m_newGameBtn = nullptr;
    QPushButton *m_pauseBtn = nullptr;
    QPushButton *m_restartBtn = nullptr;
    QLineEdit *m_trainLogEdit = nullptr;

    QTimer *m_gameTimer = nullptr;
    Invader::PlayerAction m_humanPending = Invader::PlayerAction::None;
    bool m_paused = false;

    QWidget *m_vlmConfigRow = nullptr;
    QLineEdit *m_vlmHostEdit = nullptr;
    QLineEdit *m_vlmPortEdit = nullptr;
    QSpinBox *m_vlmTimeoutSpin = nullptr;
    QPushButton *m_vlmConnectBtn = nullptr;
    QPushButton *m_vlmCancelBtn = nullptr;
    QLabel *m_vlmConnLabel = nullptr;

    QThread *m_vlmThread = nullptr;
    QPointer<TcpVlmClient::Worker> m_vlmWorker;

    QTimer *m_vlmProgressTimer = nullptr;
    QElapsedTimer m_vlmProgressClock;
    bool m_vlmInFlight = false;
    bool m_vlmHasBufferedAction = false;
    Invader::PlayerAction m_vlmBufferedAction = Invader::PlayerAction::None;
    /// VLM 틱 로깅: 2=VLM(클라이언트 로그 생략·서버 predict 로그), 1=로컬 AI 폴백.
    int m_vlmPendingLogActor = 2;

    int m_moveSeq = 0;
};