I've been assigned to make a Black Jack game. I want it to be replayable by pressing a button when the round is over. I've set it up so that I have a method named initGame() that plays each player and the dealer a card each, and enables players[] buttons. It then runs runGame(), which is supposed to check if a player is done playing (over 21/pressed stay).
When all players are deone it then sets running to false, enabling the resetbutton, which then runs initGame(). I kind of want it to loop around like this. However, the game freezes the second time runGame() is ran, when initNewGame() works fine.
private void initNewGame(){
dealer.addCard();
for(int i = 0; i < numberofplayers; i++){
players[i].addCard(); //draws card, adds score and paints card to the board
players[i].setStatus(false); //makes the buttons usable in the players[] class
}
runGame();
}
/*
* runGame() is meant to check if the players all have played out their hand, to then allow the dealer to play his. After that it sets running = false, allowing the player to either use the shuffle button or start a new game
*/
private void runGame(){
while(running){
int count=0;
for(int i = 0; i<numberofplayers; i++){ //loops through all the player checking their status
if(players[i].getStatus()) count++;
}
if(count==numberofplayers){ //checks if all players have >=21 or have pressed stay
dealer.addCard(); //plays the dealers hand
if(dealer.getPoints() >=19){ //stops when dealer have >=19
running = false; //stops entire loop
}
}
}
}
class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e){
if(!running){ //won't allow players to reset while a round is playing
if(e.getSource().equals(resetbutton)){ //Checks for rese/new game button
System.out.println("reset");
dealer.setPoints(0); //sets dealers hand to 0
dealer.paintText(); //updates delaers painted score
dealer.clearCards(); //clear the painted cards
for(int i = 0; i < numberofplayers; i++){
players[i].setPoints(0);
players[i].clearCards();
players[i].paintText();
}
gui.repaint();
running = true; //allows the runGame() to start
initNewGame(); //this one works the second time
}else if(e.getSource().equals(shufflebutton)){
d1.shuffleDeck();
System.out.println(running);
}
}
}
}