ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/Microscope.java
(Generate patch)

Comparing jsr166/src/test/loops/Microscope.java (file contents):
Revision 1.12 by jsr166, Wed Dec 31 17:00:58 2014 UTC vs.
Revision 1.13 by jsr166, Thu Jan 15 18:34:19 2015 UTC

# Line 11 | Line 11 | import java.util.concurrent.*;
11   import javax.swing.*;
12   import javax.swing.event.*;
13  
14
14   /**
15   * Microscope implements a version of the 7th Guest
16   * game found looking in the Microscope in the laboratory.
# Line 115 | Line 114 | public class Microscope extends JPanel {
114      synchronized Player getPlayer() { return player; }
115      synchronized void setPlayer(Player p) { player = p; }
116  
118
117      final AutoMover auto;                  // The move finder.
118      final User user;                       // Mover for user moves
119      Mover mover = null;    // the current Mover (always == auto or user or null)
# Line 194 | Line 192 | public class Microscope extends JPanel {
192          //        scoreLabel.setMinimumSize(labDim);
193          //        scoreLabel.setPreferredSize(labDim);
194  
197
195          topPanel.add(autoButton);
196          topPanel.add(modeButton);
197          topPanel.add(undoButton);
198          topPanel.add(scoreLabel);
199          add(topPanel);
200  
204
201          levelSlider.setLabelTable(levelSlider.createStandardLabels(1));
202          levelSlider.setPaintLabels(true);
203  
# Line 236 | Line 232 | public class Microscope extends JPanel {
232          }
233      }
234  
239
235      synchronized void setLevel(int l) {
236          lookAheads = l;
237          if (lookAheads <= 1) lookAheads = 2;
# Line 244 | Line 239 | public class Microscope extends JPanel {
239  
240      public int level() { return Microscope.lookAheads; }
241  
247
242      // process a move (called only from mover)
249
243      public void move(Move m, Mover mvr) {
244          if (mvr != mover ||
245              m == null ||
# Line 303 | Line 296 | public class Microscope extends JPanel {
296          }
297      }
298  
306
299      // handle Undo button
300      synchronized void undo() {
301          if (mover == null) {
# Line 348 | Line 340 | public class Microscope extends JPanel {
340  
341      }
342  
351
343      static final int CELL_SIZE = 40; // size of a tile/cell
344  
345      static final Color paleGreen = new Color(152, 251, 152);
# Line 356 | Line 347 | public class Microscope extends JPanel {
347  
348      static final Color possibleMoveColor = Color.yellow;
349  
359
350      public static Color displayColor(Player pl) {
351          if (pl.isBlue()) return Color.blue;
352          else if (pl.isGreen()) return darkGreen;
# Line 369 | Line 359 | public class Microscope extends JPanel {
359          else return Color.gray;
360      }
361  
372
362      class BoardPanel extends Canvas implements MouseListener {
363  
364          BoardPanel() {
# Line 535 | Line 524 | public class Microscope extends JPanel {
524  
525          }
526  
538
527          public static boolean inBounds(int row, int col) {
528              return (0 <= row)  && (row < RANKS) && (0 <= col) && (col < RANKS);
529          }
# Line 563 | Line 551 | public class Microscope extends JPanel {
551          long getBlue() { return blue_; }
552          long getGreen() { return green_; }
553  
566
554          public Player occupant(int row, int col) {
555              if ((0 <= row)  && (row < RANKS) && (0 <= col) && (col < RANKS)) {
556                  long m = 1L << (row + col * RANKS);
# Line 577 | Line 564 | public class Microscope extends JPanel {
564  
565  
566          // place a tile without taking opponent tiles
580
567          public void occupy(Player player, int row, int col) {
568              long m = 1L << (row + col * RANKS);
569              long nm = ~m;
# Line 601 | Line 587 | public class Microscope extends JPanel {
587              green_ &= nm;
588          }
589  
604
605
590          // place a tile, taking all adjacent tiles of opponent
607
591          public void take(Player player, int row, int col) {
592              int k = row + col * RANKS;
593              long dest = 1L << k;
# Line 628 | Line 611 | public class Microscope extends JPanel {
611                  ((green_ & ~BLUEBIT) == 0);
612          }
613  
631
614          public int score(Player player) {
615              if (player.isBlue()) {
616                  return score(blue_, green_);
# Line 678 | Line 660 | public class Microscope extends JPanel {
660              return hb - ((lg + hg) & 0xff);
661          }
662  
681
682
663          static int slowscore(long b, long g) {
664              int score = 0;
665              for (int l = 0; l < CELLS; ++l) {
# Line 690 | Line 670 | public class Microscope extends JPanel {
670              }
671              return score;
672          }
693
694
673      }
674  
675      /**
# Line 792 | Line 770 | public class Microscope extends JPanel {
770              return toRow != NO_VALUE && toCol != NO_VALUE;
771          }
772  
795
773          synchronized boolean possibleTo(int r, int c) { // is (r, c) a legal `to'?
774              return hasFrom() &&
775                  withinTwo(fromRow, r) &&
# Line 901 | Line 878 | public class Microscope extends JPanel {
878  
879      }
880  
904
881      /**
882       * AutoMover constructs Finders that compute actual moves
883       */
# Line 914 | Line 890 | public class Microscope extends JPanel {
890              super(ap);
891          }
892  
917
893          public synchronized boolean placing() {
894              return currentFinder != null;
895          }
# Line 923 | Line 898 | public class Microscope extends JPanel {
898              currentFinder = null;
899          }
900  
926
901          public synchronized void cancel() {
902              if (placing()) {
903                  currentFinder.cancel(false);
# Line 948 | Line 922 | public class Microscope extends JPanel {
922  
923      }
924  
951
925      /**
926       * Implements a classic all-possible-move search algorith using
927       * ForkJoinTasks.  The move finder is not all that smart. Among
# Line 999 | Line 972 | public class Microscope extends JPanel {
972                  search();
973          }
974  
1002
975          final void search() {
976              int best = NOMOVE;    // For direct evaluation when level == 1
977              Finder forked = null; // list of forked subtasks when level > 1
# Line 1121 | Line 1093 | public class Microscope extends JPanel {
1093              this.automover = automover;
1094          }
1095  
1124
1096          /**
1097           * This differs from default version by recording
1098           * and calling back with best move
# Line 1179 | Line 1150 | public class Microscope extends JPanel {
1150              automover.relay(move);
1151          }
1152      }
1182
1183
1153   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines