home *** CD-ROM | disk | FTP | other *** search
/ Internet Gallery / INTERGAL.bin / intergal / prgs / idv21 / data.z / Sokoban.java < prev    next >
Text File  |  1996-05-20  |  7KB  |  239 lines

  1. // Sokoban written by Lars Gersmannn May'96
  2.  
  3. import java.awt.*;
  4. import java.applet.Applet;
  5. import java.net.*;
  6. import java.io.*;
  7. import ImageTracker;
  8.  
  9. public class Sokoban extends Applet implements Runnable {
  10.                 Image         img          = null;
  11.              Thread        thread       = null;
  12.     volatile boolean        pleasestop   = false;    
  13.     final    int           SLEEP        = 100;
  14.               ImageTracker it            = null;
  15.  
  16.     public  final int       PLANE_X       = 22, 
  17.                           PLANE_Y       = 22,
  18.                           LEN           = 25;                 // Len = x,y of sprite
  19.             char           PlayPlane[]  = new char[PLANE_X*PLANE_Y], 
  20.                           OrgPlane[]   = new char[PLANE_X*PLANE_Y],
  21.                           LevelPlane[] = new char[PLANE_X*PLANE_Y];
  22.             
  23.             int           x            = 0, 
  24.                           y            = 0;                    // player position
  25.                boolean       complete        = false;                // true, if game completed
  26.     
  27.             int           PlayerSprite = 0;                
  28.     final   int           PLAYER_LEFT  = 0, 
  29.                           PLAYER_UP    = 1, 
  30.                           PLAYER_RIGHT = 2, 
  31.                           PLAYER_DOWN  = 3, 
  32.                           FLOOR           = 4, 
  33.                           WALL           = 5, 
  34.                           GOAL           = 6, 
  35.                           STONE           = 7;
  36.  
  37.     final  String           LEVEL_FILE   = "Level/SCREEN.";            // startup level = LEVEL_FILE + "1";    
  38.            int            Level        = 1;
  39.     final  int               MAX_LEVEL       = 10;                // count of existing levels        
  40.  
  41.     Choice LevelChoice=null;
  42.  
  43.     public Sokoban() {
  44.         super();
  45.         img = createImage( PLANE_X*LEN, PLANE_Y*LEN);
  46.     }
  47.     
  48.     public void init() {
  49.         it = new ImageTracker( this, "img");
  50.         
  51.         readLevel( LEVEL_FILE + Level );
  52.         resetLevel();
  53.  
  54.         setLayout(new BorderLayout());
  55.  
  56.         Panel p = new Panel();
  57.         p.setLayout(new FlowLayout());
  58.         p.add( new Button(" Reset ") );
  59.     
  60.         LevelChoice = new Choice();
  61.         for(int i=1; i<MAX_LEVEL; i++)
  62.             LevelChoice.addItem(" Level " + i + " ");
  63.         p.add( LevelChoice);
  64.         add( "South", p );
  65.     }
  66.  
  67.     public void paint(Graphics g) {
  68.         g.drawImage( img, 0, 0, this );
  69.     }    
  70.     
  71.     public boolean handleEvent(Event evt) {
  72.         if(evt.id == Event.KEY_ACTION) {          // Cursor keys movement
  73.             switch (evt.key) {
  74.                       case Event.UP:    PlayerSprite=PLAYER_UP; movePlayer(0,-1);
  75.                                       break;
  76.                       case Event.DOWN:  PlayerSprite=PLAYER_DOWN; movePlayer(0,1);
  77.                                       break;
  78.                       case Event.RIGHT: PlayerSprite=PLAYER_RIGHT; movePlayer(1,0);
  79.                                       break;
  80.                       case Event.LEFT:  PlayerSprite=PLAYER_LEFT; movePlayer(-1,0);
  81.                                       break;
  82.             }
  83.             return true;
  84.         }
  85.         if(evt.id == Event.ACTION_EVENT) {
  86.              if(evt.arg==" Reset ") resetLevel();
  87.             
  88.             if(evt.target instanceof Choice) {
  89.                 Level=LevelChoice.getSelectedIndex()+1;
  90.                 readLevel(LEVEL_FILE + Level);
  91.                 resetLevel();
  92.             }
  93.                 
  94.             return true;
  95.         }
  96.                  
  97.         return super.handleEvent( evt );
  98.     }
  99.  
  100.     public void start() {
  101.         thread = new Thread(this);
  102.         thread.start();
  103.     }
  104.     
  105.     public void run() {
  106.         while(true) {
  107.             getGraphics().drawImage( img, 0, 0, this);
  108.             
  109.             try    { thread.sleep(SLEEP); } catch(InterruptedException e) {};
  110.         }
  111.     }
  112.  
  113.     final int pos( int px, int py)
  114.     {
  115.         return py*PLANE_X+px;
  116.     }
  117.  
  118.     public void movePlayer(int relx, int rely) 
  119.     {
  120.         if(PlayPlane[pos(x+relx,y+rely)]!='#') {// if there is no wall ...
  121.             
  122.             Graphics g = img.getGraphics(); 
  123.             
  124.             if(g==null) return;
  125.  
  126.             if( PlayPlane[pos(x+relx,y+rely)]!='$' ) {             // ... and no stone:
  127.                 PlayPlane[pos(x,y)]=OrgPlane[pos(x,y)];        
  128.                 g.drawImage( it.getImage( OrgPlane[pos(x,y)]=='.' ? GOAL : FLOOR), x*LEN, y*LEN, this);
  129.                 PlayPlane[pos(x+=relx,y+=rely)]='@';            // move player to new position
  130.                 g.drawImage( it.getImage(PlayerSprite), x*LEN, y*LEN, this);
  131.             }
  132.             else                                                 // ... else if there a stone:
  133.                 if( PlayPlane[pos(x+2*relx,y+2*rely)]!='#'         
  134.                    && PlayPlane[pos(x+2*relx,y+2*rely)]!='$' )    //     if there is no wall or other stone
  135.                 {
  136.                     PlayPlane[pos(x,y)]=OrgPlane[pos(x,y)];        // 
  137.                     g.drawImage( it.getImage( OrgPlane[pos(x,y)]=='.' ? GOAL : FLOOR), x*LEN, y*LEN, this);
  138.                     
  139.                     PlayPlane[pos(x+=relx,y+=rely)]='@';        // move player to new position                    
  140.                     g.drawImage( it.getImage(PlayerSprite), x*LEN, y*LEN, this);
  141.                     
  142.                     PlayPlane[pos(x+relx,y+rely)]='$';            // move stone to new psoition    
  143.                     g.drawImage( it.getImage(STONE), (x+relx)*LEN, (y+rely)*LEN, this);
  144.                     int i = PLANE_X*PLANE_Y;                     // game completed ?
  145.                     complete=true;
  146.                     while(--i>=0) 
  147.                         if( OrgPlane[i]=='.' && PlayPlane[i]!='$' )    complete=false;
  148.                     
  149.                     if(complete) {
  150.                         if(Level+1 < MAX_LEVEL) {     // read next level
  151.                             LevelChoice.select(Level);
  152.                             readLevel(LEVEL_FILE + (++Level));
  153.                         }
  154.                          resetLevel();                        
  155.                         complete=false;
  156.                     }
  157.                  }
  158.         }
  159.     }
  160.  
  161.     /**    reads level from file LevelPlane_File
  162.         to LevelPlane
  163.     */
  164.  
  165.     void readLevel( String LevelPlane_File ) {
  166.         String s = null;
  167.         int    iy = 0;
  168.         showStatus("Loading next Level ...");
  169.         try { 
  170.             DataInputStream in = new DataInputStream( new URL( getDocumentBase(), 
  171.                                                                LevelPlane_File ).openStream() );
  172.             while( (s=in.readLine()) != null ) {
  173.                 s.getChars( 0, s.length(), LevelPlane, pos(0,iy));
  174.                 LevelPlane[pos(s.length(),iy++)]='\n';
  175.             }
  176.             in.close();
  177.         }                                        // let rest of LevelPlane start with '\n'
  178.         catch( IOException e ) { 
  179.             showStatus("An IOException occured us by loading levelfile \""  
  180.                        + LevelPlane_File + "\". Please reload Page."); }
  181.         
  182.         showStatus("");
  183.         while(iy<PLANE_Y) LevelPlane[pos(0,iy++)]='\n';
  184.     }
  185.     
  186.     /**    reset current level
  187.     */
  188.  
  189.     void resetLevel() {
  190.         if(img==null) img = createImage( PLANE_Y*LEN, PLANE_Y*LEN );
  191.         Graphics g = img.getGraphics();
  192.  
  193.         for( int y=0; y<PLANE_Y; y++) {
  194.             NextLine:
  195.             for( int x=0; x<PLANE_X; x++) {
  196.                 int SpriteId=-1;
  197.                  switch ( LevelPlane[ pos( x, y) ] ) {
  198.                       case ' ':    if(SpriteId==-1) SpriteId=FLOOR; 
  199.                     case '.':    if(SpriteId==-1) SpriteId=GOAL;
  200.                     case '#':    if(SpriteId==-1) SpriteId=WALL;
  201.                     case '$':    if(SpriteId==-1) SpriteId=STONE;
  202.                     case '@':   if( (PlayPlane[pos(x,y)]=LevelPlane[pos(x,y)]) =='@') {
  203.                                      g.drawImage( it.getImage(PLAYER_DOWN), x*LEN, y*LEN, this);
  204.                                     this.x = x;
  205.                                     this.y = y;
  206.                                     break;
  207.                                 }
  208.                                 g.drawImage( it.getImage(SpriteId), x*LEN, y*LEN, this);
  209.                                 break;
  210.           
  211.                     case '\r':  x--; break;                     // ignore carriage return
  212.                 
  213.                     case '\n':     while(x<PLANE_X) {                // clear rest of line
  214.                                     PlayPlane[pos(x,y)]=' ';     
  215.                                     g.drawImage( it.getImage(FLOOR), LEN*x++, LEN*y, this);
  216.                                 }
  217.                                 break NextLine;                    // goto NextLine
  218.                   }
  219.             }
  220.         }
  221.         
  222.         System.arraycopy( LevelPlane, 0, PlayPlane, 0, PLANE_X*PLANE_Y);
  223.          System.arraycopy( LevelPlane, 0, OrgPlane,  0, PLANE_X*PLANE_Y);
  224.         OrgPlane[pos(this.x,this.y)]=' ';            // delete player from OrgPlane
  225.  
  226.         int r = PLANE_X*PLANE_Y;
  227.         while(--r>=0) 
  228.             if(OrgPlane[r]=='$') OrgPlane[r]=' ';         // delete all stones from OrgPlane
  229.     }
  230.  
  231.     public Dimension minimumSize() {
  232.         return new Dimension( PLANE_X*LEN, PLANE_Y*LEN );
  233.     }
  234.  
  235.     public Dimension preferredSize() {
  236.         return minimumSize();
  237.     }
  238. }
  239.