home *** CD-ROM | disk | FTP | other *** search
/ PDA Software Library / pdasoftwarelib.iso / PILOT / PC / JUMP / LIFE / LIFE.JAV < prev    next >
Encoding:
Text File  |  1996-12-27  |  3.9 KB  |  150 lines

  1. import palmos.*;
  2.  
  3. class Life {
  4.   static final int idfMain  = 1000;
  5.   static final int idfAbout = 1100;
  6.  
  7.   static final int idcStep  = 1010;
  8.   static final int idcRun   = 1011;
  9.   static final int idcStop  = 1012;
  10.   static final int idcAbout = 1019;
  11.  
  12.   static final int XSIZE = 20;
  13.   static final int YSIZE = 18;
  14.   static final int CELLSIZE = 8;
  15.  
  16.   byte Boards[][][];
  17.   int dispboard;
  18.   byte dragstate;
  19.   boolean running;
  20.  
  21.   Rectangle r = new Rectangle();
  22.  
  23.   public static int PilotMain(int cmd, int cmdBPB, int launchFlags)
  24.   {
  25.     if (cmd == 0) {
  26.       new Life().run();
  27.     }
  28.     return 0;
  29.   }
  30.  
  31.   public void run()
  32.   {
  33.     Boards = new byte[2][][];
  34.     Boards[0] = new byte[YSIZE+2][];
  35.     Boards[1] = new byte[YSIZE+2][];
  36.     for (int i = 0; i < 2; i++) {
  37.       for (int y = 1; y <= YSIZE; y++) {
  38.         Boards[i][y] = new byte[XSIZE+2];
  39.       }
  40.     }
  41.     dispboard = 0;
  42.     running = false;
  43.  
  44.     Palm.FrmGotoForm(idfMain);
  45.     Event e = new Event();
  46.     Short err = new Short();
  47.     while (e.eType != Event.appStopEvent) {
  48.       Palm.EvtGetEvent(e, running ? 10 : -1);
  49.       if (!Palm.SysHandleEvent(e)) {
  50.         if (!Palm.MenuHandleEvent(0, e, err)) {
  51.           if (!appHandleEvent(e)) {
  52.             Palm.FrmHandleEvent(Palm.FrmGetActiveForm(), e);
  53.           }
  54.         }
  55.       }
  56.     }
  57.   }
  58.  
  59.   boolean appHandleEvent(Event e)
  60.   {
  61.     if (e.eType == Event.frmLoadEvent) {
  62.       int form = Palm.FrmInitForm(e.formID());
  63.       Palm.FrmSetActiveForm(form);
  64.       return true;
  65.     } else if (e.eType == Event.frmOpenEvent) {
  66.       Palm.FrmDrawForm(Palm.FrmGetActiveForm());
  67.       return true;
  68.     } else if (e.eType == Event.penDownEvent || e.eType == Event.penMoveEvent) {
  69.       int x = e.screenX / CELLSIZE + 1;
  70.       int y = e.screenY / CELLSIZE + 1;
  71.       if (x >= 1 && x <= XSIZE && y > 0 && y <= YSIZE) {
  72.         if (e.eType == Event.penDownEvent) {
  73.           dragstate = (byte)(Boards[dispboard][y][x] ^ 1);
  74.         }
  75.         Boards[dispboard][y][x] = dragstate;
  76.         drawCell(x, y, dragstate);
  77.         return true;
  78.       }
  79.     } else if (e.eType == Event.ctlSelectEvent) {
  80.       if (e.controlID() == idcStep) {
  81.         step();
  82.         return true;
  83.       } else if (e.controlID() == idcRun) {
  84.         step();
  85.         running = true;
  86.         return true;
  87.       } else if (e.controlID() == idcStop) {
  88.         running = false;
  89.         return true;
  90.       } else if (e.controlID() == idcAbout) {
  91.         Palm.FrmAlert(idfAbout);
  92.       }
  93.     } else if (running && e.eType == Event.nilEvent) {
  94.       step();
  95.       return true;
  96.     }
  97.     return false;
  98.   }
  99.  
  100.   void step()
  101.   {
  102.     int newboard = dispboard ^ 1;
  103.     int changes = 0;
  104.     for (int y = 1; y <= YSIZE; y++) {
  105.       for (int x = 1; x <= XSIZE; x++) {
  106.         int n = neighbors(Boards[dispboard], x, y);
  107.         byte state;
  108.         if (Boards[dispboard][y][x] != 0) {
  109.           state = (byte)(n == 2 || n == 3 ? 1 : 0);
  110.           if (state == 0) {
  111.             drawCell(x, y, (byte)0);
  112.             changes++;
  113.           }
  114.         } else {
  115.           state = (byte)(n == 3 ? 1 : 0);
  116.           if (state != 0) {
  117.             drawCell(x, y, (byte)1);
  118.             changes++;
  119.           }
  120.         }
  121.         Boards[newboard][y][x] = state;
  122.       }
  123.     }
  124.     dispboard = newboard;
  125.     if (changes == 0) {
  126.       running = false;
  127.     }
  128.   }
  129.  
  130.   int neighbors(byte[][] board, int x, int y)
  131.   {
  132.     return board[y-1][x-1] + board[y-1][x] + board[y-1][x+1]
  133.          + board[y  ][x-1] +                 board[y  ][x+1]
  134.          + board[y+1][x-1] + board[y+1][x] + board[y+1][x+1];
  135.   }
  136.  
  137.   void drawCell(int x, int y, byte on)
  138.   {
  139.     r.topLeft_x = (short)((x-1)*CELLSIZE);
  140.     r.topLeft_y = (short)((y-1)*CELLSIZE);
  141.     r.extent_x = (short)(CELLSIZE);
  142.     r.extent_y = (short)(CELLSIZE);
  143.     if (on != 0) {
  144.       Palm.WinDrawRectangle(r, CELLSIZE/2);
  145.     } else {
  146.       Palm.WinEraseRectangle(r, CELLSIZE/2);
  147.     }
  148.   }
  149. }
  150.