import javax.swing.*;
import java.awt.Font; // need for Font and Point Size Changes
import java.awt.Color; // need to change Color
public class Lab2
{
static int wins = 0;
static int losses = 0;
static int totalPlays = 0;
public static void main(String[] args)
{
runThis();
System.exit(0);
}//main
public static void runThis()
{
int userNum;
int compNum;
int value;
openingMessage();
getInt();
do
{
userNum = getInt();
compNum = rollTheDice();
compareNum (userNum, compNum);
value = JOptionPane.showConfirmDialog(null, "Continue with the game?");
}
while (value == JOptionPane.YES_OPTION); // do while
endingMessage();
}
public static void openingMessage()
{
String output = "";
Font big = new Font ( "Serif", Font.BOLD, 22 ); // set up how the letters will look
JTextArea outputArea = new JTextArea (); // create a new JTextArea named outputArea
outputArea.setFont(big); // use the font stuff from above
outputArea.setBackground ( new Color ( 0x00, 0x00, 0x80 ) ); // Navy 000080
outputArea.setForeground ( new Color ( 0xFF, 0xF8, 0xDC ) ); // Corn Silk FFF8DC
output = "Welcome to the Guess the Numbers from 1 to 6 Program \n\n"
+ "The computer will generate a random number from 1 to 6. \n"
+ " Your task is to see if you can guess the number. \n\n"
+ "The game will keep track of how many games you played \n"
+ "and it will tell you how many wins you had \n \n"
+ "\t Click OK to Begin.\n\n\t Good Luck \n";
outputArea.setText(output);
JOptionPane.showMessageDialog(null, outputArea,
"The Guess the Numbers Game", JOptionPane.INFORMATION_MESSAGE);
return;
}// opening message
public static void compareNum (int user, int comp)
{
if(user == comp)
{
JOptionPane.showMessageDialog (null, "You Win");
wins++;
totalPlays++;
System.out.println ("Wins:" + wins);
}
else
{
JOptionPane.showMessageDialog (null, "You Lost");
losses++;
totalPlays++;
System.out.println ("Losses:" + losses);
}
}
public static int getInt()
{
int val;
while (true) // a seemingly endless loop - uses break to get out
{ // loop until we get a valid int
String s;
s = JOptionPane.showInputDialog ( "Enter a number from 1 - 6:" );
if (s == null)
{
JOptionPane.showMessageDialog(null, "Leaving program"); // exit on cancel
System.exit (0);
} // cancel if
else
if (s.equals(""))
{
JOptionPane.showMessageDialog
(null, "You must make an entry in the InputBox");
} // entered nothing
try
{
val = Integer.parseInt(s);
if (val >= 1 && val <= 6)
break; // exit loop with valid int
// could have had a variable change here to get out of loop
}// try
catch (Exception e)
{
JOptionPane.showMessageDialog(null, "Enter a number from 1 - 6");
}// catch
}//while
return val; //return the number to the calling method
} //end getInt
public static void endingMessage()
{
String output = "";
Font big = new Font ( "Serif", Font.BOLD, 22 ); // set up how the letters will look
JTextArea outputArea = new JTextArea (); // create a new JTextArea named outputArea
outputArea.setFont(big); // use the font stuff from above
outputArea.setBackground ( new Color ( 0x00, 0x00, 0x80 ) ); // Navy 000080
outputArea.setForeground ( new Color ( 0xFF, 0xF8, 0xDC ) ); // Corn Silk FFF8DC
output = "Thanks for playing the Guess the Numbers from 1 to 6 Program \n\n"
+ "You have won. \n"
+ " Your task is to see if you can guess the number. \n\n"
+ "The game will keep track of how many games you played \n"
+ "and it will tell you how many wins you had \n \n"
+ "\t Click OK to Begin.\n\n\t Good Luck \n";
outputArea.setText(output);
JOptionPane.showMessageDialog(null, outputArea,
"The Guess the Numbers Game", JOptionPane.INFORMATION_MESSAGE);
return;
}// opening message
public static void dice()
{
int Total;
String output = "";
//collect from user guess of
//die total with method guesTheDice(make if statement for not to break if val<= 1 && <=6
Total = guessTheDice();
int rolledTotal;
//roll the dice in method
rolledTotal = rollTheDice();
if(Total == rolledTotal)
output = "You guessed right!" +
" The dice total is " + Total;
else
output = "Sorry. The dice total is: "
+rolledTotal;
JOptionPane.showMessageDialog(
null, output);
System.exit(0);
}//end main
public static int guessTheDice ()
{
int dice1; //first value of die1
int total;
String firstDice;
firstDice=JOptionPane.showInputDialog
("Guess die 1 (1-6): ");
dice1=Integer.parseInt(firstDice);
total=dice1;
return total;
}//end guessTheDice method
public static int rollTheDice()
{
int x;
//generating random numbers
//between 1-6 with
//Math.random
x=1 + (int) (Math.random( ) *6);
return x;
}//end rollTheDice method
}// try val = integer parseInt