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 |
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); |
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) |
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 |
|
|
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 || |
296 |
|
} |
297 |
|
} |
298 |
|
|
307 |
– |
|
299 |
|
// handle Undo button |
300 |
|
synchronized void undo() { |
301 |
|
if (mover == null) { |
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); |
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; |
359 |
|
else return Color.gray; |
360 |
|
} |
361 |
|
|
373 |
– |
|
362 |
|
class BoardPanel extends Canvas implements MouseListener { |
363 |
|
|
364 |
|
BoardPanel() { |
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; |
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 |
524 |
|
|
525 |
|
} |
526 |
|
|
541 |
– |
|
527 |
|
public static boolean inBounds(int row, int col) { |
528 |
|
return (0 <= row) && (row < RANKS) && (0 <= col) && (col < RANKS); |
529 |
|
} |
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); |
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 |
|
} |
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_; |
600 |
|
} |
601 |
|
else { |
602 |
|
blue_ = sourceBlue & ~(sourceBlue & nbrMask); |
603 |
< |
green_ = sourceGreen | dest | (sourceBlue & nbrMask); |
603 |
> |
green_ = sourceGreen | dest | (sourceBlue & nbrMask); |
604 |
|
} |
605 |
|
} |
606 |
|
|
611 |
|
((green_ & ~BLUEBIT) == 0); |
612 |
|
} |
613 |
|
|
634 |
– |
|
614 |
|
public int score(Player player) { |
615 |
|
if (player.isBlue()) { |
616 |
|
return score(blue_, green_); |
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) { |
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 |
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: |
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() { |
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) && |
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 |
|
} |
805 |
|
/** |
806 |
|
* Mover is an abstract class to simplify code dealing with |
807 |
|
* either user moves or auto moves. |
808 |
< |
**/ |
809 |
< |
|
838 |
< |
|
839 |
< |
static abstract class Mover { |
808 |
> |
*/ |
809 |
> |
abstract static class Mover { |
810 |
|
|
811 |
|
// caller for move callbacks |
812 |
|
protected Microscope game; |
825 |
|
} |
826 |
|
|
827 |
|
/** |
828 |
< |
* User builds moves via instructions/clicks by users |
829 |
< |
**/ |
860 |
< |
|
828 |
> |
* User builds moves via instructions/clicks by users |
829 |
> |
*/ |
830 |
|
static class User extends Mover { |
831 |
|
|
832 |
|
private Move current; |
878 |
|
|
879 |
|
} |
880 |
|
|
912 |
– |
|
881 |
|
/** |
882 |
< |
* AutoMover constructs Finders that compute actual moves |
883 |
< |
**/ |
916 |
< |
|
882 |
> |
* AutoMover constructs Finders that compute actual moves |
883 |
> |
*/ |
884 |
|
static class AutoMover extends Mover { |
885 |
|
|
886 |
|
boolean cancelled = false; |
890 |
|
super(ap); |
891 |
|
} |
892 |
|
|
926 |
– |
|
893 |
|
public synchronized boolean placing() { |
894 |
|
return currentFinder != null; |
895 |
|
} |
898 |
|
currentFinder = null; |
899 |
|
} |
900 |
|
|
935 |
– |
|
901 |
|
public synchronized void cancel() { |
902 |
< |
if (placing()) { |
902 |
> |
if (placing()) { |
903 |
|
currentFinder.cancel(false); |
904 |
|
stopPlacing(); |
905 |
|
} |
922 |
|
|
923 |
|
} |
924 |
|
|
960 |
– |
|
925 |
|
/** |
926 |
< |
* Implements a classic all-possible-move search algorith using |
926 |
> |
* Implements a classic all-possible-move search algorithm using |
927 |
|
* ForkJoinTasks. The move finder is not all that smart. Among |
928 |
|
* other possible improvements, it could keep a cache of explored |
929 |
|
* moves and avoid repeating them. This would likely speed it up |
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 |
< |
**/ |
970 |
< |
|
933 |
> |
*/ |
934 |
|
static class Finder extends RecursiveAction { |
935 |
|
|
936 |
|
static final int NOMOVE = Integer.MIN_VALUE; |
972 |
|
search(); |
973 |
|
} |
974 |
|
|
1012 |
– |
|
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) { |
1034 |
|
|
1035 |
|
/** |
1036 |
|
* Join all subtasks and evaluate moves. Default is sub-finder version. |
1037 |
< |
* Overridden in RootFinder |
1038 |
< |
**/ |
1077 |
< |
|
1037 |
> |
* Overridden in RootFinder. |
1038 |
> |
*/ |
1039 |
|
void collect(Finder forked) { |
1040 |
|
int best = NOMOVE; |
1041 |
|
while (forked != null) { |
1066 |
|
} |
1067 |
|
|
1068 |
|
/** |
1069 |
< |
* Cancel all forked subtasks in list |
1070 |
< |
**/ |
1110 |
< |
|
1069 |
> |
* Cancels all forked subtasks in list. |
1070 |
> |
*/ |
1071 |
|
void cancelAll(Finder forked) { |
1072 |
|
while (forked != null) { |
1073 |
|
forked.cancel(false); |
1079 |
|
|
1080 |
|
/** |
1081 |
|
* Root Finder class -- wait out other finders and issue callback to game. |
1082 |
< |
**/ |
1123 |
< |
|
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 |
|
|
1093 |
|
this.automover = automover; |
1094 |
|
} |
1095 |
|
|
1137 |
– |
|
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; |
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 |
|
} |
1195 |
– |
|
1196 |
– |
|
1153 |
|
} |