This code uses object oriented programming in Python to simulate Solitaire games.
To run one simulated game of Solitaire, use
python main.py
on the console. This will output a list of all the actions, the position of the cards at the end of play, and whether or not this was a successful game (happens about 4% of the time)
- Deck: Stack of cards that can be drawn from. This code only supports one card drawn at a time
- Play Piles: The seven piles that cards are added to and shuffled around. Each pile can contain flipped up cards that have to follow sequential order and alternate colors
- Block Piles: Also called foundations - the four piles (one for each suit) that build up from Ace to King. When all block piles are completed, the player wins the game
The code has three files:
-
card_elements.py
contains 3 classes: Card, Pile, and Deck. Each of these three classes have methods and variables that persist throughout the game. -
solitaire.py
contains 1 class: Game. This contains methods for initializing a game of solitaire, checking/outputing status, and simulating a complete game using an ad-hoc algorithm discussed below. -
main.py
contains a simple script to simulate one game, and output the elements and result.
The possible actions for each turn is as follows:
-
Make sure first card in each play pile is flipped up
-
Move any eligible cards to the block piles
-
If possible, move any kings to any empty play pile
-
If possible, add drawn card to a play pile
-
Permute flipped up cards in the play piles to see if any of them can be switched around. Only move cards between play piles if the number of cards faced down in the old pile is more than the number of cards faced down in the new pile
-
Draw new card
The algorithm will pick the first item on the list that is successful, and restart from the top on the next turn. If there are no possible moves left, the game will end, and can be checked if it is finished or not.
This is definitely not the best algorithm for this game, and better ones like this one can be found readily. This was just the simplest one I could think of that made sense and could sometimes win.