Jump to content
  • Advertisement
Sign in to follow this  
Tokiko

Card Game

This topic is 5007 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
(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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 [ i ];
array [ i ] = array [ r ];
array [ r ] = temp;
}
[/script]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!