Elegant curly brunette woman wearing trendy summer outfit with round wicker shoulder bag, white top, polka dot midi skirt, strap sandals, posing in street of European city. Copy, empty space for text

C++ code to create a Solitaire game

Start
#include<iostream>
#include<vector>

using namespace std;

// Struct to represent a card
struct Card {
    int value;
    char suit;
};

// Function to print a card
void printCard(Card c) {
    cout << c.value << c.suit << " ";
}

// Function to check if a card can be placed on top of another card
bool canPlace(Card c1, Card c2) {
    return (c1.value == c2.value - 1 && c1.suit == c2.suit);
}

int main()
{
    // Create a vector of vectors to represent the stacks
    vector<vector<Card>> stacks;

    // Create a vector of cards to represent the deck
    vector<Card> deck;

    // Create a card and push it into the deck
    Card c;
    c.value = 1;
    c.suit = 'S';
    deck.push_back(c);

    // Create the remaining cards and push them into the deck
    for (int i = 2; i <= 13; i++) {
        c.value = i;
        c.suit = 'S';
        deck.push_back(c);
        c.suit = 'H';
        deck.push_back(c);
        c.suit = 'D';
        deck.push_back(c);
        c.suit = 'C';
        deck.push_back(c);
    }

    // Shuffle the deck
    random_shuffle(deck.begin(), deck.end());

    // Create 7 stacks and push a card into each stack
    for (int i = 0; i < 7; i++) {
        vector<Card> stack;
        stack.push_back(deck[i]);
        stacks.push_back(stack);
    }

    // Remove the cards from the deck
    deck.erase(deck.begin(), deck.begin() + 7);

    // Game loop
    while (true) {
        // Print the stacks
        for (int i = 0; i < 7; i++) {
            cout << i << ": ";
            for (int j = 0; j < stacks[i].size(); j++) {
                printCard(stacks[i][j]);
            }
            cout << endl;
        }
        // Print the deck
        cout << "D: ";
        for (int i = 0; i < deck.size(); i++) {
            printCard(deck[i]);
        }
        cout << endl;

        // Get input from the user
        int from;
        int to;
        cout << "Move from: ";
        cin >> from;
        cout << "Move to: ";
        cin >> to;

        // Check if the move is valid
        if (from == -1) {
            if (to >= 0 && to <= 6 && canPlace(deck.back(), stacks[to].back())) {
                stacks[to].push_back(deck.back());
                deck.pop_back();
            }
            else {
                cout << "Invalid move!" << endl;
            }
        }
        else {
            if (to == -1 && from >= 0 && from <= 6) {
                deck.push_back(stacks[from].back());
                stacks[from].pop_back();
            }
            else if (from >= 0 && from <= 6 && to >= 0 && to <= 6 && canPlace(stacks[from].back(), stacks[to].back())) {
                stacks[to].push_back(stacks[from].back());
                stacks[from].pop_back();
            }
            else {
                cout << "Invalid move!" << endl;
            }
        }

        // Check if the game is over
        bool gameOver = true;
        for (int i = 0; i < 7; i++) {
            if (stacks[i].size() != 13) {
                gameOver = false;
                break;
            }
        }
        if (gameOver) {
            cout << "You win!" << endl;
            break;
        }
    }

    return 0;
}
Previous Story

This is Apple’s VR headset, look super cool

Next Story

Create powerful Word Processor using this Java code