Using Inheritance for card game

Reply

Join Date: Jan 2009
Posts: 55
Reputation: javaman2 has a little shameless behaviour in the past 
Solved Threads: 0
javaman2 javaman2 is offline Offline
Junior Poster in Training

Using Inheritance for card game

 
0
  #1
May 24th, 2009
hi everyone, i need help with a project that i am doing.
i need to use inheritance and arrays to make a poker game, making a card class and a poker class that extends the card class
in the card class i would need to use arrays to make a deck, make suits and ranks, etc.
in the poker class i would need to check if the output cards are one pair, three of a kind, two pair, etc. and determine the winner
can anyone help me start off on how to use arrays in this project
before i used just regular statements to initialize a deck and do the other things
also can anyone give me suggestions on how to either not use if-else statements, greatly reduce the number of if-else statements used to determine the type of hand, or how to use arrays and inheritance to do this

i greatly appreciate any help and thank you so much for helping me
as of now im still working on this but i really need you help
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 55
Reputation: javaman2 has a little shameless behaviour in the past 
Solved Threads: 0
javaman2 javaman2 is offline Offline
Junior Poster in Training

Re: Using Inheritance for card game

 
0
  #2
May 24th, 2009
i have my code so far
i forgot to add that the game must be two to four players so if anyone can help me with that i've tried to do that with the deck array
would it be better to put a multidimensional array for the two to four players and the cards in the base class(card) or derived class(poker)?
would i need a toString method and equals method, accessor, mutator methods, constuctors?

note: this is the base class
the code doesn't exactly work, if you have any suggestions on how to make it work i would greatly appreciate it
also if you have any suggestions for the derived class(poker class) i would really appreciate it
thank you for any help

  1. import java.util.Random;
  2.  
  3. public class Card_Class
  4. {
  5. private int rank;
  6. private int suit;
  7.  
  8. public void makeCards()
  9. {
  10. //Class invariant: Deck of cards, shuffles deck, names ranks and suits
  11.  
  12. int [][] deck = new int[52][a];// deck of cards
  13.  
  14. for (int i = 0; i < 52; i++)//make a new deck
  15. for(int j = 0; j < i.length; j++)
  16. {
  17. deck[i] = i;
  18. }
  19.  
  20. Random generating = new Random();//create object of Random
  21.  
  22. for (int i = 0; i < 100; i++)// shuffle the deck by swapping random two cards 100 times
  23. for(int j = 0; j < i.length; j++)
  24. {
  25. int randPos1 = generating.nextInt(52);//generate two numbers
  26. int randPos2 = generating.nextInt(52);
  27.  
  28. int temp = deck[randPos1];//swap the two numbers to shuffle the deck
  29. deck[randPos1] = deck[randPos2];
  30. deck[randPos2] = temp;
  31. }
  32.  
  33. int rank = 0;
  34. int suit = 0;
  35. String output = null;
  36.  
  37. for (int i = 0; i < 5; i++)
  38. for(int j = 0; j < i.length; j++)
  39. {
  40. rank = (deck[i] % 13) + 1;
  41. suit = deck[i] / 13;
  42.  
  43. if (rank == 1)
  44. output = "Ace of ";
  45. else if (rank == 11)
  46. output = "Jack of ";
  47. else if (rank == 12)
  48. output = "Queen of ";
  49. else if (rank == 13)
  50. output = "King of ";
  51. else
  52. output = rank + " of ";
  53.  
  54. if (suit == 0)
  55. output += "Spades";
  56. else if (suit == 1)
  57. output += "Clubs";
  58. else if (suit == 2)
  59. output += "Hearts";
  60. else
  61. output += "Diamonds";
  62. System.out.println(output);
  63. }
  64. }
  65. }
Last edited by javaman2; May 24th, 2009 at 9:08 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 1,186
Reputation: JamesCherrill is a glorious beacon of light JamesCherrill is a glorious beacon of light JamesCherrill is a glorious beacon of light JamesCherrill is a glorious beacon of light JamesCherrill is a glorious beacon of light 
Solved Threads: 172
JamesCherrill JamesCherrill is offline Offline
Veteran Poster

Re: Using Inheritance for card game

 
0
  #3
May 25th, 2009
Originally Posted by javaman2 View Post
the game must be two to four players so if anyone can help me with that i've tried to do that with the deck array
would it be better to put a multidimensional array for the two to four players and the cards in the base class(card) or derived class(poker)?
I wouldn't recommend the multi-dimensional array route - it will lead to a big central data stucture shared by all the code for the whole application. Exactly the opposite of good Object design.
Most obvious O.O. solution would be to create a Player class, and a Game class. The Player class can hold whatever Cards the player holds, and also handle the player's calls, bets, current stake money etc. The Game class can represent the game as a whole, it can contain 2-4 instances of Player, and the logic for new game (call the Deck's shuffle method etc), who bets next (call the Player's bet() method etc), etc etc.

(This is a good example of one of the most simple Object Oriented Design methods. Read the problem description and underline any significant nouns (eg: card, deck, player, game) - these are possible candidates for defining your top-level classes.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 55
Reputation: javaman2 has a little shameless behaviour in the past 
Solved Threads: 0
javaman2 javaman2 is offline Offline
Junior Poster in Training

Re: Using Inheritance for card game

 
0
  #4
May 25th, 2009
im sorry i forgot to put that there is no betting, checking, etc.
also i can only have the two classes described so is there another way to make the two to four players thing work in my code
also if anyone can help me to fix my code and/or give suggestions on ways to do the rest of my program i would really appreciate that
thank you very much on any feedback
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 55
Reputation: javaman2 has a little shameless behaviour in the past 
Solved Threads: 0
javaman2 javaman2 is offline Offline
Junior Poster in Training

Re: Using Inheritance for card game

 
0
  #5
May 25th, 2009
i think i made some progress
  1. public class Poker_Class extends Card_Class
  2. {
  3. //Class invariant: Defines types of hands(straight, two pair, etc.), determines number of players, swap method to interchange up to 3 cards
  4. //Prints out the winner and what each person has
  5.  
  6. int x = (cardRank%13)-(cardRank2%13);//determine straight
  7. int ex = (cardRank2%13)-(cardRank3%13);
  8. int exx = (cardRank3%13)-(cardRank%13);
  9.  
  10. boolean straight = false;
  11. boolean flush = false;
  12. boolean royal = false;
  13.  
  14. if((cardRank==10)&&(cardRank2==11)&&(cardRank3==12))
  15. royal = true;
  16. else if((cardRank==10)&&(cardRank2==12)&&(cardRank3==11))
  17. royal = true;
  18. else if((cardRank==11)&&(cardRank2==10)&&(cardRank3==12))
  19. royal = true;
  20. else if((cardRank==11)&&(cardRank2==12)&&(cardRank3==10))
  21. royal = true;
  22. else if((cardRank==12)&&(cardRank2==11)&&(cardRank3==10))
  23. royal = true;
  24. else if((cardRank==12)&&(cardRank2==10)&&(cardRank3==11))
  25. royal = true;
  26. else if((straight==true) && (flush==true))//test for straight flush
  27. System.out.println("Straight Flush ");
  28. else if((royal==true) && (straight==true) && (flush==true))//test for royal straight flush
  29. System.out.println("Royal Straight Flush");
  30. else if((Math.abs(x)==1)&&(Math.abs(ex)==1)&&(Math.abs(exx)==2))//test for straight
  31. {
  32. straight = true;
  33. System.out.println("Straight");
  34. }
  35. else if((Math.abs(x)==1)&&(Math.abs(ex)==2)&&(Math.abs(exx)==1))
  36. {
  37. straight = true;
  38. System.out.println("Straight");
  39. }
  40. else if((Math.abs(x)==2)&&(Math.abs(ex)==1)&&(Math.abs(exx)==1))
  41. {
  42. straight = true;
  43. System.out.println("Straight");
  44. }
  45. else if((cardRank==0&&cardRank2==11&&cardRank3==12)||(cardRank==0&&cardRank2==12&&cardRank3==11))
  46. {
  47. straight = true;
  48. System.out.println("Straight");
  49. }
  50. else if((cardRank==11&&cardRank2==0&&cardRank3==12)||(cardRank==11&&cardRank2==12&&cardRank3==0))
  51. {
  52. straight = true;
  53. System.out.println("Straight");
  54. }
  55. else if((cardRank==12&&cardRank2==11&&cardRank3==0)||(cardRank==12&&cardRank2==0&&cardRank3==11))
  56. {
  57. straight = true;
  58. System.out.println("Straight");
  59. }
  60. else if((cardSuit==0)&&(cardSuit2==0)&&(cardSuit3==0))//test for flush
  61. {
  62. flush = true;
  63. System.out.println(" Flush ");
  64. }
  65. else if((cardSuit==1)&&(cardSuit2==1)&&(cardSuit3==1))
  66. {
  67. flush = true;
  68. System.out.println(" Flush ");
  69. }
  70. else if((cardSuit==2)&&(cardSuit2==2)&&(cardSuit3==2))
  71. {
  72. flush = true;
  73. System.out.println(" Flush ");
  74. }
  75. else if((cardSuit==3)&&(cardSuit2==3)&&(cardSuit3==3))
  76. {
  77. flush = true;
  78. System.out.println(" Flush ");
  79. }
  80. else if((rank==rank2)&&(rank2==rank3))// test for three of a kind
  81. System.out.println(" Three of a kind ");
  82. else if((rank==rank2)||(rank2==rank3)||(rank==rank3))// test for one pair
  83. System.out.println(" One pair ");
  84. else// high card
  85. {
  86. System.out.println("High Card");
  87. }
  88.  
  89. }
first is there any way of making this code better and in methods since i need to inherit from the card class
and how can i inherit the deck of cards, etc. from the card class to run my poker game
please i really need help urgently
thank you
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 1,186
Reputation: JamesCherrill is a glorious beacon of light JamesCherrill is a glorious beacon of light JamesCherrill is a glorious beacon of light JamesCherrill is a glorious beacon of light JamesCherrill is a glorious beacon of light 
Solved Threads: 172
JamesCherrill JamesCherrill is offline Offline
Veteran Poster

Re: Using Inheritance for card game

 
0
  #6
May 25th, 2009
Why can you only have 2 classes?
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 55
Reputation: javaman2 has a little shameless behaviour in the past 
Solved Threads: 0
javaman2 javaman2 is offline Offline
Junior Poster in Training

Re: Using Inheritance for card game

 
0
  #7
May 25th, 2009
im guessing because my teacher doesnt want to go through all the trouble of looking at multiple classes

but if it helps me finish this project i really dont care
i just need help it doesnt matter to me
thank you

oh yeah my new code for the poker class is really off
can anyone help with that
Last edited by javaman2; May 25th, 2009 at 10:37 am.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 55
Reputation: javaman2 has a little shameless behaviour in the past 
Solved Threads: 0
javaman2 javaman2 is offline Offline
Junior Poster in Training

Re: Using Inheritance for card game

 
0
  #8
May 25th, 2009
here is my updated code with the number of players in it
the problem is that the cards repeat if you dont know what i mean please compile my code and see what i mean
also if anyone has any suggestions for other things
please feel free to tell me
thank you

  1. import java.util.Random;
  2. import java.util.Scanner;
  3.  
  4. public class Card_Class
  5. {
  6. private static int rank;
  7. private static int suit;
  8.  
  9. public static void main(String[] args)
  10. {
  11. //Class invariant: Deck of cards, shuffles deck, names ranks and suits
  12.  
  13. int [] deck = new int[52];// deck of cards
  14.  
  15. for(int i = 0; i < 52; i++)//make a new deck
  16. {
  17. deck[i] = i;
  18. }
  19.  
  20. Random generating = new Random();//create object of Random
  21.  
  22. for(int i = 0; i < 100; i++)// shuffle the deck by swapping random two cards 100 times
  23. {
  24. int randPos1 = generating.nextInt(52);//generate two numbers
  25. int randPos2 = generating.nextInt(52);
  26.  
  27. int temp = deck[randPos1];//swap the two numbers to shuffle the deck
  28. deck[randPos1] = deck[randPos2];
  29. deck[randPos2] = temp;
  30. }
  31.  
  32. int rank = 0;
  33. int suit = 0;
  34. String output = null;
  35.  
  36. Scanner keyboard = new Scanner(System.in);
  37.  
  38. System.out.println("Enter the number of players.");
  39. int p = keyboard.nextInt();
  40.  
  41. System.out.println();
  42.  
  43. if(p == 4)
  44. {
  45. for (int i = 0; i < 5; i++)
  46. {
  47. for(int j = 0; j < 4; j++)
  48. {
  49. rank = (deck[i] % 13) + 1;
  50. suit = deck[i] / 13;
  51.  
  52. if (rank == 1)
  53. output = "Ace of ";
  54. else if (rank == 11)
  55. output = "Jack of ";
  56. else if (rank == 12)
  57. output = "Queen of ";
  58. else if (rank == 13)
  59. output = "King of ";
  60. else
  61. output = rank + " of ";
  62.  
  63. if (suit == 0)
  64. output += "Spades";
  65. else if (suit == 1)
  66. output += "Clubs";
  67. else if (suit == 2)
  68. output += "Hearts";
  69. else
  70. output += "Diamonds";
  71. System.out.println(output);
  72. }
  73. }
  74. }
  75. else if(p == 3)
  76. {
  77. for (int i = 0; i < 5; i++)
  78. {
  79. for(int j = 0; j < 3; j++)
  80. {
  81. rank = (deck[i] % 13) + 1;
  82. suit = deck[i] / 13;
  83.  
  84. if (rank == 1)
  85. output = "Ace of ";
  86. else if (rank == 11)
  87. output = "Jack of ";
  88. else if (rank == 12)
  89. output = "Queen of ";
  90. else if (rank == 13)
  91. output = "King of ";
  92. else
  93. output = rank + " of ";
  94.  
  95. if (suit == 0)
  96. output += "Spades";
  97. else if (suit == 1)
  98. output += "Clubs";
  99. else if (suit == 2)
  100. output += "Hearts";
  101. else
  102. output += "Diamonds";
  103. System.out.println(output);
  104. }
  105. }
  106. }
  107. else if(p == 2)
  108. {
  109. for (int i = 0; i < 5; i++)
  110. {
  111. for(int j = 0; j < 2; j++)
  112. {
  113. rank = (deck[i] % 13) + 1;
  114. suit = deck[i] / 13;
  115.  
  116. if (rank == 1)
  117. output = "Ace of ";
  118. else if (rank == 11)
  119. output = "Jack of ";
  120. else if (rank == 12)
  121. output = "Queen of ";
  122. else if (rank == 13)
  123. output = "King of ";
  124. else
  125. output = rank + " of ";
  126.  
  127. if (suit == 0)
  128. output += "Spades";
  129. else if (suit == 1)
  130. output += "Clubs";
  131. else if (suit == 2)
  132. output += "Hearts";
  133. else
  134. output += "Diamonds";
  135. System.out.println(output);
  136. }
  137. }
  138. }
  139. else
  140. System.out.println("Error: only two to four players allowed.");
  141. }
  142. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 138
Reputation: JugglerDrummer is an unknown quantity at this point 
Solved Threads: 20
JugglerDrummer's Avatar
JugglerDrummer JugglerDrummer is offline Offline
Junior Poster

Re: Using Inheritance for card game

 
0
  #9
May 25th, 2009
To get the code for the objects, think of how things are in real life. I can't say anything about what your teacher wants, but use as many classes as you need. You will have a card class, and deck and hand classes which are containers for cards. Then maybe a game and player class. I'm working on a similar poker hand evaluation thing and its very difficult to do right, so don't worry if you don't get it right away.

Regarding your code: what's up with the p variable? Why does the card output affected by it? It should be simpler, just a switch for the rank and suit, and combine the two strings. You could even put the suit and rank even name in the Card class.

If you found a post helpful, please click the "give XXX reputation" link, to show your appreciation to those who helped you. :D
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 55
Reputation: javaman2 has a little shameless behaviour in the past 
Solved Threads: 0
javaman2 javaman2 is offline Offline
Junior Poster in Training

Re: Using Inheritance for card game

 
0
  #10
May 25th, 2009
well the cards repeat like so:

if 2 players (sample output):

2 of diamonds
2 of diamonds
Queen of Spades
Queen of Spades
6 of Hearts
6 of Hearts
8 of diamonds
8 of diamonds
7 of Hearts
7 of Hearts

i don't know how to fix this
any ideas

also, i made a card and deck class but i need help with the hand class i will try to find those classes and post them
thank you for the help
Reply With Quote Quick reply to this message  
Reply

Message:




Views: 789 | Replies: 11
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC