solitare/gamestate.h
ItsDrike 883d766dc3
Register QObject classes as QML elements via macros
Using manual registration with qmlRegisterType (or
qmlRegisterUncreatableType) isn't ideal, because Qt Creator can't pick
up on it. When using the macros, autocompletion & unknown type warnings
in the code disappear.
2024-12-03 15:05:30 +01:00

67 lines
2 KiB
C++

#ifndef GAMESTATE_H
#define GAMESTATE_H
#include <QObject>
#include <qqmlintegration.h>
#include "playingcard.h"
#include "columnslot.h"
class GameState : public QObject
{
Q_OBJECT
QML_ELEMENT
QML_UNCREATABLE("Use C++ logic to instantiate")
Q_PROPERTY(QList<PlayingCard*> drawPile READ drawPile NOTIFY drawPileChanged)
Q_PROPERTY(QList<PlayingCard*> throwawayPile READ throwawayPile NOTIFY throwawayPileChanged)
Q_PROPERTY(QList<QList<ColumnSlot*>> columns READ columns NOTIFY columnsChanged)
Q_PROPERTY(QList<QList<PlayingCard*>> foundation READ foundation NOTIFY foundationChanged)
Q_PROPERTY(bool gameWon READ gameWon NOTIFY gameWonChanged)
public:
explicit GameState(QObject *parent = nullptr);
// Getters
QList<PlayingCard*> drawPile() const;
QList<PlayingCard*> throwawayPile() const;
QList<QList<ColumnSlot*>> columns() const;
QList<QList<PlayingCard*>> foundation() const;
bool gameWon() const;
// General functions
void dealCards();
void drawNextCard();
// Manual moves (from X to Y)
bool moveCardToColumn(int columnId);
bool moveThrownCardToFoundation(PlayingCard::Suit foundationId);
bool moveColumnCardToFoundation(int columnId, PlayingCard::Suit foundationId);
// Automatic moves (from X to auto)
bool autoMoveThrownCard();
bool autoMoveColumnCard(int columnId);
signals:
void drawPileChanged();
void throwawayPileChanged();
void columnsChanged();
void foundationChanged();
void gameWonChanged();
private slots:
void onFoundationChanged();
private:
QList<PlayingCard*> m_drawPile;
QList<PlayingCard*> m_throwawayPile;
QList<QList<ColumnSlot*>> m_columns;
QList<QList<PlayingCard*>> m_foundation;
bool m_gameWon;
bool tryMoveCardToFoundation(PlayingCard::Suit foundationId, PlayingCard* cardToMove);
bool tryAutoMoveCard(PlayingCard* cardToMove);
bool isMoveToColumnLegal(PlayingCard* cardToMove, int columnId);
void ensureColumnRevealed(int columnId);
};
#endif // GAMESTATE_H