11 |
|
import javax.swing.*; |
12 |
|
import javax.swing.event.*; |
13 |
|
|
14 |
– |
|
14 |
|
/** |
15 |
|
* Microscope implements a version of the 7th Guest |
16 |
|
* game found looking in the Microscope in the laboratory. |
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() { |
523 |
|
|
524 |
|
} |
525 |
|
|
538 |
– |
|
526 |
|
public static boolean inBounds(int row, int col) { |
527 |
|
return (0 <= row) && (row < RANKS) && (0 <= col) && (col < RANKS); |
528 |
|
} |
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)) { |
555 |
|
long m = 1L << (row + col * RANKS); |
563 |
|
|
564 |
|
|
565 |
|
// place a tile without taking opponent tiles |
580 |
– |
|
566 |
|
public void occupy(Player player, int row, int col) { |
567 |
|
long m = 1L << (row + col * RANKS); |
568 |
|
long nm = ~m; |
586 |
|
green_ &= nm; |
587 |
|
} |
588 |
|
|
604 |
– |
|
605 |
– |
|
589 |
|
// place a tile, taking all adjacent tiles of opponent |
607 |
– |
|
590 |
|
public void take(Player player, int row, int col) { |
591 |
|
int k = row + col * RANKS; |
592 |
|
long dest = 1L << k; |
610 |
|
((green_ & ~BLUEBIT) == 0); |
611 |
|
} |
612 |
|
|
631 |
– |
|
613 |
|
public int score(Player player) { |
614 |
|
if (player.isBlue()) { |
615 |
|
return score(blue_, green_); |
659 |
|
return hb - ((lg + hg) & 0xff); |
660 |
|
} |
661 |
|
|
681 |
– |
|
682 |
– |
|
662 |
|
static int slowscore(long b, long g) { |
663 |
|
int score = 0; |
664 |
|
for (int l = 0; l < CELLS; ++l) { |
669 |
|
} |
670 |
|
return score; |
671 |
|
} |
693 |
– |
|
694 |
– |
|
672 |
|
} |
673 |
|
|
674 |
|
/** |
769 |
|
return toRow != NO_VALUE && toCol != NO_VALUE; |
770 |
|
} |
771 |
|
|
795 |
– |
|
772 |
|
synchronized boolean possibleTo(int r, int c) { // is (r, c) a legal `to'? |
773 |
|
return hasFrom() && |
774 |
|
withinTwo(fromRow, r) && |
877 |
|
|
878 |
|
} |
879 |
|
|
904 |
– |
|
880 |
|
/** |
881 |
|
* AutoMover constructs Finders that compute actual moves |
882 |
|
*/ |
889 |
|
super(ap); |
890 |
|
} |
891 |
|
|
917 |
– |
|
892 |
|
public synchronized boolean placing() { |
893 |
|
return currentFinder != null; |
894 |
|
} |
897 |
|
currentFinder = null; |
898 |
|
} |
899 |
|
|
926 |
– |
|
900 |
|
public synchronized void cancel() { |
901 |
|
if (placing()) { |
902 |
|
currentFinder.cancel(false); |
921 |
|
|
922 |
|
} |
923 |
|
|
951 |
– |
|
924 |
|
/** |
925 |
< |
* Implements a classic all-possible-move search algorith using |
925 |
> |
* Implements a classic all-possible-move search algorithm using |
926 |
|
* ForkJoinTasks. The move finder is not all that smart. Among |
927 |
|
* other possible improvements, it could keep a cache of explored |
928 |
|
* moves and avoid repeating them. This would likely speed it up |
971 |
|
search(); |
972 |
|
} |
973 |
|
|
1002 |
– |
|
974 |
|
final void search() { |
975 |
|
int best = NOMOVE; // For direct evaluation when level == 1 |
976 |
|
Finder forked = null; // list of forked subtasks when level > 1 |
1092 |
|
this.automover = automover; |
1093 |
|
} |
1094 |
|
|
1124 |
– |
|
1095 |
|
/** |
1096 |
|
* This differs from default version by recording |
1097 |
|
* and calling back with best move |
1149 |
|
automover.relay(move); |
1150 |
|
} |
1151 |
|
} |
1182 |
– |
|
1183 |
– |
|
1152 |
|
} |