Mon 13 Oct 08

This week we will work on the Jeopardy program.  

Mr Mulkey will be absent Wed-Fri.  If you get stuck and need help, you should talk to another student, or send an e-mail to :  Dave_Mulkey@fis.edu .  You won't get an immediate answer, but certainly within a couple hours.  

Follow the instructions in the assignment.  Bring a working program on Monday, 20 Oct to be graded.  You may share ideas with other students, but don't copy code directly.

img2.gif

This program demonstrates the use of EasyApp to create a GUI interface. (download EasyApp)
The program is a simple version of the game Jeopardy, where the user answers questions
and wins money for correct answers and loses money for incorrect answers.
After each question, the button is disabled, so that question cannot be chosen again. 


import java.awt.*;

public class Jeopardy extends EasyApp
{
   public static void main(String[] args)
   {  new Jeopardy(); }
   
   Label lJeopardy = addLabel("Jeopardy",60,30,200,60,this);
   
   Label lScore = addLabel("Score",300,50,50,30,this);
   TextField tScore = addTextField("0",350,50,80,30,this);

   Button bScience = addButton("Science",50,100,100,50,this);
   Button bSports = addButton("Sports",150,100,100,50,this);
   Button bHistory = addButton("History",250,100,100,50,this);
   Button bMath = addButton("Math",350,100,100,50,this);
   
   Button bSports2 = addButton("Sports 200",150,150,100,50,this);
   
   Button bPlayAgain = addButton("Play again",50,250,400,50,this);
   
   double score = 0;

   public Jeopardy()   // Constructor - change window appearance
   {
      setSize(500,500);
      setTitle("Jeopardy - (c) 2005 Dave Mulkey, Germany");
      lJeopardy.setFont(new Font("Arial",1,36));
      lJeopardy.setBackground(new Color(255,255,180));
      lJeopardy.setForeground(Color.blue);
      lScore.setBackground(new Color(255,255,180));
      setBackground(new Color(255,255,180));
      bScience.setFont(new Font("Arial",1,16));
      bMath.setFont(new Font("Arial",1,16));      
      bHistory.setFont(new Font("Arial",1,16));      
      bSports.setFont(new Font("Arial",1,16));  
      bSports2.setFont(new Font("Arial",1,16));
      bPlayAgain.setFont(new Font("Arial",1,16));      
      
   }
   
   public void actions(Object source,String command)
   {
      if (source == bScience)
      {   science(); }
      if (source == bSports)
      {  sports();   }
      if (source == bHistory)
      {  history();  }
      if (source == bMath)
      {  math();  }
      if (source == bSports2)
      {  sports2(); }
      if (source == bPlayAgain)
      {  
         bScience.setEnabled(true);
         bHistory.setEnabled(true);
         bSports.setEnabled(true);
         bMath.setEnabled(true);
         score = 0;
      }   
      tScore.setText(score + "");
   }
   
   public void science()
   {
      String guess = inputString("f = ma is a ________ law");
      if (guess.equals("physics"))
      {
         score = score + 100;
         output("Right!");  
      }
      else
      {  
         score = score - 100;         
         output("Wrong..." );
      }      

      bScience.setEnabled(false);
   }
   
   public void sports()
   {
      String guess = inputString("What sport did Pele play?");
      if (
          guess.equalsIgnoreCase("soccer")
          || guess.equalsIgnoreCase("football")
          || guess.equalsIgnoreCase("fussball")
         )   // || or
      {
         score = score + 100;
         output("Right!");  
      }

      else
      {  
         score = score - 100;         
         output("Wrong...");
      }      
      bSports.setEnabled(false);
   }

   public void history()
   {
      int guess = inputInt("What year did WW II end?");
      if (guess == 1945)
      {
         score = score + 100;
         output("Right!");  
      }
      else
      {  
         score = score - 100;         
         output("Wrong... ");
      }      
      bHistory.setEnabled(false);
   }
   
   public void math()
   {
      int guess = inputInt("What is 5! ?");
      if (guess == 120)
      {
         score = score + 100;
         output("Right!");  
      }
      else
      {  
         score = score - 100;         
         output("Wrong...");
      }      
      bMath.setEnabled(false);
   }
   
   public void sports2()
   {
      int guess = inputInt("What is a perfect score in bowling?");
      if ( guess == 300 )
      {
         score = score + 200;
         output("Right!");  
      }
      else
      {  
         score = score - 200;         
         output("Wrong... " );
      }      
      bSports2.setEnabled(false);
   }

}

 

/***********************************************************************

(1) Add buttons for at least 3 questions in each category (100, 200, 300) -
that makes a total of 12 questions. Make sure each button is disabled after use,
and that it adds or subtracts the correct amount.  

Improve the ANSWER algorithm(s):

(2) Change the IF commands so they accept both CAPITAL and small letters. 

(3) Add a HINT for each question by telling:
   (a) the FIRST LETTER of the answer and
   (b) the number of letters in the answer

(4) Use some STRING commands to:
   (a) make the scoring NON-case-sensitive
   (b) accept an answer as long as the first 3 letters and the last
       letter are correct - thus, it would ignore some spelling mistakes.
   (c) Allow the person to guess again if their answer is PART OF
       (contained in) the correct answer.

Make a better game:

  1. Change the game to be played by 2 players.  
    This means there must be a variable to keep track of which player is answering,
    and that must switch automatically between the two players after each question.
    There must also be 2 variables for scores (scoreA and scoreB),
    as well as two TextField boxes for displaying the scores.
     
  2. Then change the program so that if a player gets a question correct,
    they get another turn.  They should keep playing until they give an incorrect answer.
     
  3. Add a "poison" question.  This doesn't ask a question - instead, the player
    automatically loses money - and loses their turn.
     
  4. Add a "Daily Double" question, where the player can "bet" as much money as they wish,
    up to the amount they currently have.  So if the player has 500, they can be 500 or less.
    They must bet before they see the question.  Then they lose or win the amount they bet -
    not the amount showing on the button.
     
  5. LATER : Choose random questions from a group of 3 questions in each category.
    Do this the easy way, and don't try to stop repeat questions - e.g. the same question
    might be asked twice if the player is lucky.  Something like this:
        int rand = (int)(Math.random()*3);
        if (rand == 0)
        {  String ans = input("How many eggs in a dozen?");
           if (ans == "12")
           { output("right");
             money = money + 100;
           }
           else
           {
             output("wrong");
             money = money - 100;
           }
        }
        if (rand == 1)
        { ..... }
        if (rand == 2)
        { ..... }
     
  6. Add anything else you can think of to make the best program you can.
     

*********************************************************************/