Arithmetic24.java
上传用户:njled888
上传日期:2007-01-07
资源大小:29k
文件大小:11k
源码类别:

游戏

开发平台:

Java

  1. /*
  2.  * @(#)Arithmetic24.java Version 1.0 98/03/12
  3.  * 
  4.  * Copyright (c) 1998 by Huahai Yang
  5.  * 
  6.  * Use at your own risk. I do not guarantee the fitness of this 
  7.  * software for any purpose, and I am not responsible for 
  8.  * any damage you do to yourself or others by using this software.
  9.  * This file may be distributed freely, provided its contents 
  10.  * are not tampered with in any way.
  11.  *
  12.  */
  13.    
  14. import java.awt.*;
  15. import java.applet.*;
  16. import java.awt.event.*;
  17. import java.util.*;
  18. /** 
  19.  * Main class of Arithmetic24 applet. Draws basic interface and 
  20.  * coordinates among playing board, clock and score keeper.
  21.  * @see        DraggingArea
  22.  * @see        Clock
  23.  * @see        ScoreKeeper
  24.  * @version    1.0
  25.  * @author     Huahai Yang
  26.  */
  27. public class Arithmetic24 extends Applet
  28.                           implements ActionListener, 
  29.                                      ItemListener,
  30.                                      Observer
  31. {
  32.    Button noSolutionButton,
  33.        doneButton;
  34. Label  feedBackLabel;
  35. Clock  clock;
  36. Choice difficultyLevel;
  37. ScoreKeeper score;
  38. DraggingArea playingBoard;
  39. // time limit
  40. final int BEGINNER_TIME = 120,
  41.           INTERMEDIATE_TIME = 90,
  42.           EXPERT_TIME = 60;
  43.           
  44. SoundList soundList;
  45.    String correctSound = "well_done.au",
  46.           wrongSound = "missed.au",
  47.           badExpSound = "Hah.au",
  48.           timeOutSound = "bell.au",
  49.           fillSlotSound = "click2.au",
  50.           clickDeckSound = "click1.au";
  51.              
  52.    /**
  53.     * Sets up basic layout of applet and prepare sound files
  54.     */
  55.    public void init()
  56. {
  57.    setSize(600, 400);
  58.     playingBoard = new DraggingArea();
  59.     Panel panel1 = new Panel(),
  60.           panel2 = new Panel();
  61.    
  62.     difficultyLevel = new Choice();
  63.     difficultyLevel.addItem("Beginner");
  64.     difficultyLevel.addItem("Intermediate");
  65.     difficultyLevel.addItem("Expert");
  66.     difficultyLevel.addItemListener(this);
  67.     difficultyLevel.setEnabled(true);
  68.     panel1.add(difficultyLevel);
  69.    
  70.     clock = new Clock(BEGINNER_TIME);
  71.     clock.setActionCommand("timeout");
  72.     clock.addActionListener(this);
  73.     panel1.add(clock);
  74.                                
  75.     feedBackLabel = new Label("Goal: compute 24. Click deck to start.");
  76.     feedBackLabel.setFont(new Font("Helvetica", Font.BOLD, 16));
  77.    feedBackLabel.setForeground(Color.blue);
  78.    panel1.add(feedBackLabel);    
  79.    
  80.     score = new ScoreKeeper();
  81.    panel1.add(score);    
  82.    
  83.     GridBagLayout gridbag = new GridBagLayout();
  84.       GridBagConstraints c = new GridBagConstraints();
  85.       setLayout(gridbag);
  86.    c.fill = GridBagConstraints.BOTH;
  87.    c.gridwidth = GridBagConstraints.REMAINDER;
  88.     gridbag.setConstraints(panel1, c);
  89.     add(panel1);
  90.    
  91.     c.gridwidth = GridBagConstraints.REMAINDER;
  92.     c.gridheight = 4;
  93.     gridbag.setConstraints(playingBoard, c);
  94.     add(playingBoard);
  95.    
  96.     panel2.setLayout(gridbag);
  97.     c.gridwidth = GridBagConstraints.RELATIVE;
  98.    c.ipadx = 15;
  99.    c.ipady = 10;
  100.     c.insets = new Insets (5, 20, 5, 20); 
  101.    
  102.     noSolutionButton = new Button();
  103.    noSolutionButton.setActionCommand("no solution");
  104.    noSolutionButton.setLabel("No Solutions.");
  105.    noSolutionButton.setFont(new Font("Dialog", Font.PLAIN, 14));
  106.    noSolutionButton.setForeground(new Color(255));
  107.    noSolutionButton.addActionListener(this);
  108.       gridbag.setConstraints(noSolutionButton, c);
  109.    panel2.add(noSolutionButton);
  110.   
  111.    doneButton = new Button();
  112.    doneButton.setActionCommand("done");
  113.    doneButton.setLabel("I got a solution!");
  114.    doneButton.setFont(new Font("Dialog", Font.PLAIN, 14));
  115.    doneButton.setForeground(new Color(16711935));
  116.    doneButton.addActionListener(this);
  117.    gridbag.setConstraints(doneButton, c);
  118.    panel2.add(doneButton);
  119.    
  120.    c.gridheight = GridBagConstraints.RELATIVE;
  121.    gridbag.setConstraints(panel2, c);
  122.    add(panel2);
  123.    
  124.    startLoadingSounds();
  125. } //init
  126.    
  127.    /**
  128.     * Start asynchronous sound loading.
  129.     */
  130.    void startLoadingSounds() 
  131.    {
  132.       soundList = new SoundList(this, getCodeBase());
  133.       soundList.startLoading(clickDeckSound);
  134.       soundList.startLoading(fillSlotSound);
  135.       soundList.startLoading(timeOutSound);
  136.       soundList.startLoading(badExpSound);
  137.       soundList.startLoading(correctSound);
  138.       soundList.startLoading(wrongSound);
  139.    } //startLoadingSounds
  140.    
  141.    /**
  142.     * Starts applet by starting dragging thread
  143.     * @see DraggingArea
  144.     */
  145.    public void start()
  146.    {
  147.       playingBoard.start();
  148.    } // start
  149.    
  150.    /**
  151.     * Stop applet by stopping dragging thread
  152.     * @see DraggingArea
  153.     */
  154. public void stop()
  155. {
  156.    clock.stop();
  157.    playingBoard.stop();
  158.    } // stop   
  159. /**
  160.  * Called before retrieve the solution.  In the case that
  161.  * the solution is not available yet, progarm will wait here,
  162.  * so enhance the solution thread's priority to minimize the 
  163.  * waiting time
  164.  */
  165. private void beginWaitSolution()
  166. {
  167.     feedBackLabel.setText("I am thinking..."); 
  168.     playingBoard.cardDeck.setThreadPriority(Thread.NORM_PRIORITY);
  169.    } //beginWaitSolution
  170.    
  171.    /**
  172.     * reset solution thread's priority
  173.     */
  174.    private void endWaitSolution()
  175.    {
  176.       playingBoard.cardDeck.setThreadPriority(Thread.MIN_PRIORITY);
  177.    } //endWaitSolution  
  178.    
  179.    /**
  180.     * Handles playing status changes
  181.     * @see PlayingStatus
  182.     * @see DraggingArea
  183.     */
  184.    public void update(Observable observable, Object status)
  185.    {
  186.       if(observable instanceof PlayingStatus)
  187.       {
  188.          switch( ((Integer)status).intValue() )
  189.          {
  190.             case PlayingStatus.DEALED:
  191.                clock.start();
  192.                noSolutionButton.setEnabled(true);
  193.                doneButton.setEnabled(true);
  194.                difficultyLevel.setEnabled(false);
  195.                feedBackLabel.setText("Drag cards and operaters.");
  196.                break;
  197.             case PlayingStatus.WAITING:
  198.                noSolutionButton.setEnabled(false);
  199.                doneButton.setEnabled(false);
  200.                difficultyLevel.setEnabled(true);
  201.                break;
  202.             case PlayingStatus.ROUND_OVER:
  203.                //TO DO: record user score
  204.                playingBoard.stop();
  205.                feedBackLabel.setText("You got " + score.getScore() +
  206.                   " points. Lets play again.");
  207.                score.resetScore();
  208.                playingBoard.start();
  209.                noSolutionButton.setEnabled(false);
  210.                doneButton.setEnabled(false);
  211.                break;
  212.          } // switch
  213.       } // if
  214.    } // update for observer   
  215.    
  216.    /**
  217.     * Handles action events fired by buttons and clock
  218.     * @see Clock
  219.     */
  220.    public void actionPerformed(ActionEvent e)
  221.    {
  222.       double timePassed = (double)clock.getTime() / clock.getTimeLimit();
  223.       String command = e.getActionCommand();
  224.       
  225.       if(command == "no solution")
  226.       {
  227.          beginWaitSolution(); 
  228.          if( playingBoard.currentSolution() == null )
  229.          {
  230.             score.updateScore(ScoreKeeper.NO_NO, timePassed);
  231.             soundList.playClip(correctSound);
  232.             feedBackLabel.setText("Correct.");
  233.          } // if no solution
  234.          else
  235.          {
  236.             soundList.playClip(wrongSound);
  237.             feedBackLabel.setText("Let me show you a solution.");
  238.             score.updateScore(ScoreKeeper.HAS_NO, timePassed);
  239.             playingBoard.beginAnimation();
  240.          } // else if has solution 
  241.          endWaitSolution();
  242.       } // if press "no solution"
  243.       else if(command == "done")
  244.       {
  245.          Expression userDid;
  246.          double value;         
  247.          
  248.          if(playingBoard.isFullExpression())
  249.          {
  250.              userDid= new 
  251.                Expression(playingBoard.userCreatedExpression());
  252.          } // if
  253.          else 
  254.          {
  255.             soundList.playClip(badExpSound);
  256.             feedBackLabel.setText("Bad expression, try again.");
  257.             return;
  258.          } // else
  259.          
  260.          try
  261.          {
  262.             value = userDid.getValue();
  263.          } // try
  264.          catch(IllegalExpressionException exception)
  265.          {
  266.             soundList.playClip(badExpSound);
  267.             feedBackLabel.setText("Bad expression, try again."); 
  268.             return;
  269.          } //catch  
  270.       
  271.          if( value == 24.0 )
  272.          {
  273.             soundList.playClip(correctSound);
  274.             feedBackLabel.setText("Right!");   
  275.             score.updateScore(ScoreKeeper.HAS_RIGHT, timePassed);
  276.          } // if equals 24
  277.          else
  278.          {
  279.             beginWaitSolution(); 
  280.             soundList.playClip(wrongSound);
  281.             if( playingBoard.currentSolution() == null )
  282.             {
  283.                feedBackLabel.setText("No solution, you are wrong.");  
  284.                score.updateScore(ScoreKeeper.NO_HAS, timePassed);
  285.             } // if no solution
  286.             else 
  287.             {
  288.                feedBackLabel.setText("Wrong, here is a correct solution.");  
  289.                score.updateScore(ScoreKeeper.HAS_WRONG, timePassed);
  290.                playingBoard.beginAnimation();
  291.             } // else has solution
  292.             endWaitSolution();
  293.          } // else not equals 24
  294.       } // else if press "done"
  295.       else
  296.       {
  297.          soundList.playClip(timeOutSound);
  298.          score.updateScore(ScoreKeeper.TIME_OUT, 1);
  299.          if( playingBoard.currentSolution() != null )
  300.          {
  301.             feedBackLabel.setText("Time out, here is a solution.");
  302.             playingBoard.beginAnimation();
  303.          } //if   
  304.          else
  305.          {
  306.             feedBackLabel.setText("Time out, there is no solution.");
  307.          } //else   
  308.       } // else timeout   
  309.       
  310.       clock.stop();
  311.       playingBoard.cardDeck.enableClick();
  312.       playingBoard.setStatus(PlayingStatus.WAITING);
  313.       
  314.    } // actionPerformed   
  315.    
  316.    /**
  317.     * Handles difficultyLevel choice event
  318.     */
  319.    public void itemStateChanged(ItemEvent e) 
  320.    {
  321.       String select = new String(difficultyLevel.getSelectedItem());
  322.       if(select.equals("Beginner")) 
  323.       {
  324.          clock.setTimeLimit( BEGINNER_TIME );
  325.          score.setLevelWeight( ScoreKeeper.BEGINNER );
  326.       } // if   
  327.       else if( select.equals("Intermediate") )
  328.       {
  329.          score.setLevelWeight( ScoreKeeper.INTERMEDIATE );
  330.          clock.setTimeLimit( INTERMEDIATE_TIME );
  331.       } // else if
  332.       else 
  333.       {
  334.          score.setLevelWeight( ScoreKeeper.EXPERT );
  335.          clock.setTimeLimit( EXPERT_TIME );
  336.       } // else
  337.       
  338.    } // itemStateChanged   
  339.             
  340. } //Arithmetic24