Commit 38971fb9 authored by Frederik Schwarzer's avatar Frederik Schwarzer

Use enum class.

parent 1523f7df
......@@ -33,7 +33,7 @@ class UndoCommand : public QUndoCommand
{
public:
enum MoveType { Stone, Passed, Resigned };
enum class MoveType { Stone, Passed, Resigned };
UndoCommand(Player *player, MoveType moveType, const QString &undoStr)
: QUndoCommand(undoStr), m_player(player), m_moveType(moveType)
......@@ -51,7 +51,7 @@ class UndoCommand : public QUndoCommand
Game::Game(QObject *parent)
: QObject(parent)
, m_currentMove(0), m_lastUndoIndex(0), m_currentPlayer(&m_blackPlayer)
, m_blackPlayer(Player::Black), m_whitePlayer(Player::White)
, m_blackPlayer(Player::Color::Black), m_whitePlayer(Player::Color::White)
, m_komi(4.5), m_boardSize(19), m_fixedHandicap(5), m_consecutivePassMoveNumber(0)
, m_gameFinished(false)
{
......@@ -323,19 +323,19 @@ bool Game::playMove(const Player &player, const Stone &stone, bool undoable)
if (tmp->isWhite()) {
player = &m_whitePlayer;
if (stone.isValid()) {
moveType = UndoCommand::Stone;
moveType = UndoCommand::MoveType::Stone;
undoStr = i18nc("%1 stone coordinate", "White %1", stone.toString());
} else {
moveType = UndoCommand::Passed;
moveType = UndoCommand::MoveType::Passed;
undoStr = i18n("White passed");
}
} else {
player = &m_blackPlayer;
if (stone.isValid()) {
moveType = UndoCommand::Stone;
moveType = UndoCommand::MoveType::Stone;
undoStr = i18nc("%1 stone coordinate", "Black %1", stone.toString());
} else {
moveType = UndoCommand::Passed;
moveType = UndoCommand::MoveType::Passed;
undoStr = i18n("Black passed");
}
}
......@@ -395,7 +395,7 @@ bool Game::generateMove(const Player &player, bool undoable)
m_gameFinished = true;
}
m_consecutivePassMoveNumber++;
moveType = UndoCommand::Passed;
moveType = UndoCommand::MoveType::Passed;
if (tmp->isWhite()) {
undoStr = i18n("White passed");
} else {
......@@ -404,7 +404,7 @@ bool Game::generateMove(const Player &player, bool undoable)
} else if (m_response == QLatin1String("resign")) {
emit resigned(*m_currentPlayer);
m_gameFinished = true;
moveType = UndoCommand::Resigned;
moveType = UndoCommand::MoveType::Resigned;
if (tmp->isWhite()) {
undoStr = i18n("White resigned");
} else {
......@@ -414,7 +414,7 @@ bool Game::generateMove(const Player &player, bool undoable)
m_currentMove++;
m_movesList.append(Move(tmp, Stone(m_response)));
m_consecutivePassMoveNumber = 0;
moveType = UndoCommand::Stone;
moveType = UndoCommand::MoveType::Stone;
if (tmp->isWhite()) {
undoStr = i18nc("%1 response from Go engine", "White %1", m_response);
} else {
......@@ -488,10 +488,10 @@ bool Game::redoMove()
Player *player = undoCmd->player();
if (undoCmd->moveType() == UndoCommand::Passed) {
if (undoCmd->moveType() == UndoCommand::MoveType::Passed) {
//qDebug() << "Redo a pass move for" << player << undoCmd->text();
playMove(*player, Stone(), false); // E.g. pass move
} else if (undoCmd->moveType() == UndoCommand::Resigned) {
} else if (undoCmd->moveType() == UndoCommand::MoveType::Resigned) {
// Note: Altough it is possible to undo after a resign and redo it,
// it is a bit questionable whether this makes sense logically.
//qDebug() << "Redo a resign for" << player << undoCmd->text();
......@@ -650,38 +650,38 @@ int Game::captures(const Player &player)
Game::FinalState Game::finalState(const Stone &stone)
{
if (!isRunning() || !stone.isValid()) {
return FinalStateInvalid;
return FinalState::FinalStateInvalid;
}
m_process.write("final_status " + stone.toLatin1() + '\n');
if (waitResponse()) {
if (m_response == QLatin1String("alive")) return FinalAlive;
else if (m_response == QLatin1String("dead")) return FinalDead;
else if (m_response == QLatin1String("seki")) return FinalSeki;
else if (m_response == QLatin1String("white_territory")) return FinalWhiteTerritory;
else if (m_response == QLatin1String("blacK_territory")) return FinalBlackTerritory;
else if (m_response == QLatin1String("dame")) return FinalDame;
else return FinalStateInvalid;
if (m_response == QLatin1String("alive")) return FinalState::FinalAlive;
else if (m_response == QLatin1String("dead")) return FinalState::FinalDead;
else if (m_response == QLatin1String("seki")) return FinalState::FinalSeki;
else if (m_response == QLatin1String("white_territory")) return FinalState::FinalWhiteTerritory;
else if (m_response == QLatin1String("blacK_territory")) return FinalState::FinalBlackTerritory;
else if (m_response == QLatin1String("dame")) return FinalState::FinalDame;
else return FinalState::FinalStateInvalid;
} else
return FinalStateInvalid;
return FinalState::FinalStateInvalid;
}
QList<Stone> Game::finalStates(FinalState state)
{
QList<Stone> list;
if (!isRunning() || state == FinalStateInvalid) {
if (!isRunning() || state == FinalState::FinalStateInvalid) {
return list;
}
QByteArray msg("final_status_list ");
switch (state) {
case FinalAlive: msg.append("alive"); break;
case FinalDead: msg.append("dead"); break;
case FinalSeki: msg.append("seki"); break;
case FinalWhiteTerritory: msg.append("white_territory"); break;
case FinalBlackTerritory: msg.append("black_territory"); break;
case FinalDame: msg.append("dame"); break;
case FinalStateInvalid: /* Will never happen */ break;
case FinalState::FinalAlive: msg.append("alive"); break;
case FinalState::FinalDead: msg.append("dead"); break;
case FinalState::FinalSeki: msg.append("seki"); break;
case FinalState::FinalWhiteTerritory: msg.append("white_territory"); break;
case FinalState::FinalBlackTerritory: msg.append("black_territory"); break;
case FinalState::FinalDame: msg.append("dame"); break;
case FinalState::FinalStateInvalid: /* Will never happen */ break;
}
msg.append('\n');
m_process.write(msg);
......
......@@ -65,7 +65,7 @@ public:
/**
* Enumeration of all possible final states of a field when a game is over
*/
enum FinalState {
enum class FinalState {
FinalAlive = 1, ///< The stone on the field is alive
FinalDead, ///< The stone on the field is dead
FinalSeki, ///<
......
......@@ -38,19 +38,19 @@ class Player
friend class Game;
public:
enum Color {
enum class Color {
White = 1, ///< The white player
Black, ///< The black player
Invalid
};
enum Type {
enum class Type {
Human = 1, ///< A human player
Computer ///< A computer player
};
private:
explicit Player(Color color, Type type = Human);
explicit Player(Color color, Type type = Type::Human);
public:
Player(const Player &other);
......@@ -68,11 +68,11 @@ public:
void setType(Type type) { m_type = type; }
Type type() const { return m_type; }
bool isWhite() const { return m_color == White; }
bool isBlack() const { return m_color == Black; }
bool isValid() const { return m_color != Invalid; }
bool isHuman() const { return m_type == Human; }
bool isComputer() const { return m_type == Computer; }
bool isWhite() const { return m_color == Color::White; }
bool isBlack() const { return m_color == Color::Black; }
bool isValid() const { return m_color != Color::Invalid; }
bool isHuman() const { return m_type == Type::Human; }
bool isComputer() const { return m_type == Type::Computer; }
bool operator==(const Player &other) const;
......
......@@ -85,7 +85,7 @@ void GameScene::resizeScene(int width, int height)
removeItem(m_placementMarkerItem);
}
m_placementMarkerPixmapSize = QSize(static_cast<int>(m_cellSize / 4), static_cast<int>(m_cellSize / 4));
m_placementMarkerItem = addPixmap(ThemeRenderer::self()->renderElement(ThemeRenderer::PlacementMarker, m_placementMarkerPixmapSize));
m_placementMarkerItem = addPixmap(ThemeRenderer::self()->renderElement(ThemeRenderer::Element::PlacementMarker, m_placementMarkerPixmapSize));
m_placementMarkerItem->setVisible(false);
m_placementMarkerItem->setZValue(1);
}
......@@ -140,7 +140,7 @@ void GameScene::updateStoneItems()
m_stoneItems.clear();
foreach (const Stone &stone, m_game->stones(m_game->blackPlayer())) {
item = addPixmap(ThemeRenderer::self()->renderElement(ThemeRenderer::BlackStone, m_stonePixmapSize));
item = addPixmap(ThemeRenderer::self()->renderElement(ThemeRenderer::Element::BlackStone, m_stonePixmapSize));
item->setZValue(2);
int xOff = stone.x() >= 'I' ? stone.x() - 'A' - 1 : stone.x() - 'A';
item->setPos(QPointF(m_gridRect.x() + xOff * m_cellSize - halfStoneSize + 1,
......@@ -148,7 +148,7 @@ void GameScene::updateStoneItems()
m_stoneItems.append(item);
}
foreach (const Stone &stone, m_game->stones(m_game->whitePlayer())) {
item = addPixmap(ThemeRenderer::self()->renderElement(ThemeRenderer::WhiteStone, m_stonePixmapSize));
item = addPixmap(ThemeRenderer::self()->renderElement(ThemeRenderer::Element::WhiteStone, m_stonePixmapSize));
item->setZValue(2);
int xOff = stone.x() >= 'I' ? stone.x() - 'A' - 1 : stone.x() - 'A';
item->setPos(QPointF(m_gridRect.x() + xOff * m_cellSize - halfStoneSize + 1,
......@@ -199,9 +199,9 @@ void GameScene::updateHintItems()
foreach (const Stone &move, m_game->bestMoves(m_game->currentPlayer())) {
QPixmap stonePixmap;
if (m_game->currentPlayer().isWhite()) {
stonePixmap = ThemeRenderer::self()->renderElement(ThemeRenderer::WhiteStoneTransparent, m_stonePixmapSize);
stonePixmap = ThemeRenderer::self()->renderElement(ThemeRenderer::Element::WhiteStoneTransparent, m_stonePixmapSize);
} else if (m_game->currentPlayer().isBlack()) {
stonePixmap = ThemeRenderer::self()->renderElement(ThemeRenderer::BlackStoneTransparent, m_stonePixmapSize);
stonePixmap = ThemeRenderer::self()->renderElement(ThemeRenderer::Element::BlackStoneTransparent, m_stonePixmapSize);
}
QPainter painter(&stonePixmap);
......@@ -240,8 +240,8 @@ void GameScene::updateTerritoryItems()
int halfCellSize = m_cellSize / 2;
//qDebug() << "Fetching territory from engine ...";
stonePixmap = ThemeRenderer::self()->renderElement(ThemeRenderer::WhiteTerritory, QSize(m_cellSize, m_cellSize));
foreach (const Stone &stone, m_game->finalStates(Game::FinalWhiteTerritory)) {
stonePixmap = ThemeRenderer::self()->renderElement(ThemeRenderer::Element::WhiteTerritory, QSize(m_cellSize, m_cellSize));
foreach (const Stone &stone, m_game->finalStates(Game::FinalState::FinalWhiteTerritory)) {
item = addPixmap(stonePixmap);
item->setZValue(8);
int xOff = stone.x() >= 'I' ? stone.x() - 'A' - 1 : stone.x() - 'A';
......@@ -250,8 +250,8 @@ void GameScene::updateTerritoryItems()
m_territoryItems.append(item);
}
stonePixmap = ThemeRenderer::self()->renderElement(ThemeRenderer::BlackTerritory, QSize(m_cellSize, m_cellSize));
foreach (const Stone &stone, m_game->finalStates(Game::FinalBlackTerritory)) {
stonePixmap = ThemeRenderer::self()->renderElement(ThemeRenderer::Element::BlackTerritory, QSize(m_cellSize, m_cellSize));
foreach (const Stone &stone, m_game->finalStates(Game::FinalState::FinalBlackTerritory)) {
item = addPixmap(stonePixmap);
item->setZValue(8);
int xOff = stone.x() >= 'I' ? stone.x() - 'A' - 1 : stone.x() - 'A';
......@@ -296,9 +296,9 @@ void GameScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
if (m_game->currentPlayer().isHuman()) {
if (m_game->currentPlayer().isWhite()) {
map = ThemeRenderer::self()->renderElement(ThemeRenderer::WhiteStoneTransparent, m_stonePixmapSize);
map = ThemeRenderer::self()->renderElement(ThemeRenderer::Element::WhiteStoneTransparent, m_stonePixmapSize);
} else if (m_game->currentPlayer().isBlack()) {
map = ThemeRenderer::self()->renderElement(ThemeRenderer::BlackStoneTransparent, m_stonePixmapSize);
map = ThemeRenderer::self()->renderElement(ThemeRenderer::Element::BlackStoneTransparent, m_stonePixmapSize);
}
emit cursorPixmapChanged(map);
}
......@@ -328,8 +328,8 @@ void GameScene::mousePressEvent(QGraphicsSceneMouseEvent *event)
void GameScene::drawBackground(QPainter *painter, const QRectF &)
{
ThemeRenderer::self()->renderElement(ThemeRenderer::Background, painter, sceneRect());
ThemeRenderer::self()->renderElement(ThemeRenderer::Board, painter, m_boardRect);
ThemeRenderer::self()->renderElement(ThemeRenderer::Element::Background, painter, sceneRect());
ThemeRenderer::self()->renderElement(ThemeRenderer::Element::Board, painter, m_boardRect);
int width = m_cellSize / 16;
QColor color = QColor(20, 30, 20);
......
......@@ -102,34 +102,34 @@ QPixmap ThemeRenderer::renderElement(Element element, const QSize &size) const
// Determine which board element we have to render and set the cache item name
QString cacheName;
switch (element) {
case Background:
case Element::Background:
cacheName = QStringLiteral("background_%1x%2").arg(size.width()).arg(size.height());
break;
case Board:
case Element::Board:
cacheName = QStringLiteral("board_%1x%2").arg(size.width()).arg(size.height());
break;
case HandicapMark:
case Element::HandicapMark:
cacheName = QStringLiteral("handicap_mark_%1x%2").arg(size.width()).arg(size.height());
break;
case WhiteStone:
case Element::WhiteStone:
cacheName = QStringLiteral("white_stone_%1x%2").arg(size.width()).arg(size.height());
break;
case WhiteStoneTransparent:
case Element::WhiteStoneTransparent:
cacheName = QStringLiteral("white_stone_%1x%2_trans").arg(size.width()).arg(size.height());
break;
case WhiteTerritory:
case Element::WhiteTerritory:
cacheName = QStringLiteral("white_territory_%1x%2").arg(size.width()).arg(size.height());
break;
case BlackStone:
case Element::BlackStone:
cacheName = QStringLiteral("black_stone_%1x%2").arg(size.width()).arg(size.height());
break;
case BlackStoneTransparent:
case Element::BlackStoneTransparent:
cacheName = QStringLiteral("black_stone_%1x%2_trans").arg(size.width()).arg(size.height());
break;
case BlackTerritory:
case Element::BlackTerritory:
cacheName = QStringLiteral("black_territory_%1x%2").arg(size.width()).arg(size.height());
break;
case PlacementMarker:
case Element::PlacementMarker:
cacheName = QStringLiteral("placement_marker_%1x%2").arg(size.width()).arg(size.height());
break;
}
......@@ -141,36 +141,36 @@ QPixmap ThemeRenderer::renderElement(Element element, const QSize &size) const
pixmap.fill(Qt::transparent);
QPainter p(&pixmap);
switch (element) {
case Background:
case Element::Background:
m_renderer->render(&p, QStringLiteral("background"));
break;
case Board:
case Element::Board:
m_renderer->render(&p, QStringLiteral("board"));
break;
case HandicapMark:
case Element::HandicapMark:
m_renderer->render(&p, QStringLiteral("handicap_mark"));
break;
case WhiteStone:
case Element::WhiteStone:
m_renderer->render(&p, QStringLiteral("white_stone"));
break;
case WhiteStoneTransparent:
case Element::WhiteStoneTransparent:
p.setOpacity(0.5);
m_renderer->render(&p, QStringLiteral("white_stone"));
break;
case WhiteTerritory:
case Element::WhiteTerritory:
m_renderer->render(&p, QStringLiteral("white_territory"));
break;
case BlackStone:
case Element::BlackStone:
m_renderer->render(&p, QStringLiteral("black_stone"));
break;
case BlackStoneTransparent:
case Element::BlackStoneTransparent:
p.setOpacity(0.5);
m_renderer->render(&p, QStringLiteral("black_stone"));
break;
case BlackTerritory:
case Element::BlackTerritory:
m_renderer->render(&p, QStringLiteral("black_territory"));
break;
case PlacementMarker:
case Element::PlacementMarker:
m_renderer->render(&p, QStringLiteral("placement_marker"));
break;
}
......@@ -183,34 +183,34 @@ QSize ThemeRenderer::elementSize(Element element) const
{
QRectF sizeRect;
switch(element) {
case Background:
case Element::Background:
sizeRect = m_renderer->boundsOnElement(QStringLiteral("background"));
break;
case Board:
case Element::Board:
sizeRect = m_renderer->boundsOnElement(QStringLiteral("board"));
break;
case HandicapMark:
case Element::HandicapMark:
sizeRect = m_renderer->boundsOnElement(QStringLiteral("handicap_mark"));
break;
case WhiteStone:
case Element::WhiteStone:
sizeRect = m_renderer->boundsOnElement(QStringLiteral("white_stone"));
break;
case WhiteStoneTransparent:
case Element::WhiteStoneTransparent:
sizeRect = m_renderer->boundsOnElement(QStringLiteral("white_stone"));
break;
case WhiteTerritory:
case Element::WhiteTerritory:
sizeRect = m_renderer->boundsOnElement(QStringLiteral("white_territory"));
break;
case BlackStone:
case Element::BlackStone:
sizeRect = m_renderer->boundsOnElement(QStringLiteral("black_stone"));
break;
case BlackStoneTransparent:
case Element::BlackStoneTransparent:
sizeRect = m_renderer->boundsOnElement(QStringLiteral("black_stone"));
break;
case BlackTerritory:
case Element::BlackTerritory:
sizeRect = m_renderer->boundsOnElement(QStringLiteral("black_territory"));
break;
case PlacementMarker:
case Element::PlacementMarker:
sizeRect = m_renderer->boundsOnElement(QStringLiteral("placement_marker"));
break;
}
......
......@@ -56,7 +56,7 @@ public:
/**
* Enumeration of all possible renderable scene element types.
*/
enum Element {
enum class Element {
Background = 1,
Board,
HandicapMark,
......
......@@ -62,9 +62,9 @@ void GameWidget::init()
komiLabel->setText(QString::number(m_game->komi()));
handicapLabel->setText(i18np("%1 Stone", "%1 Stones", m_game->fixedHandicap()));
QPixmap whiteStone = ThemeRenderer::self()->renderElement(Kigo::ThemeRenderer::WhiteStone, QSize(48, 48));
QPixmap whiteStone = ThemeRenderer::self()->renderElement(Kigo::ThemeRenderer::Element::WhiteStone, QSize(48, 48));
whiteStoneImageLabel->setPixmap(whiteStone);
QPixmap blackStone = ThemeRenderer::self()->renderElement(Kigo::ThemeRenderer::BlackStone, QSize(48, 48));
QPixmap blackStone = ThemeRenderer::self()->renderElement(Kigo::ThemeRenderer::Element::BlackStone, QSize(48, 48));
blackStoneImageLabel->setPixmap(blackStone);
update();
......@@ -86,11 +86,11 @@ void GameWidget::update()
}
if (m_game->currentPlayer().isWhite()) {
QPixmap whiteStone = ThemeRenderer::self()->renderElement(Kigo::ThemeRenderer::WhiteStone, QSize(64, 64));
QPixmap whiteStone = ThemeRenderer::self()->renderElement(Kigo::ThemeRenderer::Element::WhiteStone, QSize(64, 64));
currentPlayerImageLabel->setPixmap(whiteStone);
currentPlayerLabel->setText(i18n("White's turn"));
} else if (m_game->currentPlayer().isBlack()) {
QPixmap blackStone = ThemeRenderer::self()->renderElement(Kigo::ThemeRenderer::BlackStone, QSize(64, 64));
QPixmap blackStone = ThemeRenderer::self()->renderElement(Kigo::ThemeRenderer::Element::BlackStone, QSize(64, 64));
currentPlayerImageLabel->setPixmap(blackStone);
currentPlayerLabel->setText(i18n("Black's turn"));
} else {
......
......@@ -37,9 +37,9 @@ SetupWidget::SetupWidget(Game *game, QWidget *parent)
setupUi(this);
startButton->setIcon(QIcon::fromTheme( QStringLiteral( "media-playback-start" )));
QPixmap whiteStone = ThemeRenderer::self()->renderElement(Kigo::ThemeRenderer::WhiteStone, QSize(48, 48));
QPixmap whiteStone = ThemeRenderer::self()->renderElement(Kigo::ThemeRenderer::Element::WhiteStone, QSize(48, 48));
whiteStoneImageLabel->setPixmap(whiteStone);
QPixmap blackStone = ThemeRenderer::self()->renderElement(Kigo::ThemeRenderer::BlackStone, QSize(48, 48));
QPixmap blackStone = ThemeRenderer::self()->renderElement(Kigo::ThemeRenderer::Element::BlackStone, QSize(48, 48));
blackStoneImageLabel->setPixmap(blackStone);
connect(startButton, &QPushButton::clicked, this, &SetupWidget::startClicked);
......@@ -203,17 +203,17 @@ void SetupWidget::commit()
saveSettings();
if (Preferences::whitePlayerHuman()) {
m_game->whitePlayer().setType(Player::Human);
m_game->whitePlayer().setType(Player::Type::Human);
} else {
m_game->whitePlayer().setType(Player::Computer);
m_game->whitePlayer().setType(Player::Type::Computer);
}
m_game->whitePlayer().setStrength(Preferences::whitePlayerStrength());
m_game->whitePlayer().setName(Preferences::whitePlayerName());
if (Preferences::blackPlayerHuman()) {
m_game->blackPlayer().setType(Player::Human);
m_game->blackPlayer().setType(Player::Type::Human);
} else {
m_game->blackPlayer().setType(Player::Computer);
m_game->blackPlayer().setType(Player::Type::Computer);
}
m_game->blackPlayer().setStrength(Preferences::blackPlayerStrength());
m_game->blackPlayer().setName(Preferences::blackPlayerName());
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment