Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

A card game program with a quick question!!

Status
Not open for further replies.

DonCL

Programmer
Jul 16, 2001
43
US
Hey everyone. I have a program for the card game war. I have tested it, and it works. My question is this: how could i get the program to play a sequence of gmaes, rather than just a single game? Here is the program. its kind of long, but i would appreciate any help. I would like to ask a question in the program such as "Would you like to play again (Y or N)?" where would this go, and would i need a for loop or a while loop? thanks for any help!
Code:
//War Card Game


# include <iostream>
# include <cstdlib>
# include <algorithm>

using namespace std;
enum suits {diamond, club, heart, spade};

class Card {
public:
		// constructors
	Card ();			// initialize a card with default values
	Card (suits, int);	// initialize a card with given values

		//d ata fields
	int rank;			// hold rank of card
	suits suit;			// hold suit of card

};

Card::Card ()
	// initialize a new Card
	// default value is the ace of spades
{
	rank = 1;
	suit = spade;
}

Card::Card (suits sv, int rv)
		// initialize a new Card using the argument values
{
	rank = rv;
	suit = sv;
}

ostream & operator << (ostream & out, Card & aCard)
	// output a textual representation of a Card
{
		//first output rank
	switch (aCard.rank) {
	case 1:  out << &quot;Ace&quot;;   break;
	case 11: out << &quot;Jack&quot;;  break;
	case 12: out << &quot;Queen&quot;; break;
	case 13: out << &quot;King&quot;;  break;
	default:   // output number
		out << aCard.rank;   break;
	}

	// then output suit

	switch (aCard.suit) {
	case diamond: out << &quot; of Diamonds&quot;; break;
	case spade: out << &quot; of Spades&quot;; break;
	case heart: out << &quot; of Hearts&quot;; break;
	case club: out << &quot; of Clubs&quot;; break;
	}
	return out;
}

class randomInteger {
public:
	unsigned int operator () (unsigned int);
} randomizer;

unsigned int randomInteger::operator () (unsigned int max)
{
	// rand return random integer
	// convert to unsigned to make postive
	// take ramainder to put in range
	unsigned int rval = rand();
	return rval % max;
}

class Deck {
public:
	// constructor
	Deck ();

		// operations
	void shuffle ()
	{ random_shuffle (cards, cards+52, randomizer); }

	bool isEmpty ()
	{ return topCard <=0; }
	Card draw ();

protected:
	Card cards[52];
	int topCard;
};

Deck::Deck ()
	// initialize a deck by creating all 52 cards
{
	topCard = 0;
	for (int i = 1; i <= 13; i++) {
		Card c1(diamond, i), c2(spade, i), c3(heart, i), c4(club, i);
		cards[topCard++] = c1;
		cards[topCard++] = c2;
		cards[topCard++] = c3;
		cards[topCard++] = c4;
	}
}

Card Deck::draw ()
	// return on card from the end of the deck
{
	if (! isEmpty())
		return cards[--topCard];
	else {
		Card spadeAce(spade, 1);
		return spadeAce;
	}
}

class Player {
public:
		// constructor
	Player (Deck &);

		// operations
	Card draw ();
	void addPoints (int);
	int score ();
	void replaceCard (Deck &);

protected:
	Card myCards[3];
	int myScore;
	int removedCard;
};

Player::Player (Deck & aDeck)
	// initialize the data fields for a player

{
	myScore = 0;
	for (int i = 0; i < 3; i++)
		myCards[i] = aDeck.draw();
	removedCard = 0;
}

Card Player::draw ()
	// return a random card form our hand
{
	removedCard = randomizer(3);
	return myCards[removedCard];
}

void Player::addPoints (int howMany)
	// add the given number of points to the current score
{
	myScore += howMany;
}

int Player::score ()
	// return the current score
{
	return myScore;
}

void Player::replaceCard (Deck & aDeck)
	//replace last card played with new card
{
	myCards[removedCard] = aDeck.draw();
}

void main() {
	Deck theDeck; // create and shuffle the deck
	int num_round = 1;
	theDeck.shuffle();

	Player player1(theDeck); // create the two players
	Player player2(theDeck);

	// play until deck is empty
	while (! theDeck.isEmpty() ) {
		cout << &quot;Round &quot; << num_round++ << &quot; -------&quot;<< endl;
		Card card1 = player1.draw();
		cout << &quot;Player 1 plays &quot; << card1 << endl;
		Card card2 = player2.draw();
		cout << &quot;Player 2 plays &quot; << card2 << endl;

		if (card1.rank == card2.rank) { // tie
			player1.addPoints(1);
			player2.addPoints(1);
			cout << &quot;Players tie\n&quot;;
		}
		else if (card1.rank> card2.rank) {
			player1.addPoints(2);
			cout << &quot;Player 1 winds round\n&quot;;
		}
		else {
			player2.addPoints(2);
			cout << &quot;Player 2 wins round\n&quot;;
		}

		// now replace the cards drawn
		player1.replaceCard(theDeck);
		player2.replaceCard(theDeck);
	}
	cout << &quot;Player 1 score &quot; << player1.score() << endl;
	cout << &quot;Player 2 score &quot; << player1.score() << endl;
}
 
I would say you should get the contents of the main function into a separate function, lets say void DoSomethingWithTheCards() and use a WHILE loop in your program, something like:

Code:
void main()
{ char c = 0;
  while ((c != 'n) && (c != 'N'))
  {   DoSomethingWithTheCards();
      cout << &quot;do ya wanna play again (Y/N)?&quot;);
      c = getch();
  }
}
This should do it.
[red]Nosferatu[/red]
We are what we eat...
There's no such thing as free meal...
once stated: methane@personal.ro
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top