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.4 by jsr166, Sat Oct 16 16:22:57 2010 UTC vs.
Revision 1.15 by jsr166, Mon Aug 10 03:13:33 2015 UTC

# Line 1 | Line 1
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
# 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;
237      }
238  
239 <    public int level () { return Microscope.lookAheads; }
246 <
239 >    public int level() { return Microscope.lookAheads; }
240  
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 479 | Line 467 | public class Microscope extends JPanel {
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
# 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 577 | Line 563 | public class Microscope extends JPanel {
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;
# Line 601 | Line 586 | public class Microscope extends JPanel {
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);
591 >            int k = row + col * RANKS;
592              long dest = 1L << k;
593              long nbrMask = adjacentMasks[k];
594              long sourceBlue = blue_;
# Line 617 | Line 599 | public class Microscope extends JPanel {
599              }
600              else {
601                  blue_ = sourceBlue & ~(sourceBlue & nbrMask);
602 <                green_ =  sourceGreen | dest | (sourceBlue & nbrMask);
602 >                green_ = sourceGreen | dest | (sourceBlue & nbrMask);
603              }
604          }
605  
# Line 628 | Line 610 | public class Microscope extends JPanel {
610                  ((green_ & ~BLUEBIT) == 0);
611          }
612  
631
613          public int score(Player player) {
614              if (player.isBlue()) {
615                  return score(blue_, green_);
# Line 678 | Line 659 | public class Microscope extends JPanel {
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) {
# Line 690 | Line 669 | public class Microscope extends JPanel {
669              }
670              return score;
671          }
693
694
672      }
673  
674      /**
# Line 751 | Line 728 | public class Microscope extends JPanel {
728  
729          // setters:
730  
731 <        synchronized void player(Player p)       { player_ = p;  }
732 <        synchronized void board(Board b)         { board_ = b;  }
733 <        synchronized void from(int sr, int sc)   { fromRow = sr; fromCol = sc;  }
731 >        synchronized void player(Player p)       { player_ = p; }
732 >        synchronized void board(Board b)         { board_ = b; }
733 >        synchronized void from(int sr, int sc)   { fromRow = sr; fromCol = sc; }
734          synchronized void to(int dr, int dc)     { toRow = dr;   toCol = dc; }
735  
736          //  accessors:
# Line 761 | Line 738 | public class Microscope extends JPanel {
738          synchronized boolean isFrom(int r, int c) {
739              return fromRow== r && fromCol == c;
740          }
741 <        synchronized boolean isTo(int r, int c)   {
741 >        synchronized boolean isTo(int r, int c) {
742              return toRow == r && toCol == c;
743          }
744          synchronized Board board() {
# Line 792 | Line 769 | public class Microscope extends JPanel {
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) &&
# Line 901 | Line 877 | public class Microscope extends JPanel {
877  
878      }
879  
904
880      /**
881       * AutoMover constructs Finders that compute actual moves
882       */
# Line 914 | Line 889 | public class Microscope extends JPanel {
889              super(ap);
890          }
891  
917
892          public synchronized boolean placing() {
893              return currentFinder != null;
894          }
# Line 923 | Line 897 | public class Microscope extends JPanel {
897              currentFinder = null;
898          }
899  
926
900          public synchronized void cancel() {
901              if (placing()) {
902                  currentFinder.cancel(false);
# Line 948 | Line 921 | public class Microscope extends JPanel {
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
# Line 999 | Line 971 | public class Microscope extends JPanel {
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
977  
978              long open = ~(ours | theirs);  // currently empty cells
979 <            long here = 1;                 // travserse through bits
979 >            long here = 1;                 // traverse through bits
980  
981              for (int k = 0; k < Board.CELLS; ++k, here <<= 1) {
982                  if ((here & ours) != 0) {
# Line 1112 | Line 1083 | public class Microscope extends JPanel {
1083          final AutoMover automover;
1084          final Player player;
1085          RootFinder(Board board, Player p, int level, AutoMover automover) {
1086 <            super( (p.isBlue()? (board.getBlue()| Board.BLUEBIT) : board.getGreen()),
1087 <                   (p.isBlue()? board.getGreen() : (board.getBlue()| Board.BLUEBIT)),
1086 >            super( (p.isBlue() ? (board.getBlue()| Board.BLUEBIT) : board.getGreen()),
1087 >                   (p.isBlue() ? board.getGreen() : (board.getBlue()| Board.BLUEBIT)),
1088                     level,
1089                     null);
1090  
# Line 1121 | Line 1092 | public class Microscope extends JPanel {
1092              this.automover = automover;
1093          }
1094  
1124
1095          /**
1096           * This differs from default version by recording
1097           * and calling back with best move
# Line 1172 | Line 1142 | public class Microscope extends JPanel {
1142  
1143                  long nextOurs = bestFinder.theirs;
1144                  long nextTheirs = bestFinder.ours;
1145 <                long blue = (player.isBlue())? nextOurs : nextTheirs;
1146 <                long green = (player.isBlue())? nextTheirs: nextOurs;
1145 >                long blue = player.isBlue() ? nextOurs : nextTheirs;
1146 >                long green = player.isBlue() ? nextTheirs : nextOurs;
1147                  move = new Move(player, new Board(blue, green), true);
1148              }
1149              automover.relay(move);
1150          }
1151      }
1182
1183
1152   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines