Results 1 to 17 of 17
  1. #1
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Question My blackjack games random generator doesnt work!

    Hi!
    I'v been studing java now from like a week soon.
    And I decided that my first program (besides those for loops programs) should be a blackjack game, because I learned alot when I tried to do it in python as a beginner.
    So anyways, to get to the point, my choose random cards can only generate 4 diffrent cards. And since it has to generate a new card every time it generates an already choosen card, therfore the program hogs up when Im pressing y to get the fith card...

    I would really appriciate helpon this, cause its not the ordinary problem where iv put the new Random; Inside the loop, cause i havnt!

    So heres my code, if you could help me I would be really glad =)

    Java Code:
    /**
     * @(#)Blackjack.java
     *
     * Blackjack application
     *
     * @Andreas
     * @version 1.00 2009/8/14
     */
     import static java.lang.System.out;
     import java.util.Scanner;
     import java.util.Random;
     
      
    
     
     
     
     class cardTypes {
     	static String takencards[] = new String[56];
     	
     	public static void main(String[] args){
    		for (int x = 0; x <57; x++) {
    			takencards[x] = "Nothing";
    			System.out.println(takencards[x]);
    		}
     	} 		
     	enum types {Harts, Diamonds, Spads, Clover};
     	
     	enum types2 {One, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King, Ace};
     	
     
     		
     }
     
     
     
    public class Blackjack {
        
        public static void main(String[] args) {
      		Random numbGen = new Random();
        	// TODO, add your application code
        	out.println("Wellcome to blackjack!");
        	String mykey;
        	int arrayNumber = 0;
        	Scanner key = new Scanner(System.in);
        	do {
        	out.println("Press enter to start!");
        	mykey = key.nextLine();
        	}while (!mykey.equals(""));
        	Scanner cont = new Scanner(System.in);
        	char reply;
     
    		do {
        		String card = getUntakenCard(arrayNumber,numbGen);
    			arrayNumber = arrayNumber+1;
    			out.println("You got the card: "+card);
    			do {
    				System.out.println("Continue? (y/n)");
    				reply = cont.findWithinHorizon(".",0).charAt(0);
    				mykey = cont.nextLine();
        		} while (reply != 'y' && reply != 'n' && mykey.equals("") );			
    			
    		}while (reply == 'y');
    				
    
        }
        
        static String getUntakenCard(int arrayNumber,Random numbGen){
        	String card;
        	Boolean found = true;
        	do{
    			card = new Blackjack().getRandomCard(numbGen);
    			found = false;
    			for (int tc = 0; tc<56; tc++){
    				//System.out.println(tc);
    				if (cardTypes.takencards[tc] != null){
    					if (cardTypes.takencards[tc].equals(card)){
    						found = true;
    					}
    				}
    			}
    
    
    
        	} while (found == true);
    			//System.out.println("ArrayNumber: "+arrayNumber);
    			cardTypes.takencards[arrayNumber] = card;
        	return card;    	
        }
        
        
        String getRandomCard(Random numbGen){
        	int n = numbGen.nextInt(4);
        	System.out.println("Getting new card");
    		cardTypes.types[] values = cardTypes.types.values(); // this static method will get an array of 'e'
    		cardTypes.types eVal = values[n];
    		
    		int n2 = numbGen.nextInt(14);
    		cardTypes.types2[] values2 = cardTypes.types2.values(); // this static method will get an array of 'e'
    		cardTypes.types2 eVal2 = values2[n];
    		
    		return eVal2+" of "+eVal;
        }
    }
    Thanks for reading =)
    Last edited by Addez; 08-16-2009 at 03:53 PM. Reason: grammar..

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by Addez View Post
    my choose random cards can only generate 4 diffrent cards. And since it has to generate a new card every time it generates an already choosen card, therfore the program hogs up when Im pressing y to get the fith card...
    Since you've not remove the selected card from the original list there is a possibility happen this as well as to not. So what you have to do, in logically you've to remove the selected card from the list before choose the next.

  3. #3
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default

    how do I do that?
    card = null; ?

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    You want to set an object to null and discard? If so as you shown here just set it to null. But you have to be careful with the usages of that object reference. After discard the object, you are using the same reference, ends with a null pointer exception.

  5. #5
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default

    ahh U ment that I should remove the card from the list?
    Well I cant, cause if i get 1 of harts then I cant remove the number 1 out of the types2 enum. Cause If i do then I also remove all the other number one cards such as 1 of diamonds and 1 of cloves.
    Therefore I made an array where I inserted all picked cards.
    And if the new card is found there then it picks a new card, but the java.random is so weard that It picks after such a simple pattern as 1,3,2,4 and then it goes around again!

    I bet this is something wrong in my code not the random function but what?

  6. #6
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    6

    Default

    Quote Originally Posted by Addez View Post
    ... 1 of cloves.
    Whoa, that's some weird deck :D

  7. #7
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default

    XD sorry, im not so good at english..
    Isnt it harts, diamonds, spads and cloves?

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

  9. #9
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default

    kk, well the main problem is that it doesnt work.
    The random doesnt give random at all, any ideas on that?

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Before you get too far, I suggest that you use your enums to help construct a Card class whose objects will hold a suit variable and a rank variable (what you call types and types2). Then I'd create a Deck object that holds Card objects.

  11. #11
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default

    I dont have to cause this works perfect! If the rndom function did the same it would all work.
    Even if I would do as u said and make an enum and spend my life on wrighting every of the 56s cards thats in a normal card deck, then it still wouldnt work!
    Cause the random function can only choose 4 of those and then it takes it all from start

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Has your code changed at all since you last posted it? If so, then please repost it with the changes. Also, if it's not too much trouble, please set your code editor to indent with spaces, not tabs (2 to 3 spaces usually do nicely), as the forum software makes tabs unusually large, making code that uses them hard to read. Thanks and good luck!

  13. #13
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default

    No I havnt changed anything in the code..
    And how do I choose identing on code tags? Or should I use like html tags? Or do they also ruin the script?

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Oh, one other thing. If you get a chance, please have a look at this Sun tutorial on enums that uses playing cards as their example: Enums

  15. #15
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default

    PHP Code:
    /**
     * @(#)Blackjack.java
     *
     * Blackjack application
     *
     * @Andreas
     * @version 1.00 2009/8/14
     */
     import static java.lang.System.out;
     import java.util.Scanner;
     import java.util.Random;
     
      
    
     
     
     
     class cardTypes {
     	static String takencards[] = new String[56];
     	
     	public static void main(String[] args){
    		for (int x = 0; x <57; x++) {
    			takencards[x] = "Nothing";
    			System.out.println(takencards[x]);
    		}
     	} 		
     	enum types {Harts, Diamonds, Spads, Clover};
     	
     	enum types2 {One, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King, Ace};
     	
     
     		
     }
     
     
     
    public class Blackjack {
        
        public static void main(String[] args) {
      		Random numbGen = new Random();
        	// TODO, add your application code
        	out.println("Wellcome to blackjack!");
        	String mykey;
        	int arrayNumber = 0;
        	Scanner key = new Scanner(System.in);
        	do {
        	out.println("Press enter to start!");
        	mykey = key.nextLine();
        	}while (!mykey.equals(""));
        	Scanner cont = new Scanner(System.in);
        	char reply;
     
    		do {
        		String card = getUntakenCard(arrayNumber,numbGen);
    			arrayNumber = arrayNumber+1;
    			out.println("You got the card: "+card);
    			do {
    				System.out.println("Continue? (y/n)");
    				reply = cont.findWithinHorizon(".",0).charAt(0);
    				mykey = cont.nextLine();
        		} while (reply != 'y' && reply != 'n' && mykey.equals("") );			
    			card = null;
    		}while (reply == 'y');
    				
    
        }
        
        static String getUntakenCard(int arrayNumber,Random numbGen){
        	String card;
        	Boolean found = true;
        	do{
    			card = new Blackjack().getRandomCard(numbGen);
    			found = false;
    			for (int tc = 0; tc<56; tc++){
    				//System.out.println(tc);
    				if (cardTypes.takencards[tc] != null){
    					if (cardTypes.takencards[tc].equals(card)){
    						found = true;
    					}
    				}
    			}
    
    
    
        	} while (found == true);
    			//System.out.println("ArrayNumber: "+arrayNumber);
    			cardTypes.takencards[arrayNumber] = card;
        	return card;    	
        }
        
        
        String getRandomCard(Random numbGen){
        	int n = numbGen.nextInt(4);
        	System.out.println("Getting new card");
    		cardTypes.types[] values = cardTypes.types.values(); // this static method will get an array of 'e'
    		cardTypes.types eVal = values[n];
    		
    		int n2 = numbGen.nextInt(14);
    		cardTypes.types2[] values2 = cardTypes.types2.values(); // this static method will get an array of 'e'
    		cardTypes.types2 eVal2 = values2[n];
    		
    		return eVal2+" of "+eVal;
        }
    }
    there, il have a look at the tutorial later

  16. #16
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default

    nvm guys! I found it!
    In the bottom I had written n instead of n2 which gave it only 4 cards to choose from XD
    Thanks for the help anyways

  17. #17
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    hmm... too bad I didn't see this earlier, could've given you a sample... I even have a class called Deck... Sigh... must stop playing games.

    You really do want to create an ArrayList or something of Cards, where class Card wraps a Suit and a Rank.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

Similar Threads

  1. Help with class project, random number generator.
    By Christopher The Great in forum New To Java
    Replies: 4
    Last Post: 03-14-2009, 02:12 AM
  2. Random Shape generator
    By scheng12 in forum New To Java
    Replies: 1
    Last Post: 03-09-2009, 02:06 AM
  3. java doesnt allow vista to work
    By 10rosas in forum New To Java
    Replies: 5
    Last Post: 12-22-2008, 04:23 PM
  4. Replies: 3
    Last Post: 05-27-2008, 01:51 PM
  5. Random Generator
    By padutch2 in forum New To Java
    Replies: 1
    Last Post: 12-03-2007, 06:43 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •