943,070 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 40321
  • Java RSS
May 3rd, 2007
0

Boolean array?

Expand Post »
Hi there... I'm pretty new to java, just learning arrays now. I've written a program that deals a "poker hand" to 4 players, using methods, but the problem with that is sometimes the same card gets dealt more than once. I'm now trying to use a boolean array of size 52, with each card initialized to false... and when one gets chosen, it changes to true (so you can't choose a card if it's already true).. And truth be told I really have no clue what to do except
Java Syntax (Toggle Plain Text)
  1. boolean[] deck = new boolean[52];
can anyone give me any insight..? I've no idea where to place it or what else to do. Thanks a bunch (: Sorry if this is a really juvenile question!

Here is my original code:
Java Syntax (Toggle Plain Text)
  1. import javax.swing.JOptionPane;
  2.  
  3. public class Poker
  4. {
  5. public static void main (String[]args)
  6. {
  7.  
  8. int face = 0, suit = 0;
  9. int player = 0, round = 0;
  10.  
  11. System.out.print("Player 1\tPlayer 2\tPlayer 3\tPlayer 4\n");
  12.  
  13.  
  14. for (round = 1; round <= 5; round++) //round of cards dealt
  15. {
  16. for (player = 1; player <= 4; player++)
  17. {
  18. face = 1 + (int) (Math.random() * 13);
  19. suit = 1 + (int) (Math.random() * 4);
  20.  
  21. cardNumber(face);
  22. cardSuit(suit);
  23.  
  24. if (player % 4 == 0)
  25. System.out.println();
  26. }
  27. }
  28.  
  29. }
  30.  
  31.  
  32. public static void cardNumber (int face)
  33. {
  34. if (face != 10)
  35. System.out.print(" ");
  36.  
  37. switch (face) {
  38. case 1: System.out.print("A of ");
  39. break;
  40. case 2: System.out.print("2 of ");
  41. break;
  42. case 3: System.out.print("3 of ");
  43. break;
  44. case 4: System.out.print("4 of ");
  45. break;
  46. case 5: System.out.print("5 of ");
  47. break;
  48. case 6: System.out.print("6 of ");
  49. break;
  50. case 7: System.out.print("7 of ");
  51. break;
  52. case 8: System.out.print("8 of ");
  53. break;
  54. case 9: System.out.print("9 of ");
  55. break;
  56. case 10: System.out.print("10 of ");
  57. break;
  58. case 11: System.out.print("J of ");
  59. break;
  60. case 12: System.out.print("Q of ");
  61. break;
  62. case 13: System.out.print("K of ");
  63. break;
  64. }
  65.  
  66. }
  67.  
  68. public static void cardSuit (int suit)
  69. {
  70. switch (suit) {
  71. case 1: System.out.print("Diamonds");
  72. break;
  73. case 2: System.out.print("Hearts");
  74. break;
  75. case 3: System.out.print("Spades");
  76. break;
  77. case 4: System.out.print("Clubs");
  78. break;
  79. }
  80. System.out.print("\t");
  81. }
  82.  
  83.  
  84. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
roketto is offline Offline
3 posts
since May 2007
May 4th, 2007
0

Re: Boolean array?

I've changed the program so that it uses arrays now instead of methods... I don't know if it will help more, but here it is (much simpler .. also, my first time really being successful with arrays)
I've also made it so instead of saying "Player 1, etc.." you can enter in player names yourself, and it will display those. I was actually suprised I could figure out how to do it!

Java Syntax (Toggle Plain Text)
  1. import javax.swing.JOptionPane;
  2.  
  3. public class Poker
  4. {
  5. public static void main (String[]args)
  6. {
  7.  
  8. int player = 0, round = 0;
  9. int i = 0, j = 0;
  10.  
  11. String[] suit = { "Clubs", "Diamonds", "Hearts", "Spades" };
  12. String[] face = { " 2", " 3", " 4", " 5", " 6", " 7", " 8", " 9", "10",
  13. " J", " Q", " K", " A" };
  14.  
  15. for (int names = 0; names < args.length; names++) //user-entered names
  16. {
  17. if (args[names].length() < 8)
  18. System.out.print(args[names] + "\t\t");
  19. else if ((args[names].length() >= 8) && (args[names].length() < 16))
  20. System.out.print(args[names] + "\t");
  21. else if (args[names].length() >= 16)
  22. System.out.print("long name!\t");
  23. }
  24. System.out.println();
  25.  
  26.  
  27. for (round = 1; round <= 5; round++) //round of cards dealt
  28. {
  29. for (player = 1; player <= 4; player++)
  30. {
  31. i = (int) (Math.random() * face.length);
  32. j = (int) (Math.random() * suit.length);
  33. String cardString = (face[i] + " of " + suit[j]);
  34.  
  35. System.out.print(cardString + "\t");
  36. if (player % 4 == 0)
  37. System.out.println();
  38. }
  39. }
  40.  
  41. }
  42. }
Still haven't figured out the boolean array thing though :/
Reputation Points: 10
Solved Threads: 0
Newbie Poster
roketto is offline Offline
3 posts
since May 2007
May 4th, 2007
0

Re: Boolean array?

Click to Expand / Collapse  Quote originally posted by roketto ...
And truth be told I really have no clue what to do except
Java Syntax (Toggle Plain Text)
  1. boolean[] deck = new boolean[52];
can anyone give me any insight..? I've no idea where to place it or what else to do.
Guess you wanna know how to use an array. Here is a crash course:
1. Declare and define an array:
java Syntax (Toggle Plain Text)
  1. type var_name[] = new type[array_size] ;
Where type could be a basic type (like int, boolean...) or a user defined type (like Poker)
2. Access a member of array which is located at index/position N:
Use the subscript operator (i.e. "[]" ) like this var_name[N].
java Syntax (Toggle Plain Text)
  1. System.out.println( "Nth element = " + String.valueOf(var_name[N]) ) ;
  2. /*PS: valueOf() is overloaded for basic types only, won't work for user-defined types*/
To assign a value:
Java Syntax (Toggle Plain Text)
  1. var_name[N] = new type() ;
3. Loop thru an array: array_variable_name.length gives you the length of an array.
java Syntax (Toggle Plain Text)
  1. for( int i = 0; i < var_name.length; i++ )
  2. System.out.println( String.valueOf( var_name[i] ) ) ;
Here is a full example:
java Syntax (Toggle Plain Text)
  1. public class test {
  2. public static void main( String[] args ) {
  3. //declare and define
  4. boolean arr[] = new boolean[10] ;
  5. //init
  6. for( int i = 0; i < arr.length; i++ )
  7. arr[i] = false ;
  8.  
  9. //access an element
  10. arr[(int) (Math.random() * 10)] = true ;
  11.  
  12. //print the contents
  13. for( int i = 0; i < arr.length; i++ )
  14. System.out.println( String.valueOf( arr[i] ) ) ;
  15. }
  16. }
Reputation Points: 254
Solved Threads: 74
Practically a Posting Shark
thekashyap is offline Offline
804 posts
since Feb 2007
May 5th, 2007
0

Re: Boolean array?

Oh! Thank you (:

So I think I've figured it out now... by inserting
Java Syntax (Toggle Plain Text)
  1. boolean [] deck = new boolean[52];
  2. for(int k = 0; k < 52; k++)
  3. deck[k] = false;
at the beginning

and changing my nested for loop to include:
Java Syntax (Toggle Plain Text)
  1. if (deck[j*13 + i] == false)
  2. {
  3. deck[j*13 + i] = true;
  4.  
  5. System.out.print(cardString + "\t");
  6. if (player % 4 == 0)
  7. System.out.println();
  8. }
  9. else
  10. player--;
It appears to work now
Reputation Points: 10
Solved Threads: 0
Newbie Poster
roketto is offline Offline
3 posts
since May 2007
May 5th, 2007
0

Re: Boolean array?

interesting
I am suggesting an entirely different approach to the card generation.
use two int values one for the card color and one for the card itself
card color cant be more than 4
card number cant be more than 13
Then use the random number generator of the Math class to produce those values.
If you dont want the same cards dealt within the same game tick off the values by creating Map and setting the dealt value accordingly.

Should work
Reputation Points: 10
Solved Threads: 1
Newbie Poster
antaryami is offline Offline
14 posts
since Apr 2007
May 6th, 2007
0

Re: Boolean array?

Click to Expand / Collapse  Quote originally posted by antaryami ...
interesting
I am suggesting an entirely different approach to the card generation.
use two int values one for the card color and one for the card itself
card color cant be more than 4
card number cant be more than 13
Then use the random number generator of the Math class to produce those values.
If you dont want the same cards dealt within the same game tick off the values by creating Map and setting the dealt value accordingly.
And why d'u think this is an easier way than the one he's already using?!

Click to Expand / Collapse  Quote originally posted by antaryami ...
Should work
That shouldn't be the aim.
Reputation Points: 254
Solved Threads: 74
Practically a Posting Shark
thekashyap is offline Offline
804 posts
since Feb 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: opening windows
Next Thread in Java Forum Timeline: need help to solve this problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC