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.3 by jsr166, Mon Sep 27 19:15:15 2010 UTC vs.
Revision 1.15 by jsr166, Mon Aug 10 03:13:33 2015 UTC

# Line 1 | Line 1
1   /*
2   * Written by Doug Lea with assistance from members of JCP JSR-166
3   * Expert Group and released to the public domain, as explained at
4 < * http://creativecommons.org/licenses/publicdomain
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   */
6  
7   import java.awt.*;
8 import javax.swing.*;
9 import java.util.*;
8   import java.awt.event.*;
9 < import javax.swing.event.*;
9 > import java.util.*;
10   import java.util.concurrent.*;
11 <
11 > import javax.swing.*;
12 > import javax.swing.event.*;
13  
14   /**
15   * Microscope implements a version of the 7th Guest
# Line 19 | Line 18 | import java.util.concurrent.*;
18   * Microscope</a> version for instructions.
19   * <p>
20   * The code has been mangled beyond recognition
21 < * as a test of ForkJoin
22 < **/
24 <
21 > * as a test of ForkJoin.
22 > */
23   public class Microscope extends JPanel {
24  
25      static final CountDownLatch cd = new CountDownLatch(1);
# Line 116 | Line 114 | public class Microscope extends JPanel {
114      synchronized Player getPlayer() { return player; }
115      synchronized void setPlayer(Player p) { player = p; }
116  
119
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 177 | Line 174 | public class Microscope extends JPanel {
174                  public synchronized void actionPerformed(ActionEvent e) {
175                      toggleDemoMode();
176                      updateStatus();
180
177                  }});
178  
179          undoButton.addActionListener(new ActionListener() {
# Line 195 | Line 191 | public class Microscope extends JPanel {
191          //        scoreLabel.setMinimumSize(labDim);
192          //        scoreLabel.setPreferredSize(labDim);
193  
198
194          topPanel.add(autoButton);
195          topPanel.add(modeButton);
196          topPanel.add(undoButton);
197          topPanel.add(scoreLabel);
198          add(topPanel);
199  
205
200          levelSlider.setLabelTable(levelSlider.createStandardLabels(1));
201          levelSlider.setPaintLabels(true);
202  
# Line 230 | Line 224 | public class Microscope extends JPanel {
224              boardPanel.repaint();
225      }
226  
227 <    public void init()  {
227 >    public void init() {
228          initializeBoard();
229          if (autostart) {
230              startMover(auto);
231          }
232      }
233  
240
234      synchronized void setLevel(int l) {
235          lookAheads = l;
236          if (lookAheads <= 1) lookAheads = 2;
237      }
238  
239 <    public int level () { return Microscope.lookAheads; }
247 <
239 >    public int level() { return Microscope.lookAheads; }
240  
241      // process a move (called only from mover)
250
242      public void move(Move m, Mover mvr) {
243          if (mvr != mover ||
244              m == null ||
# Line 304 | Line 295 | public class Microscope extends JPanel {
295          }
296      }
297  
307
298      // handle Undo button
299      synchronized void undo() {
300          if (mover == null) {
# Line 349 | Line 339 | public class Microscope extends JPanel {
339  
340      }
341  
352
342      static final int CELL_SIZE = 40; // size of a tile/cell
343  
344      static final Color paleGreen = new Color(152, 251, 152);
# Line 357 | Line 346 | public class Microscope extends JPanel {
346  
347      static final Color possibleMoveColor = Color.yellow;
348  
360
349      public static Color displayColor(Player pl) {
350          if (pl.isBlue()) return Color.blue;
351          else if (pl.isGreen()) return darkGreen;
# Line 370 | Line 358 | public class Microscope extends JPanel {
358          else return Color.gray;
359      }
360  
373
361      class BoardPanel extends Canvas implements MouseListener {
362  
363          BoardPanel() {
# Line 438 | Line 425 | public class Microscope extends JPanel {
425  
426      /**
427       *  Player is just a glorified enumeration
428 <     **/
442 <
428 >     */
429      static final class Player {
430  
431          public static final int EMPTY = 0;
# Line 480 | Line 466 | public class Microscope extends JPanel {
466       * Boards are not immutable, but are never passed around across
467       * threads (instead new ones are constructed), so don't
468       * need any synch.
469 <     **/
470 <
485 <    static final class Board   {
469 >     */
470 >    static final class Board {
471  
472          /*
473             First, some Constants and utilities that might as well be here
# Line 538 | Line 523 | public class Microscope extends JPanel {
523  
524          }
525  
541
526          public static boolean inBounds(int row, int col) {
527              return (0 <= row)  && (row < RANKS) && (0 <= col) && (col < RANKS);
528          }
# Line 566 | Line 550 | public class Microscope extends JPanel {
550          long getBlue() { return blue_; }
551          long getGreen() { return green_; }
552  
569
553          public Player occupant(int row, int col) {
554              if ((0 <= row)  && (row < RANKS) && (0 <= col) && (col < RANKS)) {
555                  long m = 1L << (row + col * RANKS);
# Line 580 | Line 563 | public class Microscope extends JPanel {
563  
564  
565          // place a tile without taking opponent tiles
583
566          public void occupy(Player player, int row, int col) {
567              long m = 1L << (row + col * RANKS);
568              long nm = ~m;
569 <            if (player.code_ == Player.BLUE)  {
569 >            if (player.code_ == Player.BLUE) {
570                  blue_ |= m;
571                  green_ &= nm;
572              }
# Line 604 | Line 586 | public class Microscope extends JPanel {
586              green_ &= nm;
587          }
588  
607
608
589          // place a tile, taking all adjacent tiles of opponent
610
590          public void take(Player player, int row, int col) {
591 <            int k =  (row + col * RANKS);
591 >            int k = row + col * RANKS;
592              long dest = 1L << k;
593              long nbrMask = adjacentMasks[k];
594              long sourceBlue = blue_;
# Line 620 | Line 599 | public class Microscope extends JPanel {
599              }
600              else {
601                  blue_ = sourceBlue & ~(sourceBlue & nbrMask);
602 <                green_ =  sourceGreen | dest | (sourceBlue & nbrMask);
602 >                green_ = sourceGreen | dest | (sourceBlue & nbrMask);
603              }
604          }
605  
# Line 631 | Line 610 | public class Microscope extends JPanel {
610                  ((green_ & ~BLUEBIT) == 0);
611          }
612  
634
613          public int score(Player player) {
614              if (player.isBlue()) {
615                  return score(blue_, green_);
# Line 681 | Line 659 | public class Microscope extends JPanel {
659              return hb - ((lg + hg) & 0xff);
660          }
661  
684
685
662          static int slowscore(long b, long g) {
663              int score = 0;
664              for (int l = 0; l < CELLS; ++l) {
# Line 693 | Line 669 | public class Microscope extends JPanel {
669              }
670              return score;
671          }
696
697
672      }
673  
674      /**
675       * Moves represent transitions across Board states
676 <     **/
677 <
704 <
705 <    static final class Move  {
676 >     */
677 >    static final class Move {
678  
679          static final int NO_VALUE = -1;     // row/col value if not yet set
680          static final int PASS_VALUE = -2;   // special value for pass moves
# Line 756 | Line 728 | public class Microscope extends JPanel {
728  
729          // setters:
730  
731 <        synchronized void player(Player p)       { player_ = p;  }
732 <        synchronized void board(Board b)         { board_ = b;  }
733 <        synchronized void from(int sr, int sc)   { fromRow = sr; fromCol = sc;  }
731 >        synchronized void player(Player p)       { player_ = p; }
732 >        synchronized void board(Board b)         { board_ = b; }
733 >        synchronized void from(int sr, int sc)   { fromRow = sr; fromCol = sc; }
734          synchronized void to(int dr, int dc)     { toRow = dr;   toCol = dc; }
735  
736          //  accessors:
# Line 766 | Line 738 | public class Microscope extends JPanel {
738          synchronized boolean isFrom(int r, int c) {
739              return fromRow== r && fromCol == c;
740          }
741 <        synchronized boolean isTo(int r, int c)   {
741 >        synchronized boolean isTo(int r, int c) {
742              return toRow == r && toCol == c;
743          }
744          synchronized Board board() {
# Line 797 | Line 769 | public class Microscope extends JPanel {
769              return toRow != NO_VALUE && toCol != NO_VALUE;
770          }
771  
800
772          synchronized boolean possibleTo(int r, int c) { // is (r, c) a legal `to'?
773              return hasFrom() &&
774                  withinTwo(fromRow, r) &&
# Line 821 | Line 792 | public class Microscope extends JPanel {
792          synchronized void commit() { // update board to reflect move
793              if (!committed) {
794                  committed = true;
795 <                if (isLegal() && !isPass())  {
795 >                if (isLegal() && !isPass()) {
796                      if (isJump()) board_.occupy(Player.Empty, fromRow, fromCol);
797                      board_.take(player_, toRow, toCol);
798                  }
# Line 853 | Line 824 | public class Microscope extends JPanel {
824      }
825  
826      /**
827 <     *  User builds moves via instructions/clicks by users
828 <     **/
858 <
827 >     * User builds moves via instructions/clicks by users
828 >     */
829      static class User extends Mover {
830  
831          private Move current;
# Line 907 | Line 877 | public class Microscope extends JPanel {
877  
878      }
879  
910
880      /**
881 <     *     AutoMover constructs Finders that compute actual moves
882 <     **/
914 <
881 >     * AutoMover constructs Finders that compute actual moves
882 >     */
883      static class AutoMover extends Mover {
884  
885          boolean cancelled = false;
# Line 921 | Line 889 | public class Microscope extends JPanel {
889              super(ap);
890          }
891  
924
892          public synchronized boolean placing() {
893              return currentFinder != null;
894          }
# Line 930 | Line 897 | public class Microscope extends JPanel {
897              currentFinder = null;
898          }
899  
933
900          public synchronized void cancel() {
901 <            if (placing())  {
901 >            if (placing()) {
902                  currentFinder.cancel(false);
903                  stopPlacing();
904              }
# Line 955 | Line 921 | public class Microscope extends JPanel {
921  
922      }
923  
958
924      /**
925 <     * Implements a classic all-possible-move search algorith using
925 >     * Implements a classic all-possible-move search algorithm using
926       * ForkJoinTasks.  The move finder is not all that smart. Among
927       * other possible improvements, it could keep a cache of explored
928       * moves and avoid repeating them. This would likely speed it up
929       * since most expansions are duplicates of others. It could also
930       * be changed to prune moves, although this is unlikely to work
931       * well without better partial evaluation functions.
932 <     **/
968 <
932 >     */
933      static class Finder extends RecursiveAction {
934  
935          static final int NOMOVE = Integer.MIN_VALUE;
# Line 1007 | Line 971 | public class Microscope extends JPanel {
971                  search();
972          }
973  
1010
974          final void search() {
975              int best = NOMOVE;    // For direct evaluation when level == 1
976              Finder forked = null; // list of forked subtasks when level > 1
977  
978              long open = ~(ours | theirs);  // currently empty cells
979 <            long here = 1;                 // travserse through bits
979 >            long here = 1;                 // traverse through bits
980  
981              for (int k = 0; k < Board.CELLS; ++k, here <<= 1) {
982                  if ((here & ours) != 0) {
# Line 1070 | Line 1033 | public class Microscope extends JPanel {
1033  
1034          /**
1035           * Join all subtasks and evaluate moves. Default is sub-finder version.
1036 <         * Overridden in RootFinder
1037 <         **/
1075 <
1036 >         * Overridden in RootFinder.
1037 >         */
1038          void collect(Finder forked) {
1039              int best = NOMOVE;
1040              while (forked != null) {
# Line 1103 | Line 1065 | public class Microscope extends JPanel {
1065          }
1066  
1067          /**
1068 <         * Cancel all forked subtasks in list
1069 <         **/
1108 <
1068 >         * Cancels all forked subtasks in list.
1069 >         */
1070          void cancelAll(Finder forked) {
1071              while (forked != null) {
1072                  forked.cancel(false);
# Line 1117 | Line 1078 | public class Microscope extends JPanel {
1078  
1079      /**
1080       * Root Finder class -- wait out other finders and issue callback to game.
1081 <     **/
1121 <
1081 >     */
1082      static class RootFinder extends Finder {
1083          final AutoMover automover;
1084          final Player player;
1085          RootFinder(Board board, Player p, int level, AutoMover automover) {
1086 <            super( (p.isBlue()? (board.getBlue()| Board.BLUEBIT) : board.getGreen()),
1087 <                   (p.isBlue()? board.getGreen() : (board.getBlue()| Board.BLUEBIT)),
1086 >            super( (p.isBlue() ? (board.getBlue()| Board.BLUEBIT) : board.getGreen()),
1087 >                   (p.isBlue() ? board.getGreen() : (board.getBlue()| Board.BLUEBIT)),
1088                     level,
1089                     null);
1090  
# Line 1132 | Line 1092 | public class Microscope extends JPanel {
1092              this.automover = automover;
1093          }
1094  
1135
1095          /**
1096           * This differs from default version by recording
1097           * and calling back with best move
1098 <         **/
1098 >         */
1099          void collect(Finder forked) {
1100              int best = NOMOVE;
1101              Finder bestFinder = null;
# Line 1183 | Line 1142 | public class Microscope extends JPanel {
1142  
1143                  long nextOurs = bestFinder.theirs;
1144                  long nextTheirs = bestFinder.ours;
1145 <                long blue = (player.isBlue())? nextOurs : nextTheirs;
1146 <                long green = (player.isBlue())? nextTheirs: nextOurs;
1145 >                long blue = player.isBlue() ? nextOurs : nextTheirs;
1146 >                long green = player.isBlue() ? nextTheirs : nextOurs;
1147                  move = new Move(player, new Board(blue, green), true);
1148              }
1149              automover.relay(move);
1150          }
1151      }
1193
1194
1152   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines