Boolean array?

Thread Solved

Join Date: May 2007
Posts: 3
Reputation: roketto is an unknown quantity at this point 
Solved Threads: 0
roketto roketto is offline Offline
Newbie Poster

Boolean array?

 
0
  #1
May 3rd, 2007
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
  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:
  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. }
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 3
Reputation: roketto is an unknown quantity at this point 
Solved Threads: 0
roketto roketto is offline Offline
Newbie Poster

Re: Boolean array?

 
0
  #2
May 4th, 2007
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!

  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 :/
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 590
Reputation: thekashyap will become famous soon enough thekashyap will become famous soon enough 
Solved Threads: 53
thekashyap's Avatar
thekashyap thekashyap is offline Offline
Posting Pro

Re: Boolean array?

 
0
  #3
May 4th, 2007
Originally Posted by roketto View Post
And truth be told I really have no clue what to do except
  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:
  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].
  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:
  1. var_name[N] = new type() ;
3. Loop thru an array: array_variable_name.length gives you the length of an array.
  1. for( int i = 0; i < var_name.length; i++ )
  2. System.out.println( String.valueOf( var_name[i] ) ) ;
Here is a full example:
  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. }
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 3
Reputation: roketto is an unknown quantity at this point 
Solved Threads: 0
roketto roketto is offline Offline
Newbie Poster

Re: Boolean array?

 
0
  #4
May 5th, 2007
Oh! Thank you (:

So I think I've figured it out now... by inserting
  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:
  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
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 14
Reputation: antaryami is an unknown quantity at this point 
Solved Threads: 1
antaryami's Avatar
antaryami antaryami is offline Offline
Newbie Poster

Re: Boolean array?

 
0
  #5
May 5th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 590
Reputation: thekashyap will become famous soon enough thekashyap will become famous soon enough 
Solved Threads: 53
thekashyap's Avatar
thekashyap thekashyap is offline Offline
Posting Pro

Re: Boolean array?

 
0
  #6
May 6th, 2007
Originally Posted by antaryami View Post
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?!

Originally Posted by antaryami View Post
Should work
That shouldn't be the aim.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 17398 | Replies: 5
Thread Tools Search this Thread



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

©2003 - 2010 DaniWeb® LLC