--- jsr166/src/test/loops/Microscope.java 2010/09/20 20:42:37 1.2 +++ jsr166/src/test/loops/Microscope.java 2016/09/15 06:45:56 1.17 @@ -1,16 +1,15 @@ /* * Written by Doug Lea with assistance from members of JCP JSR-166 * Expert Group and released to the public domain, as explained at - * http://creativecommons.org/licenses/publicdomain + * http://creativecommons.org/publicdomain/zero/1.0/ */ import java.awt.*; -import javax.swing.*; -import java.util.*; import java.awt.event.*; -import javax.swing.event.*; +import java.util.*; import java.util.concurrent.*; - +import javax.swing.*; +import javax.swing.event.*; /** * Microscope implements a version of the 7th Guest @@ -19,9 +18,8 @@ import java.util.concurrent.*; * Microscope version for instructions. *

* The code has been mangled beyond recognition - * as a test of ForkJoin - **/ - + * as a test of ForkJoin. + */ public class Microscope extends JPanel { static final CountDownLatch cd = new CountDownLatch(1); @@ -116,7 +114,6 @@ public class Microscope extends JPanel { synchronized Player getPlayer() { return player; } synchronized void setPlayer(Player p) { player = p; } - final AutoMover auto; // The move finder. final User user; // Mover for user moves Mover mover = null; // the current Mover (always == auto or user or null) @@ -177,7 +174,6 @@ public class Microscope extends JPanel { public synchronized void actionPerformed(ActionEvent e) { toggleDemoMode(); updateStatus(); - }}); undoButton.addActionListener(new ActionListener() { @@ -195,14 +191,12 @@ public class Microscope extends JPanel { // scoreLabel.setMinimumSize(labDim); // scoreLabel.setPreferredSize(labDim); - topPanel.add(autoButton); topPanel.add(modeButton); topPanel.add(undoButton); topPanel.add(scoreLabel); add(topPanel); - levelSlider.setLabelTable(levelSlider.createStandardLabels(1)); levelSlider.setPaintLabels(true); @@ -230,24 +224,21 @@ public class Microscope extends JPanel { boardPanel.repaint(); } - public void init() { + public void init() { initializeBoard(); if (autostart) { startMover(auto); } } - synchronized void setLevel(int l) { lookAheads = l; if (lookAheads <= 1) lookAheads = 2; } - public int level () { return Microscope.lookAheads; } - + public int level() { return Microscope.lookAheads; } // process a move (called only from mover) - public void move(Move m, Mover mvr) { if (mvr != mover || m == null || @@ -304,7 +295,6 @@ public class Microscope extends JPanel { } } - // handle Undo button synchronized void undo() { if (mover == null) { @@ -349,7 +339,6 @@ public class Microscope extends JPanel { } - static final int CELL_SIZE = 40; // size of a tile/cell static final Color paleGreen = new Color(152, 251, 152); @@ -357,7 +346,6 @@ public class Microscope extends JPanel { static final Color possibleMoveColor = Color.yellow; - public static Color displayColor(Player pl) { if (pl.isBlue()) return Color.blue; else if (pl.isGreen()) return darkGreen; @@ -370,7 +358,6 @@ public class Microscope extends JPanel { else return Color.gray; } - class BoardPanel extends Canvas implements MouseListener { BoardPanel() { @@ -438,8 +425,7 @@ public class Microscope extends JPanel { /** * Player is just a glorified enumeration - **/ - + */ static final class Player { public static final int EMPTY = 0; @@ -480,9 +466,8 @@ public class Microscope extends JPanel { * Boards are not immutable, but are never passed around across * threads (instead new ones are constructed), so don't * need any synch. - **/ - - static final class Board { + */ + static final class Board { /* First, some Constants and utilities that might as well be here @@ -538,9 +523,8 @@ public class Microscope extends JPanel { } - public static boolean inBounds(int row, int col) { - return (0 <= row) && (row < RANKS) && (0 <= col) && (col < RANKS); + return (0 <= row) && (row < RANKS) && (0 <= col) && (col < RANKS); } // The representation @@ -566,9 +550,8 @@ public class Microscope extends JPanel { long getBlue() { return blue_; } long getGreen() { return green_; } - public Player occupant(int row, int col) { - if ((0 <= row) && (row < RANKS) && (0 <= col) && (col < RANKS)) { + if ((0 <= row) && (row < RANKS) && (0 <= col) && (col < RANKS)) { long m = 1L << (row + col * RANKS); if ((blue_ & m) != 0L) return Player.Blue; else if ((green_ &m) != 0L) return Player.Green; @@ -578,13 +561,11 @@ public class Microscope extends JPanel { return Player.Illegal; } - - // place a tile without taking opponent tiles - + /** Places a tile without taking opponent tiles. */ public void occupy(Player player, int row, int col) { long m = 1L << (row + col * RANKS); long nm = ~m; - if (player.code_ == Player.BLUE) { + if (player.code_ == Player.BLUE) { blue_ |= m; green_ &= nm; } @@ -604,12 +585,9 @@ public class Microscope extends JPanel { green_ &= nm; } - - - // place a tile, taking all adjacent tiles of opponent - + /** Places a tile, taking all adjacent tiles of opponent. */ public void take(Player player, int row, int col) { - int k = (row + col * RANKS); + int k = row + col * RANKS; long dest = 1L << k; long nbrMask = adjacentMasks[k]; long sourceBlue = blue_; @@ -620,7 +598,7 @@ public class Microscope extends JPanel { } else { blue_ = sourceBlue & ~(sourceBlue & nbrMask); - green_ = sourceGreen | dest | (sourceBlue & nbrMask); + green_ = sourceGreen | dest | (sourceBlue & nbrMask); } } @@ -631,7 +609,6 @@ public class Microscope extends JPanel { ((green_ & ~BLUEBIT) == 0); } - public int score(Player player) { if (player.isBlue()) { return score(blue_, green_); @@ -681,8 +658,6 @@ public class Microscope extends JPanel { return hb - ((lg + hg) & 0xff); } - - static int slowscore(long b, long g) { int score = 0; for (int l = 0; l < CELLS; ++l) { @@ -693,16 +668,12 @@ public class Microscope extends JPanel { } return score; } - - } /** * Moves represent transitions across Board states - **/ - - - static final class Move { + */ + static final class Move { static final int NO_VALUE = -1; // row/col value if not yet set static final int PASS_VALUE = -2; // special value for pass moves @@ -756,9 +727,9 @@ public class Microscope extends JPanel { // setters: - synchronized void player(Player p) { player_ = p; } - synchronized void board(Board b) { board_ = b; } - synchronized void from(int sr, int sc) { fromRow = sr; fromCol = sc; } + synchronized void player(Player p) { player_ = p; } + synchronized void board(Board b) { board_ = b; } + synchronized void from(int sr, int sc) { fromRow = sr; fromCol = sc; } synchronized void to(int dr, int dc) { toRow = dr; toCol = dc; } // accessors: @@ -766,7 +737,7 @@ public class Microscope extends JPanel { synchronized boolean isFrom(int r, int c) { return fromRow== r && fromCol == c; } - synchronized boolean isTo(int r, int c) { + synchronized boolean isTo(int r, int c) { return toRow == r && toCol == c; } synchronized Board board() { @@ -797,7 +768,6 @@ public class Microscope extends JPanel { return toRow != NO_VALUE && toCol != NO_VALUE; } - synchronized boolean possibleTo(int r, int c) { // is (r, c) a legal `to'? return hasFrom() && withinTwo(fromRow, r) && @@ -821,7 +791,7 @@ public class Microscope extends JPanel { synchronized void commit() { // update board to reflect move if (!committed) { committed = true; - if (isLegal() && !isPass()) { + if (isLegal() && !isPass()) { if (isJump()) board_.occupy(Player.Empty, fromRow, fromCol); board_.take(player_, toRow, toCol); } @@ -833,10 +803,8 @@ public class Microscope extends JPanel { /** * Mover is an abstract class to simplify code dealing with * either user moves or auto moves. - **/ - - - static abstract class Mover { + */ + abstract static class Mover { // caller for move callbacks protected Microscope game; @@ -855,9 +823,8 @@ public class Microscope extends JPanel { } /** - * User builds moves via instructions/clicks by users - **/ - + * User builds moves via instructions/clicks by users + */ static class User extends Mover { private Move current; @@ -909,11 +876,9 @@ public class Microscope extends JPanel { } - /** - * AutoMover constructs Finders that compute actual moves - **/ - + * AutoMover constructs Finders that compute actual moves + */ static class AutoMover extends Mover { boolean cancelled = false; @@ -923,7 +888,6 @@ public class Microscope extends JPanel { super(ap); } - public synchronized boolean placing() { return currentFinder != null; } @@ -932,9 +896,8 @@ public class Microscope extends JPanel { currentFinder = null; } - public synchronized void cancel() { - if (placing()) { + if (placing()) { currentFinder.cancel(false); stopPlacing(); } @@ -957,17 +920,15 @@ public class Microscope extends JPanel { } - /** - * Implements a classic all-possible-move search algorith using + * Implements a classic all-possible-move search algorithm using * ForkJoinTasks. The move finder is not all that smart. Among * other possible improvements, it could keep a cache of explored * moves and avoid repeating them. This would likely speed it up * since most expansions are duplicates of others. It could also * be changed to prune moves, although this is unlikely to work * well without better partial evaluation functions. - **/ - + */ static class Finder extends RecursiveAction { static final int NOMOVE = Integer.MIN_VALUE; @@ -1009,13 +970,12 @@ public class Microscope extends JPanel { search(); } - final void search() { int best = NOMOVE; // For direct evaluation when level == 1 Finder forked = null; // list of forked subtasks when level > 1 long open = ~(ours | theirs); // currently empty cells - long here = 1; // travserse through bits + long here = 1; // traverse through bits for (int k = 0; k < Board.CELLS; ++k, here <<= 1) { if ((here & ours) != 0) { @@ -1072,9 +1032,8 @@ public class Microscope extends JPanel { /** * Join all subtasks and evaluate moves. Default is sub-finder version. - * Overridden in RootFinder - **/ - + * Overridden in RootFinder. + */ void collect(Finder forked) { int best = NOMOVE; while (forked != null) { @@ -1105,9 +1064,8 @@ public class Microscope extends JPanel { } /** - * Cancel all forked subtasks in list - **/ - + * Cancels all forked subtasks in list. + */ void cancelAll(Finder forked) { while (forked != null) { forked.cancel(false); @@ -1119,14 +1077,13 @@ public class Microscope extends JPanel { /** * Root Finder class -- wait out other finders and issue callback to game. - **/ - + */ static class RootFinder extends Finder { final AutoMover automover; final Player player; RootFinder(Board board, Player p, int level, AutoMover automover) { - super( (p.isBlue()? (board.getBlue()| Board.BLUEBIT) : board.getGreen()), - (p.isBlue()? board.getGreen() : (board.getBlue()| Board.BLUEBIT)), + super( (p.isBlue() ? (board.getBlue()| Board.BLUEBIT) : board.getGreen()), + (p.isBlue() ? board.getGreen() : (board.getBlue()| Board.BLUEBIT)), level, null); @@ -1134,11 +1091,10 @@ public class Microscope extends JPanel { this.automover = automover; } - /** * This differs from default version by recording * and calling back with best move - **/ + */ void collect(Finder forked) { int best = NOMOVE; Finder bestFinder = null; @@ -1185,13 +1141,11 @@ public class Microscope extends JPanel { long nextOurs = bestFinder.theirs; long nextTheirs = bestFinder.ours; - long blue = (player.isBlue())? nextOurs : nextTheirs; - long green = (player.isBlue())? nextTheirs: nextOurs; + long blue = player.isBlue() ? nextOurs : nextTheirs; + long green = player.isBlue() ? nextTheirs : nextOurs; move = new Move(player, new Board(blue, green), true); } automover.relay(move); } } - - }