Adblock breaks this site

Java game help

Discussion in 'Programming General' started by germaine, Apr 14, 2012.

  1. germaine

    germaine Newcomer

    Joined:
    Apr 14, 2012
    Posts:
    11
    Referrals:
    0
    Sythe Gold:
    0
    Java game help

    Hi,

    Would someone be able to help me with a Java game I am making. I am making a Blackjack game using the MVC architecture, which should eventually end up with a rather simple GUI to run with.

    I've currently made a Card/Deck/Hand/Player/Dealer class and have made a start on the model to the point where I can initiate a new game and deal out cards to all of the players, I am completely stuck and would like some help finishing the project and adding the GUI!

    Any help is more than welcome! Would pay too :p

    Thanks xo
     
  2. iJava

    iJava .Previously known as RSGoldRush
    $200 USD Donor New

    Joined:
    Nov 21, 2011
    Posts:
    1,197
    Referrals:
    11
    Sythe Gold:
    485
    Discord Unique ID:
    220055593568829441
    Java game help

    Code:
    public class NewGame() {
    
    Player[] players;
    public NewGame(int numberCards, int numberPlayers) {
    players = new Player[numberPlayers];
    for(int a = 0; a < numberCards; a++) {
    for(int i = 0; i < numberPlayers; i++) {
    players[i].dealCard();
    }
    }
    Or depending on what you got

    Code:
    public class NewGame() {
    
    Player[] players;
    public NewGame(int numberCards, int numberPlayers) {
    players = new Player[numberPlayers];
    for(Player player : players) {
    player.dealCards(numberCards);
    }
     
  3. germaine

    germaine Newcomer

    Joined:
    Apr 14, 2012
    Posts:
    11
    Referrals:
    0
    Sythe Gold:
    0
    Java game help

    I have already created a newGame method inside my Blackjack class, my plan was to have the GUI and panels as seperate classes and then create an instance of the GUI within Blackjack in order to just run "Blackjack". I'm unsure whether this correctly follows the conventions of the MVC way of things though :/

    And when it comes to creating a GUI from scratch I am rather clueless!
     
  4. germaine

    germaine Newcomer

    Joined:
    Apr 14, 2012
    Posts:
    11
    Referrals:
    0
    Sythe Gold:
    0
    Java game help

    Would anyone be able to tell me what's wrong with my code here?

    I'm trying to set a rule that you may only have between 1-3 human players playing, it successfully loops if you enter above 3 players but when you enter a correct number, i.e 1, 2 or 3 then it just halts the program and stops, whereas I want it to move over to the playGame method once there is a correct number of players.

    Code:
    import java.util.Scanner;
    
    public class Blackjack {
      Deck deck = new Deck();
      public static Player[] PLAYERS;
      public static int NUMPLAYERS;
      Scanner sc = new Scanner(System.in);
      
      public void newGame() {
        Dealer dealer = new Dealer();
        Hand dealerHand = new Hand();
        dealer.setHand(dealerHand);
        dealer.setName("Dealer");
        dealer.getHand().addCard( deck.dealCard() );
        dealer.getHand().addCard( deck.dealCard() );
    
        System.out.print("How many players? ");
        if(sc.nextInt() <= 3)
        {
        	NUMPLAYERS = sc.nextInt();
        	PLAYERS = new Player[NUMPLAYERS+1];
        }
        else
        {
        	System.out.println("Please enter max of 3 players.");
        	newGame();
        }
        
        PLAYERS[0] = dealer;
        
        for(int i=1;i<=NUMPLAYERS;i++)
        {
          PLAYERS[i] = new Player();
          System.out.print("Player #"+(i)+" name: ");
          PLAYERS[i].setName(sc.next());
          System.out.println("Welcome " + PLAYERS[i].getName() + "!\n");
        }
        
        System.out.println("Dealer Hand:");
        dealer.getHand().showHand();
        System.out.println();
        for(int i=1;i<=NUMPLAYERS;i++)
        {
          Hand hand = new Hand();
        
          hand.addCard( deck.dealCard() );
          hand.addCard( deck.dealCard() );
          
          PLAYERS[i].setHand(hand);
        }
        playGame();
      }
      
      public void playGame() {
    	  System.out.println("***Playing Game");
    	    
    	  for (int i = 1; i < PLAYERS.length; i++)
    	  {
    		  while (PLAYERS[i].getPlayerState() == 0)
    		  {
    			  //Play game for this player, show his hand so he knows what he has to do
    			  System.out.println("\nPlayer "+PLAYERS[i].getName()+" Hand: ");
    			  PLAYERS[i].getHand().showHand();
    			  System.out.println("\nScore: "+PLAYERS[i].getHand().getScore());
    			  System.out.println("hit or stand? ");
    			  String action = sc.next();
    			  PLAYERS[i].playRound(action, deck);
    	      }
    		  
    	  }
      }
    }
     
  5. iJava

    iJava .Previously known as RSGoldRush
    $200 USD Donor New

    Joined:
    Nov 21, 2011
    Posts:
    1,197
    Referrals:
    11
    Sythe Gold:
    485
    Discord Unique ID:
    220055593568829441
    Java game help

    Code:
     Deck deck = new Deck();
      public static Player[] PLAYERS;
      public static int NUMPLAYERS;
      Scanner sc = new Scanner(System.in);
      
      public void newGame() {
        Dealer dealer = new Dealer();
        Hand dealerHand = new Hand();
        dealer.setHand(dealerHand);
        dealer.setName("Dealer");
        dealer.getHand().addCard( deck.dealCard() );
        dealer.getHand().addCard( deck.dealCard() );
    
        System.out.print("How many players? ");
        if(sc.nextInt() > 3) { 
        	System.out.println("Please enter max of 3 players.");
        	newGame();
        }
     else {
        	NUMPLAYERS = sc.nextInt();
        	PLAYERS = new Player[NUMPLAYERS+1];
        
        
        PLAYERS[0] = dealer;
        
        for(int i=1;i<=NUMPLAYERS;i++) {
          PLAYERS[i] = new Player();
          System.out.print("Player #"+(i)+" name: ");
          PLAYERS[i].setName(sc.next());
          System.out.println("Welcome " + PLAYERS[i].getName() + "!\n");
        }
        
        System.out.println("Dealer Hand:");
        dealer.getHand().showHand();
        System.out.println();
        for(int i=1;i<=NUMPLAYERS;i++) {
          Hand hand = new Hand();
        
          hand.addCard( deck.dealCard() );
          hand.addCard( deck.dealCard() );
          
          PLAYERS[i].setHand(hand);
        }
     }
        playGame();
      
     }
      
      public void playGame() {
    	  System.out.println("***Playing Game");
    	    
    	  for (int i = 1; i < PLAYERS.length; i++)
    	  {
    		  while (PLAYERS[i].getPlayerState() == 0)
    		  {
    			  //Play game for this player, show his hand so he knows what he has to do
    			  System.out.println("\nPlayer "+PLAYERS[i].getName()+" Hand: ");
    			  PLAYERS[i].getHand().showHand();
    			  System.out.println("\nScore: "+PLAYERS[i].getHand().getScore());
    			  System.out.println("hit or stand? ");
    			  String action = sc.next();
    			  PLAYERS[i].playRound(action, deck);
    	      }
    		  
    	  }
      }
    }
    Should work, your bracing and formatting is disgusting.
     
  6. germaine

    germaine Newcomer

    Joined:
    Apr 14, 2012
    Posts:
    11
    Referrals:
    0
    Sythe Gold:
    0
    Java game help

    This still gets stuck once the user enters a number which is correctly between 1-3. I realise my formatting is disgusting, it all tends to look very messy until I've finished and decide to go through everything....not a good thing to start making a habit of when learning really!
     
  7. iJava

    iJava .Previously known as RSGoldRush
    $200 USD Donor New

    Joined:
    Nov 21, 2011
    Posts:
    1,197
    Referrals:
    11
    Sythe Gold:
    485
    Discord Unique ID:
    220055593568829441
    Java game help

    Are you using an IDE because most have an option to format your code for you. Try :

    Code:
    public void newGame() {
        Dealer dealer = new Dealer();
        Hand dealerHand = new Hand();
        dealer.setHand(dealerHand);
        dealer.setName("Dealer");
        dealer.getHand().addCard( deck.dealCard() );
        dealer.getHand().addCard( deck.dealCard() );
    
        System.out.print("How many players? ");
        if(sc.nextInt() > 3) { 
        	System.out.println("Please enter max of 3 players.");
        	newGame();
        }
     if(sc.nextInt() > 0 && < 4) {
        	NUMPLAYERS = sc.nextInt();
        	PLAYERS = new Player[NUMPLAYERS+1];
        
        
        PLAYERS[0] = dealer;
        
        for(int i=1;i<=NUMPLAYERS;i++) {
          PLAYERS[i] = new Player();
          System.out.print("Player #"+(i)+" name: ");
          PLAYERS[i].setName(sc.next());
          System.out.println("Welcome " + PLAYERS[i].getName() + "!\n");
        }
        
        System.out.println("Dealer Hand:");
        dealer.getHand().showHand();
        System.out.println();
        for(int i=1;i<=NUMPLAYERS;i++) {
          Hand hand = new Hand();
        
          hand.addCard( deck.dealCard() );
          hand.addCard( deck.dealCard() );
          
          PLAYERS[i].setHand(hand);
        }
     }
        playGame();
      
     }
      
      public void playGame() {
    	  System.out.println("***Playing Game");
    	    
    	  for (int i = 1; i < PLAYERS.length; i++)
    	  {
    		  while (PLAYERS[i].getPlayerState() == 0)
    		  {
    			  //Play game for this player, show his hand so he knows what he has to do
    			  System.out.println("\nPlayer "+PLAYERS[i].getName()+" Hand: ");
    			  PLAYERS[i].getHand().showHand();
    			  System.out.println("\nScore: "+PLAYERS[i].getHand().getScore());
    			  System.out.println("hit or stand? ");
    			  String action = sc.next();
    			  PLAYERS[i].playRound(action, deck);
    	      }
    		  
    	  }
      }
    }
     
  8. germaine

    germaine Newcomer

    Joined:
    Apr 14, 2012
    Posts:
    11
    Referrals:
    0
    Sythe Gold:
    0
    Java game help

    I still get an error, but after testing it by simply entering 1 multiple times, it starts after I've entered the amount of players 4 times, and takes in that 1 as below as the amount of players for the game which is correct, just that I have to type it in 4 times. Thus I'm assuming it's some sort of error in my coding regarding the sc.nextInt() coding, but I cant see what it is.

    Yes I am using an IDE, I'm currently using Eclipse, not that I like it all that much.

    Code:
    How many players? 4
    Please enter max of 3 players.
    How many players? 1
    1
    1
    1
    Player #1 name: 
     
  9. germaine

    germaine Newcomer

    Joined:
    Apr 14, 2012
    Posts:
    11
    Referrals:
    0
    Sythe Gold:
    0
    Java game help

    I also changed it from sc.nextInt() to a temporary integer to see if that effected it, instead of having to enter 1 four times I only have to enter it twice when using a temporary integer for going into the if statements. Wierd.
     
  10. iJava

    iJava .Previously known as RSGoldRush
    $200 USD Donor New

    Joined:
    Nov 21, 2011
    Posts:
    1,197
    Referrals:
    11
    Sythe Gold:
    485
    Discord Unique ID:
    220055593568829441
    Java game help

    Well I guess ti could be that it stops the scanner from continually asking for input. Try Netbeans IDE.
     
  11. germaine

    germaine Newcomer

    Joined:
    Apr 14, 2012
    Posts:
    11
    Referrals:
    0
    Sythe Gold:
    0
    Java game help

    Thanks, I fiddled around with some things with the scanner and it worked, I was forgetting the next if statement where I am setting the NUMPLAYERS to sc.nextInt() and used the temporary integer instead.

    Now to get lost and confused in the GUI :p
     
  12. iJava

    iJava .Previously known as RSGoldRush
    $200 USD Donor New

    Joined:
    Nov 21, 2011
    Posts:
    1,197
    Referrals:
    11
    Sythe Gold:
    485
    Discord Unique ID:
    220055593568829441
    Java game help

    GUI should be easy, if you get Netbeans IDE it has a GUI builder.

    If you want to make it yourself then just...

    Code:
    JFrame frame = new JFrame();
    frame.setBounds(size);
    JPanel panel = new JPanel(BorderLayout.SOUTH);
    frame.add(panel);
    It's like that.
     
  13. germaine

    germaine Newcomer

    Joined:
    Apr 14, 2012
    Posts:
    11
    Referrals:
    0
    Sythe Gold:
    0
    Java game help

    Thanks for your help so far, I will try and make a GUI using Netbeans rather than Eclipse. I think I was told to initially use Eclipse for the reasons you just said, as Netbeans has a GUI builder.
     
  14. iJava

    iJava .Previously known as RSGoldRush
    $200 USD Donor New

    Joined:
    Nov 21, 2011
    Posts:
    1,197
    Referrals:
    11
    Sythe Gold:
    485
    Discord Unique ID:
    220055593568829441
    Java game help

    How'd you get on?
     
  15. germaine

    germaine Newcomer

    Joined:
    Apr 14, 2012
    Posts:
    11
    Referrals:
    0
    Sythe Gold:
    0
    Java game help

    Hey, I have managed to get a working game with GUI, my only issue is trying to get more than 1 player playing the game, but this doesn't have to be done, it was just something I thought would be more appropriate than just one player being able to play it.
     
  16. SuF

    SuF Legend
    Pirate Retired Global Moderator

    Joined:
    Jan 21, 2007
    Posts:
    14,212
    Referrals:
    28
    Sythe Gold:
    1,234
    Discord Unique ID:
    203283096668340224
    <3 n4n0 Two Factor Authentication User Community Participant Spam Forum Participant Sythe's 10th Anniversary
    Java game help

    How do you want the second person to play? Just on the same computer?
     
< Need C++ Program Written | c# tutorial list [Online doc][Google] >


 
 
Adblock breaks this site