Skip to content

Commit

Permalink
Merge branch 'feature-ui' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
FutaAlice committed Mar 24, 2018
2 parents 9ba7849 + 80b2877 commit ae07b00
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 70 deletions.
5 changes: 5 additions & 0 deletions src/fhcore/iengine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ void IEngine::compute_sync(position::pos_t & result) noexcept
unlock();
}

void IEngine::terminate() noexcept
{
stop_calc_and_return();
}

void IEngine::wait()
{
_future.wait();
Expand Down
2 changes: 2 additions & 0 deletions src/fhcore/iengine.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ class IEngine
void configure(EngineCfg cfg) noexcept;
void compute(FUNC_CB_AIMOVE cb, void *opaque = nullptr) noexcept;
void compute_sync(position::pos_t & result) noexcept;
void terminate() noexcept;

protected:
virtual position::pos_t calc_ai_move_sync() = 0;
virtual position::pos_t stop_calc_and_return() = 0;
void wait();

private:
Expand Down
8 changes: 7 additions & 1 deletion src/fhcore/mcts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ pos_t MCTSEngine::calc_ai_move_sync()
percent = rate_of_progress;
debug(Level::Info) << setw(3) << percent << "% complate, "
<< setw((streamsize)log10(9999999) + 1) << times << " / " << "¡Þ";
if (percent == 100)
if (percent >= 100)
break;
}
}
Expand Down Expand Up @@ -114,6 +114,12 @@ pos_t MCTSEngine::calc_ai_move_sync()
return pos_t(*pBoard->pos());
}

position::pos_t MCTSEngine::stop_calc_and_return()
{
debug(Level::Warning) << "TODO";
return position::pos_t();
}

void MCTSEngine::selection(Node *& current)
{
while (current->nExpanded == current->nChildren)
Expand Down
1 change: 1 addition & 0 deletions src/fhcore/mcts.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class MCTSEngine : public IEngine
virtual ~MCTSEngine();
protected:
virtual position::pos_t calc_ai_move_sync();
virtual position::pos_t stop_calc_and_return();
private:
void selection(Node *& current);
void expansion(Node *& current);
Expand Down
176 changes: 111 additions & 65 deletions src/futahex2/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,15 @@ app::app(QWidget *parent)
ui.link_blue->setDisplayMethod(Canvas::DisplayMethod::LinkB);
resize(w * 16 / 9, w);

QObject::connect(ui.canvas, SIGNAL(clickEmptyPoint(int, int)), this, SLOT(setPiece(int, int)));
QObject::connect(ui.canvas, SIGNAL(clickEmptyPoint(int, int)),
this, SLOT(onCanvasValidClick(int, int)));
QObject::connect(ui.actionNew, SIGNAL(triggered()), this, SLOT(onNew()));
QObject::connect(ui.actionOpen, SIGNAL(triggered()), this, SLOT(onOpen()));
QObject::connect(ui.actionSave, SIGNAL(triggered()), this, SLOT(onSave()));
QObject::connect(ui.actionenable_costomize_player_color, SIGNAL(toggled(bool)),
this, SLOT(onEnableCostomizePlayerColor(bool)));
QObject::connect(ui.actionenable_beep, SIGNAL(toggled(bool)),
this, SLOT(onEnableBeep(bool)));
QObject::connect(ui.action5, SIGNAL(triggered()), this, SLOT(onAction5()));
QObject::connect(ui.action7, SIGNAL(triggered()), this, SLOT(onAction7()));
QObject::connect(ui.action9, SIGNAL(triggered()), this, SLOT(onAction9()));
Expand All @@ -44,11 +49,9 @@ app::app(QWidget *parent)
QObject::connect(ui.action15, SIGNAL(triggered()), this, SLOT(onAction15()));
QObject::connect(ui.action17, SIGNAL(triggered()), this, SLOT(onAction17()));
QObject::connect(ui.action19, SIGNAL(triggered()), this, SLOT(onAction19()));

QObject::connect(ui.actionAIRed, SIGNAL(triggered()), this, SLOT(onActionAIRed()));
QObject::connect(ui.actionAIBlue, SIGNAL(triggered()), this, SLOT(onActionAIBlue()));
QObject::connect(ui.actionAINone, SIGNAL(triggered()), this, SLOT(onActionAINone()));

QObject::connect(ui.actionPlayerRed, SIGNAL(triggered()), this, SLOT(onActionPlayerRed()));
QObject::connect(ui.actionPlayerBlue, SIGNAL(triggered()), this, SLOT(onActionPlayerBlue()));
QObject::connect(ui.actionPlayerAuto, SIGNAL(triggered()), this, SLOT(onActionPlayerAuto()));
Expand All @@ -70,6 +73,8 @@ app::app(QWidget *parent)

QObject::connect(add_default_icon(QStyle::SP_MediaPlay, "AI move"),
SIGNAL(triggered()), this, SLOT(onAIMove()));
QObject::connect(add_default_icon(QStyle::SP_MediaStop, "AI stop"),
SIGNAL(triggered()), this, SLOT(onAIStop()));
QObject::connect(add_default_icon(QStyle::SP_MediaSeekBackward, "take back"),
SIGNAL(triggered()), this, SLOT(onTakeBack()));
ui.mainToolBar->addSeparator();
Expand All @@ -78,11 +83,82 @@ app::app(QWidget *parent)
SIGNAL(triggered()), this, SLOT(onView()));
}

void app::PlayerSetPiece(int row, int col)
{
Color color = getPlayerColor();
setPiece(row, col, color);
if (_bEnableBeep)
QApplication::beep();
if (_acs != AIColorSetting::None &&
getAIColor() == _pBoard->color())
AIMove();
}

void app::AISetPiece(int row, int col)
{
Color color = getAIColor();
setPiece(row, col, color);
if (_bEnableBeep)
QApplication::beep();
}

void app::RecSetPiece(int row, int col)
{
Color color = _pBoard->color();
setPiece(row, col, color);
}

void app::AIMove()
{
if (!_pBoard)
return;

if (pEngine)
{
const char *err = "AI is busy. This operation will be ignored";
debug(Level::Warning) << err;
QMessageBox message(QMessageBox::NoIcon, "Warning",
err, QMessageBox::Ok);
message.exec();
return;
}

Color color = _pBoard->color();

EngineCfg cfg;
cfg.colorAI = color;
cfg.pBoard = _pBoard;

pEngine = new MCTSEngine(chrono::seconds(20));
pEngine->configure(cfg);
pEngine->compute([](pos_t result, void *opaque)->void * {
cout << result << endl;
auto *pApp = static_cast<app *>(opaque);
pApp->AISetPiece(result.row, result.col);
auto grabage = pApp->pEngine;
pApp->pEngine = nullptr;
delete grabage;
return NULL;
}, static_cast<void *>(this));
}

void app::AIStop()
{
if (!pEngine)
return;
pEngine->terminate();
}

void app::paintEvent(QPaintEvent * event)
{
if (!_pBoard)
{
QPainter painter(this);
painter.drawText(200, 200, QStringLiteral("plz select boardsize!"));
}
}

void app::setPiece(int row, int col)
void app::setPiece(int row, int col, color::Color color)
{
if (!_pBoard)
return;
Expand All @@ -98,7 +174,6 @@ void app::setPiece(int row, int col)
return;
}

Color color = refBoard.color();
refBoard(row, col) = color;

_rec.push_back(RecordData(row, col, _pBoard->boardsize(), color));
Expand All @@ -120,6 +195,11 @@ void app::resetPiece(int row, int col)
updateBoard();
}

void app::onCanvasValidClick(int row, int col)
{
PlayerSetPiece(row, col);
}

void app::onNew()
{
changeBoardsize(_pBoard ? 0 : 11);
Expand All @@ -133,8 +213,10 @@ Color app::getAIColor()
return Color::Red;
case AIColorSetting::Blue:
return Color::Blue;
case AIColorSetting::None:
default:
return Color::Empty;
assert(_pBoard);
return _pBoard->color();
}
}

Expand All @@ -146,11 +228,10 @@ Color app::getPlayerColor()
return Color::Red;
case PlayerColorSetting::Blue:
return Color::Blue;
case PlayerColorSetting::Auto:
default:
if (_pBoard)
return _pBoard->color();
else
return Color::Empty;
assert(_pBoard);
return _pBoard->color();
}
}

Expand All @@ -163,6 +244,10 @@ void app::setAIColor(AIColorSetting acs)
action->setChecked(index++ == static_cast<int>(acs));
}
_acs = acs;
if (_pBoard &&
_acs != AIColorSetting::None &&
getAIColor() == _pBoard->color())
AIMove();
}

void app::setPlayerColor(PlayerColorSetting pcs)
Expand Down Expand Up @@ -251,6 +336,18 @@ void app::onActionPlayerRed() { setPlayerColor(PlayerColorSetting::Red); }
void app::onActionPlayerBlue() { setPlayerColor(PlayerColorSetting::Blue); }
void app::onActionPlayerAuto() { setPlayerColor(PlayerColorSetting::Auto); }

void app::onEnableCostomizePlayerColor(bool checked)
{
ui.actionPlayerRed->setEnabled(checked);
ui.actionPlayerBlue->setEnabled(checked);
setPlayerColor(PlayerColorSetting::Auto);
}

void app::onEnableBeep(bool checked)
{
_bEnableBeep = checked;
}

void app::onOpen()
{
const char *filters = "Hexy Files (*.gam);; FutaHex Files (*.fh)";
Expand Down Expand Up @@ -289,7 +386,7 @@ void app::onOpen()
for (auto record_data : rec)
{
auto pos = record_data.pos();
setPiece(pos.row, pos.col);
RecSetPiece(pos.row, pos.col);
}
}
file.close();
Expand Down Expand Up @@ -330,60 +427,9 @@ void app::onSave()
}
}

void app::onRestart()
{
changeBoardsize(_pBoard ? 0 : 11);
}

void app::onAIMove()
{
if (!_pBoard)
return;

if (aiThinking)
{
const char *err = "AI is busy. This operation will be ignored";
debug(Level::Warning) << err;
QMessageBox message(QMessageBox::NoIcon, "Warning",
err, QMessageBox::Ok);
message.exec();
return;
}
aiThinking = true;

Color color = _pBoard->color();

EngineCfg cfg;
cfg.colorAI = color;
cfg.pBoard = _pBoard;

IEngine *pEngine = new MCTSEngine();
pEngine->configure(cfg);

struct CallbackParams
{
CallbackParams(IEngine *pEngine, app *pApp)
: pEngine(pEngine)
, pApp(pApp)
{
}
~CallbackParams()
{
delete pEngine;
}
IEngine *pEngine { nullptr };
app *pApp { nullptr };
};

pEngine->compute([](pos_t result, void *opaque)->void * {
cout << result << endl;
auto *data = static_cast<CallbackParams *>(opaque);
data->pApp->setPiece(result.row, result.col);
data->pApp->aiThinking = false;
delete data;
return NULL;
}, static_cast<void *>(new CallbackParams(pEngine, this)));
}
void app::onRestart() { changeBoardsize(_pBoard ? 0 : 11); }
void app::onAIMove() { AIMove(); }
void app::onAIStop() { AIStop(); }

void app::onTakeBack()
{
Expand Down
20 changes: 17 additions & 3 deletions src/futahex2/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "ui_app.h"

namespace board { class IBoard; }
namespace engine { class IEngine; }

class app : public QMainWindow
{
Expand All @@ -19,16 +20,17 @@ class app : public QMainWindow
public:
app(QWidget *parent = Q_NULLPTR);
static const int w { 720 };
bool aiThinking { false };
engine::IEngine *pEngine { nullptr };


public slots:
void setPiece(int row, int col);
void resetPiece(int row, int col);
void onCanvasValidClick(int row, int col);
void onNew();
void onOpen();
void onSave();
void onRestart();
void onAIMove();
void onAIStop();
void onTakeBack();
void onView();
void onAction5();
Expand All @@ -45,11 +47,20 @@ public slots:
void onActionPlayerRed();
void onActionPlayerBlue();
void onActionPlayerAuto();
void onEnableCostomizePlayerColor(bool checked);
void onEnableBeep(bool checked);

protected:
void paintEvent(QPaintEvent *event);

private:
void PlayerSetPiece(int row, int col);
void AISetPiece(int row, int col);
void RecSetPiece(int row, int col);

void AIMove();
void AIStop();

color::Color getAIColor();
color::Color getPlayerColor();
void setAIColor(AIColorSetting);
Expand All @@ -60,6 +71,8 @@ public slots:
void appendText(const char *text, color::Color color);
void appendText(std::string &str, QColor color = Qt::black);
void appendText(std::string &str, color::Color color);
void setPiece(int row, int col, color::Color color);
void resetPiece(int row, int col);

private:
board::IBoard *_pBoard { nullptr };
Expand All @@ -68,5 +81,6 @@ public slots:
AIColorSetting _acs { AIColorSetting::None };
PlayerColorSetting _pcs { PlayerColorSetting::Auto };

bool _bEnableBeep { false };
Ui::appClass ui;
};
Loading

0 comments on commit ae07b00

Please sign in to comment.