--- jsr166/src/test/loops/Microscope.java 2010/09/27 19:15:15 1.3 +++ jsr166/src/test/loops/Microscope.java 2012/01/02 23:40:59 1.10 @@ -1,7 +1,7 @@ /* * 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.*; @@ -19,9 +19,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); @@ -230,7 +229,7 @@ public class Microscope extends JPanel { boardPanel.repaint(); } - public void init() { + public void init() { initializeBoard(); if (autostart) { startMover(auto); @@ -243,7 +242,7 @@ public class Microscope extends JPanel { if (lookAheads <= 1) lookAheads = 2; } - public int level () { return Microscope.lookAheads; } + public int level() { return Microscope.lookAheads; } // process a move (called only from mover) @@ -438,8 +437,7 @@ public class Microscope extends JPanel { /** * Player is just a glorified enumeration - **/ - + */ static final class Player { public static final int EMPTY = 0; @@ -480,9 +478,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 @@ -584,7 +581,7 @@ public class Microscope extends JPanel { 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; } @@ -609,7 +606,7 @@ public class Microscope extends JPanel { // place 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 +617,7 @@ public class Microscope extends JPanel { } else { blue_ = sourceBlue & ~(sourceBlue & nbrMask); - green_ = sourceGreen | dest | (sourceBlue & nbrMask); + green_ = sourceGreen | dest | (sourceBlue & nbrMask); } } @@ -699,10 +696,8 @@ public class Microscope extends JPanel { /** * 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 +751,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 +761,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() { @@ -821,7 +816,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); } @@ -853,9 +848,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,9 +903,8 @@ 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; @@ -932,7 +925,7 @@ public class Microscope extends JPanel { public synchronized void cancel() { - if (placing()) { + if (placing()) { currentFinder.cancel(false); stopPlacing(); } @@ -964,8 +957,7 @@ public class Microscope extends JPanel { * 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; @@ -1013,7 +1005,7 @@ public class Microscope extends JPanel { 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) { @@ -1070,9 +1062,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) { @@ -1103,9 +1094,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); @@ -1117,14 +1107,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); @@ -1136,7 +1125,7 @@ public class Microscope extends JPanel { /** * This differs from default version by recording * and calling back with best move - **/ + */ void collect(Finder forked) { int best = NOMOVE; Finder bestFinder = null; @@ -1183,8 +1172,8 @@ 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);