Skip to content

Commit

Permalink
Search for possible moves.
Browse files Browse the repository at this point in the history
  • Loading branch information
ElTh0r0 committed Apr 16, 2016
1 parent 4e1f488 commit 9c80478
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 10 deletions.
18 changes: 18 additions & 0 deletions CBoard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -420,3 +420,21 @@ void CBoard::highlightNeighbourhood(QList<QPoint> neighbours) {
this->addItem(listPossibleMoves.last());
}
}

// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------

bool CBoard::findPossibleMoves(bool bStonesLeft) {
for (int y = 0; y < m_numOfFields; y++) {
for (int x = 0; x < m_numOfFields; x++) {
if (0 == m_Fields[x][y].size() && bStonesLeft) {
return true;
} else if (m_Fields[x][y].size() > 0) {
if (checkNeighbourhood(QPoint(x, y)).size() > 0) {
return true;
}
}
}
}
return false;
}
1 change: 1 addition & 0 deletions CBoard.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class CBoard : public QGraphicsScene {
void addStone(QPoint field, quint8 stone);
void removeStone(QPoint field, bool bAll = false);
QList<quint8> getField(QPoint field) const;
bool findPossibleMoves(bool bStonesLeft);

signals:
void setStone(QPoint);
Expand Down
10 changes: 10 additions & 0 deletions CPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@ bool CPlayer::getIsHuman() const {
QString CPlayer::getName() const {
return m_sName;
}
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------

void CPlayer::setCanMove(bool bCanMove) {
m_bCanMove = bCanMove;
}

bool CPlayer::getCanMove() const {
return m_bCanMove;
}

// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
Expand Down
3 changes: 3 additions & 0 deletions CPlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ class CPlayer {
quint8 getStonesLeft() const;
void increaseWonTowers();
quint8 getWonTowers() const;
void setCanMove(bool bCanMove);
bool getCanMove() const;

private:
bool m_bIsActive;
Expand All @@ -55,6 +57,7 @@ class CPlayer {
const quint8 m_nMaxStones;
quint8 m_nStonesLeft;
quint8 m_nWonTowers;
bool m_bCanMove;
};

#endif // STACKANDCONQUER_CPLAYER_H_
54 changes: 44 additions & 10 deletions CStackAndConquer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,12 +217,8 @@ void CStackAndConquer::setStone(QPoint field) {
m_pPlayer2->setStonesLeft(m_pPlayer2->getStonesLeft() - 1);
m_pBoard->addStone(field, 2);
} else {
qWarning() << "Player1 active?" << m_pPlayer1->getIsActive();
qWarning() << "Player1 stones:" << m_pPlayer1->getStonesLeft();
qWarning() << "Player2 active?" << m_pPlayer2->getIsActive();
qWarning() << "Player2 stones:" << m_pPlayer2->getStonesLeft();
QMessageBox::warning(NULL, trUtf8("Warning"),
trUtf8("Something went wrong!"));
QMessageBox::information(NULL, trUtf8("Information"),
trUtf8("No stones left! Please move a tower."));
return;
}

Expand Down Expand Up @@ -327,11 +323,13 @@ void CStackAndConquer::updatePlayers(bool bInitial) {
if (m_pSettings->getWinTowers() == m_pPlayer1->getWonTowers()) {
m_pGraphView->setInteractive(false);
QMessageBox::information(this, qApp->applicationName(),
trUtf8("%1 won the game!").arg(m_pPlayer1->getName()));
trUtf8("%1 won the game!")
.arg(m_pPlayer1->getName()));
} else if (m_pSettings->getWinTowers() == m_pPlayer2->getWonTowers()) {
m_pGraphView->setInteractive(false);
QMessageBox::information(this, qApp->applicationName(),
trUtf8("%1 won the game!").arg(m_pPlayer2->getName()));
trUtf8("%1 won the game!")
.arg(m_pPlayer2->getName()));
} else {
if (!bInitial) {
m_pPlayer1->setActive(!m_pPlayer1->getIsActive());
Expand All @@ -345,10 +343,46 @@ void CStackAndConquer::updatePlayers(bool bInitial) {
m_plblPlayer1->setStyleSheet("color: #000000");
m_plblPlayer2->setStyleSheet("color: #FF0000");
}
this->checkPossibleMoves();
}
}

// TODO: Check for possible moves!
// Otherwise game ends in a tie.
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------

void CStackAndConquer::checkPossibleMoves() {
if (m_pPlayer1->getIsActive()) {
if (m_pBoard->findPossibleMoves(m_pPlayer1->getStonesLeft() > 0)) {
m_pPlayer1->setCanMove(true);
return;
} else {
m_pPlayer1->setCanMove(false);
}
} else {
if (m_pBoard->findPossibleMoves(m_pPlayer2->getStonesLeft() > 0)) {
m_pPlayer2->setCanMove(true);
return;
} else {
m_pPlayer2->setCanMove(false);
}
}

if (!m_pPlayer1->getCanMove() && !m_pPlayer2->getCanMove()) {
m_pGraphView->setInteractive(false);
QMessageBox::information(this, qApp->applicationName(),
trUtf8("No moves possible anymore.\n"
"Game ends in a tie!"));
} else if (!m_pPlayer1->getCanMove()) {
QMessageBox::information(this, qApp->applicationName(),
trUtf8("No move possible!\n%1 has to pass.")
.arg(m_pPlayer1->getName()));
this->updatePlayers();
} else if (!m_pPlayer2->getCanMove()) {
QMessageBox::information(this, qApp->applicationName(),
trUtf8("No move possible!\n%1 has to pass.")
.arg(m_pPlayer2->getName()));
this->updatePlayers();
}
}

// ---------------------------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions CStackAndConquer.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class CStackAndConquer : public QMainWindow {

private:
void setupMenu();
void checkPossibleMoves();

Ui::CStackAndConquer *m_pUi;
QGraphicsView *m_pGraphView;
Expand Down

0 comments on commit 9c80478

Please sign in to comment.