I dont even know where to start with this program. I have been really busy and he tells us what to do. If anyone could help with this it would be great. here is what it says to do


ASSIGNMENT DESCRIPTION

This program will simulate the card game WAR!.

The following is quoted verbatim from a Wikipedia page:
The deck is divided evenly among the two players, giving each a face-down stack. In unison, each player reveals the top card on his stack (a "battle"), and the player with the higher card takes both the cards played and moves them to the bottom of his stack. If the two cards played are of equal value, each player lays down three face-down cards and a fourth card face-up (a "war"), and the higher-valued card wins all of the cards on the table, which are then added to the bottom of the player's stack. In the case of another tie, the war process is repeated until there is no tie.

A player wins by collecting all the cards. If a player runs out of cards while dealing the face-down cards of a war, he may play the last card in his deck as his face-up card and still have a chance to stay in the game.

ASSIGNMENT SPECIFICATIONS

Arrays will be required in this assignment -- it would be nearly impossible to make a readable program without them.

Arrays may serve in various different ways:

* A deck of cards may be represented as 52 distinct values in an array. The precise meaning and interpretation of each value is not specified in this assignment, but the 52 cards should be distinguishable. Rank is necessary for the game; suit is not, but may provide some color.

The values representing the cards could simply be integers. 0 to 12 (or 1 to 13) could represent 13 ranks of cards; or 0 to 51 could represent 52 diffderent possible cards.
* Each player has a stack, which would best be represented by an array.
* Battle piles representing the cards currently in competition, can also be represented as arrays -- especially in the cases of a war where the winner is not yet known. Those cards would have no known owner until the battle is resolved.
* Arrays may be assist in interpreting values in user-friendly ways -- e.g. translating some integer subscript to the name of a rank "King" or "Two". The subscript itself would be obtained from the numeric value used to stand for the card (as in the first bullet point above).

It should be noted that large segments of duplicated code that only differ in variable names strongly suggest a need for arrays.

SIMULATING A PLAYER DECK
Here are three feasible ways of handling player decks. The method chosen will affect the parameter list required to use them.

I: The simplest

0 1 2 3 49 50 51
+---+---+---+---+-- --+---+---+-- --+---+---+---+
| X | X | X | X | .... | X | | .... | | | |
+---+---+---+---+-- --+---+---+-- --+---+---+---+
size-1 size

Each deck is stored in an array (maximum 52 cards)
with the top card in position 0. Adding a card just
goes to the place indicated by size (which increases).

When a card is drawn, the rest are shifted one position
(1 to 0, 2 to 1, etc.)

Primary Advantage: simple to understand, only one array required
Primary Drawback: a lot of card shifting whenever a card is drawn

II: Maintaining a separate discard pile

0 1 2 3 49 50 51
+---+---+---+---+-- --+---+---+-- --+---+---+---+
| | | X | X | .... | X | | .... | | | |
+---+---+---+---+-- --+---+---+-- --+---+---+---+
next size-1 size


0 1 2 3 49 50 51
+---+---+---+---+-- --+---+---+-- --+---+---+---+
| X | X | X | X | .... | X | | .... | | | |
+---+---+---+---+-- --+---+---+-- --+---+---+---+
disc-1 discards

One array holds the cards that have not yet been drawn,
with a 'next' subscript indicating where the next card is.
All the collected cards are placed into a discard pile.

Once all the cards are drawn from the draw stack, then
treat the discard pile as the new draw pile. That is,
the size of the deck is equal to the number discarded into it,
and the next card to draw will be from position 0.

Primary Advantage: no shifting of cards required (very fast)
Primary Drawback: may be a little tricky switching the discard
pile into a draw pile, especially in
the middle of a big war

III: The Circular Array

0 1 2 3 49 50 51 0 1 2 3
+---+---+---+---+-- --+---+---+-- --+---+---+---+---+---+---+---+--
| | | X | X | .... | X | | .... | | | | | | | | ....
+---+---+---+---+-- --+---+---+-- --+---+---+---+---+---+---+---+--
next last-1 last

Pretend that the first element of the array immediately follows
the last element of the array. This can be done by assuring that
every time you increase any subscript, that it gets reset to zero
whenever it reaches 52.

When a card is drawn, increase the subscript that says where to draw next.
When a card is added to the deck, increase the subscript that says where
the last card is.

Primary Advantage: no shifting of cards, or manipulating multiple arrays
Primary Disadvantage: just a mental adjustment to this apparently infinite array

In CMPSC 122, you will encounter the "Double-Ended Queue",
which is most efficiently implemented as a circular array, such as this one.

IMPLEMENTATION AND DEBUGGING HINT

It may be simpler to write and test your program if you first assume that there will be no ties. This will allow you to test the ability to draw a card and collect a card successfully, and also to see that the program runs smoothly.

Then once you are confident that much of the program works correctly, you can add the feature that handles the larger battles.

OTHER DESIGN NOTES

Shuffling a deck is simply an application of a random number generator. Initialize an array to a series of known values (i.e. known to have distinct values for each card); then visit each subscript in turn and exchange that array element with some element randomly chosen from anywhere in the array. Every card will have been moved somewhere -- and no one should have any idea where.

Shuffling would be a necessary first step to this program. (If you don't believe me, see how your program runs if you don't shuffle.)

To keep the user interface simple, implement the following between each battle:

char input[3]; // not much input here

cin.getline( input, 3 );

This will proceed with a simple carriage return, even if no value is typed. No prompt is required, to keep the display uncluttered.

You could then also check the first character of this input for some value (like the letter 'q') to allow for the program to end early. I do not expect the TA to run every program until one player has completely won all 52 cards.

EXTRA CREDIT OPTION

Allow two ways to run this program (selected by user input)

Verbose Mode
As above, displaying each battle in turn, showing the cards, then accepting a carriage return to go to the next battle.

Silent Mode
Runs an entire game from beginning to the end, with no input or output, simply reporting how many battles were played along the way.

For extra value, allow this silent mode to play as many games as a user desires, reporting the length of each game.

The Wikipedia page suggests that the average number of battles per game is near 250. (Don't require 250 carriage returns to determine that!)

also here is a sample of what he gave us

Sample Functional Design for WAR!

Main Program:
Provides an overall user interface, especially for purposes
of the Extra Credit option (which will not be described below)


Function playWar:
Simulates the two-player game of WAR!
To facilitate use of the deck operations described below,
every skirmish will have a battle pile for each player.
Draw from your deck into the battle pile, and then move
those cards into the deck when a winner is determined.

Return Value: number of skirmishes required in complete game

Calls: shuffle, draw, collect


Function shuffle:
Creates and shuffles a deck of cards

Parameter: deck (output array) the deck of cards


Function draw: (simplest version)
Draws exactly one card a deck

Parameter: deck (in/out array) the deck to draw from
size (in/out integer) how many cards are in the deck
card (output integer) the card drawn


Function collect: (simplest version)
Collects cards from a battle pile into winner's deck
(This will be called twice to collect from both piles)

parameters: playerDeck (in/out array) the deck to collect into
size (in/out integer) size of that deck
rewards (input array) the cards to collect
rewardSize (input integer) how many to collect

Function printCardName:
Translates the numeric code for a card into a string for display
Uses two arrays representing the rank and suit of the card.

parameter: card the integer value representing the card

-----------------------------

Here are three feasible ways of handling player decks.
(The method chosen will affect the parameter list required to use them)

I: The simplest

0 1 2 3 49 50 51
+---+---+---+---+-- --+---+---+-- --+---+---+---+
| X | X | X | X | .... | X | | .... | | | |
+---+---+---+---+-- --+---+---+-- --+---+---+---+
size-1 size

Each deck is stored in an array (maximum 52 cards)
with the top card in position 0. Adding a card just
goes to the place indicated by size (which increases).

When a card is drawn, the rest are shifted one position
(1 to 0, 2 to 1, etc.)

Primary Advantage: simple to understand, only one array required
Primary Drawback: a lot of card shifting whenever a card is drawn

II: Maintaining a separate discard pile

0 1 2 3 49 50 51
+---+---+---+---+-- --+---+---+-- --+---+---+---+
| | | X | X | .... | X | | .... | | | |
+---+---+---+---+-- --+---+---+-- --+---+---+---+
next size-1 size


0 1 2 3 49 50 51
+---+---+---+---+-- --+---+---+-- --+---+---+---+
| X | X | X | X | .... | X | | .... | | | |
+---+---+---+---+-- --+---+---+-- --+---+---+---+
disc-1 discards

One array holds the cards that have not yet been drawn,
with a 'next' subscript indicating where the next card is.
All the collected cards are placed into a discard pile.

Once all the card are drawn from the draw stack, then
treat the discard pile as the new draw pile. That is,
the size of the deck is equal to the number discarded into it,
and the next card to draw will be from position 0.

Primary Advantage: no shifting of cards required (very fast)
Primary Drawback: may be a little tricky switching the discard
pile into a draw pile, especially in
the middle of a big war

III: The Circular Array

0 1 2 3 49 50 51 0 1 2 3
+---+---+---+---+-- --+---+---+-- --+---+---+---+---+---+---+---+--
| | | X | X | .... | X | | .... | | | | | | | | ....
+---+---+---+---+-- --+---+---+-- --+---+---+---+---+---+---+---+--
next last-1 last

Pretend that the first element of the array immediately follows
the last element of the array. This can be done by assuring that
every time you increase any subscript, that it gets reset to zero
whenever it reaches 0.

When a card is drawn, increase the subscript that says where to draw next.
When a card is added to the deck, increase the subscript that says where
the last card is.

Primary Advantage: no shifting of cards, or manipulating multiple arrays
Primary Disadvantage: just a mental adjustment to this apparently infinite array


Thanks if you can help

Recommended Answers

All 6 Replies

And your problem? We're not here to do your homework for you...

First think, then code. You have the beginnings of a design document. Turn that into a real design document.
Then use that to create the functional design, the technical design, the specs for the artwork, etc. etc.
From those start building the system piece by piece, then glue the pieces together.

Well , don't think a lot of people would be pleased for me telling you this but , I've done a similar card game , Black Jack

How to make the pack;

#include <iostream>

using namespace std;
// Show the cards
void display(long cards[53]){
  int i;
  for(i=1;i<=cards[0];i++){
      cout<<cards[i]<<" ";
    }
     
}

int pack(long cards[53],int number_of_colors,int min_value,int max_value){
    int i=0,color,cardcounter;
      for(color=1;color<=number_of_colors;color++){
         for(cardcounter=min_value;cardcounter<=max_value;cardcounter++){
                  cards[++i]=cardcounter*10+color;              
              }         
         }          
     cards[0]=i;    
    }
//And a shuffle system 
void switch_with_last(long cards[53],int pos){
  long aux; aux=cards[cards[0]]; cards[cards[0]]=cards[pos]; cards[pos]=aux;   
}
int shuffle(long cards[53]){
    int i,shuffles;
    srand(time(NULL));
    for(shuffles=1;shuffles<=500;shuffles++){
       i=1+rand()%cards[0];
       switch_with_last(cards,i);
      }

}
long card_pack[53];    
int i;
int main(){
// pack(card_pack,how many colors , smallest card , highest card )
        pack(card_pack,4,2,14);
        shuffle(card_pack);
        display(card_pack);
        }

Well i consider making the game will be a lot easier , you just have to make a calculating system for the points , good luck & have fun making the game

I agree with jwentwing.... we are not a homework service

//And a shuffle system 
void switch_with_last(long cards[53],int pos){
  long aux; aux=cards[cards[0]]; cards[cards[0]]=cards[pos]; cards[pos]=aux;   
}
int shuffle(long cards[53]){
    int i,shuffles;
    srand(time(NULL));
    for(shuffles=1;shuffles<=500;shuffles++){
       i=1+rand()%cards[0];
       switch_with_last(cards,i);
      }

}

This has 2 problems.
1) srand() should only be called once -- at the beginning of the program.
2) This is not a 'shuffle' function. This is akin to throwing the cards at a fan and picking them up. Grab a deck of cards and study what really happens when you shuffle. :icon_wink:

This is not a 'shuffle' function. This is akin to throwing the cards at a fan and picking them up. Grab a deck of cards and study what really happens when you shuffle.

. :) i got the idea but my "shuffle" idea works & it ain't really time consuming

Have a read of the fisher-yates shuffle or perhaps the durstenfeld algorithm for shuffling.

Chris

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.