--- jsr166/src/test/loops/Microscope.java 2011/12/05 04:08:46 1.8 +++ jsr166/src/test/loops/Microscope.java 2016/09/15 06:45:56 1.17 @@ -5,12 +5,11 @@ */ 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,7 +231,6 @@ public class Microscope extends JPanel { } } - synchronized void setLevel(int l) { lookAheads = l; if (lookAheads <= 1) lookAheads = 2; @@ -244,9 +238,7 @@ public class Microscope extends JPanel { 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,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 @@ -563,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; @@ -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,10 +585,7 @@ 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; long dest = 1L << k; @@ -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; } - - } /** @@ -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) { @@ -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 @@ -1173,12 +1142,10 @@ 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 green = player.isBlue() ? nextTheirs : nextOurs; move = new Move(player, new Board(blue, green), true); } automover.relay(move); } } - - }