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 |
114 |
|
synchronized Player getPlayer() { return player; } |
115 |
|
synchronized void setPlayer(Player p) { player = p; } |
116 |
|
|
118 |
– |
|
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) |
174 |
|
public synchronized void actionPerformed(ActionEvent e) { |
175 |
|
toggleDemoMode(); |
176 |
|
updateStatus(); |
179 |
– |
|
177 |
|
}}); |
178 |
|
|
179 |
|
undoButton.addActionListener(new ActionListener() { |
191 |
|
// scoreLabel.setMinimumSize(labDim); |
192 |
|
// scoreLabel.setPreferredSize(labDim); |
193 |
|
|
197 |
– |
|
194 |
|
topPanel.add(autoButton); |
195 |
|
topPanel.add(modeButton); |
196 |
|
topPanel.add(undoButton); |
197 |
|
topPanel.add(scoreLabel); |
198 |
|
add(topPanel); |
199 |
|
|
204 |
– |
|
200 |
|
levelSlider.setLabelTable(levelSlider.createStandardLabels(1)); |
201 |
|
levelSlider.setPaintLabels(true); |
202 |
|
|
231 |
|
} |
232 |
|
} |
233 |
|
|
239 |
– |
|
234 |
|
synchronized void setLevel(int l) { |
235 |
|
lookAheads = l; |
236 |
|
if (lookAheads <= 1) lookAheads = 2; |
238 |
|
|
239 |
|
public int level() { return Microscope.lookAheads; } |
240 |
|
|
247 |
– |
|
241 |
|
// process a move (called only from mover) |
249 |
– |
|
242 |
|
public void move(Move m, Mover mvr) { |
243 |
|
if (mvr != mover || |
244 |
|
m == null || |
295 |
|
} |
296 |
|
} |
297 |
|
|
306 |
– |
|
298 |
|
// handle Undo button |
299 |
|
synchronized void undo() { |
300 |
|
if (mover == null) { |
339 |
|
|
340 |
|
} |
341 |
|
|
351 |
– |
|
342 |
|
static final int CELL_SIZE = 40; // size of a tile/cell |
343 |
|
|
344 |
|
static final Color paleGreen = new Color(152, 251, 152); |
346 |
|
|
347 |
|
static final Color possibleMoveColor = Color.yellow; |
348 |
|
|
359 |
– |
|
349 |
|
public static Color displayColor(Player pl) { |
350 |
|
if (pl.isBlue()) return Color.blue; |
351 |
|
else if (pl.isGreen()) return darkGreen; |
358 |
|
else return Color.gray; |
359 |
|
} |
360 |
|
|
372 |
– |
|
361 |
|
class BoardPanel extends Canvas implements MouseListener { |
362 |
|
|
363 |
|
BoardPanel() { |
467 |
|
* threads (instead new ones are constructed), so don't |
468 |
|
* need any synch. |
469 |
|
*/ |
470 |
< |
static final class Board { |
470 |
> |
static final class Board { |
471 |
|
|
472 |
|
/* |
473 |
|
First, some Constants and utilities that might as well be here |
523 |
|
|
524 |
|
} |
525 |
|
|
538 |
– |
|
526 |
|
public static boolean inBounds(int row, int col) { |
527 |
< |
return (0 <= row) && (row < RANKS) && (0 <= col) && (col < RANKS); |
527 |
> |
return (0 <= row) && (row < RANKS) && (0 <= col) && (col < RANKS); |
528 |
|
} |
529 |
|
|
530 |
|
// The representation |
550 |
|
long getBlue() { return blue_; } |
551 |
|
long getGreen() { return green_; } |
552 |
|
|
566 |
– |
|
553 |
|
public Player occupant(int row, int col) { |
554 |
< |
if ((0 <= row) && (row < RANKS) && (0 <= col) && (col < RANKS)) { |
554 |
> |
if ((0 <= row) && (row < RANKS) && (0 <= col) && (col < RANKS)) { |
555 |
|
long m = 1L << (row + col * RANKS); |
556 |
|
if ((blue_ & m) != 0L) return Player.Blue; |
557 |
|
else if ((green_ &m) != 0L) return Player.Green; |
561 |
|
return Player.Illegal; |
562 |
|
} |
563 |
|
|
564 |
< |
|
579 |
< |
// place a tile without taking opponent tiles |
580 |
< |
|
564 |
> |
/** Places a tile without taking opponent tiles. */ |
565 |
|
public void occupy(Player player, int row, int col) { |
566 |
|
long m = 1L << (row + col * RANKS); |
567 |
|
long nm = ~m; |
585 |
|
green_ &= nm; |
586 |
|
} |
587 |
|
|
588 |
< |
|
605 |
< |
|
606 |
< |
// place a tile, taking all adjacent tiles of opponent |
607 |
< |
|
588 |
> |
/** Places a tile, taking all adjacent tiles of opponent. */ |
589 |
|
public void take(Player player, int row, int col) { |
590 |
< |
int k = (row + col * RANKS); |
590 |
> |
int k = row + col * RANKS; |
591 |
|
long dest = 1L << k; |
592 |
|
long nbrMask = adjacentMasks[k]; |
593 |
|
long sourceBlue = blue_; |
598 |
|
} |
599 |
|
else { |
600 |
|
blue_ = sourceBlue & ~(sourceBlue & nbrMask); |
601 |
< |
green_ = sourceGreen | dest | (sourceBlue & nbrMask); |
601 |
> |
green_ = sourceGreen | dest | (sourceBlue & nbrMask); |
602 |
|
} |
603 |
|
} |
604 |
|
|
609 |
|
((green_ & ~BLUEBIT) == 0); |
610 |
|
} |
611 |
|
|
631 |
– |
|
612 |
|
public int score(Player player) { |
613 |
|
if (player.isBlue()) { |
614 |
|
return score(blue_, green_); |
658 |
|
return hb - ((lg + hg) & 0xff); |
659 |
|
} |
660 |
|
|
681 |
– |
|
682 |
– |
|
661 |
|
static int slowscore(long b, long g) { |
662 |
|
int score = 0; |
663 |
|
for (int l = 0; l < CELLS; ++l) { |
668 |
|
} |
669 |
|
return score; |
670 |
|
} |
693 |
– |
|
694 |
– |
|
671 |
|
} |
672 |
|
|
673 |
|
/** |
727 |
|
|
728 |
|
// setters: |
729 |
|
|
730 |
< |
synchronized void player(Player p) { player_ = p; } |
731 |
< |
synchronized void board(Board b) { board_ = b; } |
732 |
< |
synchronized void from(int sr, int sc) { fromRow = sr; fromCol = sc; } |
730 |
> |
synchronized void player(Player p) { player_ = p; } |
731 |
> |
synchronized void board(Board b) { board_ = b; } |
732 |
> |
synchronized void from(int sr, int sc) { fromRow = sr; fromCol = sc; } |
733 |
|
synchronized void to(int dr, int dc) { toRow = dr; toCol = dc; } |
734 |
|
|
735 |
|
// accessors: |
737 |
|
synchronized boolean isFrom(int r, int c) { |
738 |
|
return fromRow== r && fromCol == c; |
739 |
|
} |
740 |
< |
synchronized boolean isTo(int r, int c) { |
740 |
> |
synchronized boolean isTo(int r, int c) { |
741 |
|
return toRow == r && toCol == c; |
742 |
|
} |
743 |
|
synchronized Board board() { |
768 |
|
return toRow != NO_VALUE && toCol != NO_VALUE; |
769 |
|
} |
770 |
|
|
795 |
– |
|
771 |
|
synchronized boolean possibleTo(int r, int c) { // is (r, c) a legal `to'? |
772 |
|
return hasFrom() && |
773 |
|
withinTwo(fromRow, r) && |
876 |
|
|
877 |
|
} |
878 |
|
|
904 |
– |
|
879 |
|
/** |
880 |
|
* AutoMover constructs Finders that compute actual moves |
881 |
|
*/ |
888 |
|
super(ap); |
889 |
|
} |
890 |
|
|
917 |
– |
|
891 |
|
public synchronized boolean placing() { |
892 |
|
return currentFinder != null; |
893 |
|
} |
896 |
|
currentFinder = null; |
897 |
|
} |
898 |
|
|
926 |
– |
|
899 |
|
public synchronized void cancel() { |
900 |
|
if (placing()) { |
901 |
|
currentFinder.cancel(false); |
920 |
|
|
921 |
|
} |
922 |
|
|
951 |
– |
|
923 |
|
/** |
924 |
< |
* Implements a classic all-possible-move search algorith using |
924 |
> |
* Implements a classic all-possible-move search algorithm using |
925 |
|
* ForkJoinTasks. The move finder is not all that smart. Among |
926 |
|
* other possible improvements, it could keep a cache of explored |
927 |
|
* moves and avoid repeating them. This would likely speed it up |
970 |
|
search(); |
971 |
|
} |
972 |
|
|
1002 |
– |
|
973 |
|
final void search() { |
974 |
|
int best = NOMOVE; // For direct evaluation when level == 1 |
975 |
|
Finder forked = null; // list of forked subtasks when level > 1 |
976 |
|
|
977 |
|
long open = ~(ours | theirs); // currently empty cells |
978 |
< |
long here = 1; // travserse through bits |
978 |
> |
long here = 1; // traverse through bits |
979 |
|
|
980 |
|
for (int k = 0; k < Board.CELLS; ++k, here <<= 1) { |
981 |
|
if ((here & ours) != 0) { |
1091 |
|
this.automover = automover; |
1092 |
|
} |
1093 |
|
|
1124 |
– |
|
1094 |
|
/** |
1095 |
|
* This differs from default version by recording |
1096 |
|
* and calling back with best move |
1142 |
|
long nextOurs = bestFinder.theirs; |
1143 |
|
long nextTheirs = bestFinder.ours; |
1144 |
|
long blue = player.isBlue() ? nextOurs : nextTheirs; |
1145 |
< |
long green = player.isBlue() ? nextTheirs: nextOurs; |
1145 |
> |
long green = player.isBlue() ? nextTheirs : nextOurs; |
1146 |
|
move = new Move(player, new Board(blue, green), true); |
1147 |
|
} |
1148 |
|
automover.relay(move); |
1149 |
|
} |
1150 |
|
} |
1182 |
– |
|
1183 |
– |
|
1151 |
|
} |