People have spent so much time playing card games of solitaire. A form of solitaire is described below. Your task is to write a computer program to play the game thus freeing hours of time for people to play more useful games.
To begin the game, 28 cards are dealt into 7 piles. The leftmost pile has 1 card, the next 2 cards, and so forth up to 7 cards in the rightmost pile. Only the uppermost card of each of the 7 piles is turned face up. The cards are dealt left to right, one card to each pile, dealing to one less pile each time, and turning the first card in each round face up.
On the topmost face-up card of each pile, you may build in descending sequences red on black or black on red. For example, on the 9 of spaded, you may place either the 8 of diamonds or the 8 of hearts. All face-up cards on a pile are moved as a unit and may be placed on another pile according to the bottommost face-up card. For example, the 7 of clubs on the 8 of hearts may be moved as a unit onto the 9 of clubs or the 9 of spades.
Whenever a face-down card is uncovered, it is turned face up. If one pile is removed completely, a face-up king may be moved from a pile (together with all cards above it) or the top of the waste pile (see below) into the vacated space. There are four output piles, one for each suit, and the object of the game is to get as many cards as possible into the output piles. Each time an Ace appears at the top of a pile or the top of the stack, it is moved into the appropriate output pile. Cards are added to the output piles in sequence, the suit for each pile being determined by the Ace on the bottom.
From the rest of the deck, called the stock, cards are turned up one by one and placed face up on a waste pile. You may always play cards off the top of the waste pile, but only one at a time. Begin by moving a card from the stock to the top of the waste pile. If there is ever more than one possible play to be made, the following order must be observed:
- Move a card from the top of a playing pile or from the top of the waste pile to an output pile. If the waste pile becomes empty, move a card from the stock to the waste pile.
- Move a card from the top of the waste pile to the leftmost playing pile to which it can be moved. If the waste pile becomes empty, move a card from the stock to the waste pile.
- Find the leftmost playing pile which can be moved and place it on top of the leftmost playing pile to which it can be moved.
- Try 1., 2. and 3. in sequence, restarting with 1. whenever a move is made.
- If no move is made via 1. - 4., move a card from the stock to the waste pile and retry 1.
Only the topmost card of the playing piles or the waste pile may be placed to an output pile. Once played or an output pile, a card may not be withdrawn to help elsewhere. The game is over either:
- all the cards have been played to the output, or
- the stock pile has been exhausted and no more cards can be moved.
When played for money, the player pays the house 52 dollars at the beginning, and wins 5 dollars for every card played to the output piles.
Write your program so that it will play several games, and determine your net winnings. Use a random generator to shuffle the deck.
Output a complete record of 2 games in easily understood form. Include as output the number of games played and the net winnings (+ or -).
Difficulty level
This exercise is mostly suitable for students
Send me your solution and get featured here !!
Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Articulation points cut bridges and cut edges