solitare/gamestate.cpp

573 lines
17 KiB
C++
Raw Normal View History

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}
{
assert(connect(this, SIGNAL(foundationChanged()), this, SLOT(onFoundationChanged())));
2024-12-03 23:04:35 +00:00
m_foundation.resize(4);
m_columns.resize(7);
2024-12-03 14:32:30 +00:00
dealCards();
2024-12-01 21:40:42 +00:00
}
void GameState::dealCards()
{
2024-12-03 14:14:27 +00:00
qDebug() << "Dealing cards";
cleanupBoard(false);
2024-12-03 14:14:27 +00:00
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
2024-12-03 23:04:35 +00:00
assert(index == 28);
2024-12-01 21:40:42 +00:00
m_drawPile = deck.mid(index);
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::setupWinningDeck()
{
// BUG: This causes a memory leak when called again
qDebug() << "Setting up a winning deck";
cleanupBoard(false);
// Create a sorted deck of cards (4 suits, ordered)
QList<PlayingCard*> deck = PlayingCard::createDeck();
// Setup the foundation with all cards except one per suit
for (int suit = 0; suit < 4; ++suit) {
QList<PlayingCard*> foundationPile;
for (int rank = 1; rank <= 12; ++rank) { // Leave the King (rank 13) out
foundationPile.prepend(deck[suit * 13 + rank - 1]);
}
m_foundation[suit] = foundationPile;
}
// The remaining four Kings are placed in the columns
for (int i = 0; i < 4; ++i) {
QList<ColumnSlot*> column;
PlayingCard* kingCard = deck[i * 13 + 12]; // King of each suit
ColumnSlot* slot = new ColumnSlot(kingCard, true); // Revealed
column.append(slot);
m_columns[i] = column;
}
// Ensure other columns are empty
for (int i = 4; i < 7; ++i) {
m_columns[i].clear();
}
// The draw pile and throwaway pile are empty
m_drawPile.clear();
m_throwawayPile.clear();
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
}
2024-12-03 23:04:35 +00:00
bool GameState::moveThrownCardToColumn(int columnId)
2024-12-01 21:40:42 +00:00
{
assert(columnId >= 0 && columnId < 7);
auto& columnStack = m_columns[columnId];
2024-12-01 21:40:42 +00:00
if (m_throwawayPile.isEmpty()) {
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)
2024-12-03 23:04:35 +00:00
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 23:04:35 +00:00
if (!isColumnMoveValid(*cardToMove, columnId)) {
2024-12-03 14:14:27 +00:00
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-03 23:04:35 +00:00
// Success, perform the move
ColumnSlot* col = new ColumnSlot(cardToMove, true);
columnStack.append(col);
2024-12-01 21:40:42 +00:00
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();
emit columnsChanged();
2024-12-01 21:40:42 +00:00
return true;
}
2024-12-03 23:04:35 +00:00
bool GameState::moveThrownCardToFoundation(int foundationId)
2024-12-01 21:40:42 +00:00
{
2024-12-03 23:04:35 +00:00
assert(foundationId >= 0 && foundationId < 4);
auto& foundationStack = m_foundation[foundationId];
2024-12-03 23:04:35 +00:00
2024-12-01 21:40:42 +00:00
if (m_throwawayPile.isEmpty()) {
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)
2024-12-03 23:04:35 +00:00
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 23:04:35 +00:00
if (!isFoundationMoveValid(*cardToMove, foundationId)) {
2024-12-03 14:14:27 +00:00
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-03 23:04:35 +00:00
// Succeess, perform the move
foundationStack.prepend(cardToMove);
2024-12-01 21:40:42 +00:00
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-03 23:04:35 +00:00
emit foundationChanged();
return true;
}
bool GameState::moveColumnCardToColumn(int fromColumnId, int toColumnId, int fromCardIndex)
{
assert(fromColumnId >= 0 && fromColumnId < 7);
assert(toColumnId >= 0 && toColumnId < 7);
auto fromColumnStack = m_columns[fromColumnId];
auto toColumnStack = m_columns[toColumnId];
if (fromColumnStack.isEmpty()) {
qWarning() << "Attempted to move card(s) to column from an empty column";
return false;
}
ColumnSlot* col = fromColumnStack[fromCardIndex];
if (!col->isRevealed()) {
qWarning() << "Attempted to card(s) to column from unrevealed column slot";
return false;
}
PlayingCard* cardToMove = col->card();
qDebug() << "Attempting to move card " << cardToMove->toString() << " from column " << fromColumnId << " to column " << toColumnId;
// Try moving the card
if (!isColumnMoveValid(*cardToMove, toColumnId)) {
qDebug() << "> Moving aborted, illegal move";
return false;
}
// Success, move the card
toColumnStack.append(col);
fromColumnStack.removeLast();
ensureColumnRevealed(fromColumnId);
qDebug() << "> Moving complete";
emit columnsChanged();
2024-12-01 21:40:42 +00:00
return true;
}
2024-12-03 23:04:35 +00:00
bool GameState::moveColumnCardToFoundation(int columnId, int foundationId)
2024-12-01 21:40:42 +00:00
{
assert(columnId >= 0 && columnId < 7);
2024-12-03 23:04:35 +00:00
assert(foundationId >= 0 && foundationId < 4);
auto& columnStack = m_columns[columnId];
auto& foundationStack = m_foundation[foundationId];
2024-12-01 21:40:42 +00:00
if (m_columns[columnId].isEmpty()) {
qWarning() << "Attempted to move card to foundation from an empty column";
2024-12-01 21:40:42 +00:00
return false;
}
// We'll be moving the last card in the column (maybe)
2024-12-03 23:04:35 +00:00
ColumnSlot* col = columnStack.last();
PlayingCard* cardToMove = col->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 23:04:35 +00:00
if (!isFoundationMoveValid(*cardToMove, foundationId)) {
2024-12-03 14:14:27 +00:00
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-03 23:04:35 +00:00
// Success, move the card
foundationStack.prepend(cardToMove);
columnStack.removeLast();
col->deleteLater();
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-03 23:04:35 +00:00
emit columnsChanged(); // CRASH (not if I remove the delete col line though)
emit foundationChanged();
2024-12-01 21:40:42 +00:00
return true;
}
2024-12-03 00:55:54 +00:00
bool GameState::autoMoveThrownCard()
{
if (m_throwawayPile.isEmpty()) {
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)
2024-12-03 23:04:35 +00:00
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 23:04:35 +00:00
if (!tryAutoMoveSingleCard(*cardToMove)) {
2024-12-03 14:14:27 +00:00
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
2024-12-03 23:04:35 +00:00
// We succeeded, the card was moved, remove it from throwaway pile
2024-12-03 00:55:54 +00:00
m_throwawayPile.removeLast();
2024-12-03 14:14:27 +00:00
qDebug() << "> Moving complete";
2024-12-03 00:55:54 +00:00
emit throwawayPileChanged();
2024-12-03 23:04:35 +00:00
// We don't know which pile the card was moved to, to be safe, emit
// a change from both
// NOTE: consider returning what changed from tryAutoMoveSingleCard
emit columnsChanged();
emit foundationChanged();
2024-12-03 00:55:54 +00:00
return true;
}
2024-12-03 23:04:35 +00:00
bool GameState::autoMoveColumnCard(int columnId, int cardIndex)
2024-12-03 00:55:54 +00:00
{
assert(columnId >= 0 && columnId < 7);
auto& columnStack = m_columns[columnId];
2024-12-03 00:55:54 +00:00
2024-12-03 23:04:35 +00:00
if (columnStack.isEmpty()) {
qWarning() << "Attempted to move card(s) to foundation from an empty column";
2024-12-03 00:55:54 +00:00
return false;
}
2024-12-03 23:04:35 +00:00
ColumnSlot* col = columnStack[cardIndex];
if (!col->isRevealed()) {
qWarning() << "Attempted to card(s) to column from unrevealed column slot";
return false;
}
if (cardIndex == columnStack.size() - 1) {
// This is a single card move (last card)
PlayingCard* cardToMove = col->card();
qDebug() << "Attempting auto-move of column " << columnId << " card " << cardToMove->toString();
if (!tryAutoMoveSingleCard(*cardToMove, columnId)) {
qDebug() << "> Moving failed, no available move found";
return false;
}
// We succeeded, the card was moved, remove it from the original column
columnStack.removeLast();
col->deleteLater();
ensureColumnRevealed(columnId);
qDebug() << "> Moving complete";
emit columnsChanged();
2024-12-03 00:55:54 +00:00
2024-12-03 23:04:35 +00:00
// we don't know where the card was moved, it could've been the foundation too
// to be safe, emit a change signal for it too
// NOTE: consider returning what changed from tryAutoMoveSingleCard
emit foundationChanged();
return true;
}
// This is a multiple cards move
qDebug() << "Attempting auto-move of column " << columnId << " card range " << cardIndex << " to " << columnStack.size() - 1;
QList<PlayingCard*> selectedCards;
for (int i = cardIndex; i < columnStack.size(); ++i) {
2024-12-03 23:04:35 +00:00
ColumnSlot* col = columnStack[i];
selectedCards.append(col->card());
}
if (!tryAutoMoveMultipleCards(selectedCards, cardIndex)) {
2024-12-03 14:14:27 +00:00
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
2024-12-03 23:04:35 +00:00
// We succeeded, the cards were moved,
// now remove the moved cards from the column
while (columnStack.size() > cardIndex) {
ColumnSlot* curSlot = columnStack.takeLast();
curSlot->deleteLater();
}
2024-12-03 00:55:54 +00:00
ensureColumnRevealed(columnId);
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();
}
void GameState::cleanupBoard(bool emitChanges)
{
// Clean up all PlayingCard objects in the draw pile
for (auto &card : m_drawPile) {
card->deleteLater();
}
m_drawPile.clear();
// Clean up all PlayingCard objects in the throwaway pile
for (auto &card : m_throwawayPile) {
card->deleteLater();
}
m_throwawayPile.clear();
// Clean up all PlayingCard objects in the foundation piles
for (auto &foundationPile : m_foundation) {
for (auto card : foundationPile)
card->deleteLater();
foundationPile.clear();
}
// Clean up all ColumnSlot objects in the columns
// alongside with the associated PlayingCard objects
// that they hold
for (auto &column : m_columns) {
for (auto slot : column) {
slot->card()->deleteLater();
slot->deleteLater();
}
column.clear();
}
// Note that we don't need to reset gameWon from here, as it's
// auto-checked from onFoundationChanged, which the emits trigger
if (emitChanges) {
emit drawPileChanged();
emit throwawayPileChanged();
emit foundationChanged();
emit columnsChanged();
}
}
2024-12-03 23:04:35 +00:00
bool GameState::tryAutoMoveSingleCard(PlayingCard &cardToMove, int skipColumnId)
2024-12-01 21:40:42 +00:00
{
2024-12-03 23:04:35 +00:00
// 1. Try moving the card to the foundation
const int foundationId = static_cast<int>(cardToMove.suit());
if (isFoundationMoveValid(cardToMove, foundationId)) {
m_foundation[foundationId].prepend(&cardToMove);
qDebug() << "* Auto-moved card " << cardToMove.toString() << " to foundation " << foundationId;
return true;
}
2024-12-01 21:40:42 +00:00
2024-12-03 23:04:35 +00:00
// 2. Try moving the card to another column
for (int columnId = 0; columnId < m_columns.size(); ++columnId) {
if (columnId == skipColumnId)
continue;
if (isColumnMoveValid(cardToMove, columnId)) {
ColumnSlot* col = new ColumnSlot(&cardToMove, true);
m_columns[columnId].append(col);
qDebug() << "* Auto-moved card " << cardToMove.toString() << " to column " << columnId;
return true;
}
}
// No available auto-move
qDebug() << "* Auto-move failed, no available moves";
return false;
}
2024-12-03 14:14:27 +00:00
2024-12-03 23:04:35 +00:00
bool GameState::tryAutoMoveMultipleCards(const QList<PlayingCard*>& cards, int skipColumnId)
{
assert(cards.size() > 1);
// If we can move the first (selected) card to another column,
// we can also move the rest of the cards below to that column,
// so we only need to care about the first card.
// (Foundation moves are impossible with multiple card movements).
PlayingCard* firstCard = cards.first();
for (int columnId = 0; columnId < m_columns.size(); ++columnId) {
if (columnId == skipColumnId)
continue;
if (isColumnMoveValid(*firstCard, columnId)) {
for (auto card : cards) {
ColumnSlot* col = new ColumnSlot(card, true);
m_columns[columnId].append(col);
qDebug() << "* Auto-moved card " << card->toString() << " to column " << columnId;
}
return true;
}
}
qDebug() << "* Auto-move failed, no available moves";
return false;
}
bool GameState::isFoundationMoveValid(const PlayingCard& cardToMove, int foundationId)
{
assert(foundationId >= 0 && foundationId < 4);
const auto foundationSuit = static_cast<PlayingCard::Suit>(foundationId);
const auto& foundationStack = m_foundation[foundationId];
2024-12-03 23:04:35 +00:00
// The card must match the suit of the foundation
if (cardToMove.suit() != foundationSuit) {
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;
2024-12-03 23:04:35 +00:00
if (foundationStack.isEmpty()) {
2024-12-01 21:40:42 +00:00
// 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
2024-12-03 23:04:35 +00:00
PlayingCard::Value curValue = foundationStack.first()->value();
2024-12-03 14:14:27 +00:00
if (curValue == PlayingCard::Value::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 23:04:35 +00:00
return cardToMove.value() == requiredValue;
2024-12-03 00:55:54 +00:00
}
2024-12-03 23:04:35 +00:00
bool GameState::isColumnMoveValid(const PlayingCard& cardToMove, int columnId) {
assert(columnId >= 0 && columnId < 7);
const auto& columnStack = m_columns[columnId];
2024-12-03 23:04:35 +00:00
if (columnStack.isEmpty()) {
// Column is empty: only a King can be placed in an empty column
2024-12-03 23:04:35 +00:00
return cardToMove.value() == PlayingCard::Value::King;
}
// Compare against the last card in the column
2024-12-03 23:04:35 +00:00
const PlayingCard& columnCard = *columnStack.last()->card();
// The card's value must be one less than the card in the column
2024-12-03 23:04:35 +00:00
if (cardToMove.value() != columnCard.value() - 1) {
qDebug() << "* Move attempt failed (wrong value)";
return false;
2024-12-03 23:04:35 +00:00
}
// The card must be of opposite color
2024-12-03 23:04:35 +00:00
return PlayingCard::areOppositeColors(cardToMove, columnCard);
}
2024-12-03 23:04:35 +00:00
2024-12-02 15:10:51 +00:00
void GameState::ensureColumnRevealed(int columnId)
{
assert(columnId >= 0 && columnId < 7);
2024-12-04 17:02:54 +00:00
auto& columnStack = m_columns[columnId];
2024-12-02 15:10:51 +00:00
// Nothing to reveal
if (m_columns[columnId].isEmpty())
return;
// Get the last column slot
2024-12-04 17:02:54 +00:00
ColumnSlot *col = columnStack.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-04 17:02:54 +00:00
qDebug() << "Revealed card " << col->card()->toString() << " in column " << columnId;
2024-12-02 15:10:51 +00:00
}
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;
}