Sunday, May 6, 2007

Programing: Card Game War (Part 2 of 5)


In my last post about this subject I talked about some of the basic concepts and posted the code for the random number generating class. Programing: Card Game War (Part 1 of 5)

This program was implemented using C++ and was designed around a very object oriented design. So much so, that many of the methods available in the classes are never used. In this section of the series I am going to post the class that implements a single card in a deck of cards. Obviously, this is the most basic element needed in having a deck of cards that will be used for a game.

A card has several features that need to be stored in order for it to have all the features of a normal playing card. These include its suit and its face value. Not to be over obvious, but there are four suits and thirteen different cards for each suit. Also, for the purposes of War each card needs a numerical value. The numerical value will be the number for the numbered cards and then for the face cards it will be 11 for J, 12 for Q, 13 for K, and 14 for A. This way the winner of each trick can be determined by comparing these numerical values. In my implementation suit does not matter (although it can be played this way to break ties, but the winner would always be the person with the ace of spades). Additionally, each card has an index between 0 and 51 used to create the card.

The features of this class should become evident upon looking at the source code. In my later construction of the game of war, many of these methods go unused, but could be used to display the games progress. In my implementation, I am mostly concerned about the number of rounds the game was completed in and if the game was a tie, but more on this later.

As stated in my earlier post I implemented this code using multiple files. Here are the headers which I used for all of my code.


#include <cstdlib>
#include <iostream>
#include <cstdlib> //Used for rand() & srand()
#include <string>
#include <fstream>


This class is stored in the header file card.h


using namespace std;

class card
{
private:
string suit;
char shortSuit;
string face;
char shortFace;
int index;
int cardValue;
string color;
void setSuit();
void setFace();
public:
card();
card(int);
int getIndex();
int getCardValue();
string getSuit();
string getFace();
char getShortSuit();
char getShortFace();
void printCardName();
void printCard();
};

card::card()
{
//VOID CONSTRUCTOR
index = -1;
}

card::card(int myIndex)
{
index = myIndex;
setSuit();
setFace();
}
int card::getIndex()
{
return index;
}
int card::getCardValue()
{
return cardValue;
}
void card::setSuit()
{
if(index >= 0 && index <= 12)
{
suit = "Spades";
shortSuit = 6;
color = "Black";
}
else if(index >= 13 && index <= 25)
{
suit = "Diamonds";
shortSuit = 4;
color = "Red";
}
else if(index >= 26 && index <= 38)
{
suit = "Clubs";
shortSuit = 5;
color = "Black";
}
else if(index >= 39 && index <= 51)
{
suit = "Hearts";
shortSuit = 3;
color = "Red";
}
}

void card::setFace()
{
int tempCard = (index+1)%13;
switch(tempCard)
{
case 1:
face = "Ace";
shortFace = 'A';
cardValue = 14;
break;
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
face = (tempCard + int('0'));
shortFace = face[0];
cardValue = tempCard;
break;
case 10:
face = "10";
shortFace = 'T';
cardValue = 10;
break;
case 11:
face = "Jack";
shortFace = 'J';
cardValue = 11;
break;
case 12:
face = "Queen";
shortFace = 'Q';
cardValue = 12;
break;
case 0:
//This is case 0 & not 13 because 13%13 = 0
face = "King";
shortFace = 'K';
cardValue = 13;
break;
default:
face = "Invalid";
shortFace = 'I';
cardValue = -1;
break;
}
}

string card::getSuit()
{
return suit;
}

string card::getFace()
{
return face;
}
char card::getShortSuit()
{
return shortSuit;
}

char card::getShortFace()
{
return shortFace;
}

void card::printCardName()
{
cout << "The " << getFace() << " of " << getSuit() << endl;
}

void card::printCard()
{
cout << shortFace << shortSuit;
}


Now, I'll explain some basics about the code. The constructor of this class requires a number between 0 and 51 to actually construct a playing card. This is the card's index. If the default constructor is used then the card's index will be -1 or the card will be the equivalent to a blank card or an empty spot.

This card class simply serves as the basis of a form of realism. This class could easily be replaced with an array of numbers containing the numbers 1 through 13 four times. This would be a more efficient implementation, but the realism of having a deck of cards would not be there. I may eventually use a more efficient method of playing war, but for this first series of posts, I am publishing the code I already have.

No comments:

Powered By Blogger