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.13 by jsr166, Thu Jan 15 18:34:19 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 195 | Line 192 | public class Microscope extends JPanel {
192          //        scoreLabel.setMinimumSize(labDim);
193          //        scoreLabel.setPreferredSize(labDim);
194  
198
195          topPanel.add(autoButton);
196          topPanel.add(modeButton);
197          topPanel.add(undoButton);
198          topPanel.add(scoreLabel);
199          add(topPanel);
200  
205
201          levelSlider.setLabelTable(levelSlider.createStandardLabels(1));
202          levelSlider.setPaintLabels(true);
203  
# Line 230 | Line 225 | public class Microscope extends JPanel {
225              boardPanel.repaint();
226      }
227  
228 <    public void init()  {
228 >    public void init() {
229          initializeBoard();
230          if (autostart) {
231              startMover(auto);
232          }
233      }
234  
240
235      synchronized void setLevel(int l) {
236          lookAheads = l;
237          if (lookAheads <= 1) lookAheads = 2;
238      }
239  
240 <    public int level () { return Microscope.lookAheads; }
247 <
240 >    public int level() { return Microscope.lookAheads; }
241  
242      // process a move (called only from mover)
250
243      public void move(Move m, Mover mvr) {
244          if (mvr != mover ||
245              m == null ||
# Line 304 | Line 296 | public class Microscope extends JPanel {
296          }
297      }
298  
307
299      // handle Undo button
300      synchronized void undo() {
301          if (mover == null) {
# Line 349 | Line 340 | public class Microscope extends JPanel {
340  
341      }
342  
352
343      static final int CELL_SIZE = 40; // size of a tile/cell
344  
345      static final Color paleGreen = new Color(152, 251, 152);
# Line 357 | Line 347 | public class Microscope extends JPanel {
347  
348      static final Color possibleMoveColor = Color.yellow;
349  
360
350      public static Color displayColor(Player pl) {
351          if (pl.isBlue()) return Color.blue;
352          else if (pl.isGreen()) return darkGreen;
# Line 370 | Line 359 | public class Microscope extends JPanel {
359          else return Color.gray;
360      }
361  
373
362      class BoardPanel extends Canvas implements MouseListener {
363  
364          BoardPanel() {
# Line 438 | Line 426 | public class Microscope extends JPanel {
426  
427      /**
428       *  Player is just a glorified enumeration
429 <     **/
442 <
429 >     */
430      static final class Player {
431  
432          public static final int EMPTY = 0;
# Line 480 | Line 467 | public class Microscope extends JPanel {
467       * Boards are not immutable, but are never passed around across
468       * threads (instead new ones are constructed), so don't
469       * need any synch.
470 <     **/
471 <
485 <    static final class Board   {
470 >     */
471 >    static final class Board {
472  
473          /*
474             First, some Constants and utilities that might as well be here
# Line 538 | Line 524 | public class Microscope extends JPanel {
524  
525          }
526  
541
527          public static boolean inBounds(int row, int col) {
528              return (0 <= row)  && (row < RANKS) && (0 <= col) && (col < RANKS);
529          }
# Line 566 | Line 551 | public class Microscope extends JPanel {
551          long getBlue() { return blue_; }
552          long getGreen() { return green_; }
553  
569
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 580 | Line 564 | public class Microscope extends JPanel {
564  
565  
566          // place a tile without taking opponent tiles
583
567          public void occupy(Player player, int row, int col) {
568              long m = 1L << (row + col * RANKS);
569              long nm = ~m;
570 <            if (player.code_ == Player.BLUE)  {
570 >            if (player.code_ == Player.BLUE) {
571                  blue_ |= m;
572                  green_ &= nm;
573              }
# Line 604 | Line 587 | public class Microscope extends JPanel {
587              green_ &= nm;
588          }
589  
607
608
590          // place a tile, taking all adjacent tiles of opponent
610
591          public void take(Player player, int row, int col) {
592 <            int k =  (row + col * RANKS);
592 >            int k = row + col * RANKS;
593              long dest = 1L << k;
594              long nbrMask = adjacentMasks[k];
595              long sourceBlue = blue_;
# Line 620 | Line 600 | public class Microscope extends JPanel {
600              }
601              else {
602                  blue_ = sourceBlue & ~(sourceBlue & nbrMask);
603 <                green_ =  sourceGreen | dest | (sourceBlue & nbrMask);
603 >                green_ = sourceGreen | dest | (sourceBlue & nbrMask);
604              }
605          }
606  
# Line 631 | Line 611 | public class Microscope extends JPanel {
611                  ((green_ & ~BLUEBIT) == 0);
612          }
613  
634
614          public int score(Player player) {
615              if (player.isBlue()) {
616                  return score(blue_, green_);
# Line 681 | Line 660 | public class Microscope extends JPanel {
660              return hb - ((lg + hg) & 0xff);
661          }
662  
684
685
663          static int slowscore(long b, long g) {
664              int score = 0;
665              for (int l = 0; l < CELLS; ++l) {
# Line 693 | Line 670 | public class Microscope extends JPanel {
670              }
671              return score;
672          }
696
697
673      }
674  
675      /**
676       * Moves represent transitions across Board states
677 <     **/
678 <
704 <
705 <    static final class Move  {
677 >     */
678 >    static final class Move {
679  
680          static final int NO_VALUE = -1;     // row/col value if not yet set
681          static final int PASS_VALUE = -2;   // special value for pass moves
# Line 756 | Line 729 | public class Microscope extends JPanel {
729  
730          // setters:
731  
732 <        synchronized void player(Player p)       { player_ = p;  }
733 <        synchronized void board(Board b)         { board_ = b;  }
734 <        synchronized void from(int sr, int sc)   { fromRow = sr; fromCol = sc;  }
732 >        synchronized void player(Player p)       { player_ = p; }
733 >        synchronized void board(Board b)         { board_ = b; }
734 >        synchronized void from(int sr, int sc)   { fromRow = sr; fromCol = sc; }
735          synchronized void to(int dr, int dc)     { toRow = dr;   toCol = dc; }
736  
737          //  accessors:
# Line 766 | Line 739 | public class Microscope extends JPanel {
739          synchronized boolean isFrom(int r, int c) {
740              return fromRow== r && fromCol == c;
741          }
742 <        synchronized boolean isTo(int r, int c)   {
742 >        synchronized boolean isTo(int r, int c) {
743              return toRow == r && toCol == c;
744          }
745          synchronized Board board() {
# Line 797 | Line 770 | public class Microscope extends JPanel {
770              return toRow != NO_VALUE && toCol != NO_VALUE;
771          }
772  
800
773          synchronized boolean possibleTo(int r, int c) { // is (r, c) a legal `to'?
774              return hasFrom() &&
775                  withinTwo(fromRow, r) &&
# Line 821 | Line 793 | public class Microscope extends JPanel {
793          synchronized void commit() { // update board to reflect move
794              if (!committed) {
795                  committed = true;
796 <                if (isLegal() && !isPass())  {
796 >                if (isLegal() && !isPass()) {
797                      if (isJump()) board_.occupy(Player.Empty, fromRow, fromCol);
798                      board_.take(player_, toRow, toCol);
799                  }
# Line 853 | Line 825 | public class Microscope extends JPanel {
825      }
826  
827      /**
828 <     *  User builds moves via instructions/clicks by users
829 <     **/
858 <
828 >     * User builds moves via instructions/clicks by users
829 >     */
830      static class User extends Mover {
831  
832          private Move current;
# Line 907 | Line 878 | public class Microscope extends JPanel {
878  
879      }
880  
910
881      /**
882 <     *     AutoMover constructs Finders that compute actual moves
883 <     **/
914 <
882 >     * AutoMover constructs Finders that compute actual moves
883 >     */
884      static class AutoMover extends Mover {
885  
886          boolean cancelled = false;
# Line 921 | Line 890 | public class Microscope extends JPanel {
890              super(ap);
891          }
892  
924
893          public synchronized boolean placing() {
894              return currentFinder != null;
895          }
# Line 930 | Line 898 | public class Microscope extends JPanel {
898              currentFinder = null;
899          }
900  
933
901          public synchronized void cancel() {
902 <            if (placing())  {
902 >            if (placing()) {
903                  currentFinder.cancel(false);
904                  stopPlacing();
905              }
# Line 955 | Line 922 | public class Microscope extends JPanel {
922  
923      }
924  
958
925      /**
926       * Implements a classic all-possible-move search algorith using
927       * ForkJoinTasks.  The move finder is not all that smart. Among
# Line 964 | Line 930 | public class Microscope extends JPanel {
930       * since most expansions are duplicates of others. It could also
931       * be changed to prune moves, although this is unlikely to work
932       * well without better partial evaluation functions.
933 <     **/
968 <
933 >     */
934      static class Finder extends RecursiveAction {
935  
936          static final int NOMOVE = Integer.MIN_VALUE;
# Line 1007 | Line 972 | public class Microscope extends JPanel {
972                  search();
973          }
974  
1010
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
978  
979              long open = ~(ours | theirs);  // currently empty cells
980 <            long here = 1;                 // travserse through bits
980 >            long here = 1;                 // traverse through bits
981  
982              for (int k = 0; k < Board.CELLS; ++k, here <<= 1) {
983                  if ((here & ours) != 0) {
# Line 1070 | Line 1034 | public class Microscope extends JPanel {
1034  
1035          /**
1036           * Join all subtasks and evaluate moves. Default is sub-finder version.
1037 <         * Overridden in RootFinder
1038 <         **/
1075 <
1037 >         * Overridden in RootFinder.
1038 >         */
1039          void collect(Finder forked) {
1040              int best = NOMOVE;
1041              while (forked != null) {
# Line 1103 | Line 1066 | public class Microscope extends JPanel {
1066          }
1067  
1068          /**
1069 <         * Cancel all forked subtasks in list
1070 <         **/
1108 <
1069 >         * Cancels all forked subtasks in list.
1070 >         */
1071          void cancelAll(Finder forked) {
1072              while (forked != null) {
1073                  forked.cancel(false);
# Line 1117 | Line 1079 | public class Microscope extends JPanel {
1079  
1080      /**
1081       * Root Finder class -- wait out other finders and issue callback to game.
1082 <     **/
1121 <
1082 >     */
1083      static class RootFinder extends Finder {
1084          final AutoMover automover;
1085          final Player player;
1086          RootFinder(Board board, Player p, int level, AutoMover automover) {
1087 <            super( (p.isBlue()? (board.getBlue()| Board.BLUEBIT) : board.getGreen()),
1088 <                   (p.isBlue()? board.getGreen() : (board.getBlue()| Board.BLUEBIT)),
1087 >            super( (p.isBlue() ? (board.getBlue()| Board.BLUEBIT) : board.getGreen()),
1088 >                   (p.isBlue() ? board.getGreen() : (board.getBlue()| Board.BLUEBIT)),
1089                     level,
1090                     null);
1091  
# Line 1132 | Line 1093 | public class Microscope extends JPanel {
1093              this.automover = automover;
1094          }
1095  
1135
1096          /**
1097           * This differs from default version by recording
1098           * and calling back with best move
1099 <         **/
1099 >         */
1100          void collect(Finder forked) {
1101              int best = NOMOVE;
1102              Finder bestFinder = null;
# Line 1183 | Line 1143 | public class Microscope extends JPanel {
1143  
1144                  long nextOurs = bestFinder.theirs;
1145                  long nextTheirs = bestFinder.ours;
1146 <                long blue = (player.isBlue())? nextOurs : nextTheirs;
1147 <                long green = (player.isBlue())? nextTheirs: nextOurs;
1146 >                long blue = player.isBlue() ? nextOurs : nextTheirs;
1147 >                long green = player.isBlue() ? nextTheirs : nextOurs;
1148                  move = new Move(player, new Board(blue, green), true);
1149              }
1150              automover.relay(move);
1151          }
1152      }
1193
1194
1153   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines