Share
Explore BrainMass

developing a game program in c

//Game specification is to write a program that plays a variant of the solitaire
//game known as Klondike. The goal is to construct four sequences of cards. The
//foundation piles, each sequence is formed by putting cards of the same suit
//into the same pile ascending order beginning with the ace. The setup of the game
//begins by shuffling a deck of cards .Seven piles of cards are dealt out to form the
//tableau, The first pile has one card. The second has 2 etc. The top card on each pile
// is turned up. The others remain hidden. A card can be added to the foundation pile
//pile if its value is one greater than that of the topmost visible card and the color
//is opposite. All of the cards of the pile can be moved to another pile if the bottommost
//visible card can legally be added to the pile. When all of the visible cards are moved,
//the top hidden card is turned over. If there are no more hidden cards the empty spot
//can be occupied by the visible cards from the other pile. A card can be added to the
//foundation pile if its value is one greater than the topmost card and the suite is the
//same. The first card in the pile must be an ace of that suit. Cards can be moved to a
//fondation pile. Cards can be moved to a foundation pile from either the top visible
//card in the tableau pile or the waste pile. In the beginning of play the waste pile is
//empty .After adjustments are made to the initial tableau a card is taken form the
//remainder of the deck and placed as the first card in the waste pile. This card can be
//moved to either a foundation pile or a tableau pile if it fits. If it doesn't fit it remains in
// the waste pile. Another card from the remaining deck is drawn and placed on the waste
//pile. if a drawn card can be placed on a pile this may open up the possibility of moving
//cards around in the tableau. It may also enable the top card in the waste pile to be placed
//.The adjustments are made until nothing can be moved. Play continues until all of the
//original cards in the deck are in play and all adjustments are made. It is then permitted
//to move all of the cards in the waste pile back into the deck .Another round of play
//resumes until the deck is emptied.

//The program should have the capability to play the game. A combination of structures
//stacke and queues to represent the various groups of cards. You can represent a card
//using a structure with one component that is an integer value to hold a the face value
// and another that in an enumerated value to hold the suit. You can use a queue to
//represent the main deck of cards you will be taking cards from the top and occasionally
//putting others back on the bottom. A foundation pile can be most easily represented by a
//queue since it is required that the contents be displayed. The hidden portion of the
//tableau should be represented by a stack to facilitate adding and removing from the top
//of the pile.The waste pile is also best represented with a stack.
//A suggested order of development is as follows
//*Generate the initial deck of cards. Store 52 cards in an array. Then randomly
//select cards from the set and insert them into a queue for the starting hand of the cards.
//* Initialize the foundation piles and the waste pile to be empty deal out the cards to the
//the seven tableau stacks then take the top cared and place it in the corresponding visible
//queue.
//*Develop a printing routine to display the contents of the foundation piles and the
//visible parts of the tableau pile and the waste pile. A textual printout is all that is needed
//(graphical output is not necessary.
// *Develop the strategy to move around cards as the result of taking one card from the
//remaining deck on the top of the waste pile. Examine first whether this card can be
//placed on the foundation pile. If not can it br played onto one of the tableau piles? Next
//determine if the visible cards on the tableau piles can be moved to another to free up a
//hidden cards. If this happens the new card should be examined for moving to another
//pile. If no visible cards can be moved to another tableau pile then determine if the
//topmost visible cards can be moved to a foundation pile.
//* all of the stepts above should be repeated in a loop until there is no further card
//movement possible.
// organize the play for an entire round. Continue to take cards out of the remaining deck
//hand and deal them until the entire hand is used. check the contents of four foundation
//piles and determine if the game is over.
//* if the game is not over move the cards from the waste pile to the deck .
//continue playing until either the game is won or an entire round goes by without any
//movement of the cards on the table.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

typedef struct{ //typedef selected to eliminate the need for structure tag
char *face;
char *suit;
}Card;

void populate_deck(Card *, char *[], char *[]);
void print_deck(Card*);
void shuffle (Card *);

main()

{

Card deck [52]; //declares an array of 52 cards

char*face[] = {"Ace","2","3","4","5","6","7","8","9","10",
"Jack","Queen","King"};
char *suit[] = {"Hearts","Diamonds","Clubs", "Spades"};

srand(time(NULL));
populate_deck(deck,face,suit);
print_deck(deck);
puts("t Shuffled Deckn");
shuffle (deck);
print_deck(deck);

}

void populate_deck(Card * wDeck, char *wFace[], char *wSuit[])
{
int i;
for(i=0; i<52; i++)
{
wDeck[i].face = wFace[i%13];
wDeck[i].suit = wSuit[i/13];
}
}

void print_deck(Card*wDeck)
{
int i;
for (i = 0; i<52;i++)
printf("%5s of %-8s%c",wDeck[i].face, wDeck[i].suit,
(i+1) % 2 ?'t':'n');
printf("n");
}

void shuffle(Card *wDeck)
{
int i,j;
Card temp;
for (i = 0;i<52;i++)
{
j = rand() %52;
temp = wDeck[i];
wDeck[i]=wDeck[j];
wDeck[j] = temp;
}
}

Attachments

Solution Preview

Dear Student,

I wrote and debugged all the necessary structures (bi-directional lists) and functions/routines. Now all you need to do is to implement the game ...

Solution Summary

The expert develops a game program in c is given.

$2.19