solitare/gamestate.h

74 lines
2.3 KiB
C
Raw Normal View History

2024-12-01 21:40:42 +00:00
#ifndef GAMESTATE_H
#define GAMESTATE_H
#include <QObject>
#include <qqmlintegration.h>
2024-12-01 21:40:42 +00:00
#include "playingcard.h"
2024-12-02 15:10:51 +00:00
#include "columnslot.h"
2024-12-01 21:40:42 +00:00
class GameState : public QObject
{
Q_OBJECT
QML_ELEMENT
2024-12-03 14:32:30 +00:00
QML_SINGLETON
2024-12-02 23:33:22 +00:00
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)
2024-12-03 00:33:12 +00:00
Q_PROPERTY(bool gameWon READ gameWon NOTIFY gameWonChanged)
2024-12-01 21:40:42 +00:00
public:
explicit GameState(QObject *parent = nullptr);
2024-12-03 00:55:54 +00:00
// Getters
2024-12-01 21:40:42 +00:00
QList<PlayingCard*> drawPile() const;
QList<PlayingCard*> throwawayPile() const;
2024-12-02 15:10:51 +00:00
QList<QList<ColumnSlot*>> columns() const;
2024-12-01 21:40:42 +00:00
QList<QList<PlayingCard*>> foundation() const;
2024-12-03 00:33:12 +00:00
bool gameWon() const;
2024-12-01 21:40:42 +00:00
2024-12-03 00:55:54 +00:00
// General functions
2024-12-03 23:04:35 +00:00
Q_INVOKABLE void dealCards();
Q_INVOKABLE void setupWinningDeck();
2024-12-03 23:04:35 +00:00
Q_INVOKABLE void drawNextCard();
2024-12-03 00:55:54 +00:00
// Manual moves (from X to Y)
2024-12-03 23:04:35 +00:00
Q_INVOKABLE bool moveThrownCardToColumn(int columnId);
Q_INVOKABLE bool moveThrownCardToFoundation(int foundationId);
Q_INVOKABLE bool moveColumnCardToColumn(int fromColumnId, int toColumnId, int fromCardIndex);
Q_INVOKABLE bool moveColumnCardToFoundation(int columnId, int foundationId);
2024-12-01 21:40:42 +00:00
2024-12-03 00:55:54 +00:00
// Automatic moves (from X to auto)
2024-12-03 23:04:35 +00:00
Q_INVOKABLE bool autoMoveThrownCard();
Q_INVOKABLE bool autoMoveColumnCard(int columnId, int cardIndex);
2024-12-03 00:55:54 +00:00
2024-12-01 21:40:42 +00:00
signals:
2024-12-02 23:33:22 +00:00
void drawPileChanged();
void throwawayPileChanged();
void columnsChanged();
void foundationChanged();
2024-12-03 00:33:12 +00:00
void gameWonChanged();
private slots:
void onFoundationChanged();
2024-12-01 21:40:42 +00:00
private:
QList<PlayingCard*> m_drawPile;
QList<PlayingCard*> m_throwawayPile;
2024-12-02 15:10:51 +00:00
QList<QList<ColumnSlot*>> m_columns;
2024-12-01 21:40:42 +00:00
QList<QList<PlayingCard*>> m_foundation;
2024-12-03 00:33:12 +00:00
bool m_gameWon;
2024-12-01 21:40:42 +00:00
void cleanupBoard(bool emitChanges);
2024-12-03 23:04:35 +00:00
bool tryAutoMoveSingleCard(PlayingCard& cardToMove, int skipColumnId = -1);
bool tryAutoMoveMultipleCards(const QList<PlayingCard*>& cards, int skipColumnId);
bool isFoundationMoveValid(const PlayingCard& cardToMove, int foundationId);
bool isColumnMoveValid(const PlayingCard& cardToMove, int columnId);
2024-12-02 15:10:51 +00:00
void ensureColumnRevealed(int columnId);
2024-12-01 21:40:42 +00:00
};
#endif // GAMESTATE_H