| | |
Boolean array?
Thread Solved |
•
•
Join Date: May 2007
Posts: 3
Reputation:
Solved Threads: 0
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 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)
boolean[] deck = new boolean[52];
Here is my original code:
Java Syntax (Toggle Plain Text)
import javax.swing.JOptionPane; public class Poker { public static void main (String[]args) { int face = 0, suit = 0; int player = 0, round = 0; System.out.print("Player 1\tPlayer 2\tPlayer 3\tPlayer 4\n"); for (round = 1; round <= 5; round++) //round of cards dealt { for (player = 1; player <= 4; player++) { face = 1 + (int) (Math.random() * 13); suit = 1 + (int) (Math.random() * 4); cardNumber(face); cardSuit(suit); if (player % 4 == 0) System.out.println(); } } } public static void cardNumber (int face) { if (face != 10) System.out.print(" "); switch (face) { case 1: System.out.print("A of "); break; case 2: System.out.print("2 of "); break; case 3: System.out.print("3 of "); break; case 4: System.out.print("4 of "); break; case 5: System.out.print("5 of "); break; case 6: System.out.print("6 of "); break; case 7: System.out.print("7 of "); break; case 8: System.out.print("8 of "); break; case 9: System.out.print("9 of "); break; case 10: System.out.print("10 of "); break; case 11: System.out.print("J of "); break; case 12: System.out.print("Q of "); break; case 13: System.out.print("K of "); break; } } public static void cardSuit (int suit) { switch (suit) { case 1: System.out.print("Diamonds"); break; case 2: System.out.print("Hearts"); break; case 3: System.out.print("Spades"); break; case 4: System.out.print("Clubs"); break; } System.out.print("\t"); } }
•
•
Join Date: May 2007
Posts: 3
Reputation:
Solved Threads: 0
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!
Still haven't figured out the boolean array thing though :/
.. 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)
import javax.swing.JOptionPane; public class Poker { public static void main (String[]args) { int player = 0, round = 0; int i = 0, j = 0; String[] suit = { "Clubs", "Diamonds", "Hearts", "Spades" }; String[] face = { " 2", " 3", " 4", " 5", " 6", " 7", " 8", " 9", "10", " J", " Q", " K", " A" }; for (int names = 0; names < args.length; names++) //user-entered names { if (args[names].length() < 8) System.out.print(args[names] + "\t\t"); else if ((args[names].length() >= 8) && (args[names].length() < 16)) System.out.print(args[names] + "\t"); else if (args[names].length() >= 16) System.out.print("long name!\t"); } System.out.println(); for (round = 1; round <= 5; round++) //round of cards dealt { for (player = 1; player <= 4; player++) { i = (int) (Math.random() * face.length); j = (int) (Math.random() * suit.length); String cardString = (face[i] + " of " + suit[j]); System.out.print(cardString + "\t"); if (player % 4 == 0) System.out.println(); } } } }
•
•
•
•
And truth be told I really have no clue what to do exceptcan anyone give me any insight..? I've no idea where to place it or what else to do.Java Syntax (Toggle Plain Text)
boolean[] deck = new boolean[52];
1. Declare and define an array:
java Syntax (Toggle Plain Text)
type var_name[] = new type[array_size] ;
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)
System.out.println( "Nth element = " + String.valueOf(var_name[N]) ) ; /*PS: valueOf() is overloaded for basic types only, won't work for user-defined types*/
Java Syntax (Toggle Plain Text)
var_name[N] = new type() ;
java Syntax (Toggle Plain Text)
for( int i = 0; i < var_name.length; i++ ) System.out.println( String.valueOf( var_name[i] ) ) ;
java Syntax (Toggle Plain Text)
public class test { public static void main( String[] args ) { //declare and define boolean arr[] = new boolean[10] ; //init for( int i = 0; i < arr.length; i++ ) arr[i] = false ; //access an element arr[(int) (Math.random() * 10)] = true ; //print the contents for( int i = 0; i < arr.length; i++ ) System.out.println( String.valueOf( arr[i] ) ) ; } }
•
•
Join Date: May 2007
Posts: 3
Reputation:
Solved Threads: 0
Oh! Thank you (:
So I think I've figured it out now... by inserting
at the beginning
and changing my nested for loop to include:
It appears to work now
So I think I've figured it out now... by inserting
Java Syntax (Toggle Plain Text)
boolean [] deck = new boolean[52]; for(int k = 0; k < 52; k++) deck[k] = false;
and changing my nested for loop to include:
Java Syntax (Toggle Plain Text)
if (deck[j*13 + i] == false) { deck[j*13 + i] = true; System.out.print(cardString + "\t"); if (player % 4 == 0) System.out.println(); } else player--;
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
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
•
•
•
•
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.
That shouldn't be the aim.
![]() |
Similar Threads
- boolean array (Java)
- boolean array (Java)
- Dynamic Boolean Array in VC++.net 2003 (C++)
- single array objects (Java)
- Cannot utilize an array properly (Java)
Other Threads in the Java Forum
- Previous Thread: opening windows
- Next Thread: need help to solve this problem
Views: 17398 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for Java
access actionlistener android api apple applet application arguments array arrays binary bluetooth c++ chat class classes client code combobox compiler component converter coordinates data database db design desktop detection draw eclipse error event exception fast file game givemetehcodez graphics gridlayout gui helpwithhomework html ide image inheritance input integer interface j2me java javaprojects jmf jpanel jtable jtextfield julia linux list loop map method methods mobile netbeans newbie number object oracle os pattern pixel print problem program programming read regex remote remove robot scanner screen search server set size sms socket sort sql string swing text threads time timer tree update windows





