I have a question that may be very basic, but I cant seem to find it on stack (and what I do find is very complicated) so I'm very sorry if it's already out there somewhere.
I'm writing a card game in java, and I want to move the piece of code that draws a new card into a separate method, so that I may call on it whenever, instead of having the same piece of code appear again and again whenever I need it.
The only problem is I don't know what type of method to use or how to call on it, since it's not going to return anything, just do a bunch of code.
Here is the piece of my code that draws a card, if that helps to show what I'm aiming to do.
if (event.getSource() == hit && endOfRound == false) {
Continue = true;
while (Continue == true) //If the random number has been drawn before, loop the random number generator again
{
cardID = (int) RandomNumber.GetRandomNumber(52);
if (cardsLeft[cardID] == true) //If a new card is drawn, continue normally
{
Continue = false; //Stop the loop that draws a new card
cardsLeft[cardID] = false; //save that the card was drawn
stay.setBackground(Color.green);
plyrSum += BlackJack.value(cardID); //add value to sum
plyrSumLabel.setText("Your sum: " + plyrSum); //display new sum
cardLabel[plyrCardCounter].setIcon(cardImage[cardID]); //Display card
plyrCardCounter++;
if (cardID >= 48) //record how many aces are in the hand so the program knows how many times it can reduce the sum.
plyrAceCounter++;
while (plyrSum > 21 && plyrAceCounter > 0) //If bust, reduce the value of an ace by 10 (from 11 to 1).
{
plyrSum -= 10;
plyrSumLabel.setText("Your sum: " + plyrSum);
plyrAceCounter--;
}
if (plyrSum > 21) //Automatically end the round if someone busts (player)
{
stay.doClick();
}
}
}
}
Declare a method drawCard() that returns void. Now copy the block inside if and paste it inside the method. Then rewrite the if part:
if (event.getSource() == hit && endOfRound == false) drawCard();