package BlackJack; /** * @(#)Game21.java * * @author Anthony J. Groves * @version 1.00 2014/1/3 */ import java.util.Random; import javax.swing.JFrame; import javax.swing.JOptionPane; public class Game21 extends JFrame{ private static final long serialVersionUID = 1L; Random rand = new Random(); int userSum = 0; int compSum = 0; boolean playAgain = false; /** * Creates a new Game 21 game which interacts with the user using JOptionPanes. * This game is a simplified (and child-friendly) version of BlackJack. No gambling, just some good fun! :-) */ public Game21(){} /** * Greets the user with the game's rules and runs the game as long as the user wants to play */ public void play(){ greetUser(); do{ userTurn(); compTurn(); giveResult(); playAgain = playAgain(); }while(playAgain); System.exit(0); } /** * Asks the user if he/she wants to play again and returns their answer in a boolean * @return a boolean value that is true if the user wishes to play again, and false if not */ private boolean playAgain() { Object[] options = {"Yes, I want to play again!", "No, I want to quit."}; int n = JOptionPane.showOptionDialog(this, "Would you like to play again?", "Play again?", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]); if(n == 0){ userSum = 0; compSum = 0; playAgain = false; return true; } else return false; } /** * Greets the user with the game's rules */ private void greetUser() { String greeting = " ~~~~~~~ Welcome to Game 21! ~~~~~~~ \n\n\n-You will initally be given two numbers. \n-And after that, you can choose to hit (receive another number) or stay (end your turn). \n-You want the sum of your numbers to equal 21 or be as close to 21 as possible! \n-If you go over 21, you bust, or automatically lose. If the you are closer to 21 than I am, or if I bust, than you win! \n-Remember, if there is a tie, I ALWAYS win! \n\nGood luck, human! You are going to need it!"; JOptionPane.showMessageDialog(this, greeting, "How To Play", JOptionPane.INFORMATION_MESSAGE); } /** * Decides the outcome of the game and outputs the result to the user. */ public void giveResult() { if(userSum == 21 && compSum != 21) JOptionPane.showMessageDialog(this, "YOU GOT 21!!!!! CONGRATULATIONS!!! \nThe sum of my numbers is " + compSum, "YOU GOT 21", JOptionPane.WARNING_MESSAGE); else if(userSum > 21) JOptionPane.showMessageDialog(this, "Well, look at that! You busted with a sum of "+userSum+"! \nI WIN!!!", "YOU BUSTED!", JOptionPane.WARNING_MESSAGE); else if(compSum > 21) JOptionPane.showMessageDialog(this, "Uh oh! I busted with a sum of "+compSum+"! :-( \nYou win! Good job, human. ", "I busted!", JOptionPane.WARNING_MESSAGE); else if(userSum > compSum) JOptionPane.showMessageDialog(this, "The sum of my numbers is " + compSum + ". \nAnd the sum of your numbers is " + userSum + ". \nWhich means that you win!! "); else JOptionPane.showMessageDialog(this, "The sum of my numbers is " + compSum + ". \nAnd the sum of your numbers is " + userSum + ". \nWhich means that I win!"); } /** * Runs the computer's turn. It will continuously hit until it has a sum of 17, and then it will stay. */ public void compTurn() { do{ compSum += rand.nextInt(11)+1; }while(compSum <= 17); } /** * Runs the user's turn. Assigns two numbers to the user and tells the user the sum of the numbers * Then, it asks the user to hit or stay repeatedly until the user chooses to stay or until he/she reaches 21 or busts */ public void userTurn() { boolean stay = false; userSum = (rand.nextInt(11)+1) + (rand.nextInt(11)+1); do{ Object[] options = {" Hit!! ", " Stay "}; int n = JOptionPane.showOptionDialog(this, "Your sum so far is: \n\t" + userSum, "Your turn!", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]); if(n ==1){ JOptionPane.showMessageDialog(this, "You have chosen to stay. Wise choice."); stay = true; } else if(n == 0) userSum += rand.nextInt(11)+1; if(userSum >= 21) break; }while(!stay); } /** * Main method to test out the Game21 game */ public static void main(String[] args) { new Game21().play(); } }