2024-12-01 21:40:42 +00:00
|
|
|
#include "gamestate.h"
|
|
|
|
#include <random>
|
2024-12-02 15:10:51 +00:00
|
|
|
#include <QDebug>
|
2024-12-01 21:40:42 +00:00
|
|
|
|
|
|
|
GameState::GameState(QObject *parent)
|
|
|
|
: QObject{parent}
|
|
|
|
{
|
|
|
|
// Initialize the foundation piles (4 suits)
|
|
|
|
for (int i = 0; i < 4; ++i) {
|
|
|
|
m_foundation.append(QList<PlayingCard*>());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize the columns (7 piles)
|
|
|
|
for (int i = 0; i < 7; ++i) {
|
2024-12-02 15:10:51 +00:00
|
|
|
m_columns.append(QList<ColumnSlot*>());
|
2024-12-01 21:40:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GameState::dealCards()
|
|
|
|
{
|
2024-12-03 14:14:27 +00:00
|
|
|
qDebug() << "Dealing cards";
|
|
|
|
|
2024-12-01 21:40:42 +00:00
|
|
|
QList<PlayingCard*> deck = PlayingCard::createDeck();
|
|
|
|
|
|
|
|
// Randomly shuffle the deck
|
|
|
|
std::random_device rd;
|
|
|
|
std::default_random_engine rng(rd());
|
|
|
|
std::shuffle(deck.begin(), deck.end(), rng);
|
|
|
|
|
|
|
|
// Deal the cards into the columns
|
|
|
|
int index = 0;
|
|
|
|
for (int i = 0; i < 7; i++) {
|
2024-12-02 15:10:51 +00:00
|
|
|
QList<ColumnSlot*> column;
|
|
|
|
|
2024-12-01 21:40:42 +00:00
|
|
|
// Deal exactly i+1 cards to the i-th column
|
2024-12-02 15:10:51 +00:00
|
|
|
for (int j = 0; j <= i; j++) {
|
|
|
|
bool revealed = (j == i);
|
|
|
|
ColumnSlot *col = new ColumnSlot(deck[index], revealed);
|
|
|
|
column.append(col);
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_columns[i] = column;
|
2024-12-01 21:40:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Use the remaining cards as the draw pile
|
|
|
|
m_drawPile = deck.mid(index);
|
|
|
|
|
|
|
|
// Reset the foundation & throwaway pile
|
|
|
|
m_throwawayPile.clear();
|
2024-12-03 01:15:49 +00:00
|
|
|
for (auto &column : m_foundation)
|
|
|
|
column.clear();
|
2024-12-01 21:40:42 +00:00
|
|
|
|
2024-12-03 00:33:12 +00:00
|
|
|
// Note that we don't need to reset gameWon from here, as it's
|
|
|
|
// auto-checked from onFoundationChanged, which the emits trigger
|
|
|
|
|
2024-12-02 23:33:22 +00:00
|
|
|
emit drawPileChanged();
|
|
|
|
emit throwawayPileChanged();
|
|
|
|
emit columnsChanged();
|
|
|
|
emit foundationChanged();
|
2024-12-01 21:40:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GameState::drawNextCard()
|
|
|
|
{
|
2024-12-03 14:14:27 +00:00
|
|
|
qDebug() << "Drawing next card.";
|
|
|
|
|
2024-12-01 21:40:42 +00:00
|
|
|
// If drawPile is empty, flip the throwawayPile to drawPile
|
|
|
|
if (m_drawPile.isEmpty()) {
|
2024-12-03 14:14:27 +00:00
|
|
|
if (m_throwawayPile.isEmpty()) {
|
|
|
|
qWarning() << "Drawing a card failed, no more cards to draw from";
|
|
|
|
return;
|
|
|
|
}
|
2024-12-01 21:40:42 +00:00
|
|
|
m_drawPile = m_throwawayPile;
|
|
|
|
m_throwawayPile.clear();
|
|
|
|
std::reverse(m_drawPile.begin(), m_drawPile.end());
|
2024-12-03 14:14:27 +00:00
|
|
|
qDebug() << "> Draw pile empty, flipping throwaway pile";
|
2024-12-01 21:40:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Draw the top card from drawPile, dropping it into throwawayPile
|
|
|
|
m_throwawayPile.append(m_drawPile.takeFirst());
|
2024-12-03 14:14:27 +00:00
|
|
|
qDebug() << "> Drawn card: " << m_throwawayPile.last()->toString();
|
2024-12-01 21:40:42 +00:00
|
|
|
|
2024-12-02 23:33:22 +00:00
|
|
|
emit drawPileChanged();
|
|
|
|
emit throwawayPileChanged();
|
2024-12-01 21:40:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool GameState::moveCardToColumn(int columnId)
|
|
|
|
{
|
|
|
|
assert(columnId >= 0 && columnId < 7);
|
|
|
|
|
|
|
|
if (m_throwawayPile.isEmpty()) {
|
2024-12-03 00:50:52 +00:00
|
|
|
qWarning() << "Attempted to move thrown card to column with empty throwaway pile";
|
2024-12-01 21:40:42 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We'll be moving the last card in the throwaway pile (maybe)
|
|
|
|
PlayingCard *cardToMove = m_throwawayPile.last();
|
2024-12-03 14:14:27 +00:00
|
|
|
qDebug() << "Attempting to move thrown card " << cardToMove->toString() << " to column " << columnId;
|
2024-12-01 21:40:42 +00:00
|
|
|
|
2024-12-03 14:14:27 +00:00
|
|
|
if (!isMoveToColumnLegal(cardToMove, columnId)) {
|
|
|
|
qDebug() << "> Moving aborted, illegal move";
|
2024-12-01 21:40:42 +00:00
|
|
|
return false;
|
2024-12-03 14:14:27 +00:00
|
|
|
}
|
2024-12-01 21:40:42 +00:00
|
|
|
|
2024-12-02 15:10:51 +00:00
|
|
|
ColumnSlot *col = new ColumnSlot(cardToMove, true);
|
|
|
|
m_columns[columnId].append(col);
|
2024-12-01 21:40:42 +00:00
|
|
|
m_throwawayPile.removeLast();
|
2024-12-02 15:10:51 +00:00
|
|
|
ensureColumnRevealed(columnId);
|
2024-12-03 14:14:27 +00:00
|
|
|
qDebug() << "> Moving complete";
|
2024-12-01 21:40:42 +00:00
|
|
|
|
2024-12-02 23:33:22 +00:00
|
|
|
emit throwawayPileChanged();
|
|
|
|
emit columnsChanged();
|
2024-12-01 21:40:42 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GameState::moveThrownCardToFoundation(PlayingCard::Suit foundationId)
|
|
|
|
{
|
|
|
|
if (m_throwawayPile.isEmpty()) {
|
2024-12-03 00:50:52 +00:00
|
|
|
qWarning() << "Attempted to move thrown card to foundation with empty throwaway pile";
|
2024-12-01 21:40:42 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We'll be moving the last card in the foundation pile (maybe)
|
|
|
|
PlayingCard *cardToMove = m_throwawayPile.last();
|
2024-12-03 14:14:27 +00:00
|
|
|
qDebug() << "Attempting to move thrown card " << cardToMove->toString() << " to foundation " << foundationId;
|
2024-12-01 21:40:42 +00:00
|
|
|
|
|
|
|
// Try moving the card into the foundation
|
2024-12-03 14:14:27 +00:00
|
|
|
if (!tryMoveCardToFoundation(foundationId, cardToMove)) {
|
|
|
|
qDebug() << "> Moving aborted, illegal move";
|
2024-12-01 21:40:42 +00:00
|
|
|
return false;
|
2024-12-03 14:14:27 +00:00
|
|
|
}
|
2024-12-01 21:40:42 +00:00
|
|
|
|
|
|
|
// We succeeded, the card is now in the appropriate foundation pile,
|
|
|
|
// let's remove the card from the throwaway pile.
|
|
|
|
m_throwawayPile.removeLast();
|
2024-12-03 14:14:27 +00:00
|
|
|
qDebug() << "> Moving complete";
|
2024-12-01 21:40:42 +00:00
|
|
|
|
2024-12-02 23:33:22 +00:00
|
|
|
emit throwawayPileChanged();
|
2024-12-01 21:40:42 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GameState::moveColumnCardToFoundation(int columnId, PlayingCard::Suit foundationId)
|
|
|
|
{
|
|
|
|
assert(columnId >= 0 && columnId < 7);
|
|
|
|
|
|
|
|
if (m_columns[columnId].isEmpty()) {
|
2024-12-03 00:50:52 +00:00
|
|
|
qWarning() << "Attempted to move card to foundation from an empty column";
|
2024-12-01 21:40:42 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-12-03 14:13:20 +00:00
|
|
|
// We'll be moving the last card in the column (maybe)
|
|
|
|
PlayingCard *cardToMove = m_columns[columnId].last()->card();
|
2024-12-03 14:14:27 +00:00
|
|
|
qDebug() << "Attempting to move card " << cardToMove->toString() << " from column " << columnId << " to foundation " << foundationId;
|
2024-12-01 21:40:42 +00:00
|
|
|
|
|
|
|
// Try moving the card into the foundation
|
2024-12-03 14:14:27 +00:00
|
|
|
if (!tryMoveCardToFoundation(foundationId, cardToMove)) {
|
|
|
|
qDebug() << "> Moving aborted, illegal move";
|
2024-12-01 21:40:42 +00:00
|
|
|
return false;
|
2024-12-03 14:14:27 +00:00
|
|
|
}
|
2024-12-01 21:40:42 +00:00
|
|
|
|
|
|
|
// We succeeded, the card is now in the appropriate foundation pile,
|
2024-12-02 15:10:51 +00:00
|
|
|
// let's remove the column slot.
|
2024-12-03 14:13:20 +00:00
|
|
|
m_columns[columnId].removeLast();
|
2024-12-02 15:10:51 +00:00
|
|
|
ensureColumnRevealed(columnId);
|
2024-12-03 14:14:27 +00:00
|
|
|
qDebug() << "> Moving complete";
|
2024-12-01 21:40:42 +00:00
|
|
|
|
2024-12-02 23:33:22 +00:00
|
|
|
emit columnsChanged();
|
2024-12-01 21:40:42 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-12-03 00:55:54 +00:00
|
|
|
bool GameState::autoMoveThrownCard()
|
|
|
|
{
|
|
|
|
// NOTE: This method is very similar to moveThrownCardToFoundation,
|
|
|
|
// consider reducing the repetitino here somehow.
|
|
|
|
|
|
|
|
if (m_throwawayPile.isEmpty()) {
|
|
|
|
// Consider raising an exception here instead
|
|
|
|
qWarning() << "Attempted to move thrown card to foundation with empty throwaway pile";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We'll be moving the last card in the foundation pile (maybe)
|
|
|
|
PlayingCard *cardToMove = m_throwawayPile.last();
|
2024-12-03 14:14:27 +00:00
|
|
|
qDebug() << "Attempting auto-move of thrown card " << cardToMove->toString();
|
2024-12-03 00:55:54 +00:00
|
|
|
|
|
|
|
// Try moving the card into the foundation
|
2024-12-03 14:14:27 +00:00
|
|
|
if (!tryAutoMoveCard(cardToMove)) {
|
|
|
|
qDebug() << "> Moving failed, no available move found";
|
2024-12-03 00:55:54 +00:00
|
|
|
return false;
|
2024-12-03 14:14:27 +00:00
|
|
|
}
|
2024-12-03 00:55:54 +00:00
|
|
|
|
|
|
|
// We succeeded, the card is now in the appropriate foundation pile,
|
|
|
|
// let's remove the card from the throwaway pile.
|
|
|
|
m_throwawayPile.removeLast();
|
2024-12-03 14:14:27 +00:00
|
|
|
qDebug() << "> Moving complete";
|
2024-12-03 00:55:54 +00:00
|
|
|
|
|
|
|
emit throwawayPileChanged();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GameState::autoMoveColumnCard(int columnId)
|
|
|
|
{
|
|
|
|
// NOTE: This method is very similar to moveColumnCardToFoundation,
|
|
|
|
// consider reducing the repetitino here somehow.
|
|
|
|
|
|
|
|
assert(columnId >= 0 && columnId < 7);
|
|
|
|
|
|
|
|
if (m_columns[columnId].isEmpty()) {
|
|
|
|
// Consider raising an exception here instead
|
|
|
|
qWarning() << "Attempted to move card to foundation from an empty column";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-12-03 14:13:20 +00:00
|
|
|
// We'll be moving the last card in the column (maybe)
|
|
|
|
PlayingCard *cardToMove = m_columns[columnId].last()->card();
|
2024-12-03 14:14:27 +00:00
|
|
|
qDebug() << "Attempting auto-move of column card " << cardToMove->toString();
|
2024-12-03 00:55:54 +00:00
|
|
|
|
2024-12-03 14:14:27 +00:00
|
|
|
if (!tryAutoMoveCard(cardToMove)) {
|
|
|
|
qDebug() << "> Moving failed, no available move found";
|
2024-12-03 00:55:54 +00:00
|
|
|
return false;
|
2024-12-03 14:14:27 +00:00
|
|
|
}
|
2024-12-03 00:55:54 +00:00
|
|
|
|
|
|
|
// We succeeded, the card is now in the appropriate foundation pile,
|
|
|
|
// let's remove the column slot.
|
2024-12-03 14:13:20 +00:00
|
|
|
m_columns[columnId].removeLast();
|
2024-12-03 00:55:54 +00:00
|
|
|
ensureColumnRevealed(columnId);
|
2024-12-03 14:14:27 +00:00
|
|
|
qDebug() << "> Moving complete";
|
2024-12-03 00:55:54 +00:00
|
|
|
|
|
|
|
emit columnsChanged();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-12-03 00:33:12 +00:00
|
|
|
void GameState::onFoundationChanged()
|
|
|
|
{
|
|
|
|
// Check if the game is won (can only happen on a foundation pile change)
|
|
|
|
bool gameWon = true;
|
|
|
|
for (const QList<PlayingCard*> &foundationPile : std::as_const(m_foundation)) {
|
|
|
|
// The piles need to contain all 13 card values each, otherwise the game isn't won
|
|
|
|
if (foundationPile.size() != 13) {
|
|
|
|
gameWon = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gameWon == m_gameWon)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (gameWon)
|
|
|
|
qDebug() << "The game was won!";
|
|
|
|
|
|
|
|
m_gameWon = gameWon;
|
|
|
|
emit gameWonChanged();
|
|
|
|
}
|
|
|
|
|
2024-12-01 21:40:42 +00:00
|
|
|
bool GameState::tryMoveCardToFoundation(PlayingCard::Suit foundationId, PlayingCard* cardToMove)
|
|
|
|
{
|
2024-12-03 01:15:49 +00:00
|
|
|
assert(foundationId >= PlayingCard::Suit::Clubs && foundationId <= PlayingCard::Suit::Spades);
|
2024-12-01 21:40:42 +00:00
|
|
|
|
2024-12-03 14:14:27 +00:00
|
|
|
qDebug() << "* Trying to move card " << cardToMove->toString() << " to foundation " << foundationId;
|
|
|
|
|
|
|
|
if (cardToMove->suit() != foundationId) {
|
|
|
|
qDebug() << "* Move attempt failed (wrong suit)";
|
2024-12-01 21:40:42 +00:00
|
|
|
return false;
|
2024-12-03 14:14:27 +00:00
|
|
|
}
|
2024-12-01 21:40:42 +00:00
|
|
|
|
|
|
|
PlayingCard::Value requiredValue;
|
|
|
|
if (m_foundation[foundationId].isEmpty()) {
|
|
|
|
// If the pile is empty, only an ace can go in
|
|
|
|
requiredValue = PlayingCard::Value::Ace;
|
|
|
|
} else {
|
|
|
|
// Otherwise it's the next card by value, unless we're already at king
|
|
|
|
PlayingCard::Value curValue = m_foundation[foundationId].first()->value();
|
2024-12-03 14:14:27 +00:00
|
|
|
if (curValue == PlayingCard::Value::King) {
|
|
|
|
qDebug() << "* Move attempt failed (expected King)";
|
2024-12-01 21:40:42 +00:00
|
|
|
return false;
|
2024-12-03 14:14:27 +00:00
|
|
|
}
|
2024-12-01 21:40:42 +00:00
|
|
|
|
|
|
|
// Clever trick to get the next value. Note that this relies on the enum having
|
|
|
|
// the variants defined in correct order.
|
|
|
|
requiredValue = static_cast<PlayingCard::Value>(static_cast<int>(curValue) + 1);
|
|
|
|
}
|
|
|
|
|
2024-12-03 14:14:27 +00:00
|
|
|
if (cardToMove->value() != requiredValue) {
|
|
|
|
qDebug() << "* Move attempt failed (expected value: " << requiredValue << ")";
|
2024-12-01 21:40:42 +00:00
|
|
|
return false;
|
2024-12-03 14:14:27 +00:00
|
|
|
}
|
2024-12-01 21:40:42 +00:00
|
|
|
|
|
|
|
m_foundation[foundationId].push_front(cardToMove);
|
2024-12-03 14:14:27 +00:00
|
|
|
qDebug() << "* Moved card " << cardToMove->toString() << " to foundation " << foundationId;
|
2024-12-01 21:40:42 +00:00
|
|
|
|
2024-12-02 23:33:22 +00:00
|
|
|
emit foundationChanged();
|
2024-12-01 21:40:42 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-12-03 00:55:54 +00:00
|
|
|
bool GameState::tryAutoMoveCard(PlayingCard *cardToMove)
|
|
|
|
{
|
|
|
|
// 1. Try moving the card to the foundation
|
|
|
|
for (PlayingCard::Suit suit : {PlayingCard::Suit::Clubs, PlayingCard::Suit::Diamonds, PlayingCard::Suit::Hearts, PlayingCard::Suit::Spades})
|
2024-12-03 14:14:27 +00:00
|
|
|
if (cardToMove->suit() == suit && tryMoveCardToFoundation(suit, cardToMove)) {
|
|
|
|
qDebug() << "* Auto-moved card " << cardToMove->toString() << " to foundation " << suit;
|
2024-12-03 00:55:54 +00:00
|
|
|
return true;
|
2024-12-03 14:14:27 +00:00
|
|
|
}
|
2024-12-03 00:55:54 +00:00
|
|
|
|
|
|
|
// 2. Try moving the card to another column
|
|
|
|
for (int columnId = 0; columnId < m_columns.size(); ++columnId)
|
|
|
|
if (isMoveToColumnLegal(cardToMove, columnId)) {
|
|
|
|
moveCardToColumn(columnId);
|
2024-12-03 14:14:27 +00:00
|
|
|
qDebug() << "* Auto-moved card " << cardToMove->toString() << " to column " << columnId;
|
2024-12-03 00:55:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// No available auto-move
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-12-03 00:54:01 +00:00
|
|
|
bool GameState::isMoveToColumnLegal(PlayingCard *cardToMove, int columnId)
|
|
|
|
{
|
|
|
|
assert(columnId >= 0 && columnId < 7);
|
|
|
|
|
|
|
|
if (m_columns[columnId].isEmpty()) {
|
|
|
|
// Column is empty: only a King can be placed in an empty column
|
|
|
|
return cardToMove->value() == PlayingCard::Value::King;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compare against the last card in the column
|
|
|
|
PlayingCard* columnCard = m_columns[columnId].last()->card();
|
|
|
|
|
|
|
|
// The card's value must be one less than the card in the column
|
|
|
|
if (cardToMove->value() != columnCard->value() - 1)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// The card must be of opposite color
|
|
|
|
return PlayingCard::areOppositeColors(*cardToMove, *columnCard);
|
|
|
|
}
|
|
|
|
|
2024-12-02 15:10:51 +00:00
|
|
|
void GameState::ensureColumnRevealed(int columnId)
|
|
|
|
{
|
|
|
|
assert(columnId >= 0 && columnId < 7);
|
|
|
|
|
|
|
|
// Nothing to reveal
|
|
|
|
if (m_columns[columnId].isEmpty())
|
|
|
|
return;
|
|
|
|
|
2024-12-03 14:13:20 +00:00
|
|
|
// Get the last column slot
|
|
|
|
ColumnSlot *col = m_columns[columnId].last();
|
2024-12-02 15:10:51 +00:00
|
|
|
|
|
|
|
// If it's already revealed, there's nothing to do
|
|
|
|
if (col->isRevealed())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// First slot in the column must always be revealed, reveal it
|
|
|
|
col->reveal();
|
|
|
|
}
|
|
|
|
|
2024-12-01 21:40:42 +00:00
|
|
|
QList<PlayingCard *> GameState::drawPile() const
|
|
|
|
{
|
|
|
|
return m_drawPile;
|
|
|
|
}
|
|
|
|
|
|
|
|
QList<PlayingCard *> GameState::throwawayPile() const
|
|
|
|
{
|
|
|
|
return m_throwawayPile;
|
|
|
|
}
|
|
|
|
|
2024-12-02 15:10:51 +00:00
|
|
|
QList<QList<ColumnSlot *> > GameState::columns() const
|
2024-12-01 21:40:42 +00:00
|
|
|
{
|
|
|
|
return m_columns;
|
|
|
|
}
|
|
|
|
|
|
|
|
QList<QList<PlayingCard *> > GameState::foundation() const
|
|
|
|
{
|
|
|
|
return m_foundation;
|
|
|
|
}
|
2024-12-03 00:33:12 +00:00
|
|
|
|
|
|
|
bool GameState::gameWon() const
|
|
|
|
{
|
|
|
|
return m_gameWon;
|
|
|
|
}
|