--- jsr166/src/test/loops/Microscope.java 2010/10/16 16:22:57 1.4 +++ jsr166/src/test/loops/Microscope.java 2015/09/12 19:09:00 1.16 @@ -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 @@ -115,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) @@ -176,7 +174,6 @@ public class Microscope extends JPanel { public synchronized void actionPerformed(ActionEvent e) { toggleDemoMode(); updateStatus(); - }}); undoButton.addActionListener(new ActionListener() { @@ -194,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); @@ -236,17 +231,14 @@ public class Microscope extends JPanel { } } - 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 || @@ -303,7 +295,6 @@ public class Microscope extends JPanel { } } - // handle Undo button synchronized void undo() { if (mover == null) { @@ -348,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); @@ -356,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; @@ -369,7 +358,6 @@ public class Microscope extends JPanel { else return Color.gray; } - class BoardPanel extends Canvas implements MouseListener { BoardPanel() { @@ -479,7 +467,7 @@ public class Microscope extends JPanel { * 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 @@ -535,7 +523,6 @@ public class Microscope extends JPanel { } - public static boolean inBounds(int row, int col) { return (0 <= row) && (row < RANKS) && (0 <= col) && (col < RANKS); } @@ -563,7 +550,6 @@ 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)) { long m = 1L << (row + col * RANKS); @@ -575,9 +561,7 @@ 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; @@ -601,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_; @@ -617,7 +598,7 @@ public class Microscope extends JPanel { } else { blue_ = sourceBlue & ~(sourceBlue & nbrMask); - green_ = sourceGreen | dest | (sourceBlue & nbrMask); + green_ = sourceGreen | dest | (sourceBlue & nbrMask); } } @@ -628,7 +609,6 @@ public class Microscope extends JPanel { ((green_ & ~BLUEBIT) == 0); } - public int score(Player player) { if (player.isBlue()) { return score(blue_, green_); @@ -678,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) { @@ -690,8 +668,6 @@ public class Microscope extends JPanel { } return score; } - - } /** @@ -751,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: @@ -761,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() { @@ -792,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) && @@ -901,7 +876,6 @@ public class Microscope extends JPanel { } - /** * AutoMover constructs Finders that compute actual moves */ @@ -914,7 +888,6 @@ public class Microscope extends JPanel { super(ap); } - public synchronized boolean placing() { return currentFinder != null; } @@ -923,7 +896,6 @@ public class Microscope extends JPanel { currentFinder = null; } - public synchronized void cancel() { if (placing()) { currentFinder.cancel(false); @@ -948,9 +920,8 @@ 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 @@ -999,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) { @@ -1112,8 +1082,8 @@ public class Microscope extends JPanel { 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); @@ -1121,7 +1091,6 @@ public class Microscope extends JPanel { this.automover = automover; } - /** * This differs from default version by recording * and calling back with best move @@ -1172,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); } } - - }