home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 23 / IOPROG_23.ISO / SOFT / INMAZE.ZIP / HungryRat.java < prev    next >
Encoding:
Java Source  |  1998-07-13  |  6.7 KB  |  308 lines

  1. /**
  2. *        @author Kaushik Raj
  3. *        @see    http://www.cis.syr.edu/~kraj/java/
  4. *    @email    kraj@syr.edu
  5. *    
  6. *        (c) copyright-98 Kaushik Raj
  7. *        All Rights Reserved.
  8. *
  9. *       This source code is free for all non-commercial purposes, as long as the name
  10. *       and the url address given above is written with the modified program. The author does not gives
  11. *       the permission to publish the source code in any book or any other publishing media.
  12. *
  13. *
  14. *       The author doesnot takes any responsibility for the software/hardware damages
  15. *       that may occur due to this program.
  16. */
  17.  
  18.  
  19.  
  20. /**
  21. *    File : HungryRat.java
  22. *    Last update : 10 July,1998
  23. */
  24.  
  25.  
  26. import java.applet.*;
  27. import java.awt.*;
  28.  
  29. /**
  30. *    HungryRat class manages the control of the rat over the board.
  31. */
  32. public class HungryRat implements Runnable{
  33.     /** present position of the rat */
  34.     private int ratX,ratY;
  35.  
  36.     /** previous position of the rat */
  37.     private int oldx,oldy;
  38.  
  39.     /** thread to have rat independent */
  40.     private Thread ratThread=null;
  41.  
  42.     /** counter to keep check if any click has been done otherwise random movement */
  43.     private int click=0;
  44.  
  45.     /** conuter keeping track of rat's energy */
  46.     private int foodclick=100;
  47.  
  48.     /** how long rat before reacting randomely */
  49.     private final int RatReactionTime=3;
  50.  
  51.     /** how many blocks the mouse can smell */
  52.     private final int noOfSmellBlocks;
  53.  
  54.     /** number of blocks the rat can move random */
  55.     private final int noOfBlocks=1;
  56.  
  57.     private MazeCanvas mc;
  58.     private Maze mz;
  59.     private MazeGame mg;
  60.  
  61.     private int foodAt[][];
  62.  
  63.     /** boolean to know whether the game is still going on */
  64.     private boolean gameOver=false;
  65.  
  66.     /**
  67.     *    constructor
  68.     */
  69.     public HungryRat(int ratX,int ratY,MazeCanvas mc,Maze mz,int noOfSmellBlocks,MazeGame mg){
  70.         this.ratX=ratX;
  71.         this.ratY=ratY;
  72.         oldx=ratX;
  73.         oldy=ratY;
  74.         this.mc=mc;
  75.         this.mz=mz;
  76.         this.mg=mg;
  77.         this.noOfSmellBlocks=noOfSmellBlocks;
  78.         foodclick=100;
  79.         foodAt=new int[mz.getCols()][mz.getRows()];
  80.         for(int i=0;i<mz.getCols();i++)
  81.             for (int j=0;j<mz.getRows();j++) foodAt[i][j]=0;
  82.     }
  83.  
  84.     /**
  85.     *    moves the rat given by the position x,y
  86.     */
  87.     synchronized public void moveRatAt(int x,int y){
  88.         if ((x>=mz.getCols())||(y>=mz.getRows())) return;
  89.         oldx=ratX;
  90.         oldy=ratY;
  91.         ratX=x;
  92.         ratY=y;
  93.         removeFoodFrom(new Point(x,y));
  94.         click=0;
  95.     }
  96.  
  97.     /**
  98.     *    returns the Point of the rat at that movement
  99.     */
  100.     public Point getRatPosition(){
  101.         return (new Point(ratX,ratY));
  102.     }
  103.  
  104.     /** returnsd the older position of the rat */
  105.     public Point getOldPosition(){
  106.         return (new Point(oldx,oldy));
  107.     }
  108.  
  109.     /**
  110.     *    returns if the point given by x,y is reachable by the rat or not
  111.     */
  112.     public boolean isReachable(int x,int y){
  113.         return mz.isReachable(new Point(ratX,ratY),new Point(x,y),noOfBlocks);
  114.     }
  115.  
  116.     /**
  117.     *    runs 
  118.     */
  119.     public void run(){
  120.         while(!gameOver){
  121.             click++;
  122.             sleep(1000);
  123.  
  124.             if(anyFoodNearBy(getRatPosition(),noOfSmellBlocks).x!=-1){
  125.                 Point p=new Point(anyFoodNearBy(getRatPosition(),noOfSmellBlocks).x,anyFoodNearBy(getRatPosition(),noOfSmellBlocks).y);
  126.                 moveRatAt(p.x,p.y);
  127.                 increaseEnergy();
  128.                 mg.updateGraphics();
  129.                 click=0;
  130.             }
  131.  
  132.             /* wait for three seconds before moving randomly */
  133.             if (click>=RatReactionTime){
  134.                 click=0;
  135.                 decreaseEnergy();
  136.  
  137.                 while(true){
  138.                         int i=(int) (Math.random()*10%2);
  139.                         int j=(int) (Math.random()*10%2);
  140.  
  141.                         if (randomBoolean()) i*=-1;
  142.                         if (randomBoolean()) j*=-1;
  143.  
  144.                         if (((i!=0)||(j!=0))&&(isReachable(getRatPosition().x+i,getRatPosition().y+j))){
  145.                             moveRatAt(getRatPosition().x+i,getRatPosition().y+j);
  146.                             mg.updateGraphics();
  147.                             break;
  148.                         }
  149.  
  150.                 }//:~while
  151.             }
  152.  
  153.             /** if no more food left game lost */
  154.             if (foodclick<=0) {
  155.                     mg.gameLost();
  156.                     gameOver=true;
  157.                     break;
  158.  
  159.             }
  160.  
  161.  
  162.         }//:while
  163.     }//:~run()
  164.  
  165.     /**
  166.     *    associates a thread to the rat
  167.     */
  168.     public void init(){
  169.         gameOver=false;
  170.         if (ratThread==null){
  171.             ratThread=new Thread(this);
  172.             ratThread.start();
  173.             System.out.println("ratThread started");
  174.         }
  175.  
  176.     }
  177.  
  178.     /**
  179.     *    stops the thread associated with the rat
  180.     */
  181.     public void stop(){
  182.         gameOver=true;
  183.         if (ratThread!=null){
  184.             ratThread.stop();
  185.             ratThread=null;
  186.             System.out.println("ratThread stop");
  187.         }
  188.     }//:~stop()
  189.  
  190.     /**
  191.     *    tries to sleep the thread
  192.     */
  193.     public void sleep(int ms){
  194.         try{
  195.             ratThread.sleep(ms);
  196.         }catch(Exception e){
  197.         }
  198.     }//:~sleep
  199.  
  200.     /**
  201.     *    returns a random boolean
  202.     */
  203.     public boolean randomBoolean(){
  204.         int i=(int) (Math.random()*10%2);
  205.  
  206.         if (i==1) return true;
  207.         else return false;
  208.     }
  209.  
  210.  
  211.     /**
  212.     *    checks whether the mouse position has been changed or not
  213.     */
  214.     public boolean hasChanged(){
  215.         boolean flag=false;
  216.         if (oldx!=ratX) flag=true;
  217.         if (oldy!=ratY) flag=true;
  218.  
  219.         return flag;
  220.     }
  221.  
  222.  
  223.     /**
  224.     *
  225.     *    keeps the food at the point 
  226.     */
  227.     public void putFoodAt(Point p){
  228.         foodAt[p.x][p.y]=1;
  229.     }
  230.  
  231.     /**
  232.     * returns false if there was no food at that point
  233.     *
  234.     */
  235.     public boolean removeFoodFrom(Point p){
  236.         if (foodAt[p.x][p.y]==0) return false;
  237.         foodAt[p.x][p.y]=0;
  238.         return true;
  239.     }
  240.     
  241.     /**
  242.     *    returns the nearest point that comes otherwise returns (-1,-1) if there is no
  243.     *    food available at the place.
  244.     */
  245.     public Point anyFoodNearBy(Point p,int noOfBlocks){
  246.         int minBlocksTaken=9999;
  247.         Point newp=new Point(-1,-1);
  248.  
  249.         if (foodAt[p.x][p.y]==1){
  250.             return p;
  251.         }
  252.  
  253.         //check in all the vicinity area if there is a food or not
  254.         //return the nearest point.
  255.         for (int i=-noOfBlocks;i<noOfBlocks+1;i++){
  256.             for (int j=-noOfBlocks;j<noOfBlocks+1;j++){
  257.                 //first check this area is within the range
  258.                 if ((p.x+i>=0)&&(p.y+j>=0)&&(p.x+i<mz.getCols())&&(p.y+j<mz.getRows())){ 
  259.                     //check if this place is reachable
  260.                     if (foodAt[p.x+i][p.y+j]==1)
  261.                     if (mz.isReachable(p,new Point(p.x+i,p.y+j),noOfBlocks)){
  262.                         //check now that this point is the smallest path
  263.                         int blocksTaken=mz.getPath(p,new Point(p.x+i,p.y+j));
  264.                         if (blocksTaken<minBlocksTaken) {
  265.                             minBlocksTaken=blocksTaken;
  266.                             newp=new Point(p.x+i,p.y+j);
  267.                         }
  268.                     }
  269.                 }
  270.             }
  271.         }
  272.  
  273.         return (newp);
  274.     }
  275.  
  276.     /** method to stop the game and rat thread */
  277.     public void gameOver(){
  278.         gameOver=true;
  279.         stop();
  280.     }
  281.  
  282.     /** increases the rat energy */
  283.     public void increaseEnergy(){
  284.         foodclick+=10;
  285.         if (foodclick>100) foodclick=100;
  286.     }
  287.  
  288.     /** decreasses the rat energu */
  289.     public void decreaseEnergy(){
  290.         foodclick-=10;
  291.     }
  292.  
  293.     /** returns the strenght of the rat */
  294.     public int getEnergy(){
  295.         return foodclick;
  296.     }
  297.  
  298.     /** returnsd the time left before rat is going to die */
  299.     public int getTimeLeft(){
  300.         return (int)((RatReactionTime-click)*100/RatReactionTime);
  301.     }
  302.  
  303.     /** returns the food matrix */
  304.     public int[][] getFoodPosition(){
  305.         return foodAt;
  306.     }
  307.  
  308. }//:~HungryRat