• Log in or sign up for an account here: https://www.sythe.org/login

Java game help

germaine

Newcomer
Joined
Apr 14, 2012
Sythe Gold
0
Messages
11
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
 
PHP Shit Show 2k26
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);
}
 
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!
 
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);
	      }
		  
	  }
  }
}
 
PHP Shit Show 2k26
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.
 
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!
 
PHP Shit Show 2k26
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);
	      }
		  
	  }
  }
}
 
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:
 
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.
 
PHP Shit Show 2k26
Well I guess ti could be that it stops the scanner from continually asking for input. Try Netbeans IDE.
 
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
 
PHP Shit Show 2k26
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.
 
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.
 
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.
 
<3 n4n0
Discord Verified
Two Factor Authentication User
Community Participant
Spam Forum Participant
Sythe's 10th Anniversary
Sythe Awards 2011 Winner
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.

How do you want the second person to play? Just on the same computer?
 
Runescape Gamblingrunescape gambling
Back
Top