Keep track of move amount

This commit is contained in:
ItsDrike 2024-12-08 13:58:18 +01:00
parent fab342c71f
commit 7ef0ade0b3
Signed by: ItsDrike
GPG key ID: FA2745890B7048C0
3 changed files with 29 additions and 0 deletions

View file

@ -53,6 +53,8 @@ ApplicationWindow {
ScoreBar { ScoreBar {
id: scoreBar id: scoreBar
moves: GameState.moveAmount
height: Math.max(parent.height * 0.08, 50) height: Math.max(parent.height * 0.08, 50)
anchors.top: parent.top anchors.top: parent.top
anchors.left: parent.left anchors.left: parent.left

View file

@ -53,6 +53,7 @@ void GameState::dealCards() {
emit throwawayPileChanged(); emit throwawayPileChanged();
emit columnsChanged(); emit columnsChanged();
emit foundationChanged(); emit foundationChanged();
emit moveAmountChanged();
} }
void GameState::setupWinningDeck() { void GameState::setupWinningDeck() {
@ -84,6 +85,7 @@ void GameState::setupWinningDeck() {
emit throwawayPileChanged(); emit throwawayPileChanged();
emit columnsChanged(); emit columnsChanged();
emit foundationChanged(); emit foundationChanged();
emit moveAmountChanged();
} }
bool GameState::drawNextCard() { bool GameState::drawNextCard() {
@ -104,6 +106,7 @@ bool GameState::drawNextCard() {
m_throwawayPile.append(m_drawPile.takeFirst()); m_throwawayPile.append(m_drawPile.takeFirst());
qDebug() << "> Drawn card: " << m_throwawayPile.last()->toString(); qDebug() << "> Drawn card: " << m_throwawayPile.last()->toString();
incrementMoveAmt();
emit drawPileChanged(); emit drawPileChanged();
emit throwawayPileChanged(); emit throwawayPileChanged();
return true; return true;
@ -133,6 +136,7 @@ bool GameState::moveThrownCardToColumn(int columnId) {
m_throwawayPile.removeLast(); m_throwawayPile.removeLast();
qDebug() << "> Moving complete"; qDebug() << "> Moving complete";
incrementMoveAmt();
emit throwawayPileChanged(); emit throwawayPileChanged();
emit columnsChanged(); emit columnsChanged();
return true; return true;
@ -162,6 +166,7 @@ bool GameState::moveThrownCardToFoundation(int foundationId) {
m_throwawayPile.removeLast(); m_throwawayPile.removeLast();
qDebug() << "> Moving complete"; qDebug() << "> Moving complete";
incrementMoveAmt();
emit throwawayPileChanged(); emit throwawayPileChanged();
emit foundationChanged(); emit foundationChanged();
return true; return true;
@ -208,6 +213,7 @@ bool GameState::moveColumnCardToColumn(int fromColumnId, int toColumnId, int fro
ensureColumnRevealed(fromColumnId); ensureColumnRevealed(fromColumnId);
qDebug() << "> Moving complete"; qDebug() << "> Moving complete";
incrementMoveAmt();
emit columnsChanged(); emit columnsChanged();
return true; return true;
} }
@ -241,6 +247,7 @@ bool GameState::moveColumnCardToFoundation(int columnId, int foundationId) {
ensureColumnRevealed(columnId); ensureColumnRevealed(columnId);
qDebug() << "> Moving complete"; qDebug() << "> Moving complete";
incrementMoveAmt();
emit columnsChanged(); emit columnsChanged();
emit foundationChanged(); emit foundationChanged();
return true; return true;
@ -273,6 +280,7 @@ bool GameState::autoMoveThrownCard() {
// NOTE: consider returning what changed from tryAutoMoveSingleCard // NOTE: consider returning what changed from tryAutoMoveSingleCard
emit columnsChanged(); emit columnsChanged();
emit foundationChanged(); emit foundationChanged();
incrementMoveAmt();
return true; return true;
} }
@ -313,6 +321,7 @@ bool GameState::autoMoveColumnCard(int columnId, int cardIndex) {
// to be safe, emit a change signal for it too // to be safe, emit a change signal for it too
// NOTE: consider returning what changed from tryAutoMoveSingleCard // NOTE: consider returning what changed from tryAutoMoveSingleCard
emit foundationChanged(); emit foundationChanged();
incrementMoveAmt();
return true; return true;
} }
@ -339,6 +348,7 @@ bool GameState::autoMoveColumnCard(int columnId, int cardIndex) {
ensureColumnRevealed(columnId); ensureColumnRevealed(columnId);
incrementMoveAmt();
emit columnsChanged(); emit columnsChanged();
return true; return true;
} }
@ -432,6 +442,8 @@ void GameState::cleanupBoard(bool emitChanges) {
column.clear(); column.clear();
} }
m_moveAmt = 0;
// Note that we don't need to reset gameWon from here, as it's // Note that we don't need to reset gameWon from here, as it's
// auto-checked from onFoundationChanged, which the emits trigger // auto-checked from onFoundationChanged, which the emits trigger
@ -440,6 +452,7 @@ void GameState::cleanupBoard(bool emitChanges) {
emit throwawayPileChanged(); emit throwawayPileChanged();
emit foundationChanged(); emit foundationChanged();
emit columnsChanged(); emit columnsChanged();
emit moveAmountChanged();
} }
} }
@ -617,6 +630,11 @@ void GameState::ensureColumnRevealed(int columnId) {
qDebug() << "Revealed card " << col->card()->toString() << " in column " << columnId; qDebug() << "Revealed card " << col->card()->toString() << " in column " << columnId;
} }
void GameState::incrementMoveAmt() {
m_moveAmt++;
emit moveAmountChanged();
}
std::pair<std::optional<bool>, int> GameState::canWinThroughSimulation(QSet<QString>& visitedStates, QElapsedTimer timer) const { std::pair<std::optional<bool>, int> GameState::canWinThroughSimulation(QSet<QString>& visitedStates, QElapsedTimer timer) const {
if (m_gameWon) if (m_gameWon)
return std::make_pair(true, 0); // Already won at depth 0 return std::make_pair(true, 0); // Already won at depth 0
@ -803,6 +821,10 @@ QVariantList GameState::foundation() const {
return lst; return lst;
} }
int GameState::moveAmount() const {
return m_moveAmt;
}
bool GameState::gameWon() const { bool GameState::gameWon() const {
return m_gameWon; return m_gameWon;
} }

View file

@ -21,6 +21,7 @@ class GameState : public QObject {
Q_PROPERTY(QVariantList throwawayPile READ throwawayPile NOTIFY throwawayPileChanged) Q_PROPERTY(QVariantList throwawayPile READ throwawayPile NOTIFY throwawayPileChanged)
Q_PROPERTY(QVariantList columns READ columns NOTIFY columnsChanged) Q_PROPERTY(QVariantList columns READ columns NOTIFY columnsChanged)
Q_PROPERTY(QVariantList foundation READ foundation NOTIFY foundationChanged) Q_PROPERTY(QVariantList foundation READ foundation NOTIFY foundationChanged)
Q_PROPERTY(int moveAmount READ moveAmount NOTIFY moveAmountChanged)
Q_PROPERTY(bool gameWon READ gameWon NOTIFY gameWonChanged) Q_PROPERTY(bool gameWon READ gameWon NOTIFY gameWonChanged)
public: public:
@ -32,6 +33,7 @@ class GameState : public QObject {
QVariantList throwawayPile() const; QVariantList throwawayPile() const;
QVariantList columns() const; QVariantList columns() const;
QVariantList foundation() const; QVariantList foundation() const;
int moveAmount() const;
bool gameWon() const; bool gameWon() const;
// General functions // General functions
@ -55,6 +57,7 @@ class GameState : public QObject {
void throwawayPileChanged(); void throwawayPileChanged();
void columnsChanged(); void columnsChanged();
void foundationChanged(); void foundationChanged();
void moveAmountChanged();
void gameWonChanged(); void gameWonChanged();
private slots: private slots:
@ -65,6 +68,7 @@ class GameState : public QObject {
QList<PlayingCard*> m_throwawayPile; QList<PlayingCard*> m_throwawayPile;
QList<QList<ColumnSlot*>> m_columns; QList<QList<ColumnSlot*>> m_columns;
QList<QList<PlayingCard*>> m_foundation; QList<QList<PlayingCard*>> m_foundation;
int m_moveAmt;
bool m_gameWon; bool m_gameWon;
GameState* clone() const; GameState* clone() const;
@ -78,6 +82,7 @@ class GameState : public QObject {
bool isColumnMoveValid(const PlayingCard& cardToMove, int columnId) const; bool isColumnMoveValid(const PlayingCard& cardToMove, int columnId) const;
void ensureColumnRevealed(int columnId); void ensureColumnRevealed(int columnId);
void incrementMoveAmt();
std::pair<std::optional<bool>, int> canWinThroughSimulation(QSet<QString>& visitedStates, QElapsedTimer timer) const; std::pair<std::optional<bool>, int> canWinThroughSimulation(QSet<QString>& visitedStates, QElapsedTimer timer) const;
}; };