Results 1 to 9 of 9
- 04-14-2011, 09:48 AM #1
Member
- Join Date
- Apr 2011
- Posts
- 5
- Rep Power
- 0
Poker Class , removing cards already dealt
Hi there , I am trying to to create a poker program to return a full deck , deal a hand and deal the 5 cards in the middle. The only problem I have is removing cards that have already been dealt from the deck..:confused:
PS, using an IO(terminal class )
THX
public class Card {
String face;
String suit;
public Card(String face,String suit) {
this.face = face;
this.suit = suit;
}
public String getFace() {
return face;
}
public String getSuit() {
return suit;
}
}
import tcdIO.*;
public class PokerProgram {
public static void main(String[] arg) {
Card[] card = new Card[52];
String[] face = new String[13];
String[] suit = new String[4];
DeckOfCard[] deck = new DeckOfCard[52];
// DeckOfCard deck;
// Card seb = new Card("Seb", "seb");
Terminal t = new Terminal("");
int counter = 0;
int choice = 0;
face[0] = "ace";
face[1] = "deuce";
face[2] = "three";
face[3] = "four";
face[4] = "five";
face[5] = "six";
face[6] = "seven";
face[7] = "eight";
face[8] = "nine";
face[9] = "ten";
face[10] = "jack";
face[11] = "queen";
face[12] = "king";
suit[0] = "heart";
suit[1] = "spades";
suit[2] = "clubs";
suit[3] = "diamonds";
while (counter < 52) {
for (int i = 0; i < suit.length; i++) {
for (int j = 0; j < face.length; j++) {
card[counter] = new Card(face[j], suit[i]);
counter++;
}
}
}
while (choice != 4) {
t.println(" MENU ");
t.println("---------------------------------------");
t.println("(1)Show card deack");
t.println("(2)Deal Hand");
t.println("(3)Deal Floop");
t.println("(4)Exit");
t.println("---------------------------------------");
choice = t.readInt("Please select:");
switch (choice) {
case 1:
for (int i = 0; i < card.length; i++) {
t.println(card[i].getFace() + " of " + card[i].getSuit());
}
break;
case 2:
int card1,
card2;
card1 = (int) (Math.random() * 52 + 1);
card2 = (int) (Math.random() * 52 + 1);
if (card1 != card2) {
t.println("your cards are:");
t.println(card[card1].getFace() + " of "
+ card[card1].getSuit());
t.println(card[card2].getFace() + " of "
+ card[card2].getSuit());
}
break;
case 3:
for (int i = 1; i < 6; i++) {
t.println(card[(int) (Math.random() * 52 + 1)].getFace()
+ " of "
+ card[(int) (Math.random() * 52 + 1)].getSuit());
}
break;
case 4:
t.println("Goodbye");
break;
}
}
}
}
- 04-14-2011, 10:39 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
Arrays never change their length. So if you are modelling something like a stack of cards that are to be dealt (which can change its size) then it might be better to use something like a list. Implementations of List provide a method that removes elements, while the Collections class provides a method to shuffle a list.
If you are stuck with using an array (because it's an assignment condition or whatever) then things become less straightforward. One approach would be to "mark" the array to show that a particular card had been dealt: for instance by making the element at that index null. Your use of random() will have to change since you would have to check that the card you have selected is not null and, if it is, call keep calling random() until you get a card that is still in the array.
- 04-14-2011, 10:42 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
ps - Your main() method is already too big. Consider using some other methods: A method to initialise the deck, and methods to implement each of the switch cases would be a good start.
- 04-14-2011, 11:09 AM #4
Member
- Join Date
- Apr 2011
- Posts
- 5
- Rep Power
- 0
Hi pbrockway2 , Thanks for your answer .
You were saying to implement a method for each switch case , how would one do that and in what Class should i consider implementing them?
Thanks :)
- 04-14-2011, 08:34 PM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
The methods would be in PokerProgram.
Java Code:private static Card[] card; public static void main(String[] arg) { card = new Card[52]; // etc switch(choice) { case 1: displayCards(); break; case 2: // etc } private static void displayCards() { for(int i = 0; i < card.length; i++) { t.println(card[i].getFace() + " of " + card[i].getSuit()); } }
You will probably need to consult a textbook to become familiar with the use of methods. The aim is for no single method to be more than a handful of lines long.
(A better design would not involve use of the "static" keyword at all except for the main() method.)
Other techniques to avoid a logical "tangle" include making sure you have (and document) a clear purpose for each method you write. And compile often so you can address the problems which the compiler alerts.
------------------
Reading your code more closely I see you have another type, DeckOfCard, that you haven't described anywhere. It isn't clear what role it plays (you currently declare an array of such decks but don't use the array anywhere).
- 04-15-2011, 01:19 AM #6
I have noticed a bug where you may get a ArrayIndexOutOfBoundsException.
See "case 3" in the switch statement, it says "(int)Math.round(Math.random()*52+1)". Your "cards" array is only 52 indices long, 0-51. This will generate numbers from 1-53. Delete the "+1" and change 52 to 51.
- 04-15-2011, 07:46 PM #7
Member
- Join Date
- May 2010
- Posts
- 45
- Rep Power
- 0
I haven't read much of the thread, so maybe this has been said already. But what I did to create a new deck was create an array unshuffledDeck[] of just 0-51. Then an array shuffledDeck[] where I put each one from the unshuffledDeck[] into a random spot. Then I just had a method, getNextCard() that would get next card from the shuffledDeck[] and add 1 to the nextCard counter
Looking back you could probably get rid of the unshuffledDeck[] array
- 04-16-2011, 03:18 AM #8
Member
- Join Date
- Apr 2011
- Location
- Canada!
- Posts
- 30
- Rep Power
- 0
Why don't you keep your cards in an arrayList??
Then you could do something like:
arrayList.remove(playedCard);
- 04-16-2011, 10:23 AM #9
Member
- Join Date
- Apr 2011
- Posts
- 5
- Rep Power
- 0
Thanks for all the answers guys!
It seems I need to familiarize myself more with methods and how to implement them , cheers pbrockway2 for the method you posted it makes more sense to do it that way and its "cleaner".
ra4king - Thanks for pointing that out , for some reason i thought you needed to add the +1 so that the Math.random would give me a number between 1-52 but i now see that 52 would make it Out of bound.
Maximus-EVG- Hi Maximus , I am pretty new to programing and will dig into Array Lists , they seem very usefull!
Similar Threads
-
Creating a card class for a Deck of cards
By adjit in forum New To JavaReplies: 13Last Post: 04-06-2011, 10:20 PM -
Poker Simulation (shuffling a deck of cards?)
By StateofMind in forum New To JavaReplies: 4Last Post: 04-06-2010, 08:59 PM -
Please help Me out with this poker Problem
By ckrisasa in forum NetworkingReplies: 8Last Post: 12-03-2009, 01:59 PM -
how i use the random class to random the cards i have
By yanipao in forum New To JavaReplies: 14Last Post: 10-19-2009, 10:57 AM -
Deck of Cards
By khunmato in forum New To JavaReplies: 13Last Post: 09-06-2009, 05:47 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks