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) |
192 |
|
// scoreLabel.setMinimumSize(labDim); |
193 |
|
// scoreLabel.setPreferredSize(labDim); |
194 |
|
|
197 |
– |
|
195 |
|
topPanel.add(autoButton); |
196 |
|
topPanel.add(modeButton); |
197 |
|
topPanel.add(undoButton); |
198 |
|
topPanel.add(scoreLabel); |
199 |
|
add(topPanel); |
200 |
|
|
204 |
– |
|
201 |
|
levelSlider.setLabelTable(levelSlider.createStandardLabels(1)); |
202 |
|
levelSlider.setPaintLabels(true); |
203 |
|
|
232 |
|
} |
233 |
|
} |
234 |
|
|
239 |
– |
|
235 |
|
synchronized void setLevel(int l) { |
236 |
|
lookAheads = l; |
237 |
|
if (lookAheads <= 1) lookAheads = 2; |
238 |
|
} |
239 |
|
|
240 |
< |
public int level () { return Microscope.lookAheads; } |
246 |
< |
|
240 |
> |
public int level() { return Microscope.lookAheads; } |
241 |
|
|
242 |
|
// process a move (called only from mover) |
249 |
– |
|
243 |
|
public void move(Move m, Mover mvr) { |
244 |
|
if (mvr != mover || |
245 |
|
m == null || |
296 |
|
} |
297 |
|
} |
298 |
|
|
306 |
– |
|
299 |
|
// handle Undo button |
300 |
|
synchronized void undo() { |
301 |
|
if (mover == null) { |
340 |
|
|
341 |
|
} |
342 |
|
|
351 |
– |
|
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 |
|
|
359 |
– |
|
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 |
|
|
372 |
– |
|
362 |
|
class BoardPanel extends Canvas implements MouseListener { |
363 |
|
|
364 |
|
BoardPanel() { |
468 |
|
* threads (instead new ones are constructed), so don't |
469 |
|
* need any synch. |
470 |
|
*/ |
471 |
< |
static final class Board { |
471 |
> |
static final class Board { |
472 |
|
|
473 |
|
/* |
474 |
|
First, some Constants and utilities that might as well be here |
524 |
|
|
525 |
|
} |
526 |
|
|
538 |
– |
|
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 |
|
|
566 |
– |
|
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 |
580 |
– |
|
567 |
|
public void occupy(Player player, int row, int col) { |
568 |
|
long m = 1L << (row + col * RANKS); |
569 |
|
long nm = ~m; |
587 |
|
green_ &= nm; |
588 |
|
} |
589 |
|
|
604 |
– |
|
605 |
– |
|
590 |
|
// place a tile, taking all adjacent tiles of opponent |
607 |
– |
|
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 |
|
|
631 |
– |
|
614 |
|
public int score(Player player) { |
615 |
|
if (player.isBlue()) { |
616 |
|
return score(blue_, green_); |
660 |
|
return hb - ((lg + hg) & 0xff); |
661 |
|
} |
662 |
|
|
681 |
– |
|
682 |
– |
|
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 |
|
} |
693 |
– |
|
694 |
– |
|
673 |
|
} |
674 |
|
|
675 |
|
/** |
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 |
|
|
795 |
– |
|
773 |
|
synchronized boolean possibleTo(int r, int c) { // is (r, c) a legal `to'? |
774 |
|
return hasFrom() && |
775 |
|
withinTwo(fromRow, r) && |
878 |
|
|
879 |
|
} |
880 |
|
|
904 |
– |
|
881 |
|
/** |
882 |
|
* AutoMover constructs Finders that compute actual moves |
883 |
|
*/ |
890 |
|
super(ap); |
891 |
|
} |
892 |
|
|
917 |
– |
|
893 |
|
public synchronized boolean placing() { |
894 |
|
return currentFinder != null; |
895 |
|
} |
898 |
|
currentFinder = null; |
899 |
|
} |
900 |
|
|
926 |
– |
|
901 |
|
public synchronized void cancel() { |
902 |
|
if (placing()) { |
903 |
|
currentFinder.cancel(false); |
922 |
|
|
923 |
|
} |
924 |
|
|
951 |
– |
|
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 |
972 |
|
search(); |
973 |
|
} |
974 |
|
|
1002 |
– |
|
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) { |
1093 |
|
this.automover = automover; |
1094 |
|
} |
1095 |
|
|
1124 |
– |
|
1096 |
|
/** |
1097 |
|
* This differs from default version by recording |
1098 |
|
* and calling back with best move |
1144 |
|
long nextOurs = bestFinder.theirs; |
1145 |
|
long nextTheirs = bestFinder.ours; |
1146 |
|
long blue = player.isBlue() ? nextOurs : nextTheirs; |
1147 |
< |
long green = player.isBlue() ? nextTheirs: nextOurs; |
1147 |
> |
long green = player.isBlue() ? nextTheirs : nextOurs; |
1148 |
|
move = new Move(player, new Board(blue, green), true); |
1149 |
|
} |
1150 |
|
automover.relay(move); |
1151 |
|
} |
1152 |
|
} |
1182 |
– |
|
1183 |
– |
|
1153 |
|
} |