Advertisement
lewapkon

wator.java

Mar 27th, 2014
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.52 KB | None | 0 0
  1. import java.applet.*;
  2. import java.awt.*;
  3. import java.util.*;
  4.  
  5. /* WATOR predator-prey simulation based on A.K. Dewdney "Sharks and fish
  6.    wage an ecological war on the toroidal planet Wa-Tor" (Scientific
  7.    American, December 1984).  Source code recovered from my 1996 compiled
  8.    applet with help from JDK 1.2 disassembler.  This version compiled
  9.    with JDK 1.1.7A and exactly matches original's disassembler output.
  10.    Placed in the public domain.  This product is supplied "as is."
  11.    Lawrence Leinweber, Cleveland, Ohio, U.S.A. 1999. */
  12.  
  13. public class wator extends Applet implements Runnable {
  14.  
  15. Label lb_size;
  16. Label lb_nshark;
  17. Label lb_nfish;
  18. Label lb_sbreed;
  19. Label lb_fbreed;
  20. Label lb_starve;
  21. Scrollbar sb_size;
  22. Scrollbar sb_nshark;
  23. Scrollbar sb_nfish;
  24. Scrollbar sb_sbreed;
  25. Scrollbar sb_fbreed;
  26. Scrollbar sb_starve;
  27. Button bn_go;
  28. Button bn_stop;
  29. Button bn_remix;
  30. Panel pn_tp;
  31. Panel pn_sc;
  32. Panel pn_bn;
  33. panelmap pn_mp;
  34. panelgrf pn_gf;
  35. int ni;
  36. int nj;
  37. int nshark;
  38. int nfish;
  39. boolean goflag;
  40. boolean remixflag;
  41. boolean brkflag;
  42. Object mutex;
  43. private Thread thread;
  44.  
  45. public void start()
  46. {
  47.     synchronized (mutex) {
  48.         if (thread != null) return;
  49.         thread = new Thread(this);
  50.         thread.setPriority(Thread.MIN_PRIORITY);
  51.         thread.start();
  52.     }
  53. }
  54.  
  55. public void stop()
  56. {
  57.     synchronized (mutex) {
  58.         if (thread != null && thread.isAlive()) thread.stop();
  59.         thread = null;
  60.     }
  61. }
  62.  
  63. public void run()
  64. {
  65.     int i, j, s, f;
  66.  
  67.     while (remixflag || pn_mp.paintflag || brkflag || goflag) {
  68.         if (remixflag) {
  69.             synchronized (mutex)
  70.                 { i = ni; j = nj; s = nshark; f = nfish; }
  71.             if (fish.nshark != s || fish.nfish != f)
  72.                 fish.make(s, f);
  73.             pn_mp.remix(i, j);
  74.             pn_mp.paintflag = true;
  75.             pn_gf.brk();
  76.             remixflag = false;
  77.         }
  78.         if (pn_mp.paintflag)
  79.             { pn_mp.paintflag = false; pn_mp.dopaint(); }
  80.         if (brkflag)
  81.             { brkflag = false; pn_gf.brk(); }
  82.         if (goflag) {
  83.             pn_mp.gen();
  84.             pn_gf.gen(ni * nj, fish.nshark, fish.nfish);
  85.             synchronized (mutex) {
  86.                 if (!remixflag) {
  87.                     setnshark(fish.nshark);
  88.                     setnfish(fish.nfish);
  89.                 }
  90.             }
  91.         }
  92.     }
  93.     thread = null;
  94. }
  95.  
  96. public boolean handleEvent(Event e)
  97. {
  98.     if (e.target instanceof Button)
  99.         switch (e.id) {
  100.         case e.ACTION_EVENT:
  101.             if (e.target == bn_go) goflag = true;
  102.             if (e.target == bn_stop) goflag = false;
  103.             if (e.target == bn_remix) remixflag = true;
  104.             if (goflag || remixflag) start();
  105.             return (true);
  106.         }
  107.     else if (e.target instanceof Scrollbar)
  108.         switch (e.id) {
  109.         case e.SCROLL_ABSOLUTE:
  110.         case e.SCROLL_LINE_UP:
  111.         case e.SCROLL_LINE_DOWN:
  112.         case e.SCROLL_PAGE_UP:
  113.         case e.SCROLL_PAGE_DOWN:
  114.             synchronized (mutex) {
  115.             if (e.target == sb_size)
  116.                 remixflag = setsize(sb_size.getValue());
  117.             if (e.target == sb_nshark)
  118.                 remixflag = setnshark(sb_nshark.getValue());
  119.             if (e.target == sb_nfish)
  120.                 remixflag = setnfish(sb_nfish.getValue());
  121.             if (e.target == sb_sbreed)
  122.                 brkflag = setsbreed(sb_sbreed.getValue());
  123.             if (e.target == sb_fbreed)
  124.                 brkflag = setfbreed(sb_fbreed.getValue());
  125.             if (e.target == sb_starve)
  126.                 brkflag = setstarve(sb_starve.getValue());
  127.             }
  128.             if (remixflag) start();
  129.             return (true);
  130.         }
  131.     return (super.handleEvent(e));
  132. }
  133.  
  134. public void init()
  135. {
  136.     super.init();
  137.     pn_mp = new panelmap();
  138.     pn_mp.that = this;
  139.     setLayout(new GridLayout(2, 1, 10, 10));
  140.     pn_tp = new Panel();
  141.     pn_tp.setLayout(new BorderLayout(3, 0));
  142.     add(pn_tp);
  143.     pn_tp.setBackground(Color.white);
  144.     pn_sc = new Panel();
  145.     pn_sc.setLayout(new GridLayout(14, 1, 3, 3));
  146.     pn_tp.add("East", pn_sc);
  147.     pn_sc.add(lb_size = new Label());
  148.     lb_size.setForeground(pn_mp.getColor(Color.blue));
  149.     pn_sc.add(sb_size = new Scrollbar(0));
  150.     pn_sc.add(lb_nshark = new Label());
  151.     lb_nshark.setForeground(pn_mp.getColor(Color.red));
  152.     pn_sc.add(sb_nshark = new Scrollbar(0));
  153.     pn_sc.add(lb_nfish = new Label());
  154.     lb_nfish.setForeground(pn_mp.getColor(Color.green));
  155.     pn_sc.add(sb_nfish = new Scrollbar(0));
  156.     pn_sc.add(lb_sbreed = new Label());
  157.     lb_sbreed.setForeground(pn_mp.getColor(Color.red));
  158.     pn_sc.add(sb_sbreed = new Scrollbar(0));
  159.     pn_sc.add(lb_fbreed = new Label());
  160.     lb_fbreed.setForeground(pn_mp.getColor(Color.green));
  161.     pn_sc.add(sb_fbreed = new Scrollbar(0));
  162.     pn_sc.add(lb_starve = new Label());
  163.     lb_starve.setForeground(pn_mp.getColor(Color.red));
  164.     pn_sc.add(sb_starve = new Scrollbar(0));
  165.     pn_sc.add(new Label());
  166.     pn_bn = new Panel();
  167.     pn_bn.setLayout(new GridLayout(1, 3, 3, 0));
  168.     pn_sc.add(pn_bn);
  169.     pn_bn.add(bn_go = new Button("Go"));
  170.     pn_bn.add(bn_stop = new Button("Stop"));
  171.     pn_bn.add(bn_remix = new Button("Remix"));
  172.     pn_tp.add("Center", pn_mp);
  173.     pn_gf = new panelgrf();
  174.     add(pn_gf);
  175.     validate();
  176.     pn_gf.init(pn_mp.getColor(Color.red), pn_mp.getColor(Color.green),
  177.             pn_mp.getColor(Color.blue));
  178.     nshark = nfish = 0;
  179.     setsize(1);
  180.     while (ni * nj < 1000) setsize(Math.min(ni, nj) + 1);
  181.     setnshark(1);
  182.     setnfish(ni * nj - 1);
  183.     setsbreed(10);
  184.     setfbreed(3);
  185.     setstarve(3);
  186.     fish.seedrnd();
  187.     remixflag = true;
  188.     goflag = true;
  189.     mutex = new Object();
  190.     start();
  191. }
  192.  
  193. private boolean setsize(int n)
  194. {
  195.     int x, y;
  196.  
  197.     x = pn_mp.size().width - 2;
  198.     y = pn_mp.size().height - 2;
  199.     if (n < 1) n = 1;
  200.     if (x < y) { nj = n; ni = n * y / x; }
  201.     else { ni = n; nj = n * x / y; }
  202.     sb_size.setValues(n, Math.min(x, y) / 10, 1, Math.min(x, y));
  203.     lb_size.setText("Size " + Integer.toString(ni * nj) +
  204.             " [1-" + Integer.toString(x * y) + "]");
  205.     if (nfish + nshark > ni * nj) {
  206.         setnfish(ni * nj * nfish / (nfish + nshark));
  207.         setnshark(ni * nj - nfish);
  208.     }
  209.     return (true);
  210. }
  211.  
  212. private boolean setnshark(int n)
  213. {
  214.     sb_nshark.setValues(n, ni * nj / 10, 0, ni * nj);
  215.     lb_nshark.setText("Sharks " + Integer.toString(n) +
  216.             " [0-" + Integer.toString(ni * nj) + "]");
  217.     nshark = n;
  218.     if (nfish + nshark > ni * nj) setnfish(ni * nj - nshark);
  219.     return (true);
  220. }
  221.  
  222. private boolean setnfish(int n)
  223. {
  224.     sb_nfish.setValues(n, ni * nj / 10, 0, ni * nj);
  225.     lb_nfish.setText("Fish " + Integer.toString(n) +
  226.             " [0-" + Integer.toString(ni * nj) + "]");
  227.     nfish = n;
  228.     if (nshark + nfish > ni * nj) setnshark(ni * nj - nfish);
  229.     return (true);
  230. }
  231.  
  232. private boolean setsbreed(int n)
  233. {
  234.     sb_sbreed.setValues(n, 10, 1, 100);
  235.     lb_sbreed.setText("Shark Breed " + Integer.toString(n) + " [1-100]");
  236.     fish.sbreed = n;
  237.     return (true);
  238. }
  239.  
  240. private boolean setfbreed(int n)
  241. {
  242.     sb_fbreed.setValues(n, 10, 1, 100);
  243.     lb_fbreed.setText("Fish Breed " + Integer.toString(n) + " [1-100]");
  244.     fish.fbreed = n;
  245.     return (true);
  246. }
  247.  
  248. private boolean setstarve(int n)
  249. {
  250.     sb_starve.setValues(n, 10, 1, 100);
  251.     lb_starve.setText("Shark Starve " + Integer.toString(n) + " [1-100]");
  252.     fish.sstarve = n;
  253.     return (true);
  254. }
  255.  
  256. } /* end of class */
  257.  
  258. class panelmap extends Panel {
  259.  
  260. Color cl_rd;
  261. Color cl_gn;
  262. Color cl_bl;
  263. fish map[][];
  264. int x[];
  265. int y[];
  266. int dx[];
  267. int dy[];
  268. public int ni;
  269. public int nj;
  270. public boolean paintflag;
  271. Applet that;
  272.  
  273. public Color getColor(Color c)
  274. {
  275.     if (c == Color.red) return (cl_rd);
  276.     if (c == Color.green) return (cl_gn);
  277.     return (cl_bl);
  278. }
  279.  
  280. public panelmap()
  281. {
  282.     cl_rd = new Color(128, 0, 0);
  283.     cl_gn = new Color(0, 64, 0);
  284.     cl_bl = new Color(128, 128, 255);
  285.     setBackground(cl_bl);
  286. }
  287.  
  288. public void remix(int newi, int newj)
  289. {
  290.     fish f;
  291.     int i, j, k;
  292.  
  293.     ni = newi;
  294.     nj = newj;
  295.     x = new int[nj];
  296.     y = new int[ni];
  297.     dx = new int[nj];
  298.     dy = new int[ni];
  299.     k = size().width - 2;
  300.     for (i = 0; i < nj; i++)
  301.         dx[i] = ((k * i + k) / nj + 1) - (x[i] = (k * i) / nj + 1);
  302.     k = size().height - 2;
  303.     for (i = 0; i < ni; i++)
  304.         dy[i] = ((k * i + k) / ni + 1) - (y[i] = (k * i) / ni + 1);
  305.     map = new fish[ni][nj];
  306.     for (f = fish.first(); f != null; f = f.next()) {
  307.         do {
  308.             i = fish.nrnd(ni);
  309.             j = fish.nrnd(nj);
  310.         } while (map[i][j] != null);
  311.         map[f.i = i][f.j = j] = f;
  312.     }
  313. }
  314.  
  315. public void gen()
  316. {
  317.     fish f, p;
  318.     int r, i, j, en, pn;
  319.     int[] ei, ej, pi, pj;
  320.     Graphics g12;
  321.  
  322.     g12 = getGraphics();
  323.     ei = new int[9];
  324.     ej = new int[9];
  325.     pi = new int[9];
  326.     pj = new int[9];
  327.     for (f = fish.first(); f != null; f = f.next()) {
  328.         en = pn = 0;
  329.         i = f.i; j = f.j;
  330.         if (--i < 0) i += ni;
  331.         if ((p = map[i][j]) == null) { ei[en] = i; ej[en] = j; en++; }
  332.         else if (p.starve < 0) { pi[pn] = i; pj[pn] = j; pn++; }
  333.         i = f.i; j = f.j;
  334.         if (--j < 0) j += nj;
  335.         if ((p = map[i][j]) == null) { ei[en] = i; ej[en] = j; en++; }
  336.         else if (p.starve < 0) { pi[pn] = i; pj[pn] = j; pn++; }
  337.         i = f.i; j = f.j;
  338.         if (++i >= ni) i -= ni;
  339.         if ((p = map[i][j]) == null) { ei[en] = i; ej[en] = j; en++; }
  340.         else if (p.starve < 0) { pi[pn] = i; pj[pn] = j; pn++; }
  341.         i = f.i; j = f.j;
  342.         if (++j >= nj) j -= nj;
  343.         if ((p = map[i][j]) == null) { ei[en] = i; ej[en] = j; en++; }
  344.         else if (p.starve < 0) { pi[pn] = i; pj[pn] = j; pn++; }
  345.         if (f.starve > 0 && pn > 0) {   /* eat */
  346.             r = fish.nrnd(pn);
  347.             i = pi[r]; j = pj[r];
  348.             map[i][j].starve = 0;
  349.             map[i][j] = null;
  350.             fish.nfish--;
  351.             f.starve = fish.sstarve;
  352.         }
  353.         else if (f.starve > 0 && --f.starve == 0) { /* starve */
  354.             fish.nshark--;
  355.             map[f.i][f.j] = null;
  356.             if (!paintflag) {
  357.                 g12.setColor(cl_bl);
  358.                 g12.fillRect(x[f.j], y[f.i], dx[f.j], dy[f.i]);
  359.             }
  360.             continue;
  361.         }
  362.         else if (en > 0) { /* move */
  363.             r = fish.nrnd(en);
  364.             i = ei[r]; j = ej[r];
  365.         }
  366.         else {  /* surrounded */
  367.             if (f.breed > 1) f.breed--;
  368.             continue;
  369.         }
  370.         if (--f.breed == 0) { /* breed */
  371.             p = new fish(f.starve < 0);
  372.             map[p.i = f.i][p.j = f.j] = p;
  373.             if (f.starve < 0) fish.nfish++; else fish.nshark++;
  374.             f.breed = f.starve < 0? fish.fbreed: fish.sbreed;
  375.         }
  376.         else { /* move from */
  377.             map[f.i][f.j] = null;
  378.             if (!paintflag) {
  379.                 g12.setColor(cl_bl);
  380.                 g12.fillRect(x[f.j], y[f.i], dx[f.j], dy[f.i]);
  381.             }
  382.         }
  383.         map[f.i = i][f.j = j] = f; /* move to */
  384.         if (!paintflag) {
  385.             g12.setColor(f.starve < 0? cl_gn: cl_rd);
  386.             g12.fillRect(x[f.j], y[f.i], dx[f.j], dy[f.i]);
  387.         }
  388.     }
  389. }
  390.  
  391. public void paint(Graphics g)
  392. {
  393.     paintflag = true;
  394.     that.start();
  395. }
  396.  
  397. public void dopaint()
  398. {
  399.     fish f;
  400.     Image i;
  401.     Graphics g;
  402.  
  403.     i = createImage(size().width, size().height);
  404.     g = i.getGraphics();
  405.     g.setColor(cl_bl);
  406.     g.fillRect(0, 0, size().width, size().height);
  407.     g.setColor(Color.black);
  408.     g.drawRect(0, 0, size().width - 1, size().height - 1);
  409.     for (f = fish.first(); f != null; f = f.next()) {
  410.         g.setColor(f.starve < 0? cl_gn: cl_rd);
  411.         g.fillRect(x[f.j], y[f.i], dx[f.j], dy[f.i]);
  412.     }
  413.     getGraphics().drawImage(i, 0, 0, this);
  414. }
  415.  
  416. } /* end of class */
  417.  
  418. class panelgrf extends Panel {
  419.  
  420. int x;
  421. int y;
  422. int pos;
  423. Color cr;
  424. Color cg;
  425. Color cb;
  426. boolean cont;
  427. int stab[];
  428. int ftab[];
  429. Color ctab[];
  430.  
  431. public void init(Color r, Color g, Color b)
  432. {
  433.     int i;
  434.  
  435.     cr = r;
  436.     cg = g;
  437.     cb = b;
  438.     setBackground(cb);
  439.     x = size().width - 2;
  440.     y = size().height - 2;
  441.     stab = new int[x];
  442.     ftab = new int[x];
  443.     ctab = new Color[x];
  444.     for (i = 0; i < x; i++) {
  445.         stab[i] = ftab[i] = -1;
  446.         ctab[i] = cb;
  447.     }
  448.     ctab[0] = Color.black;
  449. }
  450.  
  451. public void brk()
  452. {
  453.     Graphics g;
  454.     int i;
  455.  
  456.     if (!cont) return;
  457.     cont = false;
  458.     synchronized (g = getGraphics()) {
  459.         if ((i = pos + 1) >= x) i = 0;
  460.         ctab[i] = Color.black;
  461.         stab[i] = ftab[i] = -1;
  462.         drawgen(g, i);
  463.         ctab[pos] = Color.white;
  464.         stab[i] = ftab[i] = -1;
  465.         drawgen(g, pos);
  466.         pos = i;
  467.     }
  468. }
  469.  
  470. public void gen(int n, int s, int f)
  471. {
  472.     Graphics g;
  473.     int i;
  474.  
  475.     cont = true;
  476.     synchronized (g = getGraphics()) {
  477.         if ((i = pos + 1) >= x) i = 0;
  478.         ctab[i] = Color.black;
  479.         stab[i] = ftab[i] = -1;
  480.         drawgen(g, i);
  481.         stab[pos] = (n - s) * (y - 1) / n + 1;
  482.         ftab[pos] = (n - f) * (y - 1) / n + 1;
  483.         ctab[pos] = cb;
  484.         drawgen(g, pos);
  485.         pos = i;
  486.     }
  487. }
  488.  
  489. public void paint(Graphics g)
  490. {
  491.     int i;
  492.  
  493.     synchronized (g) {
  494.         g.setColor(Color.black);
  495.         g.drawRect(0, 0, size().width - 1, size().height - 1);
  496.         for (i = 0; i < x; i++) drawgen(g, i);
  497.     }
  498. }
  499.  
  500. public void drawgen(Graphics g, int i)
  501. {
  502.     int i0, n0, n;
  503.  
  504.     g.setColor(ctab[i]);
  505.     g.drawLine(i + 1, 1, i + 1, y);
  506.     i0 = i == 0? x - 1: i - 1;
  507.     if ((n = stab[i]) != -1) {
  508.         n0 = stab[i0];
  509.         if (n0 == -1) n0 = n;
  510.         if (n0 < n) n0++;
  511.         if (n0 > n) n0--;
  512.         g.setColor(cr);
  513.         g.drawLine(i + 1, n0, i + 1, n);
  514.     }
  515.     if ((n = ftab[i]) != -1) {
  516.         n0 = ftab[i0];
  517.         if (n0 == -1) n0 = n;
  518.         if (n0 < n) n0++;
  519.         if (n0 > n) n0--;
  520.         g.setColor(cg);
  521.         g.drawLine(i + 1, n0, i + 1, n);
  522.     }
  523. }
  524.  
  525. } /* end of class */
  526.  
  527. class fish extends Object {
  528.  
  529. public int i;
  530. public int j;
  531. public int breed;
  532. public int starve;
  533. private fish f_next;
  534. private static fish fish_hed;
  535. static int nshark;
  536. static int nfish;
  537. static int sbreed;
  538. static int fbreed;
  539. static int sstarve;
  540. private static int rnd;
  541.  
  542. public fish(boolean isfish)
  543. {
  544.     f_next = fish_hed;
  545.     fish_hed = this;
  546.     i = isfish? fbreed: sbreed;
  547.     breed = nrnd(isfish? fbreed: sbreed) + nrnd(isfish? fbreed: sbreed) + 1;
  548.     starve = isfish? -1: nrnd(sstarve) + nrnd(sstarve) + 1;
  549. }
  550.  
  551. public static fish first()
  552. {
  553.     while (fish_hed != null && fish_hed.starve == 0)
  554.         fish_hed = fish_hed.f_next;
  555.     return (fish_hed);
  556. }
  557.  
  558. public fish next()
  559. {
  560.     while (f_next != null && f_next.starve == 0)
  561.         f_next = f_next.f_next;
  562.     return (f_next);
  563. }
  564.  
  565. public static void make(int newnshark, int newnfish)
  566. {
  567.     int i, j;
  568.     fish f;
  569.  
  570.     while ((f = fish_hed) != null) {
  571.         fish_hed = f.f_next;
  572.         f.f_next = null;
  573.     }
  574.     nshark = newnshark;
  575.     nfish = newnfish;
  576.     for (i = 0, j = (nfish - nshark) / 2; i < nshark + nfish; i++) {
  577.         if (j > 0) j -= nshark;
  578.         else j += nfish;
  579.         new fish(j > 0);
  580.     }
  581. }
  582.  
  583. public static void seedrnd()
  584. {
  585.     rnd = new Random().nextInt() >> 16 & 077777;
  586. }
  587.  
  588. public static int nrnd(int n)
  589. {
  590.     if (n <= 1) return (0);
  591.     return (((rnd = rnd * 1103515245 + 12345) >> 16 & 077777) * n >> 15);
  592. }
  593.  
  594. } /* end of class */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement