Card Game

Started by
18 comments, last by TriSwords 19 years, 3 months ago
I have attempted making a card game in the past with C++, but now im trying to do it using Java for my programming class. Im having trouble thinking of how to represent all the card values and suits, and how to go about shuffling them. I've looked around the web and I found some source code examples, but I couldn't follow most of it, so could someone lead me in the right direction? Any help would be greatly appreciated and I'm not going trying to get anyone to do my homework for me. It's just an open project Im working on. Thanks
Advertisement
You can use a 2d array. There would be 4 columns(for each suit) and whatever many rows for the numbers. To shuffle you would have a random variable for the suit and for the value. Once a certain combination comes up once, x it out so it won't come up again. That way cards won't repeat.
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" width="88" height="70" id="H2lvl" align="middle"> <param name="allowScriptAccess" value="sameDomain" /> <param name="movie" value="http://www.ironhive.com/CFCs/H2lvl.swf?gamertag=jBaddog" /> <param name="quality" value="high" /> <param name="wmode" value="transparent"> <embed src="http://www.ironhive.com/CFCs/H2lvl.swf?gamertag=jBaddog" quality="high" bgcolor="#ffffff" width="88" height="70" name="H2lvl" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /> </object>
(by the way,havent done any java. only c++)Dont think that this would be a good idea or not, but here it is:
have a 13 by 4 array of chars, and have the 13 side have all the values,and the 4 side have the suites.
You dont need to shuffle. just keep the cards the way they are,and draw a random element of the remaning cards in that two-dementional array.
I pity the fool that uses numerical operators as boolean evaluators!
I don't really like that solution; it doesn't make sense. I think it makes more sense to make a card object and create a list or other collection of all the cards. Here's a (simple) example card class:

public class Card{    //Define suites (I wish Java had enum)    public static final int Spade   = 0;    public static final int Club    = 1;    public static final int Diamond = 2;    public static final int Heart   = 3;    //Define special cards; other cards are just integers    public static final int Ace = 1;    public static final int Jack = 0;    public static final int Queen = 0;    public static final int King = 0;    public int Suite = 0;    public int Value = 1;        //tells whether the card is face down    public bool Visible = true;}


To fill the list I would just use nested for loops to create each card; that way you have a list representing the deck that has one copy of each card. To shuffle you can just randomize the order of the objects in the list. (Do Java collections have a way to randomize the order? I mainly use C#.)

I just think that this way is easier to program with because it makes more sense. As you play the game you can have one collection for each place a card can be. For example, with Hearts you could have a list for each person's hand, a list for the cards each person has taken, and a list for the cards in the middle.

Basically, have an object for each card and a list for each pile/group of cards you would have if you were physically playing the game. You can just move the card objects from one group to another as the game logic dictates.
lol,said almost the same thing in almost the same way in almost the same time.
I pity the fool that uses numerical operators as boolean evaluators!
a shuffle example


any way im pretty sure this will work in terms of shuffle i would make a function like so:
void Shuffle(Card** DeckToShuffle){  Card* Temp_Array[52];  for(int i = 0; i < 52; i++)  {    Temp_Array = NULL;//make all pointers null (not sure if we need this)  };  for(int j = 0; j < 52; j++)  {   int C = rand()%52;//whatever   while(DeckToShuffle[C] == NULL)//we loop through till we find a cell that hasnt been read from if its null we make C a diffrent number   {    C = rand()%52;//im dont know how you get random numbers in java   };   Temp_Array[j] = DeckToShuffle[C];//the Temp array is filling each cell sequentialy while were reading from the DeckToShuffle randomly this is the actual shuffle   DeckToShuffle[C] = NULL;//make it NULL so we know that it has been read from  };  for(int k = 0; k < 52; k++)  {    Temp_Array[k] = DeckToShuffle[k];//transfer the newly shuffled data back to the origonal array passed to the function  }};

i dont know any java so just take it at face value [smile]
good luck[smile]

NOTE: note sure if java supports typedef if so i would use those for suits and types
____________________________"This just in, 9 out of 10 americans agree that 1 out of 10 americans will disagree with the other 9"- Colin Mochrie
Quote:Original post by raptorstrike
you could just use numbers but that could get confusing so i recoment typedef's

for example

typedef WORD Suit;
const Suit HEARTS = 0,
CLUBS = 1,
DIAMONDS = 2,
SPADES = 3;
typedef WORD Number;
const Number Ace = 1,
Two = 2,
Three = 3,
Four = 4,
Five = 5,
Six = 6,
Seven = 7,
Eight = 8,
Nine = 9,
Ten = 10,
Jack = 11,
Queen = 12,
Kind = 13;


this is what Enum's are for.

enum Suit
{
HEARTS,CLUBS,DIAMONDS,SPADES
};

the same should go for "Number" as well. the main advantage as that this is type safe.
FTA, my 2D futuristic action MMORPG
That's C++...

But yeah... Just have 2 ints for the suit and number also to shuffle, just do
[script]
for ( int i = 0 ; i < count; ++i )
{
r = Math.Rand () * count;
Card temp = array ;<br> array = array [ r ];<br> array [ r ] = temp;<br>}<br>[/script]
yeah i looked back at the post and it said changing for C++ to Java not C++ from Java and im like "o crap" [looksaround] and i edited the post

thanks though [smile]
____________________________"This just in, 9 out of 10 americans agree that 1 out of 10 americans will disagree with the other 9"- Colin Mochrie
Thanks for the help everyone. Im still having some trouble getting this to work. Perhaps my teacher will be able to help me out. For some reason when I try to tackle this type of project I always give up because I can't get this stupid card stuff working. I feel like an idiot because I have been programming for a while now, and I can't get this simple thing working :) oh well, I'll try again tomorrow.

Thanks

This topic is closed to new replies.

Advertisement