Lab3e
Random things

Copy, paste and run this program - It does not do anything yet.
We are going to make it give us a random number between 1 and 10.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;

public class Lab3e1 extends JFrame implements ActionListener
{
   // variable declarations
   JButton b1, b2;
   JTextField t1, t2;
   JLabel L1,L2, L3;

  public Lab3e1 ()
  {
    super("Random numbers");
    Container container = getContentPane();
    container.setLayout(new FlowLayout());
     setSize(200,300);
   // create objects

   L1 = new JLabel("The lucky number game ");

   t1 = new JTextField(15); // sets column size to 15

   b1 = new JButton("Get your number ");
   b1.addActionListener(this);

   // add to container

   container.add(L1);
   container.add(b1);
   container.add(t1);

   setVisible(true); //Make Window Visible
   }

    public void actionPerformed(ActionEvent e)
    {
     if(e.getSource() == b1)
      {

       }// end of b1

    }//end of actionPerformed

   public static void main(String args[])
   {
   Lab3e1 prog = new Lab3e1 ();
   prog.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   }
}

1. Random numbers are numbers that the computer makes up for us. Most games have random things.
   Add this line to the imports at the top.
     import java.util.Random;

2. We need to create an object for the random number. This is needed only once in a program.
    We will need some integers to hold the random numbers in memory.
    Add these lines where we declare the GUI variables

    Random rd = new Random(); /// new line
    int a = 0, b = 0; // for the random numbers


3. Compile your program and run it - nothing has changed yet.

Please read this lesson.

  rd.nextInt(x) is the command to generate a random number between 0 and (x - 1).
  This means rd.nextInt(6) could be a number like 0, 1, 2, 3, 4, 5
  rd.nextInt(4) could be a number like 0, 1, 2, 3
  rd.nextInt(10) could be a number like 0, 1, 2, 3, 4, 5 , 6, 7, 8, 9

  The problem is humans like their numbers to start with 1, so we might use
  rd.nextInt(6)+1 to give us a number like 1, 2, 3, 4, 5, 6 - the numbers on a die
  rd.nextInt(4)+5 to give us a number like 5, 6, 7, 8 for some game application
  rd.nextInt(10)+1 to give us a number between 1 and 10 inclusive

4. Inside the actionPerformed method, inside the conditional for b1 add these 2 lines

a = rd.nextInt(10)+1; // random number from 1-10
t1.setText("Your lucky number is "+a);

 Now try your program - click many times - you will see the number change

 

Fancy changes

We can make the program change based on the random number using color.

1. Try this example. Add this conditional BEFORE the setText line

if( a <= 5)
   t1.setBackground(Color.yellow);
else
   t1.setBackground(Color.cyan);

Try it - When/why does the background of the JTextField change as you are clicking the JButton?

2. Try this instead - paste it over the if..else we just added - now you get one color for even numbers and one color for odd numbers

if( a % 2 == 0)
   t1.setBackground(Color.pink);
else
   t1.setBackground(Color.green);

Try it - I like this one better. Use whichever one you like.

Creating a little game

1. Add these lines where we create objects

L2 = new JLabel(); // for the win or lose part

t2 = new JTextField(15);

b2 = new JButton("computer's number");
b2.addActionListener(this);

2. Add these to the end of the container.add section

container.add(b2);
container.add(t2);
container.add(L2);

3. compile and run - you see the new JButton and the new JTextField but the button does not work yet
Add this after the end brace for getSource == b1

if(e.getSource()==b2)
{
 b = rd.nextInt(10)+1; // random numbers from 1-10
 if( b%2 ==0)
    t2.setBackground(Color.yellow);
 else
    t2.setBackground(Color.cyan);
t2.setText("the computer number is "+b);

}// end of b2

4. It would be nice to let the program tell us who wins.
Add the code before the end brace of b2
My code uses nested if.. else but you could have used 3 if's. Try the program now.

if(a > b)
{
  L2.setText("you beat the computer ");
}
else
  if(a < b)
 {
    L2.setText("the computer beat you");
  }
  else
   {
    L2.setText("tie - play again");
    }

5. It would be nice to keep track of how many times you beat the computer.
These are called "counters". You saw a counter before in Lab3d1 where the computer counted the the number of times you clicked the JButton. They must always be set to zero before you start.
Add these to the variable declaration section

int total = 0; // for the total number of games played
int wins = 0; // for the number of times you win

Add these in the appropriate locations.
      a. Create the JLabel object.

L3 = new JLabel("");

      b. Add it to the container

container.add(L3);

6. Add these inside the getSource == b2. Be very careful, because they go in different places.

This goes before the if(a > b) conditional       total = total+1;

This goes inside the braces of if(a > b) conditional     wins = wins+1;

This goes after the nest if..elses     L3.setText(total+" games you win "+wins+" times");

Try the game now. there are still some flaws that we need to fix. Can you find them?

Fixing flaws

1. We want to force the player to click their button first so we turn off b2 until b1 is clicked.
Add this line with the b2 JButton stuff

b2.setEnabled(false); //turn off button

2. Before the end brace of the conditional getSource == b1 add these lines

b1.setEnabled(false); //turn off b1
b2.setEnabled(true); //turn on b2

3. Before the end brace of the conditional getSource == b2 add these lines

b2.setEnabled(false); //turn off b2
b1.setEnabled(true); //turn on b1

Try it now - the player is forced to click JButtons in a certain order.

Making game more friendly

At present, to restart the game, you must quit and then run it again. We are going to add a "reset score" JButton

1. Add b3 to your JButton declarations
2. Create a JButton b3 that says "reset" on it. Be sure to add an actionListener for this JButton
3. Add it to the end of the container.add section

Compile and run. You should see the reset JButton but it does nothing yet.

Now comes the logic part. What do we want to reset?
a. the number of games and the number of wins should be the default value of zero
b. the JTextFields and JLabels need to cleared
c. the JButtons need to go back to their original state.

Add this inside the actionPerfomed method at the end.

if(e.getSource()== b3)
{
 total =0;
 wins =0;
 t2.setText(""); //blank out the JTextfields
 t1.setText("");
 L2.setText("The game is reset");
 L3.setText("no games played");
 b2.setEnabled(false); //turn off button
 b1.setEnabled(true); //turn on button
}// end of reset JButton

Try the program now. it should be pretty good.

Assignment

save Lab3e1 as Lab3e2 - 60 points. Colorize, add fonts and icons and make it look good.

Here are the icons that I used.

Lab3e3   40points
start with Lab3e1 (without color, fonts and icons) and save as Lab3e3

Instead a single number, we are going to roll dice. There are some changes needed

1. Random numbers are now 1 ... 6
2. Each getSource section needed 2 random numbers
3. You need 2 new integers to get the sum of the random numbers.
     Change what it says in the JTextFields
4. the nested if elses now use the sums instead of a and b

Here are some pics of my sample - I did not use any colors, fonts or icons. We are going use this Lab again.