ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/Microscope.java
(Generate patch)

Comparing jsr166/src/test/loops/Microscope.java (file contents):
Revision 1.9 by jsr166, Mon Dec 19 19:18:35 2011 UTC vs.
Revision 1.16 by jsr166, Sat Sep 12 19:09:00 2015 UTC

# Line 5 | Line 5
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
# Line 115 | Line 114 | public class Microscope extends JPanel {
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)
# Line 176 | Line 174 | public class Microscope extends JPanel {
174                  public synchronized void actionPerformed(ActionEvent e) {
175                      toggleDemoMode();
176                      updateStatus();
179
177                  }});
178  
179          undoButton.addActionListener(new ActionListener() {
# Line 194 | Line 191 | public class Microscope extends JPanel {
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  
# Line 236 | Line 231 | public class Microscope extends JPanel {
231          }
232      }
233  
239
234      synchronized void setLevel(int l) {
235          lookAheads = l;
236          if (lookAheads <= 1) lookAheads = 2;
# Line 244 | Line 238 | public class Microscope extends JPanel {
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 ||
# Line 303 | Line 295 | public class Microscope extends JPanel {
295          }
296      }
297  
306
298      // handle Undo button
299      synchronized void undo() {
300          if (mover == null) {
# Line 348 | Line 339 | public class Microscope extends JPanel {
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);
# Line 356 | Line 346 | public class Microscope extends JPanel {
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;
# Line 369 | Line 358 | public class Microscope extends JPanel {
358          else return Color.gray;
359      }
360  
372
361      class BoardPanel extends Canvas implements MouseListener {
362  
363          BoardPanel() {
# Line 535 | Line 523 | public class Microscope extends JPanel {
523  
524          }
525  
538
526          public static boolean inBounds(int row, int col) {
527              return (0 <= row)  && (row < RANKS) && (0 <= col) && (col < RANKS);
528          }
# Line 563 | Line 550 | public class Microscope extends JPanel {
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);
# Line 575 | Line 561 | public class Microscope extends JPanel {
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;
# Line 601 | Line 585 | public class Microscope extends JPanel {
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;
591              long dest = 1L << k;
# Line 628 | Line 609 | public class Microscope extends JPanel {
609                  ((green_ & ~BLUEBIT) == 0);
610          }
611  
631
612          public int score(Player player) {
613              if (player.isBlue()) {
614                  return score(blue_, green_);
# Line 678 | Line 658 | public class Microscope extends JPanel {
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) {
# Line 690 | Line 668 | public class Microscope extends JPanel {
668              }
669              return score;
670          }
693
694
671      }
672  
673      /**
# Line 792 | Line 768 | public class Microscope extends JPanel {
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) &&
# Line 901 | Line 876 | public class Microscope extends JPanel {
876  
877      }
878  
904
879      /**
880       * AutoMover constructs Finders that compute actual moves
881       */
# Line 914 | Line 888 | public class Microscope extends JPanel {
888              super(ap);
889          }
890  
917
891          public synchronized boolean placing() {
892              return currentFinder != null;
893          }
# Line 923 | Line 896 | public class Microscope extends JPanel {
896              currentFinder = null;
897          }
898  
926
899          public synchronized void cancel() {
900              if (placing()) {
901                  currentFinder.cancel(false);
# Line 948 | Line 920 | public class Microscope extends JPanel {
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
# Line 999 | Line 970 | public class Microscope extends JPanel {
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) {
# Line 1121 | Line 1091 | public class Microscope extends JPanel {
1091              this.automover = automover;
1092          }
1093  
1124
1094          /**
1095           * This differs from default version by recording
1096           * and calling back with best move
# Line 1173 | Line 1142 | public class Microscope extends JPanel {
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   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines