Playing Cards

1. Senior Member
Join Date
Dec 1969
Posts
11,334

Playing Cards

Ok, it&#039;s Friday, I&#039;m burnt, and this seemingly simple problem is bugging me.<BR><BR>I figured I&#039;d throw together a little card game for my daughter... War (I hate playing it, so I should code it, eh?)<BR><BR>Sounds soooo simple, but I can&#039;t get an eloquoent solution to this. How do you store the cards????<BR><BR>I have come up with a few ways:<BR><BR>one array holding all 52 cards -- a bit of repitition for the numbers<BR><BR>2d array. One dimension holding suit, second holding number. This may cause difficulty shuffling later on, and determining who is who<BR><BR>Using the stack. Seems to be the most efficient, but it&#039;s very confusing, and you can&#039;t push and pop anything except what&#039;s on the top.<BR><BR><BR>There is a reason behind this madness... I&#039;m trying to learn a new concept, and this is one of the examples or problems to help through it. I really thought this would be a breeze until I sat down and started coding it. Anyone have an idea for my sun baked brain?

2. Senior Member
Join Date
Dec 1969
Posts
1,431

RE: Playing Cards

Seems to me you need an array for each player also to hold the cards they have won and also possibly an array holding the cards in the pile. Need counters for each player also possibly redimming their arrays each time they play a card and win the pile. So let&#039;s say 2 players their 2 arrays start off at ubound= 26<BR><BR>need some randomize function to deal the cards. Probably the easiest is to start rand 1-52 seelct the card from the deck array reduce the dim of the deck by one then select again from 1-51 etc. <BR><BR>Any way counter 1 and 2 both start at 1. Always compare each counter to the ubound of that person&#039;s array to see if can increment or not or start from the top of their pile again. Play cards decrement arrays, compare cards increment appropriate array eg stack or player arrays depending on outcome. Play again etc.

3. Senior Member
Join Date
Dec 1969
Posts
11,334

RE: Playing Cards

Right. I have the game rules down, it&#039;s just a problem setting it up.<BR><BR>So you&#039;re saying to go with one array with 52 elements? Seems redundant. I was trying to keep an array of type int, due to speed. Also, if I do the one array, there would be more complex conditions... for instance:<BR><BR>Array(0) = Ace Of Clubs<BR>Array(10) = 3 of hearts &#039;for example...<BR><BR>I can&#039;t simply do<BR><BR>if array(0) &#062; array(10)<BR><BR>What I was trying to do was something like this: (this is Java, but I want to implement it in any language, even ASP)<BR><BR>private static final int HEARTS = 0;<BR> private static final int CLUBS = 1;<BR> private static final int DIAMONDS = 2;<BR> private static final int SPADES = 3;<BR> <BR> private static final int TWO = 2;<BR> private static final int THREE = 3;<BR> private static final int FOUR = 4;<BR> private static final int FIVE = 5;<BR> private static final int SIX = 6;<BR> private static final int SEVEN = 7;<BR> private static final int EIGHT = 8;<BR> private static final int NINE = 9;<BR> private static final int TEN = 10;<BR> private static final int JACK = 11;<BR> private static final int QUEEN = 12;<BR> private static final int KING = 13;<BR> private static final int ACE = 14;<BR><BR>int [][] fullDeck = new int [3][51];<BR><BR><BR>Now this way, as long as I can keep the deck&#039;s suit intact, it would be very easy to just compare the 2nd dimension as I did in the above example.<BR><BR>The example I have that they want is to use the stack... have you ever worked with popping and pushing the stack?<BR><BR>Thanks Krank AKA Kirby ;)<BR><BR>

4. Senior Member
Join Date
Dec 1969
Posts
1,431

RE: Playing Cards

back in assembly language days but I was pertty inexperienced then so I didn&#039;t have a clue what was going on. Kirby hehe the bad old days. <BR><BR>I dunno man I just came in from doing some weeding and the heat index here is about 104 so I feel like I&#039;m going to collapse. If I think of anything later I&#039;ll post another thread.

5. Senior Member
Join Date
Dec 1969
Posts
11,334

104? C'mon!

It was that temp here at 8AM! Going to 111 today -- ick.<BR><BR>Sure, I&#039;ll keep playing with it a bit today... maybe we can take it up again next week if need be. I really don&#039;t feel like doing much work today.<BR><BR>Contrary to what doctors say, Beer can rehydrate you if you drink enough of it ;)

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•