├── Asynchronous
├── .classpath
├── .gitignore
├── .project
├── .settings
│ └── org.eclipse.jdt.core.prefs
└── src
│ ├── Asynchronous
│ └── Asynchronous.java
│ └── Monitor
│ └── AsynchMonitor.java
├── Images
├── .project
└── ReadMe
│ ├── BruteForce_deep_analysis1.JPG
│ ├── GUI.JPG
│ ├── Local_analysis1.JPG
│ ├── No_information_on_edge.JPG
│ ├── Probability_engine1.JPG
│ ├── Probability_engine2.JPG
│ ├── Trivial_analysis1.JPG
│ └── Trivial_analysis2.jpg
├── MineSweeperSolver
├── .classpath
├── .gitignore
├── .project
└── src
│ └── minesweeper
│ └── solver
│ ├── BoardState.java
│ ├── BoardStateCache.java
│ ├── BruteForce.java
│ ├── BruteForceAnalysis.java
│ ├── BruteForceAnalysisModel.java
│ ├── CrunchResult.java
│ ├── Cruncher.java
│ ├── EfficiencyHelper.java
│ ├── FiftyFiftyHelper.java
│ ├── LocationEvaluator.java
│ ├── LongTermRiskHelper.java
│ ├── LongTermRiskHelperOld.java
│ ├── ProbabilityEngineFast.java
│ ├── ProbabilityEngineModel.java
│ ├── ProgressEvaluator.java
│ ├── PseudoHelper.java
│ ├── RecursiveSafetyEvaluator.java
│ ├── RolloutGenerator.java
│ ├── SecondarySafetyEvaluator.java
│ ├── SolutionCounter.java
│ ├── Solver.java
│ ├── SpaceCounter.java
│ ├── WitnessWeb.java
│ ├── bulk
│ ├── BulkController.java
│ ├── BulkEventController.java
│ ├── BulkEventGame.java
│ ├── BulkEventMain.java
│ ├── BulkListener.java
│ ├── BulkPlayer.java
│ ├── BulkRequest.java
│ ├── BulkRequestGame.java
│ ├── BulkRollout.java
│ ├── BulkWorker.java
│ ├── GamePostListener.java
│ ├── GamePreListener.java
│ └── StaticCounter.java
│ ├── coach
│ ├── CoachModel.java
│ └── CoachSilent.java
│ ├── constructs
│ ├── Box.java
│ ├── CandidateLocation.java
│ ├── ChordLocation.java
│ ├── EvaluatedLocation.java
│ ├── InformationLocation.java
│ ├── LinkedLocation.java
│ ├── LocationValue.java
│ ├── Square.java
│ ├── Witness.java
│ └── WitnessData.java
│ ├── iterator
│ ├── Iterator.java
│ ├── RandomIterator.java
│ ├── SequentialIterator.java
│ └── WitnessWebIterator.java
│ ├── settings
│ ├── PlayStyle.java
│ ├── SettingsFactory.java
│ └── SolverSettings.java
│ └── utility
│ ├── BigDecimalCache.java
│ ├── Binomial.java
│ ├── BinomialCache.java
│ ├── Logger.java
│ ├── PrimeSieve.java
│ ├── ProgressMonitor.java
│ └── Timer.java
├── Minesweeper
├── .classpath
├── .gitignore
├── .project
├── .settings
│ └── org.eclipse.jdt.core.prefs
├── build.fxbuild
└── src
│ └── minesweeper
│ ├── Animator.java
│ ├── Custom.fxml
│ ├── CustomController.java
│ ├── Graphics.java
│ ├── Minesweeper.java
│ ├── Rotator.java
│ ├── Screen.css
│ ├── Screen.fxml
│ ├── ScreenController.java
│ ├── bulk
│ ├── BulkController.java
│ ├── BulkResults.fxml
│ ├── BulkRunner.java
│ ├── BulkScreen.fxml
│ ├── ResultsController.java
│ └── TableData.java
│ ├── coach
│ ├── Helper.fxml
│ ├── HelperController.java
│ └── helper.css
│ ├── gamestate
│ └── msx
│ │ ├── GameStateX.java
│ │ ├── ScreenLocation.java
│ │ └── ScreenScanner.java
│ └── resources
│ ├── ms_button.png
│ ├── ms_eight.png
│ ├── ms_five.png
│ ├── ms_flag.png
│ ├── ms_four.png
│ ├── ms_mine.png
│ ├── ms_mine_bang.png
│ ├── ms_one.png
│ ├── ms_question.png
│ ├── ms_seven.png
│ ├── ms_six.png
│ ├── ms_three.png
│ ├── ms_two.png
│ └── ms_zero.png
├── MinesweeperBulk
├── .classpath
├── .gitignore
├── .project
├── .settings
│ └── org.eclipse.jdt.core.prefs
└── src
│ └── minesweeperbulk
│ ├── BbbvMonitor.java
│ ├── EfficiencyMonitor.java
│ ├── GuessMonitor.java
│ ├── MinesweeperBulk.java
│ ├── PreActions.java
│ ├── RandomGuesser.java
│ ├── RemainingMonitor.java
│ ├── StartStrategy.java
│ └── StartStrategyResign.java
├── MinesweeperBulkCompare
├── .classpath
├── .gitignore
├── .project
├── .settings
│ └── org.eclipse.jdt.core.prefs
└── src
│ └── bulkCompare
│ ├── BulkCompare.java
│ ├── CompareMonitor.java
│ ├── GuessMonitor.java
│ ├── PreActions.java
│ ├── StartStrategy.java
│ ├── StartStrategyResign.java
│ └── TwoWayCompare.java
├── MinesweeperExplorer
├── .classpath
├── .gitignore
├── .project
├── .settings
│ └── org.eclipse.jdt.core.prefs
├── build.fxbuild
└── src
│ ├── minesweeper
│ └── explorer
│ │ ├── busy
│ │ ├── BusyController.java
│ │ ├── BusyScreen.fxml
│ │ ├── MonitorTask.java
│ │ └── ParallelTask.java
│ │ ├── gamestate
│ │ └── GameStateExplorer.java
│ │ ├── main
│ │ ├── BoardMonitor.java
│ │ ├── Explorer.java
│ │ ├── Graphics.java
│ │ ├── MainScreen.fxml
│ │ ├── MainScreenController.java
│ │ ├── TileValueData.java
│ │ ├── TileValuesController.java
│ │ ├── TileValuesScreen.fxml
│ │ └── application.css
│ │ ├── rollout
│ │ ├── BulkRunner.java
│ │ ├── RolloutController.java
│ │ └── RolloutScreen.fxml
│ │ └── structure
│ │ ├── Board.java
│ │ ├── Expander.java
│ │ ├── LedDigit.java
│ │ ├── LedDigits.java
│ │ └── Tile.java
│ └── resources
│ └── images
│ ├── 0.png
│ ├── 1.png
│ ├── 2.png
│ ├── 3.png
│ ├── 4.png
│ ├── 5.png
│ ├── 6.png
│ ├── 7.png
│ ├── 8.png
│ ├── exploded.png
│ ├── flagged.png
│ ├── flaggedWrong.png
│ ├── flaggedWrong_thin.png
│ ├── hidden.png
│ ├── led0.png
│ ├── led1.png
│ ├── led2.png
│ ├── led3.png
│ ├── led4.png
│ ├── led5.png
│ ├── led6.png
│ ├── led7.png
│ ├── led8.png
│ ├── led9.png
│ ├── led_blank.png
│ ├── mine.png
│ └── mine_transparent.png
├── MinesweeperGameState
├── .classpath
├── .gitignore
├── .project
├── .settings
│ └── org.eclipse.jdt.core.prefs
└── src
│ └── minesweeper
│ ├── gamestate
│ ├── GameFactory.java
│ ├── GameStateEasy.java
│ ├── GameStateHard.java
│ ├── GameStateModel.java
│ ├── GameStateModelViewer.java
│ ├── GameStateReader.java
│ ├── GameStateStandard.java
│ ├── GameStateStandardWith8.java
│ └── MoveMethod.java
│ ├── random
│ ├── DefaultRNG.java
│ ├── RNG.java
│ ├── RNGJSF.java
│ ├── RNGJava.java
│ └── RNGKiss64.java
│ ├── settings
│ ├── GameSettings.java
│ └── GameType.java
│ └── structure
│ ├── Action.java
│ ├── Area.java
│ └── Location.java
├── README.md
└── WindowController
├── .classpath
├── .project
├── .settings
└── org.eclipse.jdt.core.prefs
├── bin
└── window
│ └── controller
│ ├── GetWindowRect$GetWindowRectException.class
│ ├── GetWindowRect$MyUser32.class
│ ├── GetWindowRect$WindowNotFoundException.class
│ └── GetWindowRect.class
├── jna-4.2.1.jar
├── jna-platform-4.2.1.jar
└── src
└── window
└── controller
└── GetWindowRect.java
/Asynchronous/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/Asynchronous/.gitignore:
--------------------------------------------------------------------------------
1 | /bin/
2 |
--------------------------------------------------------------------------------
/Asynchronous/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | Asynchronous
4 |
5 |
6 |
7 |
8 |
9 | org.eclipse.jdt.core.javabuilder
10 |
11 |
12 |
13 |
14 |
15 | org.eclipse.jdt.core.javanature
16 |
17 |
18 |
--------------------------------------------------------------------------------
/Asynchronous/.settings/org.eclipse.jdt.core.prefs:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5 | org.eclipse.jdt.core.compiler.compliance=1.8
6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate
8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
11 | org.eclipse.jdt.core.compiler.source=1.8
12 |
--------------------------------------------------------------------------------
/Asynchronous/src/Asynchronous/Asynchronous.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this template, choose Tools | Templates
3 | * and open the template in the editor.
4 | */
5 | package Asynchronous;
6 |
7 | /**
8 | *
9 | * This interface defines an object which can be run asynchronously
10 | */
11 | public interface Asynchronous {
12 |
13 | public void start();
14 |
15 | public void requestStop();
16 |
17 | public V getResult();
18 |
19 | }
20 |
--------------------------------------------------------------------------------
/Images/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | Images
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/Images/ReadMe/BruteForce_deep_analysis1.JPG:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/DavidNHill/Minesweeper/0806bb80042184e4f2b1413f4092c4e09526c237/Images/ReadMe/BruteForce_deep_analysis1.JPG
--------------------------------------------------------------------------------
/Images/ReadMe/GUI.JPG:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/DavidNHill/Minesweeper/0806bb80042184e4f2b1413f4092c4e09526c237/Images/ReadMe/GUI.JPG
--------------------------------------------------------------------------------
/Images/ReadMe/Local_analysis1.JPG:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/DavidNHill/Minesweeper/0806bb80042184e4f2b1413f4092c4e09526c237/Images/ReadMe/Local_analysis1.JPG
--------------------------------------------------------------------------------
/Images/ReadMe/No_information_on_edge.JPG:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/DavidNHill/Minesweeper/0806bb80042184e4f2b1413f4092c4e09526c237/Images/ReadMe/No_information_on_edge.JPG
--------------------------------------------------------------------------------
/Images/ReadMe/Probability_engine1.JPG:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/DavidNHill/Minesweeper/0806bb80042184e4f2b1413f4092c4e09526c237/Images/ReadMe/Probability_engine1.JPG
--------------------------------------------------------------------------------
/Images/ReadMe/Probability_engine2.JPG:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/DavidNHill/Minesweeper/0806bb80042184e4f2b1413f4092c4e09526c237/Images/ReadMe/Probability_engine2.JPG
--------------------------------------------------------------------------------
/Images/ReadMe/Trivial_analysis1.JPG:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/DavidNHill/Minesweeper/0806bb80042184e4f2b1413f4092c4e09526c237/Images/ReadMe/Trivial_analysis1.JPG
--------------------------------------------------------------------------------
/Images/ReadMe/Trivial_analysis2.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/DavidNHill/Minesweeper/0806bb80042184e4f2b1413f4092c4e09526c237/Images/ReadMe/Trivial_analysis2.jpg
--------------------------------------------------------------------------------
/MineSweeperSolver/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/MineSweeperSolver/.gitignore:
--------------------------------------------------------------------------------
1 | /bin/
2 |
--------------------------------------------------------------------------------
/MineSweeperSolver/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | MineSweeperSolver
4 |
5 |
6 |
7 |
8 |
9 | org.eclipse.jdt.core.javabuilder
10 |
11 |
12 |
13 |
14 |
15 | org.eclipse.jdt.core.javanature
16 |
17 |
18 |
--------------------------------------------------------------------------------
/MineSweeperSolver/src/minesweeper/solver/BoardStateCache.java:
--------------------------------------------------------------------------------
1 | package minesweeper.solver;
2 |
3 | import java.util.ArrayList;
4 | import java.util.Iterator;
5 | import java.util.List;
6 |
7 | import minesweeper.structure.Location;
8 |
9 | public class BoardStateCache {
10 |
11 | private final static int[] DX = {0, 1, 1, 1, 0, -1, -1, -1};
12 | private final static int[] DY = {-1, -1, 0, 1, 1, 1, 0, -1};
13 |
14 | protected class Cache {
15 |
16 | private int width;
17 | private int height;
18 |
19 | protected Location[][] locations;
20 |
21 | protected AdjacentSquares[][] adjacentLocations1;
22 | protected AdjacentSquares[][] adjacentLocations2;
23 |
24 | protected Location getLocation(int x, int y) {
25 | return locations[x][y];
26 | }
27 |
28 | }
29 |
30 | // iterator for adjacent squares
31 | protected class AdjacentSquares implements Iterable {
32 |
33 | private Location loc;
34 | private final int size;
35 | private List locations;
36 |
37 | AdjacentSquares(Cache cache, Location l, int width, int height, int size) {
38 | this.loc = l;
39 | this.size = size;
40 |
41 | if (size == 1) {
42 | locations = new ArrayList<>(8);
43 | for (int i=0; i < DX.length; i++) {
44 | if (loc.x + DX[i] >= 0 && loc.x + DX[i] < width && loc.y + DY[i] >= 0 && loc.y + DY[i] < height) {
45 | locations.add(cache.getLocation(loc.x + DX[i], loc.y + DY[i]));
46 | }
47 |
48 | }
49 | } else {
50 | int startX = Math.max(0, loc.x - this.size);
51 | int endX = Math.min(width - 1, loc.x + this.size);
52 |
53 | int startY = Math.max(0, loc.y - this.size);
54 | int endY = Math.min(height - 1, loc.y + this.size);
55 |
56 | locations = new ArrayList<>((this.size * 2 - 1) * (this.size * 2 - 1));
57 | for (int i=startX; i <= endX; i++) {
58 | for (int j=startY; j <= endY; j++) {
59 | if (i == loc.x && j == loc.y) {
60 | // don't send back the central location
61 | } else {
62 | locations.add(cache.getLocation(i,j));
63 | }
64 |
65 | }
66 | }
67 | }
68 |
69 |
70 | }
71 |
72 | @Override
73 | public Iterator iterator() {
74 | return locations.iterator();
75 | }
76 |
77 |
78 | }
79 |
80 | private static List cacheAdjSqu = new ArrayList<>();
81 | private static BoardStateCache me;
82 |
83 |
84 | public synchronized Cache getAdjacentSquares1(int width, int height) {
85 |
86 | for (Cache cache: cacheAdjSqu) {
87 | if (cache.height == height && cache.width == width) {
88 | return cache;
89 | }
90 | }
91 |
92 | Cache cache = new Cache();
93 |
94 | cache.height = height;
95 | cache.width = width;
96 |
97 | cache.locations = new Location[width][height];
98 |
99 | // Create a Location for each entry yon the board
100 | for (int x=0; x < width; x++) {
101 | for (int y=0; y < height; y++) {
102 | cache.locations[x][y] = new Location(x,y);
103 | }
104 | }
105 |
106 | cache.adjacentLocations1 = new AdjacentSquares[width][height];
107 | cache.adjacentLocations2 = new AdjacentSquares[width][height];
108 |
109 | // set up how many adjacent locations there are to each square - they are all unrevealed to start with
110 | for (int x=0; x < width; x++) {
111 | for (int y=0; y < height; y++) {
112 |
113 | cache.adjacentLocations1[x][y] = new AdjacentSquares(cache, cache.getLocation(x,y), width, height, 1);
114 | cache.adjacentLocations2[x][y] = new AdjacentSquares(cache, cache.getLocation(x,y), width, height, 2);
115 |
116 | }
117 | }
118 |
119 | cacheAdjSqu.add(cache);
120 |
121 | return cache;
122 | }
123 |
124 | public static synchronized BoardStateCache getInstance() {
125 |
126 | if (me == null) {
127 | me = new BoardStateCache();
128 | }
129 |
130 | return me;
131 | }
132 |
133 |
134 | }
135 |
--------------------------------------------------------------------------------
/MineSweeperSolver/src/minesweeper/solver/BruteForceAnalysisModel.java:
--------------------------------------------------------------------------------
1 | package minesweeper.solver;
2 |
3 | import java.math.BigDecimal;
4 | import java.util.List;
5 |
6 | import minesweeper.structure.Action;
7 | import minesweeper.structure.Area;
8 | import minesweeper.structure.Location;
9 |
10 | abstract public class BruteForceAnalysisModel {
11 |
12 | protected boolean completed = false;
13 | protected boolean tooMany = false;
14 |
15 | abstract protected void addSolution(byte[] solution);
16 |
17 | abstract protected void process();
18 |
19 |
20 | protected boolean isComplete() {
21 | return this.completed;
22 | }
23 |
24 | protected boolean tooMany() {
25 | return this.tooMany;
26 | }
27 |
28 | protected boolean isShallow() {
29 | return false;
30 | }
31 |
32 | abstract protected int getSolutionCount();
33 |
34 | abstract protected int getMovesProcessed();
35 |
36 | abstract protected int getMovesToProcess();
37 |
38 | abstract protected Location checkForBetterMove(Location location);
39 |
40 | abstract protected long getNodeCount();
41 |
42 | abstract protected Action getNextMove(BoardState boardState);
43 |
44 | abstract protected Location getExpectedMove();
45 |
46 | abstract protected boolean allDead();
47 |
48 | abstract Area getDeadLocations();
49 |
50 | abstract BigDecimal getSolveChance();
51 |
52 | abstract List extends Location> getLocations();
53 |
54 | }
55 |
--------------------------------------------------------------------------------
/MineSweeperSolver/src/minesweeper/solver/LocationEvaluator.java:
--------------------------------------------------------------------------------
1 | package minesweeper.solver;
2 |
3 | import java.util.Collection;
4 | import java.util.List;
5 |
6 | import minesweeper.solver.constructs.CandidateLocation;
7 | import minesweeper.solver.constructs.EvaluatedLocation;
8 | import minesweeper.structure.Action;
9 | import minesweeper.structure.Location;
10 |
11 | public interface LocationEvaluator {
12 |
13 | abstract public Action[] bestMove();
14 | abstract public List getEvaluatedLocations();
15 | abstract public void evaluateLocations();
16 | abstract public void showResults();
17 |
18 | abstract public void evaluateOffEdgeCandidates(List allUnrevealedSquares);
19 | abstract public void addLocations(Collection tiles);
20 | }
21 |
--------------------------------------------------------------------------------
/MineSweeperSolver/src/minesweeper/solver/ProbabilityEngineModel.java:
--------------------------------------------------------------------------------
1 | package minesweeper.solver;
2 |
3 | import java.math.BigDecimal;
4 | import java.math.BigInteger;
5 | import java.util.List;
6 |
7 | import minesweeper.solver.constructs.Box;
8 | import minesweeper.solver.constructs.CandidateLocation;
9 | import minesweeper.solver.constructs.LinkedLocation;
10 | import minesweeper.solver.utility.BinomialCache;
11 | import minesweeper.structure.Area;
12 | import minesweeper.structure.Location;
13 |
14 | abstract public class ProbabilityEngineModel {
15 |
16 | abstract public void process();
17 |
18 | abstract protected long getDuration();
19 | abstract protected long getIndependentGroups();
20 | abstract Box getBox(Location l);
21 | abstract public BigDecimal getSafety(Location l);
22 | abstract protected List getBestCandidates(BigDecimal freshhold, boolean excludeDead);
23 | abstract protected List getProbableMines(BigDecimal freshhold);
24 | abstract protected List getFiftyPercenters();
25 | abstract protected BigInteger getSolutionCount();
26 | //abstract protected BigDecimal getBestOnEdgeProb();
27 | abstract protected BigDecimal getOffEdgeSafety();
28 | abstract protected BigInteger getOffEdgeTally();
29 | abstract protected boolean foundCertainty();
30 | abstract protected Area getDeadLocations();
31 | abstract boolean allDead();
32 | abstract protected int getDeadValueDelta(Location l);
33 | abstract protected List getMines();
34 | abstract protected List getLinkedLocations();
35 | abstract protected LinkedLocation getLinkedLocation(Location tile);
36 | abstract protected List getIsolatedEdges();
37 | abstract protected boolean isBestGuessOffEdge();
38 | abstract protected int getLivingClearCount();
39 | abstract protected int getSafeTileCount();
40 | abstract protected int getTilesOffEdgeCount();
41 | abstract protected List getEmptyBoxes();
42 |
43 | abstract protected BigDecimal getBlendedSafety();
44 | abstract protected BigDecimal getBestSafety();
45 | abstract protected BigDecimal getBestLivingSafety();
46 |
47 | abstract protected Location getSingleSafestTile();
48 |
49 | abstract public void setBinomialCache(BinomialCache cache);
50 |
51 | }
52 |
--------------------------------------------------------------------------------
/MineSweeperSolver/src/minesweeper/solver/SpaceCounter.java:
--------------------------------------------------------------------------------
1 | package minesweeper.solver;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 |
6 | import minesweeper.structure.Location;
7 |
8 | public class SpaceCounter {
9 |
10 | protected final static int[] DX = {0, 1, 1, 1, 0, -1, -1, -1};
11 | protected final static int[] DY = {-1, -1, 0, 1, 1, 1, 0, -1};
12 |
13 | private final BoardState board;
14 | private final int threshold;
15 |
16 | private final byte[][] data;
17 | private final int width;
18 | private final int height;
19 |
20 |
21 | public SpaceCounter(BoardState board, int threshold) {
22 |
23 | this.board = board;
24 | this.threshold = threshold; // an area is considered large if greater than or equal to the threshold
25 | this.height = this.board.getGameHeight();
26 | this.width = this.board.getGameWidth();
27 |
28 | this.data = new byte[this.width][this.height];
29 |
30 | }
31 |
32 |
33 | public boolean meetsThreshold(Location loc) {
34 |
35 | boolean large = false;
36 |
37 |
38 | if (data[loc.x][loc.y] != 0) {
39 | large = (data[loc.x][loc.y] == 1);
40 |
41 | } else {
42 |
43 | int index = 0;
44 |
45 | List tiles = new ArrayList<>();
46 |
47 | tiles.add(loc);
48 |
49 | data[loc.x][loc.y] = -1;
50 |
51 | top: while (tiles.size() != index) {
52 |
53 | Location m = tiles.get(index);
54 |
55 | for (int j=0; j < DX.length; j++) {
56 | final int x1 = m.x + DX[j];
57 | final int y1 = m.y + DY[j];
58 |
59 | if (x1 >= 0 && x1 < this.width && y1 >= 0 && y1 < this.height) {
60 | if (this.board.isUnrevealed(x1, y1)) {
61 | if (data[x1][y1] == 0) { // unprocessed tile
62 | data[x1][y1] = -1;
63 | tiles.add(this.board.getLocation(x1, y1));
64 |
65 | } else if (data[x1][y1] == 1) { // if we meet a tile which is already in a large area, we are in a large area
66 | large = true;
67 | break top;
68 |
69 | } else { // something has gone wrong since we can't encounter a small area
70 | //this.board.getLogger().log(Level.ERROR, "Space counter encountered an area below threshold");
71 | }
72 |
73 | } else if (this.board.isRevealed(x1, y1)) { // if he board is revealed then see if we can hop over the tile into more open space
74 |
75 | for (int k=0; k < DX.length; k++) {
76 | final int x2 = x1 + DX[k];
77 | final int y2 = y1 + DY[k];
78 | if (x2 >= 0 && x2 < this.width && y2 >= 0 && y2 < this.height) {
79 | if (this.board.isUnrevealed(x2, y2)) {
80 | if (data[x2][y2] == 0) { // unprocessed tile
81 | data[x2][y2] = -1;
82 | tiles.add(this.board.getLocation(x2, y2));
83 |
84 | } else if (data[x2][y2] == 1) { // if we meet a tile which is already in a large area, we are in a large area
85 | large = true;
86 | break top;
87 |
88 | }
89 | }
90 | }
91 |
92 |
93 | }
94 | }
95 |
96 | }
97 | }
98 |
99 | if (tiles.size() >= this.threshold) {
100 | large = true;
101 | break;
102 | }
103 |
104 | index++;
105 |
106 | }
107 |
108 | // record the area if it exceeds the threshold
109 | if (large) {
110 | for (Location l: tiles) {
111 | data[l.x][l.y] = 1;
112 | }
113 | }
114 |
115 | }
116 |
117 | if (large) {
118 | return true;
119 | } else {
120 | return false;
121 | }
122 |
123 | }
124 |
125 |
126 | }
127 |
--------------------------------------------------------------------------------
/MineSweeperSolver/src/minesweeper/solver/bulk/BulkEventController.java:
--------------------------------------------------------------------------------
1 | package minesweeper.solver.bulk;
2 |
3 | import java.math.BigDecimal;
4 |
5 | import minesweeper.gamestate.GameStateModel;
6 |
7 | public class BulkEventController {
8 |
9 | private final int gamesToPlay;
10 |
11 | private volatile int gamesPlayed = 0;
12 |
13 | private volatile int failedToStart = 0;
14 | private volatile int wins = 0;
15 | private volatile int guesses = 0;
16 | private volatile int noGuessWins = 0;
17 |
18 | private volatile int totalActions = 0;
19 | private volatile long total3BV = 0;
20 | private volatile long total3BVSolved = 0;
21 | private volatile BigDecimal fairness = BigDecimal.ZERO;
22 | private volatile int currentWinStreak = 0;
23 | private volatile int bestWinStreak = 0;
24 | private volatile int currentMastery = 0;
25 | private volatile int bestMastery = 0;
26 |
27 | private volatile boolean[] mastery = new boolean[100];
28 |
29 | //private volatile BulkEventGame event;
30 | private volatile BulkEventGame finalEvent;
31 |
32 | BulkEventController(int gamesToPlay) {
33 | this.gamesToPlay = gamesToPlay;
34 | }
35 |
36 |
37 | /**
38 | * When the process is finished you can get the final results from here
39 | */
40 | public BulkEventGame getResults() {
41 | return this.finalEvent;
42 | }
43 |
44 |
45 |
46 | void processGame(BulkRequest request, int index) {
47 |
48 | int masteryIndex = request.sequence % 100;
49 |
50 | BulkRequestGame game = request.getRequestGames()[index];
51 |
52 | gamesPlayed++;
53 |
54 | if (game.gs.getGameState() == GameStateModel.WON) {
55 | wins++;
56 |
57 | if (game.guesses == 0) {
58 | noGuessWins++;
59 | }
60 |
61 | currentWinStreak++;
62 | if (currentWinStreak > bestWinStreak) {
63 | bestWinStreak = currentWinStreak;
64 | }
65 |
66 | // if we lost 100 games ago then mastery is 1 more
67 | if (!mastery[masteryIndex]) {
68 | mastery[masteryIndex] = true;
69 | currentMastery++;
70 | if (currentMastery > bestMastery) {
71 | bestMastery = currentMastery;
72 | }
73 | }
74 |
75 | //double efficiency = 100 * ((double) game.gs.getTotal3BV() / (double) game.gs.getActionCount());
76 |
77 |
78 | } else {
79 |
80 | if (!game.startedOkay) {
81 | failedToStart++;
82 | }
83 |
84 | currentWinStreak = 0;
85 |
86 | // if we won 100 games ago, then mastery is now 1 less
87 | if (mastery[masteryIndex]) {
88 | mastery[masteryIndex] = false;
89 | currentMastery--;
90 | }
91 | }
92 |
93 | // accumulate the total actions taken
94 | totalActions = totalActions + game.gs.getActionCount();
95 |
96 | // accumulate 3BV in the game and how much was solved
97 | total3BV = total3BV + game.gs.getTotal3BV();
98 | total3BVSolved = total3BVSolved + game.gs.getCleared3BV();
99 |
100 | // accumulate total guesses made
101 | guesses = guesses + game.guesses;
102 |
103 | fairness = fairness.add(game.fairness);
104 |
105 | }
106 |
107 | BulkEventGame createEvent() {
108 |
109 | BulkEventGame event = new BulkEventGame();
110 |
111 | event.setGamesToPlay(gamesToPlay);
112 | event.setGamesPlayed(gamesPlayed);
113 | event.setGamesWon(wins);
114 |
115 | event.setTotalGuesses(guesses);
116 | event.setNoGuessWins(noGuessWins);
117 | if (guesses != 0) {
118 | event.setFairness(fairness.doubleValue() / guesses);
119 | } else {
120 | event.setFairness(0);
121 | }
122 |
123 | event.setMastery(bestMastery);
124 | event.setWinStreak(bestWinStreak);
125 | event.setTotalActions(totalActions);
126 | event.setFailedToStart(failedToStart);
127 | event.setTotal3BV(total3BV);
128 | event.setTotal3BVSolved(total3BVSolved);
129 |
130 | return event;
131 |
132 | }
133 |
134 |
135 |
136 | }
137 |
--------------------------------------------------------------------------------
/MineSweeperSolver/src/minesweeper/solver/bulk/BulkEventGame.java:
--------------------------------------------------------------------------------
1 | package minesweeper.solver.bulk;
2 |
3 | public class BulkEventGame {
4 |
5 | private int gamesToPlay;
6 | private int gamesPlayed;
7 | private int gamesWon;
8 | private int noGuessWins;
9 | private int totalActions;
10 |
11 | private long total3BV;
12 | private long total3BVSolved;
13 |
14 | private int totalGuesses;
15 | private double fairness;
16 |
17 | private int winStreak;
18 | private int mastery;
19 |
20 | private int failedToStart; // this is when the game didn't survive the start sequence
21 |
22 |
23 | public int getFailedToStart() {
24 | return failedToStart;
25 | }
26 | public int getGamesToPlay() {
27 | return gamesToPlay;
28 | }
29 | public int getGamesPlayed() {
30 | return gamesPlayed;
31 | }
32 | public int getGamesWon() {
33 | return gamesWon;
34 | }
35 | public int getNoGuessWins() {
36 | return noGuessWins;
37 | }
38 | public int getTotalGuesses() {
39 | return totalGuesses;
40 | }
41 | public double getFairness() {
42 | return fairness;
43 | }
44 | public int getWinStreak() {
45 | return winStreak;
46 | }
47 | public int getMastery() {
48 | return mastery;
49 | }
50 | public int getTotalActions() {
51 | return totalActions;
52 | }
53 |
54 | public long getTotal3BV() {
55 | return total3BV;
56 | }
57 | public long getTotal3BVSolved() {
58 | return total3BVSolved;
59 | }
60 |
61 | protected void setFailedToStart(int failedToStart) {
62 | this.failedToStart = failedToStart;
63 | }
64 | protected void setGamesToPlay(int gamesToPlay) {
65 | this.gamesToPlay = gamesToPlay;
66 | }
67 | protected void setGamesPlayed(int gamesPlayed) {
68 | this.gamesPlayed = gamesPlayed;
69 | }
70 | protected void setGamesWon(int gamesWon) {
71 | this.gamesWon = gamesWon;
72 | }
73 | protected void setNoGuessWins(int noGuessWins) {
74 | this.noGuessWins = noGuessWins;
75 | }
76 | protected void setTotalGuesses(int totalGuesses) {
77 | this.totalGuesses = totalGuesses;
78 | }
79 | protected void setFairness(double fairness) {
80 | this.fairness = fairness;
81 | }
82 | protected void setWinStreak(int winStreak) {
83 | this.winStreak = winStreak;
84 | }
85 | protected void setMastery(int mastery) {
86 | this.mastery = mastery;
87 | }
88 | protected void setTotalActions(int actions) {
89 | this.totalActions = actions;
90 | }
91 | protected void setTotal3BV(long total3bv) {
92 | total3BV = total3bv;
93 | }
94 | protected void setTotal3BVSolved(long total3bvSolved) {
95 | total3BVSolved = total3bvSolved;
96 | }
97 |
98 |
99 | }
100 |
--------------------------------------------------------------------------------
/MineSweeperSolver/src/minesweeper/solver/bulk/BulkEventMain.java:
--------------------------------------------------------------------------------
1 | package minesweeper.solver.bulk;
2 |
3 | public class BulkEventMain {
4 |
5 | private int gamesToPlay;
6 | private int gamesPlayed;
7 |
8 | private long timeSoFar;
9 | private long estimatedTimeLeft;
10 |
11 | private BulkEventGame[] events;
12 | private boolean isFinished = false;
13 |
14 | public int getGamesToPlay() {
15 | return gamesToPlay;
16 | }
17 | public int getGamesPlayed() {
18 | return gamesPlayed;
19 | }
20 |
21 | protected void setGamesToPlay(int gamesToPlay) {
22 | this.gamesToPlay = gamesToPlay;
23 | }
24 | protected void setGamesPlayed(int gamesPlayed) {
25 | this.gamesPlayed = gamesPlayed;
26 | }
27 | public long getTimeSoFar() {
28 | return timeSoFar;
29 | }
30 | public long getEstimatedTimeLeft() {
31 | return estimatedTimeLeft;
32 | }
33 | protected void setTimeSoFar(long timeSoFar) {
34 | this.timeSoFar = timeSoFar;
35 | }
36 | protected void setEstimatedTimeLeft(long estimatedTimeLeft) {
37 | this.estimatedTimeLeft = estimatedTimeLeft;
38 | }
39 | public boolean isFinished() {
40 | return isFinished;
41 | }
42 | protected void setFinished(boolean isFinished) {
43 | this.isFinished = isFinished;
44 | }
45 |
46 | public BulkEventGame[] getGameEvents() {
47 | return events;
48 | }
49 | protected void setGameEvents(BulkEventGame[] events) {
50 | this.events = events;
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/MineSweeperSolver/src/minesweeper/solver/bulk/BulkListener.java:
--------------------------------------------------------------------------------
1 | package minesweeper.solver.bulk;
2 |
3 | public abstract class BulkListener {
4 |
5 | /**
6 | * This is run at regular intervals and should be used to provide any out put that is needed
7 | */
8 | abstract public void intervalAction(BulkEventMain event);
9 |
10 | }
11 |
--------------------------------------------------------------------------------
/MineSweeperSolver/src/minesweeper/solver/bulk/BulkPlayer.java:
--------------------------------------------------------------------------------
1 | package minesweeper.solver.bulk;
2 |
3 | import java.util.Random;
4 |
5 | import minesweeper.gamestate.GameFactory;
6 | import minesweeper.gamestate.GameStateModel;
7 | import minesweeper.settings.GameSettings;
8 | import minesweeper.settings.GameType;
9 | import minesweeper.solver.settings.SolverSettings;
10 |
11 | public class BulkPlayer extends BulkController {
12 |
13 | protected final GameType gameType;
14 | protected final GameSettings gameSettings;
15 | //private List preActions;
16 |
17 | /**
18 | * Use the bulk controller to play games from the beginning
19 | */
20 | public BulkPlayer(Random seeder, int gamesToPlay, GameType gameType, GameSettings gameSettings, SolverSettings solverSettings, int workers) {
21 | super(seeder, gamesToPlay, solverSettings, workers);
22 | this.gameType = gameType;
23 | this.gameSettings = gameSettings;
24 |
25 | }
26 |
27 | public BulkPlayer(Random seeder, int gamesToPlay, GameType gameType, GameSettings gameSettings, SolverSettings solverSettings, int workers, int bufferPerWorker) {
28 | super(seeder, gamesToPlay, new SolverSettings[] {solverSettings}, workers, bufferPerWorker);
29 | this.gameType = gameType;
30 | this.gameSettings = gameSettings;
31 | }
32 |
33 | public BulkPlayer(Random seeder, int gamesToPlay, GameType gameType, GameSettings gameSettings, SolverSettings[] solverSettings, int workers, int bufferPerWorker) {
34 | super(seeder, gamesToPlay, solverSettings, workers, bufferPerWorker);
35 | this.gameType = gameType;
36 | this.gameSettings = gameSettings;
37 | }
38 |
39 | protected GameStateModel getGameState(long seed) {;
40 |
41 | GameStateModel gs = GameFactory.create(gameType, gameSettings, seed);
42 |
43 | return gs;
44 |
45 | }
46 |
47 |
48 | }
49 |
--------------------------------------------------------------------------------
/MineSweeperSolver/src/minesweeper/solver/bulk/BulkRequest.java:
--------------------------------------------------------------------------------
1 | package minesweeper.solver.bulk;
2 |
3 | public class BulkRequest {
4 |
5 | protected final static BulkRequest WAIT = new BulkRequest() {
6 | {
7 | action = BulkAction.WAIT;
8 | }
9 | };
10 |
11 | protected final static BulkRequest STOP = new BulkRequest() {
12 | {
13 | action = BulkAction.STOP;
14 | }
15 | };
16 |
17 | public enum BulkAction {
18 | STOP,
19 | WAIT,
20 | RUN;
21 | }
22 |
23 | protected BulkAction action;
24 | protected int sequence; // the sequence number for this request
25 | protected int slot; // the slot the request is to be store in the buffer
26 |
27 | protected BulkRequestGame[] games;
28 |
29 | public BulkRequestGame[] getRequestGames( ) {
30 | return this.games;
31 | }
32 |
33 |
34 | }
35 |
--------------------------------------------------------------------------------
/MineSweeperSolver/src/minesweeper/solver/bulk/BulkRequestGame.java:
--------------------------------------------------------------------------------
1 | package minesweeper.solver.bulk;
2 |
3 | import java.math.BigDecimal;
4 |
5 | import minesweeper.gamestate.GameStateModel;
6 | import minesweeper.solver.settings.SolverSettings;
7 |
8 | public class BulkRequestGame {
9 |
10 | protected SolverSettings solverSettings;
11 | protected GameStateModel gs;
12 |
13 | protected int guesses = 0;
14 | protected BigDecimal fairness = BigDecimal.ZERO;
15 | protected boolean startedOkay = true;
16 |
17 | public GameStateModel getGame( ) {
18 | return this.gs;
19 | }
20 |
21 | public int getGuesses() {
22 | return this.guesses;
23 | }
24 |
25 | }
26 |
--------------------------------------------------------------------------------
/MineSweeperSolver/src/minesweeper/solver/bulk/BulkRollout.java:
--------------------------------------------------------------------------------
1 | package minesweeper.solver.bulk;
2 |
3 | import java.util.Random;
4 |
5 | import minesweeper.gamestate.GameStateModel;
6 | import minesweeper.solver.RolloutGenerator;
7 | import minesweeper.solver.settings.SolverSettings;
8 | import minesweeper.structure.Action;
9 | import minesweeper.structure.Location;
10 |
11 | public class BulkRollout extends BulkController {
12 |
13 | protected final RolloutGenerator generator;
14 | protected final Location safeTile;
15 | protected final Location startTile;
16 |
17 | /**
18 | * Use the bulk controller to play games from the beginning
19 | */
20 | public BulkRollout(Random seeder, int gamesToPlay, RolloutGenerator generator, Location startTile, boolean safeStart, SolverSettings solverSettings, int workers) {
21 | super(seeder, gamesToPlay, solverSettings, workers);
22 |
23 | this.generator = generator;
24 | this.startTile = startTile;
25 |
26 | if (safeStart) {
27 | this.safeTile = startTile;
28 | } else {
29 | this.safeTile = null;
30 | }
31 |
32 | }
33 |
34 |
35 | protected GameStateModel getGameState(long seed) {
36 |
37 | GameStateModel gs = generator.generateGame(seed, safeTile);
38 |
39 | // play the start tile and return the game
40 | gs.doAction(new Action(startTile, Action.CLEAR));
41 |
42 | return gs;
43 |
44 | }
45 |
46 |
47 | }
48 |
--------------------------------------------------------------------------------
/MineSweeperSolver/src/minesweeper/solver/bulk/GamePostListener.java:
--------------------------------------------------------------------------------
1 | package minesweeper.solver.bulk;
2 |
3 | /**
4 | * The "postAction" method is run after each game finishes
5 | */
6 | public abstract class GamePostListener {
7 |
8 | /**
9 | * This is run after each game finishes
10 | */
11 | abstract public void postAction(BulkRequest request);
12 |
13 | /**
14 | * Place the results you want to show here
15 | */
16 | abstract public void postResults();
17 |
18 | }
19 |
--------------------------------------------------------------------------------
/MineSweeperSolver/src/minesweeper/solver/bulk/GamePreListener.java:
--------------------------------------------------------------------------------
1 | package minesweeper.solver.bulk;
2 |
3 | import minesweeper.gamestate.GameStateModel;
4 |
5 | public abstract class GamePreListener {
6 |
7 | /**
8 | * This is run before each game starts
9 | */
10 | abstract public void preAction(GameStateModel game);
11 |
12 | }
13 |
--------------------------------------------------------------------------------
/MineSweeperSolver/src/minesweeper/solver/bulk/StaticCounter.java:
--------------------------------------------------------------------------------
1 | package minesweeper.solver.bulk;
2 |
3 | public class StaticCounter {
4 |
5 | public enum SCType {
6 | LONG_5050 (0, "Long 50/50"),
7 | RECURSIVE_SAFETY_CACHE_HIT (1, "Recursive Safety cache hit")
8 | ;
9 |
10 | final public String name;
11 | final private int index;
12 |
13 | private SCType(int index, String name) {
14 | this.index = index;
15 | this.name = name;
16 | }
17 | }
18 |
19 | private final static long[] counter = new long[SCType.values().length];
20 |
21 | private StaticCounter() {
22 | }
23 |
24 | final static public void count(SCType type) {
25 | counter[type.index]++;
26 | }
27 |
28 |
29 | final static public long report(SCType type) {
30 | return counter[type.index];
31 | }
32 |
33 |
34 | }
35 |
--------------------------------------------------------------------------------
/MineSweeperSolver/src/minesweeper/solver/coach/CoachModel.java:
--------------------------------------------------------------------------------
1 | package minesweeper.solver.coach;
2 |
3 | public interface CoachModel {
4 |
5 |
6 | abstract public void clearScreen();
7 |
8 | abstract public void writeLine(String text);
9 |
10 | abstract public void setOkay();
11 |
12 | abstract public void setWarn();
13 |
14 | abstract public void setError();
15 |
16 | abstract public void kill();
17 |
18 | abstract public boolean analyseFlags();
19 |
20 | }
21 |
--------------------------------------------------------------------------------
/MineSweeperSolver/src/minesweeper/solver/coach/CoachSilent.java:
--------------------------------------------------------------------------------
1 | package minesweeper.solver.coach;
2 |
3 | /**
4 | * A implementation of the CoachModel class which does nothing
5 | * @author David
6 | *
7 | */
8 | public class CoachSilent implements CoachModel {
9 |
10 |
11 | @Override
12 | public void clearScreen() {
13 | }
14 |
15 | @Override
16 | public void writeLine(String text) {
17 | }
18 |
19 | @Override
20 | public void setOkay() {
21 | }
22 |
23 | @Override
24 | public void setWarn() {
25 | }
26 |
27 | @Override
28 | public void setError() {
29 | }
30 |
31 | @Override
32 | public void kill() {
33 | }
34 |
35 | @Override
36 | public boolean analyseFlags() {
37 | return false;
38 | }
39 |
40 | }
41 |
--------------------------------------------------------------------------------
/MineSweeperSolver/src/minesweeper/solver/constructs/CandidateLocation.java:
--------------------------------------------------------------------------------
1 | package minesweeper.solver.constructs;
2 |
3 | import java.math.BigDecimal;
4 | import java.util.Comparator;
5 |
6 | import minesweeper.gamestate.MoveMethod;
7 | import minesweeper.structure.Action;
8 | import minesweeper.structure.Location;
9 | public class CandidateLocation extends Location {
10 |
11 | private final BigDecimal prob;
12 | private String description = "";
13 | private final int adjSquares;
14 | private final int adjFlags;
15 | private final boolean dead; // Whether the tile is dead
16 | private final boolean deferGuessing; // Whether the tile is not a good idea to guess
17 |
18 | public CandidateLocation(int x, int y, BigDecimal prob, int adjSquares, int adjFlags) {
19 | this(x, y, prob, adjSquares, adjFlags, false, false);
20 |
21 | }
22 |
23 | public CandidateLocation(int x, int y, BigDecimal prob, int adjSquares, int adjFlags, boolean dead, boolean deferGuessing) {
24 | super(x,y);
25 |
26 | this.prob = prob;
27 | this.adjSquares = adjSquares;
28 | this.adjFlags = adjFlags;
29 | this.dead = dead;
30 | this.deferGuessing = deferGuessing;
31 |
32 | }
33 |
34 | public BigDecimal getProbability() {
35 | return this.prob;
36 | }
37 |
38 | public boolean isDead() {
39 | return this.dead;
40 | }
41 |
42 | public void setDescription(String desc) {
43 | this.description = desc;
44 | }
45 |
46 | public void appendDescription(String desc) {
47 | if (this.description != "") {
48 | this.description = this.description + " " + desc;
49 | } else {
50 | this.description = desc;
51 | }
52 |
53 | }
54 |
55 | public String getDescription() {
56 | return this.description;
57 | }
58 |
59 | public boolean getDeferGuessing() {
60 | return this.deferGuessing;
61 | }
62 |
63 | public Action buildAction(MoveMethod method) {
64 |
65 | String comment = description;
66 |
67 | if (prob.compareTo(BigDecimal.ZERO) == 0) { // the best move is to place a flag
68 | return new Action(this, Action.FLAG, method, comment, BigDecimal.ONE);
69 | } else {
70 | return new Action(this, Action.CLEAR, method, comment, prob);
71 | }
72 |
73 | }
74 |
75 | /**
76 | * This sorts by highest probability of not being a mine then the number of adjacent flags, unrevealed squares and finally Location order
77 | */
78 | static public final Comparator SORT_BY_PROB_FLAG_FREE = new Comparator() {
79 | @Override
80 | public int compare(CandidateLocation o1, CandidateLocation o2) {
81 |
82 | int c = 0;
83 |
84 | c = -o1.prob.compareTo(o2.prob); // highest probability first
85 | if (c == 0) {
86 | if (o1.deferGuessing && !o2.deferGuessing) {
87 | c = -1;
88 | } else if (!o1.deferGuessing && o2.deferGuessing) {
89 | c = 1;
90 | } else {
91 | c = -(o1.adjFlags - o2.adjFlags); // highest number of flags 2nd
92 | if (c == 0) {
93 | c= o1.adjSquares - o2.adjSquares; // lowest adjacent free squares
94 | if (c == 0) {
95 | c = o1.sortOrder - o2.sortOrder; // location order
96 | }
97 | }
98 | }
99 | }
100 |
101 | return c;
102 |
103 | }
104 | };
105 |
106 | /**
107 | * This sorts by highest probability of not being a mine then the number unrevealed squares (lowest first), then of adjacent flags (highest first) ,and finally Location order
108 | */
109 | static public final Comparator SORT_BY_PROB_FREE_FLAG = new Comparator() {
110 | @Override
111 | public int compare(CandidateLocation o1, CandidateLocation o2) {
112 |
113 | int c = -o1.prob.compareTo(o2.prob); // highest probability first
114 | if (c == 0) {
115 | int a1, a2;
116 | if (o1.adjSquares == 0) {
117 | a1 = 9;
118 | } else {
119 | a1 = o1.adjSquares;
120 | }
121 | if (o2.adjSquares == 0) {
122 | a2 = 9;
123 | } else {
124 | a2 = o2.adjSquares;
125 | }
126 | c= a1 - a2; // lowest adjacent free squares (except zero treated as 9)
127 | if (c == 0) {
128 | c = -(o1.adjFlags - o2.adjFlags); // highest number of flags
129 | if (c == 0) {
130 | c = o1.sortOrder - o2.sortOrder; // location order
131 | }
132 | }
133 | }
134 |
135 | return c;
136 |
137 | }
138 | };
139 |
140 | }
141 |
--------------------------------------------------------------------------------
/MineSweeperSolver/src/minesweeper/solver/constructs/ChordLocation.java:
--------------------------------------------------------------------------------
1 | package minesweeper.solver.constructs;
2 |
3 | import java.math.BigDecimal;
4 | import java.util.Comparator;
5 | import java.util.List;
6 |
7 | import minesweeper.structure.Location;
8 |
9 | public class ChordLocation extends Location {
10 |
11 | private final int benefit;
12 | private final int cost;
13 | private final int exposedTiles;
14 | private final BigDecimal netBenefit;
15 | private final BigDecimal scale;
16 | private final List mines;
17 |
18 | public ChordLocation(int x, int y, int benefit, int cost, int exposedTiles, BigDecimal scale, List mines) {
19 | super(x, y);
20 |
21 | this.benefit = benefit;
22 | this.cost = cost;
23 | this.exposedTiles = exposedTiles;
24 |
25 | this.netBenefit = chordReward(benefit, cost).multiply(scale);
26 | //this.netBenefit = chordReward(benefit, cost);
27 |
28 | this.scale = scale; // probability of being a mine
29 | this.mines = mines;
30 | }
31 |
32 | public int getBenefit() {
33 | return this.benefit;
34 | }
35 |
36 | public int getCost() {
37 | return this.cost;
38 | }
39 |
40 | public BigDecimal getNetBenefit() {
41 | return this.netBenefit;
42 | }
43 |
44 | public int getExposedTileCount() {
45 | return this.exposedTiles;
46 | }
47 |
48 | public List getMines() {
49 | return this.mines;
50 | }
51 |
52 | public BigDecimal getScale() {
53 | return this.scale;
54 | }
55 |
56 | static public final BigDecimal chordReward(int benefit, int cost) {
57 |
58 | BigDecimal netBenefit; // benefit as a ratio of the cost
59 |
60 | /*
61 | if (cost == 0) {
62 | netBenefit = BigDecimal.valueOf(benefit);
63 | } else {
64 | netBenefit = BigDecimal.valueOf(benefit - cost).divide(BigDecimal.valueOf(cost), 10, RoundingMode.HALF_UP); // benefit as a ratio of the cost
65 | }
66 | */
67 |
68 | /*
69 | if (cost == 0) {
70 | netBenefit = BigDecimal.valueOf(benefit);
71 | } else {
72 | netBenefit = BigDecimal.valueOf(benefit).divide(BigDecimal.valueOf(cost), 10, RoundingMode.HALF_UP); // benefit as a ratio of the cost
73 | }
74 | */
75 |
76 | netBenefit = BigDecimal.valueOf(benefit - cost); // absolute benefit without regard for the cost
77 |
78 | return netBenefit;
79 |
80 | }
81 |
82 | static public final Comparator SORT_BY_BENEFIT_DESC = new Comparator() {
83 | @Override
84 | public int compare(ChordLocation o1, ChordLocation o2) {
85 |
86 | int c = o2.netBenefit.compareTo(o1.netBenefit);
87 |
88 | if (c==0) {
89 | c = o2.exposedTiles - o1.exposedTiles;
90 | }
91 |
92 | if (c==0) {
93 | c = o2.scale.compareTo(o1.scale);
94 | }
95 |
96 | return c;
97 |
98 | //if (o2.netBenefit == o1.netBenefit) {
99 | // return o2.exposedTiles - o1.exposedTiles;
100 | //} else {
101 | // return o2.netBenefit.compareTo(o1.netBenefit);
102 | //}
103 |
104 | }
105 | };
106 |
107 |
108 | }
109 |
--------------------------------------------------------------------------------
/MineSweeperSolver/src/minesweeper/solver/constructs/EvaluatedLocation.java:
--------------------------------------------------------------------------------
1 | package minesweeper.solver.constructs;
2 |
3 | import java.math.BigDecimal;
4 | import java.math.RoundingMode;
5 | import java.util.Comparator;
6 | import java.util.List;
7 |
8 | import minesweeper.gamestate.MoveMethod;
9 | import minesweeper.structure.Action;
10 | import minesweeper.structure.Location;
11 |
12 | public class EvaluatedLocation extends Location {
13 |
14 | private final BigDecimal safeProbability;
15 | private BigDecimal weight;
16 | private final BigDecimal maxValueProgress;
17 | private String description = "";
18 | private BigDecimal expectedClears;
19 | private final int fixedClears; //number of tiles which are clears regardless of what value is revealed
20 | private List emptyBoxes;
21 | private boolean pruned = false;
22 | private boolean deferGuessing = false;
23 | private Location dominatingLocation; // this tile is a better choice
24 |
25 | public EvaluatedLocation(int x, int y, BigDecimal safeProbability, BigDecimal weight, BigDecimal expectedClears, int fixedClears,
26 | List emptyBoxes, BigDecimal maxValueProgress) {
27 | super(x,y);
28 |
29 | this.safeProbability = safeProbability;
30 | this.weight = weight.setScale(8, RoundingMode.UP); // give a slight bump up, so those coming later have to be actually better
31 | this.expectedClears = expectedClears;
32 | this.fixedClears = fixedClears;
33 | this.maxValueProgress = maxValueProgress;
34 | this.emptyBoxes = emptyBoxes;
35 |
36 | }
37 |
38 | public BigDecimal getProbability() {
39 | return this.safeProbability;
40 | }
41 |
42 | public BigDecimal getWeighting() {
43 | return this.weight;
44 | }
45 |
46 | public BigDecimal getMaxValueProgress() {
47 | return maxValueProgress;
48 | }
49 |
50 | public List getEmptyBoxes() {
51 | return emptyBoxes;
52 | }
53 |
54 |
55 | public Action buildAction(MoveMethod method) {
56 |
57 | String comment = description;
58 |
59 | return new Action(this, Action.CLEAR, method, comment, safeProbability);
60 |
61 | }
62 |
63 | public void setPruned() {
64 | this.pruned = true;
65 | }
66 |
67 | public void setDeferGuessing(boolean deferGuessing) {
68 | this.deferGuessing = deferGuessing;
69 | }
70 |
71 | public boolean isDeferGuessing() {
72 | return this.deferGuessing;
73 | }
74 |
75 | public void setDominatingLocation(Location loc) {
76 | this.dominatingLocation = loc;
77 | }
78 |
79 | public Location getDominatingLocation() {
80 | return this.dominatingLocation;
81 | }
82 |
83 | @Override
84 | public String toString() {
85 |
86 | String prunedString;
87 | if (this.pruned) {
88 | prunedString = " ** Pruned";
89 | } else {
90 | prunedString = "";
91 | }
92 |
93 | return super.toString() + " Fixed clears is " + fixedClears + " expected clears is " + expectedClears.toPlainString()
94 | + ", final weight is " + weight + ", maximum tile value prob is " + maxValueProgress + ", defer guessing " + deferGuessing + prunedString;
95 |
96 | }
97 |
98 | /**
99 | * This sorts by ...
100 | */
101 | static public final Comparator SORT_BY_WEIGHT = new Comparator() {
102 | @Override
103 | public int compare(EvaluatedLocation o1, EvaluatedLocation o2) {
104 |
105 |
106 | int c = 0;
107 |
108 | if (c == 0) {
109 | if (o1.deferGuessing && !o2.deferGuessing) {
110 | c = 1;
111 | } else if (!o1.deferGuessing && o2.deferGuessing) {
112 | c = -1;
113 | }
114 | }
115 |
116 | if (c == 0) {
117 | c = -o1.weight.compareTo(o2.weight); // tile with the highest weighting
118 | }
119 |
120 | if (c == 0) {
121 | c = -o1.expectedClears.compareTo(o2.expectedClears); // then highest expected number of clears
122 | }
123 |
124 | return c;
125 |
126 | }
127 | };
128 |
129 | static public final Comparator SORT_BY_SAFETY_MINIMAX = new Comparator() {
130 | @Override
131 | public int compare(EvaluatedLocation o1, EvaluatedLocation o2) {
132 |
133 |
134 | int c = 0;
135 |
136 | c = -o1.safeProbability.compareTo(o2.safeProbability); // safest tiles
137 |
138 | if (c == 0) {
139 | c = o1.maxValueProgress.compareTo(o2.maxValueProgress); // then lowest max value ... the Minimax;
140 | }
141 |
142 | // go back to the weight option
143 | if (c == 0) {
144 | c = SORT_BY_WEIGHT.compare(o1, o2);
145 | }
146 |
147 | return c;
148 |
149 | }
150 | };
151 |
152 |
153 | }
154 |
--------------------------------------------------------------------------------
/MineSweeperSolver/src/minesweeper/solver/constructs/InformationLocation.java:
--------------------------------------------------------------------------------
1 | package minesweeper.solver.constructs;
2 |
3 | import java.math.BigDecimal;
4 | import java.math.RoundingMode;
5 | import java.util.ArrayList;
6 | import java.util.List;
7 |
8 | import minesweeper.solver.ProgressEvaluator;
9 | import minesweeper.solver.Solver;
10 | import minesweeper.structure.Location;
11 |
12 | public class InformationLocation extends Location {
13 |
14 | //public final static BigDecimal ONE_HUNDRED = BigDecimal.valueOf(100);
15 |
16 | public class ByValue {
17 |
18 | public final int value;
19 | public final int clears;
20 | public final BigDecimal probability;
21 |
22 | private ByValue(int value, int clears, BigDecimal prob) {
23 | this.value = value;
24 | this.clears = clears;
25 | this.probability = prob;
26 | }
27 |
28 | }
29 |
30 |
31 | private BigDecimal safety;
32 | private BigDecimal expectedClears;
33 | private BigDecimal progressProbability;
34 | private BigDecimal weighting;
35 | private BigDecimal expectedSolutionSpaceReduction;
36 | private BigDecimal mTanzerRatio;
37 | private BigDecimal secondarySafety;
38 |
39 | private BigDecimal longTermSafety;
40 | //private BigDecimal poweredRatio;
41 |
42 |
43 | private List byValues;
44 |
45 | public InformationLocation(int x, int y) {
46 | super(x, y);
47 | }
48 |
49 | public void calculate() {
50 |
51 | BigDecimal expClears = BigDecimal.ZERO;
52 | BigDecimal progProb = BigDecimal.ZERO;
53 | BigDecimal ess = BigDecimal.ONE.subtract(this.safety); // expect solution space = p(mine) + sum[ P(n)*p(n) ]
54 | BigDecimal powerClears = BigDecimal.ZERO;
55 |
56 |
57 | if (byValues == null) {
58 | return;
59 | }
60 |
61 | for (ByValue bv: byValues) {
62 |
63 | //essr = essr.add(bv.probability.multiply(BigDecimal.ONE.subtract(bv.probability))); // sum of p(1-p)
64 | ess = ess.add(bv.probability.multiply(bv.probability)); // sum of p^2
65 |
66 | if (bv.clears != 0) {
67 | progProb = progProb.add(bv.probability);
68 | expClears = expClears.add(bv.probability.multiply(BigDecimal.valueOf(bv.clears)));
69 | //powerClears = powerClears.add(bv.probability.multiply(new BigDecimal(Math.pow(1.15d, bv.clears))));
70 | }
71 |
72 | }
73 |
74 | this.expectedClears = expClears;
75 | this.progressProbability = progProb;
76 | this.expectedSolutionSpaceReduction = ess;
77 |
78 | BigDecimal bonus = BigDecimal.ONE.add(progressProbability.multiply(ProgressEvaluator.PROGRESS_VALUE));
79 |
80 | this.weighting = this.safety.multiply(bonus);
81 |
82 | if (this.safety.compareTo(BigDecimal.ONE) != 0) {
83 | this.mTanzerRatio = this.progressProbability.divide(BigDecimal.ONE.subtract(safety), 4, RoundingMode.HALF_DOWN);
84 | }
85 |
86 | //if (this.prob.compareTo(BigDecimal.ONE) != 0) {
87 | // this.poweredRatio = powerClears.divide(BigDecimal.ONE.subtract(prob), 4, RoundingMode.HALF_DOWN);
88 | //}
89 |
90 | }
91 |
92 | public BigDecimal getSafety() {
93 | return this.safety;
94 | }
95 |
96 | public void setSafety(BigDecimal prob) {
97 | this.safety = prob;
98 | }
99 |
100 | public void setByValue(int value, int clears, BigDecimal prob) {
101 | if (byValues == null) {
102 | byValues = new ArrayList<>();
103 | }
104 |
105 | byValues.add(new ByValue(value, clears, prob));
106 |
107 | }
108 |
109 | public void setSecondarySafety(BigDecimal safety2) {
110 | this.secondarySafety = safety2;
111 | }
112 |
113 | public void setLongTermSafety(BigDecimal lts) {
114 | this.longTermSafety = lts;
115 | }
116 |
117 | public BigDecimal getSecondarySafety() {
118 | return this.secondarySafety;
119 | }
120 |
121 | public BigDecimal getLongTermSafety() {
122 | return this.longTermSafety;
123 | }
124 |
125 | public List getByValueData() {
126 | return this.byValues;
127 | }
128 |
129 | public BigDecimal getExpectedClears() {
130 | return expectedClears;
131 | }
132 |
133 | public BigDecimal getProgressProbability() {
134 | return progressProbability;
135 | }
136 |
137 | public BigDecimal getWeighting() {
138 | return weighting;
139 | }
140 |
141 | public BigDecimal getExpectedSolutionSpaceReduction() {
142 | return this.expectedSolutionSpaceReduction;
143 | }
144 |
145 | public BigDecimal getMTanzerRatio() {
146 | return this.mTanzerRatio;
147 | }
148 |
149 | //public BigDecimal getPoweredRatio() {
150 | // return this.poweredRatio;
151 | //}
152 | }
153 |
--------------------------------------------------------------------------------
/MineSweeperSolver/src/minesweeper/solver/constructs/LinkedLocation.java:
--------------------------------------------------------------------------------
1 | package minesweeper.solver.constructs;
2 |
3 | import java.util.Comparator;
4 | import java.util.HashSet;
5 | import java.util.List;
6 | import java.util.Set;
7 |
8 | import minesweeper.structure.Location;
9 |
10 | public class LinkedLocation extends Location {
11 |
12 | private Set partners = new HashSet<>();
13 |
14 | private int links = 0;
15 |
16 | public LinkedLocation(int x, int y, List extends Location> partner) {
17 | super(x, y);
18 |
19 | incrementLinks(partner);
20 |
21 | }
22 |
23 | public void incrementLinks(List extends Location> partner) {
24 |
25 | for (Location p: partner) {
26 | if (partners.add(p)) {
27 | links++;
28 | }
29 | }
30 | }
31 |
32 | public int getLinksCount() {
33 | return links;
34 | }
35 |
36 | public Set getLinkedLocations() {
37 | return partners;
38 | }
39 |
40 | static public final Comparator SORT_BY_LINKS_DESC = new Comparator() {
41 | @Override
42 | public int compare(LinkedLocation o1, LinkedLocation o2) {
43 | return o2.links - o1.links;
44 | }
45 | };
46 |
47 | }
48 |
--------------------------------------------------------------------------------
/MineSweeperSolver/src/minesweeper/solver/constructs/LocationValue.java:
--------------------------------------------------------------------------------
1 | package minesweeper.solver.constructs;
2 |
3 | import minesweeper.structure.Location;
4 |
5 | public class LocationValue extends Location {
6 |
7 |
8 | private final int value;
9 |
10 | public LocationValue(Location loc, int value) {
11 | super(loc.x, loc.y);
12 | this.value = value;
13 | }
14 |
15 | public int getValue() {
16 | return this.value;
17 | }
18 |
19 | }
20 |
--------------------------------------------------------------------------------
/MineSweeperSolver/src/minesweeper/solver/constructs/Square.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this template, choose Tools | Templates
3 | * and open the template in the editor.
4 | */
5 | package minesweeper.solver.constructs;
6 |
7 | import java.util.ArrayList;
8 | import java.util.List;
9 |
10 | import minesweeper.structure.Location;
11 |
12 | /**
13 | *
14 | * @author David
15 | */
16 | public class Square extends Location {
17 |
18 | private final List witnesses = new ArrayList<>();
19 |
20 | private int webNum = 0;
21 |
22 |
23 | public Square(Location loc) {
24 | super(loc.x, loc.y);
25 |
26 | }
27 |
28 | public void addWitness(Witness wit) {
29 | witnesses.add(wit);
30 | }
31 |
32 | public List getWitnesses() {
33 | return witnesses;
34 | }
35 |
36 |
37 | public int getWebNum() {
38 | return webNum;
39 | }
40 |
41 | public void setWebNum(int webNum) {
42 | this.webNum = webNum;
43 | }
44 |
45 | }
46 |
--------------------------------------------------------------------------------
/MineSweeperSolver/src/minesweeper/solver/constructs/Witness.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this template, choose Tools | Templates
3 | * and open the template in the editor.
4 | */
5 | package minesweeper.solver.constructs;
6 |
7 | import java.util.ArrayList;
8 | import java.util.Comparator;
9 | import java.util.List;
10 |
11 | import minesweeper.solver.Solver;
12 | import minesweeper.structure.Location;
13 |
14 | /**
15 | *
16 | * @author David
17 | */
18 | public class Witness extends Location {
19 |
20 | private final int minesToFind; // the number of mines left to find
21 |
22 | private final int iterations;
23 |
24 | private int webNum = 0;
25 |
26 | private final List squares;
27 |
28 | private final List boxes = new ArrayList<>();
29 |
30 | private boolean processed = false;
31 |
32 | public Witness(Location loc, int mines, List adjSqu) {
33 | super(loc.x, loc.y);
34 |
35 | this.minesToFind = mines;
36 | squares = adjSqu;
37 |
38 | this.iterations = Solver.combination(mines, squares.size()).intValue();
39 |
40 | }
41 |
42 | public List getSquares() {
43 | return this.squares;
44 | }
45 |
46 | public int getMines() {
47 | return minesToFind;
48 | }
49 |
50 | public void addSquare(Square squ) {
51 |
52 | squares.add(squ);
53 |
54 | }
55 |
56 | public void addBox(Box box) {
57 | boxes.add(box);
58 | }
59 |
60 | public List getBoxes() {
61 | return this.boxes;
62 | }
63 |
64 | public int getWebNum() {
65 | return webNum;
66 | }
67 |
68 | public boolean isProcessed() {
69 | return this.processed;
70 | }
71 | public void setProcessed(boolean processed) {
72 | this.processed = processed;
73 | }
74 |
75 |
76 | public void setWebNum(int webNum) {
77 | this.webNum = webNum;
78 | }
79 |
80 | // if two witnesses have the same Squares around them they are equivalent
81 | public boolean equivalent(Witness wit) {
82 |
83 | // if the number of squares is different then they can't be equal
84 | if (squares.size() != wit.getSquares().size()) {
85 | return false;
86 | }
87 |
88 | // if the locations are too far apart they can't share the same squares
89 | if (Math.abs(wit.x - this.x) > 2 || Math.abs(wit.y - this.y) > 2) {
90 | return false;
91 | }
92 |
93 | for (Square l1: squares) {
94 | boolean found = false;
95 | for (Square l2: wit.getSquares()) {
96 | if (l2.equals(l1)) {
97 | found = true;
98 | break;
99 | }
100 | }
101 | if (!found) {
102 | return false;
103 | }
104 | }
105 |
106 | return true;
107 | }
108 |
109 | public boolean overlap(Witness w) {
110 |
111 | // if the locations are too far apart they can't share any of the same squares
112 | if (Math.abs(w.x - this.x) > 2 || Math.abs(w.y - this.y) > 2) {
113 | return false;
114 | }
115 |
116 | boolean result = false;
117 |
118 | top: for (Square s: w.squares) {
119 | for (Square s1: this.squares) {
120 | if (s.equals(s1)) {
121 | result = true;
122 | break top;
123 | }
124 | }
125 | }
126 |
127 | return result;
128 |
129 | }
130 |
131 | /**
132 | * This sorts by the number of iterations around this witness descending
133 | */
134 | static public final Comparator SORT_BY_ITERATIONS_DESC = new Comparator() {
135 | @Override
136 | public int compare(Witness o1, Witness o2) {
137 |
138 | return -(o1.iterations - o2.iterations);
139 |
140 | }
141 | };
142 |
143 | }
144 |
--------------------------------------------------------------------------------
/MineSweeperSolver/src/minesweeper/solver/constructs/WitnessData.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package minesweeper.solver.constructs;
7 |
8 | import minesweeper.structure.Location;
9 |
10 | /**
11 | *
12 | * @author David
13 | */
14 | public class WitnessData {
15 |
16 | public Location location;
17 | public boolean witnessRestFlag = true;
18 | public boolean witnessRestClear = true;
19 | public int witnessGood;
20 | public int currentFlags;
21 | public boolean alwaysSatisfied;
22 |
23 | }
24 |
--------------------------------------------------------------------------------
/MineSweeperSolver/src/minesweeper/solver/iterator/Iterator.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this template, choose Tools | Templates
3 | * and open the template in the editor.
4 | */
5 | package minesweeper.solver.iterator;
6 |
7 | import minesweeper.structure.Location;
8 |
9 | /**
10 | *
11 | * @author David
12 | */
13 | abstract public class Iterator {
14 |
15 | final int numberBalls;
16 | final int numberHoles;
17 |
18 | public Iterator(int n, int m) {
19 |
20 | this.numberBalls = n;
21 | this.numberHoles = m;
22 |
23 | }
24 |
25 |
26 | public int[] getSample(int start) {
27 | return null;
28 | }
29 |
30 | public int[] getSample() {
31 | return getSample(numberBalls - 1);
32 | }
33 |
34 | public int getBalls() {
35 | return numberBalls;
36 | }
37 |
38 | public int getHoles() {
39 | return numberHoles;
40 | }
41 |
42 | // if this is true then the checkSample logic can ignore this witness
43 | // This is used by the WitnessWebIterator since the IndependentWitnesses
44 | // must always be satisified.
45 | public boolean witnessAlwaysSatisfied(Location l) {
46 | return false;
47 | }
48 |
49 | }
50 |
--------------------------------------------------------------------------------
/MineSweeperSolver/src/minesweeper/solver/iterator/RandomIterator.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this template, choose Tools | Templates
3 | * and open the template in the editor.
4 | */
5 | package minesweeper.solver.iterator;
6 |
7 | /**
8 | *
9 | * @author David
10 | */
11 | public class RandomIterator extends Iterator {
12 |
13 | int max;
14 | int count=0;
15 |
16 | int[] shuffle;
17 |
18 | int[] sample;
19 |
20 | public RandomIterator(int n, int m, int max) {
21 | super(n,m);
22 |
23 | this.max = max;
24 |
25 | shuffle = new int[m];
26 |
27 | for (int i=0; i < shuffle.length; i++) {
28 | shuffle[i] = i;
29 | }
30 |
31 | sample = new int[n];
32 |
33 | }
34 |
35 | @Override
36 | public int[] getSample() {
37 |
38 | if (count >= max) {
39 | return null;
40 | }
41 | count++;
42 |
43 | int top = shuffle.length -1;
44 |
45 | // create a random sample
46 | for (int i=0; i < sample.length; i++) {
47 |
48 | int e = (int) Math.floor(Math.random()*top);
49 |
50 | sample[i] = shuffle[e];
51 |
52 | // swap shuffle[e] to the top off the array
53 | shuffle[e] = shuffle[top];
54 | shuffle[top] = sample[i];
55 |
56 | // reduce top by 1 so, shuffle[e] can't be picked again
57 | top--;
58 |
59 | }
60 |
61 | return sample;
62 |
63 | }
64 |
65 | }
66 |
--------------------------------------------------------------------------------
/MineSweeperSolver/src/minesweeper/solver/iterator/SequentialIterator.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this template, choose Tools | Templates
3 | * and open the template in the editor.
4 | */
5 | package minesweeper.solver.iterator;
6 |
7 | /**
8 | *
9 | * @author David
10 | */
11 | public class SequentialIterator extends Iterator {
12 |
13 | final private int[] sample;
14 |
15 | private boolean more = true;
16 |
17 | private int index;
18 |
19 |
20 | // a sequential iterator that puts n-balls in m-holes once in each possible way
21 | public SequentialIterator(int n, int m) {
22 | super(n,m);
23 |
24 | sample = new int[n];
25 |
26 | index = n - 1;
27 |
28 | for (int i=0; i < sample.length; i++) {
29 | sample[i] = i;
30 | }
31 |
32 | // reduce the iterator by 1, since the first getSample() will increase it
33 | // by 1 again
34 | sample[index]--;
35 |
36 | }
37 |
38 |
39 |
40 | @Override
41 | public int[] getSample(int start) {
42 |
43 | if (!more) {
44 | System.err.println("trying to iterate after the end");
45 | return null;
46 | }
47 |
48 | index = start;
49 |
50 | // add on one to the iterator
51 | sample[index]++;
52 |
53 | // if we have rolled off the end then move backwards until we can fit
54 | // the next iteration
55 | while (sample[index] >= numberHoles - numberBalls + 1 + index) {
56 | if (index == 0) {
57 | more = false;
58 | return null;
59 | } else {
60 | index--;
61 | sample[index]++;
62 | }
63 | }
64 |
65 | // roll forward
66 | while (index != numberBalls - 1) {
67 | index++;
68 | sample[index] = sample[index-1] + 1;
69 | }
70 |
71 | return sample;
72 |
73 | }
74 |
75 | }
76 |
--------------------------------------------------------------------------------
/MineSweeperSolver/src/minesweeper/solver/settings/PlayStyle.java:
--------------------------------------------------------------------------------
1 | package minesweeper.solver.settings;
2 |
3 | public enum PlayStyle {
4 | FLAGGED(false, false),
5 | NO_FLAG(true, false),
6 | EFFICIENCY(false, true),
7 | NO_FLAG_EFFICIENCY(true, true);
8 |
9 | public final boolean flagless;
10 | public final boolean efficiency;
11 |
12 | private PlayStyle(boolean flagless, boolean efficiency) {
13 | this.flagless = flagless;
14 | this.efficiency = efficiency;
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/MineSweeperSolver/src/minesweeper/solver/settings/SettingsFactory.java:
--------------------------------------------------------------------------------
1 | package minesweeper.solver.settings;
2 |
3 | import java.math.BigInteger;
4 |
5 | public class SettingsFactory {
6 |
7 | public enum Setting {
8 | NO_BRUTE_FORCE,
9 | TINY_ANALYSIS,
10 | SMALL_ANALYSIS,
11 | LARGE_ANALYSIS,
12 | VERY_LARGE_ANALYSIS,
13 | MAX_ANALYSIS;
14 | }
15 |
16 | final static public SolverSettings GetSettings(Setting setting) {
17 |
18 | if (setting == Setting.SMALL_ANALYSIS) {
19 | return smallAnalysis();
20 | } else if (setting == Setting.TINY_ANALYSIS) {
21 | return tinyAnalysis();
22 | } else if (setting == Setting.LARGE_ANALYSIS) {
23 | return largeAnalysis();
24 | } else if (setting == Setting.NO_BRUTE_FORCE) {
25 | return noBruteForce();
26 | } else if (setting == Setting.VERY_LARGE_ANALYSIS) {
27 | return veryLargeAnalysis();
28 | } else if (setting == Setting.MAX_ANALYSIS) {
29 | return maxAnalysis();
30 | }
31 |
32 | return smallAnalysis();
33 |
34 | }
35 |
36 |
37 | private static SolverSettings noBruteForce() {
38 |
39 | SolverSettings settings = new SolverSettings();
40 |
41 | settings.bruteForceMaxSolutions = 0;
42 | settings.bruteForceVariableSolutions = 0;
43 | settings.bruteForceMaxNodes = 0;
44 | settings.bruteForceTreeDepth = 10;
45 | settings.bruteForceMaxIterations = BigInteger.ZERO;
46 |
47 | return settings;
48 | };
49 |
50 | private static SolverSettings tinyAnalysis() {
51 |
52 | SolverSettings settings = new SolverSettings();
53 |
54 | settings.bruteForceMaxSolutions = 40;
55 | settings.bruteForceVariableSolutions = 15;
56 | settings.bruteForceMaxNodes = 150000;
57 | settings.bruteForceTreeDepth = 10;
58 | settings.bruteForceMaxIterations = new BigInteger("1000000"); // 5 million
59 |
60 | return settings;
61 | };
62 |
63 | /**
64 | * Does trivial, Local and Probability Engine searches.
65 | * Does a small brute force search with a 400 solution deep analysis.
66 | * This is suitable for bulk runs.
67 | */
68 | private static SolverSettings smallAnalysis() {
69 |
70 | SolverSettings settings = new SolverSettings();
71 |
72 | settings.bruteForceMaxSolutions = 400;
73 | settings.bruteForceVariableSolutions = 250;
74 | settings.bruteForceMaxNodes = 300000;
75 | settings.bruteForceTreeDepth = 10;
76 | settings.bruteForceMaxIterations = new BigInteger("10000000"); // 10 million
77 |
78 | return settings;
79 | };
80 |
81 |
82 | /**
83 | * Does trivial, Local and Probability Engine searches.
84 | * Does a large brute force search with a 4000 solution deep analysis.
85 | * This is probably not suitable for bulk runs.
86 | */
87 | private static SolverSettings largeAnalysis() {
88 |
89 | SolverSettings settings = new SolverSettings();
90 |
91 | settings.bruteForceMaxSolutions = 4000;
92 | settings.bruteForceVariableSolutions = 2000;
93 | settings.bruteForceMaxNodes = 20000000; // 20 million
94 | settings.bruteForceTreeDepth = 10;
95 | settings.bruteForceMaxIterations = new BigInteger("10000000"); // 10 million
96 |
97 | return settings;
98 | };
99 |
100 | private static SolverSettings veryLargeAnalysis() {
101 |
102 | SolverSettings settings = new SolverSettings();
103 |
104 | settings.bruteForceMaxSolutions = 20000;
105 | settings.bruteForceVariableSolutions = 10000;
106 | settings.bruteForceMaxNodes = 200000000; // 200 million
107 | settings.bruteForceTreeDepth = 3;
108 | settings.bruteForceMaxIterations = new BigInteger("50000000"); // 50 million
109 |
110 | return settings;
111 | };
112 |
113 | private static SolverSettings maxAnalysis() {
114 |
115 | SolverSettings settings = new SolverSettings();
116 |
117 | settings.bruteForceMaxSolutions = 200000;
118 | settings.bruteForceVariableSolutions = 100000;
119 | settings.bruteForceMaxNodes = 2000000000; // 2000 million
120 | settings.bruteForceTreeDepth = 3;
121 | settings.bruteForceMaxIterations = new BigInteger("500000000"); // 500 million
122 |
123 | return settings;
124 | };
125 |
126 | }
127 |
--------------------------------------------------------------------------------
/MineSweeperSolver/src/minesweeper/solver/utility/BigDecimalCache.java:
--------------------------------------------------------------------------------
1 | package minesweeper.solver.utility;
2 |
3 | import java.math.BigDecimal;
4 |
5 | public class BigDecimalCache {
6 |
7 | private static BigDecimal[] cache = new BigDecimal[4000];
8 | static {
9 | for (int i=0; i < cache.length; i++) {
10 | cache[i] = BigDecimal.valueOf(i);
11 | }
12 | }
13 |
14 |
15 | public static BigDecimal get(int i) {
16 |
17 | if (i < cache.length) {
18 | return cache[i];
19 | } else {
20 | return BigDecimal.valueOf(i);
21 | }
22 |
23 |
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/MineSweeperSolver/src/minesweeper/solver/utility/Binomial.java:
--------------------------------------------------------------------------------
1 | package minesweeper.solver.utility;
2 | import java.math.BigInteger;
3 |
4 | public class Binomial {
5 |
6 | private final int max;
7 | private final PrimeSieve ps;
8 |
9 | private final BigInteger[][] binomialLookup;
10 | private final int lookupLimit;
11 |
12 | public Binomial(int max, int lookup) {
13 |
14 | this.max = max;
15 |
16 | ps = new PrimeSieve(this.max);
17 |
18 | if (lookup < 10) {
19 | lookup = 10;
20 | }
21 | this.lookupLimit = lookup;
22 |
23 | final int lookup2 = lookup / 2;
24 |
25 | binomialLookup = new BigInteger[lookup + 1][lookup2 + 1];
26 |
27 | for (int total = 1; total <= lookup; total++) {
28 | for (int choose = 0; choose <= total / 2; choose++) {
29 | try {
30 | binomialLookup[total][choose] = generate(choose, total);
31 | //System.out.println("Binomial " + total + " choose " + choose + " is " + binomialLookup[total][choose]);
32 | } catch (Exception e) {
33 | e.printStackTrace();
34 | }
35 | }
36 |
37 |
38 | }
39 |
40 | }
41 |
42 |
43 | public BigInteger generate(int k, int n) throws Exception {
44 |
45 | if (n == 0 && k == 0) {
46 | return BigInteger.ONE;
47 | }
48 |
49 | if (n < 1 || n > max) {
50 | throw new Exception("Binomial: 1 <= n and n <= max required, but n was " + n + " and max was " + max);
51 | }
52 |
53 | if (0 > k || k > n) {
54 | throw new Exception("Binomial: 0 <= k and k <= n required, but n was " + n + " and k was " + k);
55 | }
56 |
57 | int choose = Math.min(k, n-k);
58 |
59 | if (n <= lookupLimit && binomialLookup[n][choose] != null) {
60 | return binomialLookup[n][choose];
61 | } else if (choose < 125) {
62 | return combination(choose, n);
63 | } else {
64 | return combinationLarge(choose, n);
65 | }
66 |
67 | }
68 |
69 | private static BigInteger combination(int mines, int squares) {
70 |
71 | BigInteger top = BigInteger.ONE;
72 | BigInteger bot = BigInteger.ONE;
73 |
74 | int range = Math.min(mines, squares - mines);
75 |
76 | // calculate the combination.
77 | for (int i = 0; i < range; i++) {
78 | top = top.multiply(BigInteger.valueOf(squares - i));
79 | bot = bot.multiply(BigInteger.valueOf(i+1));
80 | }
81 |
82 | BigInteger result = top.divide(bot);
83 |
84 | return result;
85 |
86 | }
87 |
88 |
89 | private BigInteger combinationLarge(int k, int n) throws Exception {
90 |
91 | if ((k == 0) || (k == n)) return BigInteger.ONE;
92 |
93 | int n2 = n / 2;
94 |
95 | if (k > n2) {
96 | k = n - k;
97 | }
98 |
99 | int nk = n - k;
100 |
101 | int rootN = (int) Math.floor(Math.sqrt(n));
102 |
103 | BigInteger result = BigInteger.ONE;
104 |
105 |
106 | for (int prime : ps.getPrimesIterable(2, n)) {
107 |
108 | if (prime > nk) {
109 | result = result.multiply(BigInteger.valueOf(prime));
110 | continue;
111 | }
112 |
113 | if (prime > n2) {
114 | continue;
115 | }
116 |
117 | if (prime > rootN) {
118 | if (n % prime < k % prime) {
119 | result = result.multiply(BigInteger.valueOf(prime));
120 | }
121 | continue;
122 | }
123 |
124 | int r = 0, N = n, K = k, p = 1;
125 |
126 | while (N > 0) {
127 | r = (N % prime) < (K % prime + r) ? 1 : 0;
128 | if (r == 1)
129 | {
130 | p *= prime;
131 | }
132 | N /= prime;
133 | K /= prime;
134 | }
135 | if (p > 1) {
136 | result = result.multiply(BigInteger.valueOf(p));
137 | }
138 | }
139 |
140 | return result;
141 | }
142 |
143 | }
144 |
--------------------------------------------------------------------------------
/MineSweeperSolver/src/minesweeper/solver/utility/BinomialCache.java:
--------------------------------------------------------------------------------
1 | package minesweeper.solver.utility;
2 |
3 | import java.math.BigInteger;
4 | import java.util.Arrays;
5 |
6 | public class BinomialCache {
7 |
8 | private class BinomialEntry implements Comparable {
9 | final private int n;
10 | final private int k;
11 | final private BigInteger bco;
12 |
13 | private long lastUsed;
14 |
15 | private BinomialEntry(int k, int n, BigInteger bco) {
16 | this.k = k;
17 | this.n = n;
18 | this.bco = bco;
19 | }
20 |
21 | @Override
22 | public int compareTo(BinomialEntry o) {
23 | if (lastUsed < o.lastUsed) {
24 | return -1;
25 | } else if (lastUsed > o.lastUsed) {
26 | return 1;
27 | } else {
28 | return 0;
29 | }
30 | }
31 |
32 | }
33 | private BinomialEntry[] cache;
34 | private int start = -1;
35 | private long useCount = 0;
36 |
37 | private int cacheHits = 0;
38 | private int cacheStored = 0;
39 | private int sumDepth = 0;
40 | private int nearMissHits = 0;
41 | private int fullCalc = 0;
42 |
43 | private final int cacheSize;
44 | private final int cacheFreshold;
45 | private final int compressRemoval;
46 |
47 | private final Binomial binomialEngine;
48 |
49 | public BinomialCache(int cacheSize, int cacheFreshold, Binomial binomialEngine) {
50 |
51 | this.cacheSize = cacheSize;
52 | this.cacheFreshold = cacheFreshold;
53 | this.binomialEngine = binomialEngine;
54 | this.compressRemoval = cacheSize / 2;
55 |
56 | this.cache = new BinomialEntry[this.cacheSize];
57 |
58 | }
59 |
60 | public BigInteger getBinomial(int k, int n) {
61 |
62 | // if the binomial is below the size freshold then just go get it
63 | if (n <= cacheFreshold) {
64 | try {
65 | return binomialEngine.generate(k, n);
66 | } catch (Exception e) {
67 | e.printStackTrace();
68 | return BigInteger.ONE;
69 | }
70 | }
71 |
72 | useCount++;
73 |
74 | // if there are some details in the cache then search it
75 | BinomialEntry nearMissK = null;
76 | BinomialEntry nearMissN = null;
77 | if (start != -1) {
78 | int tally = 0;
79 | for (int i = start; i >= 0; i--) {
80 | BinomialEntry entry = cache[i];
81 | tally++;
82 | if (entry.n == n && entry.k == k) {
83 | entry.lastUsed = useCount;
84 | cacheHits++;
85 | sumDepth = sumDepth + tally;
86 |
87 | //if (cacheHits % 250 == 0) {
88 | // Arrays.sort(cache, 0, start);
89 | //}
90 |
91 | return entry.bco;
92 | }
93 | if (entry.n == n && entry.k == k + 1) {
94 | nearMissK = entry;
95 | }
96 | if (entry.n == n + 1 && entry.k == k) {
97 | nearMissN = entry;
98 | }
99 | }
100 | }
101 |
102 | BigInteger b;
103 | if (nearMissK != null) { // one below a cached entry we can do quickly
104 | b = nearMissK.bco.multiply(BigInteger.valueOf(nearMissK.k)).divide(BigInteger.valueOf(nearMissK.n - nearMissK.k + 1));
105 | nearMissHits++;
106 |
107 | } else if (nearMissN != null) { // one below a cached entry we can do quickly
108 | b = nearMissN.bco.multiply(BigInteger.valueOf(nearMissN.n - nearMissN.k)).divide(BigInteger.valueOf(nearMissN.n));
109 | nearMissHits++;
110 |
111 | } else { // not in the cache, so generate it
112 | try {
113 | b = binomialEngine.generate(k, n);
114 | fullCalc++;
115 | } catch (Exception e) {
116 | e.printStackTrace();
117 | b = BigInteger.ONE;
118 | }
119 | }
120 |
121 | if (start == cacheSize - 1) {
122 | compressCache();
123 | }
124 | //else if (useCount % 50 == 0) {
125 | // Arrays.sort(cache, 0, start);
126 | //}
127 |
128 | start++;
129 | BinomialEntry be = new BinomialEntry(k, n, b);
130 | be.lastUsed = useCount;
131 | cacheStored++;
132 |
133 | cache[start] = be;
134 |
135 | return b;
136 | }
137 |
138 | // remove the least used binomials from the cache
139 | private void compressCache() {
140 |
141 | //System.out.print(Thread.currentThread().getName() + " Cache compressing...");
142 |
143 | Arrays.sort(cache);
144 |
145 | if (cache[0].lastUsed > cache[1].lastUsed) {
146 | System.out.println("Sort order wrong!");
147 | }
148 |
149 | for (int i=0; i < this.cacheSize - this.compressRemoval; i++) {
150 | cache[i] = cache[i + this.compressRemoval];
151 | }
152 |
153 | this.start = this.start - this.compressRemoval;
154 | //System.out.println("Cache compressed ..." + this.start);
155 | }
156 |
157 | public void showStats() {
158 |
159 | int avgDepth = 0;
160 | if (cacheHits != 0) {
161 | avgDepth = sumDepth / cacheHits;
162 | }
163 |
164 |
165 | System.out.println(Thread.currentThread().getName() + " Cache stored: " + cacheStored + ", cache Hits: " + cacheHits + ", Near Miss Hits: " + nearMissHits
166 | + ", Full Calculation: " + fullCalc + ", current entries: " + (start + 1) + ", avg depth: " + avgDepth);
167 | }
168 |
169 |
170 | }
171 |
--------------------------------------------------------------------------------
/MineSweeperSolver/src/minesweeper/solver/utility/Logger.java:
--------------------------------------------------------------------------------
1 | package minesweeper.solver.utility;
2 |
3 | public class Logger {
4 |
5 | /**
6 | * Nothing gets logged
7 | */
8 | public final static Logger NO_LOGGING = new Logger(Level.NONE);
9 |
10 | public enum Level {
11 | DEBUG(1),
12 | INFO(2),
13 | WARN(3),
14 | ERROR(4),
15 | ALWAYS(90),
16 | NONE(99);
17 |
18 | private int value;
19 | private Level(int value) {
20 | this.value = value;
21 | }
22 | }
23 |
24 | private final String logName;
25 | private final Level logLevel;
26 | private final String prefix;
27 |
28 | public Logger(Level level) {
29 | this(level, "");
30 | }
31 |
32 | public Logger(Level level, String logName) {
33 | this.logLevel = level;
34 | this.logName = logName;
35 | if (this.logName.isEmpty()) {
36 | this.prefix = " ";
37 | } else {
38 | this.prefix = " " + this.logName + " ";
39 | }
40 |
41 | }
42 |
43 | public void log(Level level, String format, Object... parms) {
44 |
45 | if (level.value < logLevel.value) {
46 | return;
47 | }
48 |
49 | String output;
50 | try {
51 | output = String.format(format, parms);
52 |
53 | } catch (Exception e) { // if it goes wrong show the unformated information
54 | StringBuilder sb = new StringBuilder();
55 | sb.append(format);
56 | sb.append(" Parms:");
57 | for (Object parm: parms) {
58 | sb.append("[");
59 | sb.append(parm);
60 | sb.append("]");
61 | }
62 | output = sb.toString();
63 | }
64 |
65 | System.out.println(level + prefix + output);
66 |
67 | }
68 |
69 | }
70 |
--------------------------------------------------------------------------------
/MineSweeperSolver/src/minesweeper/solver/utility/PrimeSieve.java:
--------------------------------------------------------------------------------
1 | package minesweeper.solver.utility;
2 | import java.util.Iterator;
3 |
4 | public class PrimeSieve {
5 |
6 | // iterator for prime numbers
7 | private class Primes implements Iterable, Iterator {
8 |
9 | private int index = 0;
10 | private int stop;
11 | private int nextPrime;
12 |
13 | private Primes(int start, int stop) {
14 | this.index = start;
15 | this.stop = stop;
16 | this.nextPrime = findNext();
17 | }
18 |
19 | @Override
20 | public Iterator iterator() {
21 | return this;
22 | }
23 |
24 | @Override
25 | public boolean hasNext() {
26 | return (nextPrime != -1);
27 | }
28 |
29 | @Override
30 | public Integer next() {
31 |
32 | int result = nextPrime;
33 | nextPrime = findNext();
34 |
35 | return result;
36 | }
37 |
38 | private int findNext() {
39 |
40 | int next = -1;
41 | while (index <= stop && next == -1) {
42 | if (!composite[index]) {
43 | next = index;
44 | }
45 | index++;
46 | }
47 |
48 | return next;
49 |
50 | }
51 |
52 | }
53 |
54 |
55 | private final boolean[] composite;
56 | private final int max;
57 |
58 | public PrimeSieve(int n) {
59 |
60 | if (n < 2) {
61 | max = 2;
62 | } else {
63 | max = n;
64 | }
65 |
66 | composite = new boolean[max + 1];
67 |
68 | final int rootN = (int) Math.floor(Math.sqrt(n));
69 |
70 | for (int i=2; i < rootN; i++) {
71 |
72 | // if this is a prime number (not composite) then sieve the array
73 | if (!composite[i]) {
74 | int index = i + i;
75 | while (index <= max) {
76 | composite[index] = true;
77 | index = index + i;
78 | }
79 | }
80 | }
81 |
82 | }
83 |
84 |
85 |
86 | public boolean isPrime(int n) throws Exception {
87 | if (n <= 1 || n > max) {
88 | throw new Exception("Test value " + n + " is out of range 2 - " + max);
89 | }
90 |
91 | return !composite[n];
92 | }
93 |
94 | protected Iterable getPrimesIterable(int start, int stop) throws Exception {
95 |
96 | if (start > stop) {
97 | throw new Exception("start " + start + " must be <= to stop " + stop);
98 | }
99 | if (start <= 1 || start > max) {
100 | throw new Exception("Start value " + start + " is out of range 2 - " + max);
101 | }
102 | if (stop <= 1 || stop > max) {
103 | throw new Exception("Stop value " + stop + " is out of range 2 - " + max);
104 | }
105 |
106 | return new Primes(start, stop);
107 | }
108 |
109 | }
110 |
--------------------------------------------------------------------------------
/MineSweeperSolver/src/minesweeper/solver/utility/ProgressMonitor.java:
--------------------------------------------------------------------------------
1 | package minesweeper.solver.utility;
2 |
3 | public class ProgressMonitor {
4 |
5 | private String step;
6 | private int maxProgress;
7 | private int progress;
8 |
9 | public void SetMaxProgress(String step, int max) {
10 | this.step = step;
11 | this.maxProgress = max;
12 | this.progress = 0;
13 | }
14 |
15 | public int getMaxProgress() {
16 | return this.maxProgress;
17 | }
18 |
19 | public void setProgress(int progress) {
20 | this.progress = progress;
21 | }
22 |
23 | public int getProgress() {
24 | return this.progress;
25 | }
26 |
27 | }
28 |
--------------------------------------------------------------------------------
/MineSweeperSolver/src/minesweeper/solver/utility/Timer.java:
--------------------------------------------------------------------------------
1 | package minesweeper.solver.utility;
2 |
3 | import java.text.DecimalFormat;
4 | import java.text.NumberFormat;
5 |
6 | public class Timer {
7 |
8 | private final static NumberFormat FORMAT = new DecimalFormat("###0.000");
9 |
10 | private long start;
11 | private boolean running = false;
12 | private long duration = 0;
13 | private final String label;
14 |
15 | public Timer(String label) {
16 |
17 | this.label = label;
18 |
19 | }
20 |
21 | public Timer start() {
22 | if (running) { // if already started then ignore
23 | return this;
24 | }
25 | start = System.nanoTime();
26 | running = true;
27 | return this;
28 | }
29 |
30 | public Timer stop() {
31 | if (!running) { // if not running then ignore
32 | return this;
33 | }
34 | running = false;
35 | duration = duration + System.nanoTime() - start;
36 | return this;
37 | }
38 |
39 | /**
40 | * Return the duration in milliseconds
41 | * @return
42 | */
43 | public double getDuration() {
44 |
45 | long result;
46 |
47 | if (running) {
48 | result = duration + System.nanoTime() - start;
49 | } else {
50 | result = duration;
51 | }
52 |
53 | double milli = (double) result / 1000000d;
54 |
55 | return milli;
56 | }
57 |
58 | static public String humanReadable(long ms) {
59 |
60 | long milliseconds = ms % 1000;
61 | long rem = (ms - milliseconds) / 1000;
62 | long seconds = rem % 60;
63 | rem = (rem - seconds) / 60;
64 | long minutes = rem % 60;
65 | long hours = (rem - minutes) /60;
66 |
67 | String result;
68 | if (hours > 0) {
69 | result = hours(hours) + " " + minutes(minutes);
70 | } else if (minutes > 0) {
71 | result = minutes(minutes) + " " + seconds(seconds);
72 | } else if (seconds > 0){
73 | result = seconds(seconds);
74 | } else {
75 | result = "< 1 second";
76 | }
77 |
78 | return result;
79 | }
80 |
81 | static private String hours(long hours) {
82 | if (hours == 1) {
83 | return "1 hour";
84 | } else {
85 | return hours + " hours";
86 | }
87 | }
88 |
89 | static private String minutes(long minutes) {
90 |
91 | if (minutes == 0) {
92 | return "";
93 | } else if (minutes == 1) {
94 | return "1 minute";
95 | } else {
96 | return minutes + " minutes";
97 | }
98 | }
99 |
100 | static private String seconds(long seconds) {
101 |
102 | if (seconds == 0) {
103 | return "";
104 | } else if (seconds == 1) {
105 | return "1 second";
106 | } else {
107 | return seconds + " seconds";
108 | }
109 | }
110 |
111 | @Override
112 | public String toString() {
113 | return label + " " + FORMAT.format(getDuration()) + " milliseconds";
114 | }
115 |
116 | }
117 |
--------------------------------------------------------------------------------
/Minesweeper/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/Minesweeper/.gitignore:
--------------------------------------------------------------------------------
1 | /bin/
2 | /build/
3 |
--------------------------------------------------------------------------------
/Minesweeper/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | Minesweeper
4 |
5 |
6 |
7 |
8 |
9 | org.eclipse.jdt.core.javabuilder
10 |
11 |
12 |
13 |
14 | org.eclipse.xtext.ui.shared.xtextBuilder
15 |
16 |
17 |
18 |
19 |
20 | org.eclipse.xtext.ui.shared.xtextNature
21 | org.eclipse.jdt.core.javanature
22 |
23 |
24 |
--------------------------------------------------------------------------------
/Minesweeper/.settings/org.eclipse.jdt.core.prefs:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5 | org.eclipse.jdt.core.compiler.compliance=1.8
6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate
8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
11 | org.eclipse.jdt.core.compiler.source=1.8
12 |
--------------------------------------------------------------------------------
/Minesweeper/build.fxbuild:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/Minesweeper/src/minesweeper/Animator.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this template, choose Tools | Templates
3 | * and open the template in the editor.
4 | */
5 | package minesweeper;
6 |
7 | import javafx.application.Platform;
8 |
9 | /**
10 | *
11 | * @author David
12 | */
13 | public class Animator implements Runnable{
14 |
15 | private Thread animator;
16 |
17 | private ScreenController scon;
18 |
19 | private boolean stopped = false;
20 | private long gameTime = 0;
21 |
22 | public Animator(ScreenController scon) {
23 |
24 | this.scon = scon;
25 |
26 | // start animating the display pane - see the run method
27 | animator = new Thread(this, "Animator");
28 |
29 | }
30 |
31 | public void start() {
32 |
33 | animator.start();
34 |
35 | }
36 |
37 | public void stop() {
38 |
39 | stopped = true;
40 |
41 | }
42 |
43 |
44 | @Override
45 | public void run() {
46 |
47 | long timeDiff, sleep, timeDelay;
48 |
49 | timeDelay = 50;
50 |
51 | gameTime = System.currentTimeMillis();
52 |
53 | while (!stopped) {
54 |
55 | Platform.runLater(new Runnable() {
56 | @Override public void run() {
57 | scon.updateTime();
58 | scon.moveCheck();
59 | scon.highlightMove();
60 | }
61 | });
62 |
63 |
64 | // calculate how long the work took
65 | timeDiff = System.currentTimeMillis() - gameTime;
66 |
67 | // pause for the ramainder of timeDelay
68 | sleep = timeDelay - timeDiff;
69 |
70 | if (sleep < 0) {
71 | sleep = 2;
72 | }
73 |
74 | try {
75 | Thread.sleep(sleep);
76 | } catch (InterruptedException e) {
77 | System.out.println("interrupted");
78 | }
79 |
80 | gameTime += timeDelay;
81 |
82 | }
83 |
84 | System.out.println("Animator thread stopped");
85 |
86 | }
87 |
88 | }
89 |
--------------------------------------------------------------------------------
/Minesweeper/src/minesweeper/Custom.fxml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
35 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
--------------------------------------------------------------------------------
/Minesweeper/src/minesweeper/Graphics.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this template, choose Tools | Templates
3 | * and open the template in the editor.
4 | */
5 | package minesweeper;
6 |
7 | import javafx.scene.image.Image;
8 |
9 | /**
10 | *
11 | * @author David
12 | */
13 | public class Graphics {
14 |
15 | public static final double SIZE = 50;
16 |
17 |
18 | private static final Image button;
19 | private static final Image mineBang;
20 | private static final Image flag;
21 | private static final Image[] number = new Image[9];
22 | private static final Image mine;
23 |
24 |
25 | static {
26 |
27 | button = clean(new Image(Graphics.class.getResource("resources/ms_button.png").toExternalForm(), SIZE, SIZE, true, true));
28 | mineBang = clean(new Image(Graphics.class.getResource("resources/ms_mine_bang.png").toExternalForm(), SIZE, SIZE, true, true));
29 | flag = clean(new Image(Graphics.class.getResource("resources/ms_flag.png").toExternalForm(), SIZE, SIZE, true, true));
30 | mine = clean(new Image(Graphics.class.getResource("resources/ms_mine.png").toExternalForm(), SIZE, SIZE, true, true));
31 |
32 | number[0] = clean(new Image(Graphics.class.getResource("resources/ms_zero.png").toExternalForm(), SIZE, SIZE, true, true));
33 | number[1] = clean(new Image(Graphics.class.getResource("resources/ms_one.png").toExternalForm(), SIZE, SIZE, true, true));
34 | number[2] = clean(new Image(Graphics.class.getResource("resources/ms_two.png").toExternalForm(), SIZE, SIZE, true, true));
35 | number[3] = clean(new Image(Graphics.class.getResource("resources/ms_three.png").toExternalForm(), SIZE, SIZE, true, true));
36 | number[4] = clean(new Image(Graphics.class.getResource("resources/ms_four.png").toExternalForm(), SIZE, SIZE, true, true));
37 | number[5] = clean(new Image(Graphics.class.getResource("resources/ms_five.png").toExternalForm(), SIZE, SIZE, true, true));
38 | number[6] = clean(new Image(Graphics.class.getResource("resources/ms_six.png").toExternalForm(), SIZE, SIZE, true, true));
39 | number[7] = clean(new Image(Graphics.class.getResource("resources/ms_seven.png").toExternalForm(), SIZE, SIZE, true, true));
40 | number[8] = clean(new Image(Graphics.class.getResource("resources/ms_eight.png").toExternalForm(), SIZE, SIZE, true, true));
41 |
42 |
43 | }
44 |
45 | static public Image getNumber(int c) {
46 |
47 | return number[c];
48 |
49 | }
50 |
51 | static public Image getMineBang() {
52 |
53 | return mineBang;
54 |
55 | }
56 |
57 | static public Image getMine() {
58 |
59 | return mine;
60 |
61 | }
62 |
63 | static public Image getFlag() {
64 |
65 | return flag;
66 |
67 | }
68 |
69 | static public Image getButton() {
70 |
71 | return button;
72 |
73 | }
74 |
75 | // in case we want to do some image manipulation
76 | static private Image clean(Image image) {
77 |
78 | return image;
79 |
80 | }
81 |
82 |
83 | }
84 |
--------------------------------------------------------------------------------
/Minesweeper/src/minesweeper/Rotator.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this template, choose Tools | Templates
3 | * and open the template in the editor.
4 | */
5 | package minesweeper;
6 |
7 | import javafx.scene.Node;
8 |
9 | /**
10 | *
11 | * @author David
12 | */
13 | public class Rotator implements Runnable {
14 |
15 | private Thread rotator;
16 |
17 | Node object;
18 |
19 |
20 | public Rotator(Node object) {
21 |
22 | this.object = object;
23 |
24 | rotator = new Thread(this, "Rotator");
25 |
26 | }
27 |
28 | public void start() {
29 |
30 | rotator.start();
31 |
32 | }
33 |
34 | @Override
35 | public void run() {
36 |
37 | for (int i=0; i < 360; i=i+20) {
38 |
39 | this.object.setRotate(i);
40 |
41 | try {
42 | Thread.sleep(20);
43 | } catch (InterruptedException e) {
44 | System.out.println("interrupted");
45 | }
46 |
47 |
48 | }
49 |
50 | this.object.setRotate(0);
51 |
52 | }
53 |
54 |
55 |
56 | }
57 |
--------------------------------------------------------------------------------
/Minesweeper/src/minesweeper/Screen.css:
--------------------------------------------------------------------------------
1 | /*
2 | Document : Screen
3 | Created on : 29-Nov-2013, 15:48:32
4 | Author : David
5 | Description:
6 | Purpose of the stylesheet follows.
7 | */
8 |
9 | .root {
10 |
11 | -fx-background-color: linear-gradient(GREEN, DARKGREEN);
12 |
13 | -fx-background-color: linear-gradient(#61a2b1, #2A5058);
14 | }
15 |
16 | .menu-bar {
17 |
18 | -fx-background-color: linear-gradient(#61a2b1, #2A5058);
19 |
20 | }
21 |
22 |
23 | #myPane {
24 |
25 | -fx-border-color: black;
26 | -fx-border-width: 1;
27 | -fx-border-style: solid outside;
28 |
29 | }
30 |
31 | #scoreLabel {
32 |
33 | -fx-border-color: black;
34 | -fx-border-width: 1;
35 | -fx-border-style: solid outside;
36 | -fx-border-radius: 15;
37 |
38 | }
39 |
40 | #timeLabel {
41 |
42 | -fx-border-color: black;
43 | -fx-border-width: 1;
44 | -fx-border-style: solid outside;
45 | -fx-border-radius: 15;
46 |
47 | }
--------------------------------------------------------------------------------
/Minesweeper/src/minesweeper/bulk/BulkResults.fxml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/Minesweeper/src/minesweeper/bulk/BulkRunner.java:
--------------------------------------------------------------------------------
1 | package minesweeper.bulk;
2 |
3 | import minesweeper.gamestate.GameFactory;
4 | import minesweeper.gamestate.GameStateModel;
5 | import minesweeper.random.DefaultRNG;
6 | import minesweeper.random.RNG;
7 | import minesweeper.settings.GameSettings;
8 | import minesweeper.settings.GameType;
9 | import minesweeper.solver.Solver;
10 | import minesweeper.solver.settings.SolverSettings;
11 | import minesweeper.structure.Action;
12 | import minesweeper.structure.Location;
13 |
14 | public class BulkRunner implements Runnable {
15 |
16 | private boolean stop = false;
17 | private final int maxSteps;
18 | private final long seed;
19 | private final BulkController controller;
20 | private final GameSettings gameSettings;
21 | private final GameType gameType;
22 | private final Location startLocation;
23 | private final SolverSettings preferences;
24 |
25 | //private final Random seeder;
26 | private int steps = 0;
27 | private int wins = 0;
28 |
29 | private final RNG seeder;
30 |
31 | private ResultsController resultsController;
32 | private boolean showGames;
33 | private boolean winsOnly;
34 |
35 | public BulkRunner(BulkController controller, int iterations, GameSettings gameSettings, GameType gameType,
36 | long seed, Location startLocation, boolean showGames, boolean winsOnly, SolverSettings preferences) {
37 |
38 | maxSteps = iterations;
39 | this.seed = seed;
40 | this.controller = controller;
41 | this.gameSettings = gameSettings;
42 | this.gameType = gameType;
43 | this.startLocation = startLocation;
44 | this.seeder = DefaultRNG.getRNG(seed);
45 | this.showGames = showGames;
46 | this.winsOnly = winsOnly;
47 | this.preferences = preferences;
48 |
49 | if (startLocation != null) {
50 | this.preferences.setStartLocation(startLocation);
51 | }
52 |
53 | if (showGames) {
54 | resultsController = ResultsController.launch(null, gameSettings, gameType);
55 | }
56 |
57 |
58 | }
59 |
60 | @Override
61 | public void run() {
62 |
63 | System.out.println("At BulkRunner run method");
64 |
65 | while (!stop && steps < maxSteps) {
66 |
67 | steps++;
68 |
69 | GameStateModel gs = GameFactory.create(gameType, gameSettings, seeder.random(0));
70 |
71 | Solver solver = new Solver(gs, preferences, false);
72 |
73 |
74 | boolean win = playGame(gs, solver);
75 |
76 | if (win) {
77 | wins++;
78 | }
79 |
80 | if (showGames && (win || !win && !winsOnly)) {
81 | if (!resultsController.update(gs)) { // this returns false if the window has been closed
82 | showGames = false;
83 | resultsController = null;
84 | System.out.println("Results window has been closed... will no longer send data to it");
85 | }
86 | }
87 |
88 |
89 | controller.update(steps, maxSteps, wins);
90 |
91 | }
92 |
93 | stop = true;
94 | System.out.println("BulkRunner run method ending");
95 |
96 | }
97 |
98 |
99 | private boolean playGame(GameStateModel gs, Solver solver) {
100 |
101 | int state;
102 |
103 | play: while (true) {
104 |
105 | Action[] moves;
106 | try {
107 | solver.start();
108 | moves = solver.getResult();
109 | } catch (Exception e) {
110 | System.out.println("Game " + gs.showGameKey() + " has thrown an exception!");
111 | stop = true;
112 | return false;
113 | }
114 |
115 | if (moves.length == 0) {
116 | System.err.println("No moves returned by the solver for game " + gs.showGameKey());
117 | stop = true;
118 | return false;
119 | }
120 |
121 | // play all the moves until all done, or the game is won or lost
122 | for (int i=0; i < moves.length; i++) {
123 |
124 | boolean result = gs.doAction(moves[i]);
125 |
126 | state = gs.getGameState();
127 |
128 | if (state == GameStateModel.LOST || state == GameStateModel.WON) {
129 | break play;
130 | }
131 | }
132 | }
133 |
134 |
135 | if (state == GameStateModel.LOST) {
136 | return false;
137 | } else {
138 | return true;
139 | }
140 |
141 |
142 | }
143 |
144 | public void forceStop() {
145 | System.out.println("Bulk run being requested to stop");
146 |
147 | stop = true;
148 | }
149 |
150 | public boolean isFinished() {
151 | return stop;
152 | }
153 |
154 | }
155 |
--------------------------------------------------------------------------------
/Minesweeper/src/minesweeper/bulk/BulkScreen.fxml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
19 |
24 |
29 |
34 |
39 |
44 |
45 |
46 |
51 |
52 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
--------------------------------------------------------------------------------
/Minesweeper/src/minesweeper/bulk/TableData.java:
--------------------------------------------------------------------------------
1 | package minesweeper.bulk;
2 |
3 | import java.math.BigDecimal;
4 | import java.math.RoundingMode;
5 | import java.text.DecimalFormat;
6 |
7 | import javafx.beans.property.BooleanProperty;
8 | import javafx.beans.property.DoubleProperty;
9 | import javafx.beans.property.FloatProperty;
10 | import javafx.beans.property.IntegerProperty;
11 | import javafx.beans.property.LongProperty;
12 | import javafx.beans.property.SimpleBooleanProperty;
13 | import javafx.beans.property.SimpleDoubleProperty;
14 | import javafx.beans.property.SimpleFloatProperty;
15 | import javafx.beans.property.SimpleIntegerProperty;
16 | import javafx.beans.property.SimpleLongProperty;
17 | import javafx.beans.property.SimpleStringProperty;
18 | import javafx.beans.property.StringProperty;
19 | import minesweeper.gamestate.GameStateModel;
20 |
21 | public class TableData {
22 |
23 | private final static BigDecimal ONE_HUNDRED = BigDecimal.valueOf(100);
24 | private final static DecimalFormat PERCENT = new DecimalFormat("#0.000%");
25 |
26 | private final IntegerProperty count;
27 | private final LongProperty seed;
28 | private final IntegerProperty complete;
29 | private final StringProperty result;
30 |
31 | public TableData(int count, GameStateModel gs) {
32 |
33 | this.count = new SimpleIntegerProperty(count);
34 |
35 | seed = new SimpleLongProperty(gs.getSeed());
36 |
37 | /*
38 | BigDecimal areaToReveal = BigDecimal.valueOf(gs.getx() * gs.gety() - gs.getMines());
39 | BigDecimal areaLeftToReveal = BigDecimal.valueOf(gs.getHidden() - gs.getMines());
40 |
41 | BigDecimal percentageToDo = ONE_HUNDRED.multiply(BigDecimal.ONE.subtract(areaLeftToReveal.divide(areaToReveal, 6, RoundingMode.HALF_UP))).setScale(2, RoundingMode.HALF_UP);
42 |
43 | System.out.println(percentageToDo + " " + (gs.getGameState() == GameStateModel.WON) + " " + areaLeftToReveal);
44 | */
45 |
46 | complete = new SimpleIntegerProperty(gs.getWidth() * gs.getHeight() - gs.getHidden());
47 |
48 | if (gs.getGameState() == GameStateModel.WON) {
49 | result = new SimpleStringProperty("Won");
50 | } else {
51 | result = new SimpleStringProperty("Lost");
52 | }
53 |
54 | }
55 |
56 |
57 |
58 |
59 | public LongProperty seedProperty() {
60 | return seed;
61 | }
62 |
63 | public IntegerProperty completeProperty() {
64 | return complete;
65 | }
66 |
67 | public StringProperty resultProperty() {
68 | return result;
69 | }
70 |
71 | public IntegerProperty countProperty() {
72 | return count;
73 | }
74 | }
75 |
--------------------------------------------------------------------------------
/Minesweeper/src/minesweeper/coach/Helper.fxml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
--------------------------------------------------------------------------------
/Minesweeper/src/minesweeper/coach/helper.css:
--------------------------------------------------------------------------------
1 | /*
2 | * Empty Stylesheet file.
3 | */
4 |
5 | .mainFxmlClass {
6 |
7 | }
8 |
--------------------------------------------------------------------------------
/Minesweeper/src/minesweeper/gamestate/msx/GameStateX.java:
--------------------------------------------------------------------------------
1 | package minesweeper.gamestate.msx;
2 |
3 | import minesweeper.gamestate.GameStateModel;
4 | import minesweeper.gamestate.GameStateModelViewer;
5 | import minesweeper.settings.GameSettings;
6 | import minesweeper.structure.Location;
7 |
8 | public class GameStateX extends GameStateModelViewer {
9 |
10 | private ScreenScanner scanner;
11 |
12 | public GameStateX(ScreenScanner scanner) {
13 | super(GameSettings.create(scanner.getColumns(), scanner.getRows(), scanner.getMines()));
14 |
15 | this.scanner = scanner;
16 |
17 | doAutoComplete = false; // minesweeperX will do this itself
18 |
19 | }
20 |
21 |
22 | @Override
23 | protected void placeFlagHandle(Location m) {
24 | scanner.flag(m.x, m.y);
25 |
26 | // give it time to set the flag
27 | try {
28 | Thread.sleep(20);
29 | } catch (InterruptedException e) {
30 | e.printStackTrace();
31 | }
32 |
33 | try {
34 | scanner.updateField();
35 | } catch (Exception e) {
36 | // TODO Auto-generated catch block
37 | e.printStackTrace();
38 | }
39 |
40 | }
41 |
42 | @Override
43 | protected boolean clearSquareHitMine(Location m) {
44 |
45 | scanner.clear(m.x, m.y);
46 |
47 | try {
48 | Thread.sleep(20);
49 | } catch (InterruptedException e) {
50 | e.printStackTrace();
51 | }
52 |
53 | try {
54 | scanner.updateField();
55 | } catch (Exception e) {
56 | // TODO Auto-generated catch block
57 | e.printStackTrace();
58 | }
59 |
60 | // since the click could have expanded more areas we need to allow for that
61 | for (int x=0; x < scanner.getColumns(); x++) {
62 | for (int y=0; y < scanner.getRows(); y++) {
63 | if (scanner.getValue(x, y) != ScreenScanner.HIDDEN && scanner.getValue(x, y) != ScreenScanner.FLAG) {
64 | setRevealed(x,y);
65 | }
66 |
67 | // if a flag has been revealed by minesweeperX then place that in the model (this happens at the end of a game)
68 | if (scanner.getValue(x, y) == ScreenScanner.FLAG && this.query(new Location(x,y)) == HIDDEN) {
69 | setFlag(x,y);
70 | }
71 | }
72 | }
73 |
74 | return scanner.isGameLost();
75 | }
76 |
77 | @Override
78 | protected void startHandle(Location m) {
79 |
80 | }
81 |
82 | @Override
83 | protected int queryHandle(int x, int y) {
84 |
85 | int value = scanner.getValue(x, y);
86 |
87 | if (value == ScreenScanner.HIDDEN) {
88 | System.out.println("value at (" + x + "," + y + ") is hidden and yet queried!");
89 | }
90 | if (value == ScreenScanner.FLAG) {
91 | System.out.println("value at (" + x + "," + y + ") is a flag and yet queried!");
92 | }
93 |
94 |
95 | if (value == ScreenScanner.HIDDEN) {
96 | return GameStateModel.HIDDEN;
97 | } else if (value == ScreenScanner.EMPTY) {
98 | return 0;
99 | } else if (value == ScreenScanner.FLAG) {
100 | return GameStateModel.HIDDEN;
101 | } else if (value == ScreenScanner.BOMB) {
102 | return GameStateModel.MINE;
103 | } else if (value == ScreenScanner.EXPLODED_BOMB) {
104 | return GameStateModel.EXPLODED_MINE;
105 | } else {
106 | return value;
107 | }
108 | }
109 |
110 |
111 | @Override
112 | /**
113 | * No privileged access since MinesweeperX can't show it
114 | */
115 | public int privilegedQuery(Location m, boolean showMines) {
116 | return query(m);
117 | }
118 |
119 | @Override
120 | protected boolean clearSurroundHandle(Location m) {
121 |
122 | scanner.clearAll(m.x, m.y);
123 |
124 | try {
125 | Thread.sleep(20);
126 | } catch (InterruptedException e) {
127 | e.printStackTrace();
128 | }
129 |
130 | try {
131 | scanner.updateField();
132 | } catch (Exception e) {
133 | e.printStackTrace();
134 | }
135 |
136 | // since the click could have expanded more areas we need to allow for that
137 | for (int x=0; x < scanner.getColumns(); x++) {
138 | for (int y=0; y < scanner.getRows(); y++) {
139 | if (scanner.getValue(x, y) != ScreenScanner.HIDDEN && scanner.getValue(x, y) != ScreenScanner.FLAG) {
140 | setRevealed(x,y);
141 | }
142 |
143 | // if a flag has been revealed by minesweeperX then place that in the model (this happens at the end of a game)
144 | if (scanner.getValue(x, y) == ScreenScanner.FLAG && this.query(new Location(x,y)) == HIDDEN) {
145 | setFlag(x,y);
146 | }
147 | }
148 | }
149 |
150 | return true;
151 | }
152 |
153 |
154 | @Override
155 | public String showGameKey() {
156 | return "Minesweeper X";
157 | }
158 |
159 |
160 | }
161 |
--------------------------------------------------------------------------------
/Minesweeper/src/minesweeper/gamestate/msx/ScreenLocation.java:
--------------------------------------------------------------------------------
1 | package minesweeper.gamestate.msx;
2 |
3 | public class ScreenLocation {
4 |
5 | public int topX, topY, botX, botY;
6 |
7 | public boolean found = false;
8 |
9 | final int botOffsetX=15;
10 | final int botOffsetY=15;
11 | final int offsetX=15;
12 | final int offsetY=-101;
13 |
14 | int width;
15 | int height;
16 |
17 | }
18 |
--------------------------------------------------------------------------------
/Minesweeper/src/minesweeper/resources/ms_button.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/DavidNHill/Minesweeper/0806bb80042184e4f2b1413f4092c4e09526c237/Minesweeper/src/minesweeper/resources/ms_button.png
--------------------------------------------------------------------------------
/Minesweeper/src/minesweeper/resources/ms_eight.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/DavidNHill/Minesweeper/0806bb80042184e4f2b1413f4092c4e09526c237/Minesweeper/src/minesweeper/resources/ms_eight.png
--------------------------------------------------------------------------------
/Minesweeper/src/minesweeper/resources/ms_five.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/DavidNHill/Minesweeper/0806bb80042184e4f2b1413f4092c4e09526c237/Minesweeper/src/minesweeper/resources/ms_five.png
--------------------------------------------------------------------------------
/Minesweeper/src/minesweeper/resources/ms_flag.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/DavidNHill/Minesweeper/0806bb80042184e4f2b1413f4092c4e09526c237/Minesweeper/src/minesweeper/resources/ms_flag.png
--------------------------------------------------------------------------------
/Minesweeper/src/minesweeper/resources/ms_four.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/DavidNHill/Minesweeper/0806bb80042184e4f2b1413f4092c4e09526c237/Minesweeper/src/minesweeper/resources/ms_four.png
--------------------------------------------------------------------------------
/Minesweeper/src/minesweeper/resources/ms_mine.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/DavidNHill/Minesweeper/0806bb80042184e4f2b1413f4092c4e09526c237/Minesweeper/src/minesweeper/resources/ms_mine.png
--------------------------------------------------------------------------------
/Minesweeper/src/minesweeper/resources/ms_mine_bang.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/DavidNHill/Minesweeper/0806bb80042184e4f2b1413f4092c4e09526c237/Minesweeper/src/minesweeper/resources/ms_mine_bang.png
--------------------------------------------------------------------------------
/Minesweeper/src/minesweeper/resources/ms_one.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/DavidNHill/Minesweeper/0806bb80042184e4f2b1413f4092c4e09526c237/Minesweeper/src/minesweeper/resources/ms_one.png
--------------------------------------------------------------------------------
/Minesweeper/src/minesweeper/resources/ms_question.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/DavidNHill/Minesweeper/0806bb80042184e4f2b1413f4092c4e09526c237/Minesweeper/src/minesweeper/resources/ms_question.png
--------------------------------------------------------------------------------
/Minesweeper/src/minesweeper/resources/ms_seven.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/DavidNHill/Minesweeper/0806bb80042184e4f2b1413f4092c4e09526c237/Minesweeper/src/minesweeper/resources/ms_seven.png
--------------------------------------------------------------------------------
/Minesweeper/src/minesweeper/resources/ms_six.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/DavidNHill/Minesweeper/0806bb80042184e4f2b1413f4092c4e09526c237/Minesweeper/src/minesweeper/resources/ms_six.png
--------------------------------------------------------------------------------
/Minesweeper/src/minesweeper/resources/ms_three.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/DavidNHill/Minesweeper/0806bb80042184e4f2b1413f4092c4e09526c237/Minesweeper/src/minesweeper/resources/ms_three.png
--------------------------------------------------------------------------------
/Minesweeper/src/minesweeper/resources/ms_two.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/DavidNHill/Minesweeper/0806bb80042184e4f2b1413f4092c4e09526c237/Minesweeper/src/minesweeper/resources/ms_two.png
--------------------------------------------------------------------------------
/Minesweeper/src/minesweeper/resources/ms_zero.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/DavidNHill/Minesweeper/0806bb80042184e4f2b1413f4092c4e09526c237/Minesweeper/src/minesweeper/resources/ms_zero.png
--------------------------------------------------------------------------------
/MinesweeperBulk/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/MinesweeperBulk/.gitignore:
--------------------------------------------------------------------------------
1 | /bin/
2 |
--------------------------------------------------------------------------------
/MinesweeperBulk/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | MinesweeperBulk
4 |
5 |
6 |
7 |
8 |
9 | org.eclipse.jdt.core.javabuilder
10 |
11 |
12 |
13 |
14 | org.eclipse.xtext.ui.shared.xtextBuilder
15 |
16 |
17 |
18 |
19 |
20 | org.eclipse.xtext.ui.shared.xtextNature
21 | org.eclipse.jdt.core.javanature
22 |
23 |
24 |
--------------------------------------------------------------------------------
/MinesweeperBulk/.settings/org.eclipse.jdt.core.prefs:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5 | org.eclipse.jdt.core.compiler.compliance=1.8
6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate
8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
11 | org.eclipse.jdt.core.compiler.source=1.8
12 |
--------------------------------------------------------------------------------
/MinesweeperBulk/src/minesweeperbulk/BbbvMonitor.java:
--------------------------------------------------------------------------------
1 | package minesweeperbulk;
2 |
3 | import java.text.DecimalFormat;
4 |
5 | import minesweeper.gamestate.GameStateModel;
6 | import minesweeper.solver.bulk.BulkRequest;
7 | import minesweeper.solver.bulk.BulkRequestGame;
8 | import minesweeper.solver.bulk.GamePostListener;
9 |
10 | public class BbbvMonitor extends GamePostListener {
11 |
12 | private static final DecimalFormat MASK = new DecimalFormat("#0.000");
13 |
14 | private class GuessData {
15 | private int total = 0;
16 | private int sum3BV = 0;
17 | }
18 |
19 | private GuessData noGuess = new GuessData();
20 | private GuessData guess = new GuessData();
21 |
22 | private int played = 0;
23 | private int total3BV = 0;
24 |
25 | @Override
26 | public void postAction(BulkRequest request) {
27 |
28 | BulkRequestGame req = request.getRequestGames()[0];
29 | GameStateModel game = request.getRequestGames()[0].getGame();
30 |
31 | played++;
32 | total3BV = total3BV + game.getTotal3BV();
33 |
34 | GuessData target;
35 | if (req.getGuesses() == 0) {
36 | target = noGuess;
37 | } else {
38 | target = guess;
39 | }
40 |
41 | target.total++;
42 | target.sum3BV = target.sum3BV + game.getTotal3BV();
43 |
44 |
45 | }
46 |
47 | @Override
48 | public void postResults() {
49 |
50 | if (noGuess.total != 0) {
51 | double avg3BV1 = (double) noGuess.sum3BV / (double) noGuess.total;
52 | System.out.println("No Guess boards " + noGuess.total + " total 3BV " + noGuess.sum3BV + " average 3BV " + MASK.format(avg3BV1));
53 | }
54 |
55 | if (guess.total != 0) {
56 | double avg3BV2 = (double) guess.sum3BV / (double) guess.total;
57 | System.out.println("Guessing boards " + guess.total + " total 3BV " + guess.sum3BV + " average 3BV " + MASK.format(avg3BV2));
58 | }
59 |
60 | double avg3BV = (double) total3BV / (double) played;
61 | System.out.println("All boards " + played + " total 3BV " + total3BV + " average 3BV " + MASK.format(avg3BV));
62 | }
63 |
64 | }
65 |
--------------------------------------------------------------------------------
/MinesweeperBulk/src/minesweeperbulk/EfficiencyMonitor.java:
--------------------------------------------------------------------------------
1 | package minesweeperbulk;
2 |
3 | import java.io.File;
4 | import java.util.ArrayList;
5 | import java.util.HashMap;
6 | import java.util.List;
7 | import java.util.Map;
8 |
9 | import minesweeper.gamestate.GameStateModel;
10 | import minesweeper.solver.bulk.BulkRequest;
11 | import minesweeper.solver.bulk.GamePostListener;
12 |
13 | public class EfficiencyMonitor extends GamePostListener {
14 |
15 | private final Map table = new HashMap<>();
16 | private int eff100 = 0;
17 | private int wins = 0;
18 | private int played = 0;
19 | private int announcementCount = 0;
20 | private int clicks = 0;
21 |
22 | private final double reportThreshold;
23 | private final double saveThreshold;
24 |
25 | public EfficiencyMonitor(double reportThreshold) {
26 | this(reportThreshold, reportThreshold);
27 | }
28 |
29 | public EfficiencyMonitor(double reportThreshold, double saveThreshold) {
30 | this.reportThreshold = reportThreshold;
31 | this.saveThreshold = saveThreshold;
32 | }
33 |
34 | @Override
35 | public void postAction(BulkRequest request) {
36 |
37 | GameStateModel game = request.getRequestGames()[0].getGame();
38 |
39 | played++;
40 | clicks = clicks + game.getActionCount();
41 |
42 | if (game.getGameState() == GameStateModel.WON) {
43 | wins++;
44 | double efficiency = 100 * ((double) game.getTotal3BV() / (double) game.getActionCount());
45 |
46 | // report the board if over a certain threshold
47 | if (efficiency >= reportThreshold) {
48 | announcementCount++;
49 | System.out.println(announcementCount + ") " + game.getSeed() + " has 3BV " + game.getTotal3BV() + " efficiency " + efficiency);
50 | }
51 |
52 | // save the board if over a certain threshold
53 | if (efficiency >= saveThreshold) {
54 | File saveFile = new File("C:\\Users\\david\\Documents\\Minesweeper\\Positions\\Saved", "Pos_" + game.getSeed() + ".mine");
55 | try {
56 | System.out.println("Saving position in file " + saveFile.getAbsolutePath());
57 | game.savePosition(saveFile, "Efficiency of " + efficiency);
58 | } catch (Exception e) {
59 | System.out.println("Save position failed: " + e.getMessage());
60 | }
61 | }
62 |
63 | int inteff = (int) Math.floor(efficiency);
64 | if (inteff >= 100) {
65 | eff100++;
66 | }
67 |
68 | if (inteff >= 0) {
69 | if (table.containsKey(inteff)) {
70 | int tally = table.get(inteff);
71 | tally++;
72 | table.put(inteff, tally);
73 | } else {
74 | table.put(inteff, 1);
75 | }
76 | }
77 | }
78 |
79 | }
80 |
81 | public void postResults() {
82 |
83 | int clicksPerEff100 = 0;
84 | if (eff100 > 0) {
85 | clicksPerEff100 = clicks / eff100;
86 | }
87 |
88 | System.out.println("Efficiency >= 100% : " + eff100 + " from " + wins + " wins out of " + played + " played, clicks " + clicks + ", clicks/eff100 " + clicksPerEff100);
89 |
90 | List results = new ArrayList<>(table.keySet());
91 | results.sort(null);
92 |
93 | for (int key: results) {
94 | System.out.println("Efficiency " + key + " occurs " + table.get(key));
95 | }
96 |
97 | }
98 |
99 | }
100 |
--------------------------------------------------------------------------------
/MinesweeperBulk/src/minesweeperbulk/GuessMonitor.java:
--------------------------------------------------------------------------------
1 | package minesweeperbulk;
2 |
3 | import java.text.DecimalFormat;
4 | import java.util.ArrayList;
5 | import java.util.HashMap;
6 | import java.util.List;
7 | import java.util.Map;
8 |
9 | import minesweeper.gamestate.GameStateModel;
10 | import minesweeper.solver.bulk.BulkRequest;
11 | import minesweeper.solver.bulk.BulkRequestGame;
12 | import minesweeper.solver.bulk.GamePostListener;
13 |
14 | public class GuessMonitor extends GamePostListener {
15 |
16 | private static final DecimalFormat MASK = new DecimalFormat("#0.000");
17 |
18 | private class GuessData {
19 | private int total;
20 | private int sum3BV;
21 | private int[] values = new int[9];
22 | }
23 |
24 | private final Map winTable = new HashMap<>();
25 | private final Map loseTable = new HashMap<>();
26 | private int wins = 0;
27 | private int played = 0;
28 | private int eights = 0;
29 | private int ng8 = 0;
30 |
31 | @Override
32 | public void postAction(BulkRequest request) {
33 |
34 | BulkRequestGame req = request.getRequestGames()[0];
35 | GameStateModel game = request.getRequestGames()[0].getGame();
36 |
37 | played++;
38 |
39 | Map table;
40 |
41 | if (game.getGameState() == GameStateModel.WON) {
42 | table = winTable;
43 | wins++;
44 | } else {
45 | table = loseTable;
46 | }
47 |
48 | // save every 1000th game
49 | /*
50 | if (played % 1000 == 0) {
51 | File saveFile = new File("C:\\Users\\david\\Documents\\Minesweeper\\Positions\\Saved", "Pos_" + game.getSeed() + ".mine");
52 | try {
53 | System.out.println("Saving position in file " + saveFile.getAbsolutePath());
54 | game.savePosition(saveFile, "");
55 | } catch (Exception e) {
56 | System.out.println("Save position failed: " + e.getMessage());
57 | }
58 | }
59 | */
60 |
61 | int guesses = req.getGuesses();
62 |
63 | eights = eights + game.getValueCount(8);
64 | if (game.getGameState() == GameStateModel.WON && req.getGuesses() == 0) {
65 | ng8 = ng8 + + game.getValueCount(8);
66 | }
67 |
68 | if (table.containsKey(guesses)) {
69 | GuessData gd = table.get(guesses);
70 | gd.total++;
71 | gd.sum3BV = gd.sum3BV + game.getTotal3BV();
72 |
73 | for (int i=0; i < gd.values.length; i++) {
74 | gd.values[i] += game.getValueCount(i);
75 | }
76 |
77 | table.put(guesses, gd);
78 | } else {
79 | GuessData gd = new GuessData();
80 | gd.total = 1;
81 | gd.sum3BV = game.getTotal3BV();
82 | table.put(guesses, gd);
83 | }
84 | }
85 |
86 | @Override
87 | public void postResults() {
88 |
89 | System.out.println(wins + " wins out of " + played + " played");
90 | System.out.println(eights + " Eights, of which " + ng8 + " in no-guess games");
91 |
92 | List winResults = new ArrayList<>(winTable.keySet());
93 | winResults.sort(null);
94 |
95 | int winWeight = 0;
96 |
97 | System.out.println("Histogram of guesses to win");
98 | for (int key: winResults) {
99 | GuessData gd = winTable.get(key);
100 | double avg3BV = (double) gd.sum3BV / (double) gd.total;
101 |
102 | System.out.println("guesses " + key + " occurs " + gd.total + " average 3bv " + MASK.format(avg3BV) + " number of 8's " + gd.values[8]);
103 |
104 | winWeight = winWeight + gd.total * key;
105 | }
106 | if (wins != 0) {
107 | double avgGuessesToWin = (double) winWeight / (double) wins;
108 | System.out.println("Average guesses to win " + MASK.format(avgGuessesToWin));
109 | }
110 |
111 |
112 | List loseResults = new ArrayList<>(loseTable.keySet());
113 | loseResults.sort(null);
114 |
115 | int loseWeight = 0;
116 |
117 | System.out.println("Histogram of guesses to lose");
118 | for (int key: loseResults) {
119 | GuessData gd = loseTable.get(key);
120 | double avg3BV = (double) gd.sum3BV / (double) gd.total;
121 |
122 | System.out.println("guesses " + key + " occurs " + gd.total + " average 3bv " + MASK.format(avg3BV) + " number of 8's " + gd.values[8]);
123 |
124 | loseWeight = loseWeight + gd.total * key;
125 | }
126 | if (wins != 0) {
127 | double avgGuessesToLose = (double) loseWeight / (double) (played - wins) ;
128 | System.out.println("Average guesses to lose " + MASK.format(avgGuessesToLose));
129 | }
130 |
131 |
132 | }
133 |
134 | }
135 |
--------------------------------------------------------------------------------
/MinesweeperBulk/src/minesweeperbulk/PreActions.java:
--------------------------------------------------------------------------------
1 | package minesweeperbulk;
2 |
3 | import java.util.List;
4 |
5 | import minesweeper.gamestate.GameStateModel;
6 | import minesweeper.solver.bulk.GamePreListener;
7 | import minesweeper.structure.Action;
8 |
9 | public class PreActions extends GamePreListener {
10 |
11 | private List preActions;
12 |
13 | public PreActions(List preActions) {
14 | this.preActions = preActions;
15 | }
16 |
17 |
18 | @Override
19 | public void preAction(GameStateModel game) {
20 |
21 | for (Action a: preActions) {
22 | game.doAction(a);
23 | if (game.getGameState() == GameStateModel.LOST || game.getGameState() == GameStateModel.WON) {
24 | break;
25 | }
26 | }
27 |
28 | }
29 |
30 | }
31 |
--------------------------------------------------------------------------------
/MinesweeperBulk/src/minesweeperbulk/RandomGuesser.java:
--------------------------------------------------------------------------------
1 | package minesweeperbulk;
2 |
3 | import java.util.ArrayList;
4 | import java.util.Collections;
5 | import java.util.Iterator;
6 | import java.util.List;
7 |
8 | import minesweeper.gamestate.GameStateModel;
9 | import minesweeper.settings.GameSettings;
10 | import minesweeper.solver.bulk.GamePreListener;
11 | import minesweeper.structure.Action;
12 |
13 | public class RandomGuesser extends GamePreListener {
14 |
15 | final private GameSettings settings;
16 | final private int border = 1;
17 |
18 | final List interior = new ArrayList<>();
19 | final Action[][] board;
20 |
21 | private int eights = 0;
22 | private int clicks = 0;
23 | private int played = 0;
24 |
25 | public RandomGuesser(GameSettings settings) {
26 |
27 | this.settings = settings;
28 |
29 | board = new Action[settings.width][settings.height];
30 |
31 | for (int i=border; i < settings.width - border; i++) {
32 | for (int j=border; j < settings.height - border; j++) {
33 | Action a = new Action(i,j, Action.CLEAR);
34 | interior.add(a);
35 | board[i][j] = a;
36 |
37 | }
38 | }
39 |
40 | }
41 |
42 |
43 | @Override
44 | public void preAction(GameStateModel game) {
45 |
46 | played++;
47 |
48 | // tiles we can guess shuffled
49 | List available = new ArrayList<>(interior);
50 | Collections.shuffle(available);
51 |
52 | boolean[][] processed = new boolean[settings.width][settings.height];
53 |
54 | // first guess
55 | Action guess = available.get(0);
56 |
57 | // while we have moves and the game hasn't blasted
58 | while (true) {
59 |
60 | clicks++;
61 | game.doAction(guess);
62 |
63 | // if we've lost stop
64 | if (game.getGameState() == GameStateModel.LOST) {
65 | break;
66 | }
67 |
68 | if (game.query(guess) == 8) {
69 | eights++;
70 | }
71 |
72 | // remove guesses near cleared tiles
73 | for (int i=border; i < settings.width - border; i++) {
74 | for (int j=border; j < settings.height - border; j++) {
75 | if (!processed[i][j]) {
76 | if (game.query(board[i][j]) != GameStateModel.HIDDEN) {
77 |
78 | //System.out.println(i + " " + j + " is not hidden");
79 |
80 | processed[i][j] = true;
81 |
82 | int offset;
83 | if (game.query(guess) < 3) {
84 | offset = 2;
85 | } else {
86 | offset = 1;
87 | }
88 |
89 | // remove tiles adjacent to an open area
90 | Iterator iterator = available.iterator();
91 | while (iterator.hasNext()) {
92 | Action a = iterator.next();
93 | if (distance(a.x, a.y, i, j) <= offset) {
94 | iterator.remove();
95 | }
96 | }
97 | }
98 | }
99 |
100 | }
101 | }
102 |
103 | if (available.isEmpty()) {
104 | //System.out.println("No guesses left");
105 | break;
106 | }
107 |
108 | // next guess
109 | guess = available.get(0);
110 |
111 | }
112 |
113 | }
114 |
115 | private int distance(int x1, int y1, int x2, int y2) {
116 |
117 | int dx = Math.abs(x1 - x2);
118 | int dy = Math.abs(y1 - y2);
119 |
120 | return Math.max(dx, dy);
121 |
122 | }
123 |
124 |
125 | public void displayTable() {
126 |
127 | System.out.println(eights + " eights out of " + played + " played and " + clicks + " clicks");
128 |
129 | }
130 |
131 | }
132 |
--------------------------------------------------------------------------------
/MinesweeperBulk/src/minesweeperbulk/RemainingMonitor.java:
--------------------------------------------------------------------------------
1 | package minesweeperbulk;
2 |
3 | import java.util.ArrayList;
4 | import java.util.HashMap;
5 | import java.util.List;
6 | import java.util.Map;
7 |
8 | import minesweeper.gamestate.GameStateModel;
9 | import minesweeper.solver.bulk.BulkRequest;
10 | import minesweeper.solver.bulk.GamePostListener;
11 |
12 | public class RemainingMonitor extends GamePostListener {
13 |
14 | private final Map table = new HashMap<>();
15 | private int wins = 0;
16 | private int played = 0;
17 |
18 | @Override
19 | public void postAction(BulkRequest request) {
20 |
21 | GameStateModel game = request.getRequestGames()[0].getGame();
22 |
23 | played++;
24 |
25 | if (game.getGameState() == GameStateModel.WON) {
26 | wins++;
27 | }
28 |
29 | int bbvLeft = game.getTotal3BV() - game.getCleared3BV();
30 | int tilesLeft = game.getHidden() - game.getMines();
31 |
32 | if (table.containsKey(tilesLeft)) {
33 | int tally = table.get(tilesLeft);
34 | tally++;
35 | table.put(tilesLeft, tally);
36 | } else {
37 | table.put(tilesLeft, 1);
38 | }
39 | }
40 |
41 | public void postResults() {
42 |
43 | System.out.println(wins + " wins out of " + played + " played");
44 |
45 | List results = new ArrayList<>(table.keySet());
46 | results.sort(null);
47 |
48 | for (int key: results) {
49 | System.out.println("Tiles left to clear " + key + " occurs " + table.get(key));
50 | }
51 |
52 | }
53 |
54 | }
55 |
--------------------------------------------------------------------------------
/MinesweeperBulk/src/minesweeperbulk/StartStrategy.java:
--------------------------------------------------------------------------------
1 | package minesweeperbulk;
2 |
3 | import java.util.List;
4 |
5 | import minesweeper.gamestate.GameStateModel;
6 | import minesweeper.solver.bulk.GamePreListener;
7 | import minesweeper.structure.Action;
8 |
9 | public class StartStrategy extends GamePreListener {
10 |
11 | private List preActions;
12 | private int requiredZeros;
13 |
14 | /**
15 | * Play the strategy until the required number of zeros has been found
16 | */
17 | public StartStrategy(List preActions, int requiredZeros) {
18 | this.preActions = preActions;
19 | this.requiredZeros = requiredZeros;
20 | }
21 |
22 |
23 | @Override
24 | public void preAction(GameStateModel game) {
25 |
26 | int zeros = 0;
27 | int actionCount = 0;
28 |
29 | for (Action a: preActions) {
30 | game.doAction(a);
31 | actionCount++;
32 |
33 | if (game.getGameState() == GameStateModel.LOST || game.getGameState() == GameStateModel.WON) {
34 | break;
35 | }
36 | if (game.query(a) == 0) {
37 | zeros++;
38 | if (zeros >= requiredZeros) {
39 | break;
40 | }
41 | }
42 | }
43 |
44 | }
45 |
46 | }
47 |
--------------------------------------------------------------------------------
/MinesweeperBulk/src/minesweeperbulk/StartStrategyResign.java:
--------------------------------------------------------------------------------
1 | package minesweeperbulk;
2 |
3 | import java.util.List;
4 |
5 | import minesweeper.gamestate.GameStateModel;
6 | import minesweeper.solver.bulk.GamePreListener;
7 | import minesweeper.structure.Action;
8 |
9 | public class StartStrategyResign extends GamePreListener {
10 |
11 | private List preActions;
12 | private int requiredZeros;
13 |
14 | /**
15 | * Play the strategy until the required number of zeros has been found. If first guess isn't a zero then resign
16 | */
17 | public StartStrategyResign(List preActions, int requiredZeros) {
18 | this.preActions = preActions;
19 | this.requiredZeros = requiredZeros;
20 | }
21 |
22 |
23 | @Override
24 | public void preAction(GameStateModel game) {
25 |
26 | int zeros = 0;
27 | int actionCount = 0;
28 |
29 | for (Action a: preActions) {
30 | game.doAction(a);
31 | actionCount++;
32 |
33 | if (game.getGameState() == GameStateModel.LOST || game.getGameState() == GameStateModel.WON) {
34 | break;
35 | }
36 | if (actionCount == 1 && game.query(a) != 0) {
37 | game.resign();
38 | break;
39 | }
40 | if (game.query(a) == 0) {
41 | zeros++;
42 | if (zeros >= requiredZeros) {
43 | break;
44 | }
45 | }
46 | }
47 |
48 | }
49 |
50 | }
51 |
--------------------------------------------------------------------------------
/MinesweeperBulkCompare/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/MinesweeperBulkCompare/.gitignore:
--------------------------------------------------------------------------------
1 | /bin/
2 |
--------------------------------------------------------------------------------
/MinesweeperBulkCompare/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | MinesweeperBulkCompare
4 |
5 |
6 |
7 |
8 |
9 | org.eclipse.jdt.core.javabuilder
10 |
11 |
12 |
13 |
14 | org.eclipse.xtext.ui.shared.xtextBuilder
15 |
16 |
17 |
18 |
19 |
20 | org.eclipse.xtext.ui.shared.xtextNature
21 | org.eclipse.jdt.core.javanature
22 |
23 |
24 |
--------------------------------------------------------------------------------
/MinesweeperBulkCompare/.settings/org.eclipse.jdt.core.prefs:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5 | org.eclipse.jdt.core.compiler.compliance=1.8
6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate
8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
11 | org.eclipse.jdt.core.compiler.source=1.8
12 |
--------------------------------------------------------------------------------
/MinesweeperBulkCompare/src/bulkCompare/CompareMonitor.java:
--------------------------------------------------------------------------------
1 | package bulkCompare;
2 |
3 | import minesweeper.gamestate.GameStateModel;
4 | import minesweeper.solver.bulk.BulkRequest;
5 | import minesweeper.solver.bulk.BulkRequestGame;
6 | import minesweeper.solver.bulk.GamePostListener;
7 |
8 | public class CompareMonitor extends GamePostListener {
9 |
10 | private int[] wins;
11 | private int played = 0;
12 |
13 | @Override
14 | public void postAction(BulkRequest request) {
15 |
16 | played++;
17 |
18 | // initialise
19 | if (wins == null) {
20 | wins = new int[request.getRequestGames().length];
21 | }
22 |
23 | int index = 0;
24 | StringBuilder sb = new StringBuilder();
25 | for (BulkRequestGame brg: request.getRequestGames()) {
26 |
27 | GameStateModel game = brg.getGame();
28 | if (game.getGameState() == GameStateModel.WON ) {
29 | wins[index]++;
30 | }
31 |
32 | if (sb.length() != 0) {
33 | sb.append(", ");
34 | }
35 | sb.append("Run ").append(index).append(" wins=").append(wins[index]);
36 | index++;
37 | }
38 |
39 | // every 100th set of games show the comparison
40 | if (played % 100 == 0) {
41 | System.out.println(sb);
42 | }
43 |
44 |
45 | }
46 |
47 | public void postResults() {
48 |
49 | if (wins == null) {
50 | System.out.println("-- No data to report --");
51 | return;
52 | }
53 | StringBuilder sb = new StringBuilder();
54 | for (int i=0; i < wins.length; i++) {
55 | if (sb.length() != 0) {
56 | sb.append(", ");
57 | }
58 | sb.append("Run ").append(i).append(" wins=").append(wins[i]);
59 | }
60 | System.out.println(sb);
61 |
62 | }
63 |
64 | }
65 |
--------------------------------------------------------------------------------
/MinesweeperBulkCompare/src/bulkCompare/GuessMonitor.java:
--------------------------------------------------------------------------------
1 | package bulkCompare;
2 |
3 | import java.text.DecimalFormat;
4 | import java.util.ArrayList;
5 | import java.util.HashMap;
6 | import java.util.List;
7 | import java.util.Map;
8 |
9 | import minesweeper.gamestate.GameStateModel;
10 | import minesweeper.solver.bulk.BulkRequest;
11 | import minesweeper.solver.bulk.BulkRequestGame;
12 | import minesweeper.solver.bulk.GamePostListener;
13 |
14 | public class GuessMonitor extends GamePostListener {
15 |
16 | private static final DecimalFormat MASK = new DecimalFormat("#0.000");
17 |
18 | private class GuessData {
19 | private int total;
20 | private int sum3BV;
21 | private int[] values = new int[9];
22 | }
23 |
24 | private final Map winTable = new HashMap<>();
25 | private final Map loseTable = new HashMap<>();
26 | private int wins = 0;
27 | private int played = 0;
28 | private int eights = 0;
29 | private int ng8 = 0;
30 |
31 | @Override
32 | public void postAction(BulkRequest request) {
33 |
34 | BulkRequestGame req = request.getRequestGames()[0];
35 | GameStateModel game = request.getRequestGames()[0].getGame();
36 |
37 | played++;
38 |
39 | Map table;
40 |
41 | if (game.getGameState() == GameStateModel.WON) {
42 | table = winTable;
43 | wins++;
44 | } else {
45 | table = loseTable;
46 | }
47 |
48 | int guesses = req.getGuesses();
49 |
50 | eights = eights + game.getValueCount(8);
51 | if (game.getGameState() == GameStateModel.WON && req.getGuesses() == 0) {
52 | ng8 = ng8 + + game.getValueCount(8);
53 | }
54 |
55 | if (table.containsKey(guesses)) {
56 | GuessData gd = table.get(guesses);
57 | gd.total++;
58 | gd.sum3BV = gd.sum3BV + game.getTotal3BV();
59 |
60 | for (int i=0; i < gd.values.length; i++) {
61 | gd.values[i] += game.getValueCount(i);
62 | }
63 |
64 | table.put(guesses, gd);
65 | } else {
66 | GuessData gd = new GuessData();
67 | gd.total = 1;
68 | gd.sum3BV = game.getTotal3BV();
69 | table.put(guesses, gd);
70 | }
71 | }
72 |
73 | @Override
74 | public void postResults() {
75 |
76 | System.out.println(wins + " wins out of " + played + " played");
77 | System.out.println(eights + " Eights, of which " + ng8 + " in no-guess games");
78 |
79 | List winResults = new ArrayList<>(winTable.keySet());
80 | winResults.sort(null);
81 |
82 | int winWeight = 0;
83 |
84 | System.out.println("Histogram of guesses to win");
85 | for (int key: winResults) {
86 | GuessData gd = winTable.get(key);
87 | double avg3BV = (double) gd.sum3BV / (double) gd.total;
88 |
89 | System.out.println("guesses " + key + " occurs " + gd.total + " average 3bv " + MASK.format(avg3BV) + " number of 8's " + gd.values[8]);
90 |
91 | winWeight = winWeight + gd.total * key;
92 | }
93 | if (wins != 0) {
94 | double avgGuessesToWin = (double) winWeight / (double) wins;
95 | System.out.println("Average guesses to win " + MASK.format(avgGuessesToWin));
96 | }
97 |
98 |
99 | List loseResults = new ArrayList<>(loseTable.keySet());
100 | loseResults.sort(null);
101 |
102 | int loseWeight = 0;
103 |
104 | System.out.println("Histogram of guesses to lose");
105 | for (int key: loseResults) {
106 | GuessData gd = loseTable.get(key);
107 | double avg3BV = (double) gd.sum3BV / (double) gd.total;
108 |
109 | System.out.println("guesses " + key + " occurs " + gd.total + " average 3bv " + MASK.format(avg3BV) + " number of 8's " + gd.values[8]);
110 |
111 | loseWeight = loseWeight + gd.total * key;
112 | }
113 | if (wins != 0) {
114 | double avgGuessesToLose = (double) loseWeight / (double) (played - wins) ;
115 | System.out.println("Average guesses to lose " + MASK.format(avgGuessesToLose));
116 | }
117 |
118 |
119 | }
120 |
121 | }
122 |
--------------------------------------------------------------------------------
/MinesweeperBulkCompare/src/bulkCompare/PreActions.java:
--------------------------------------------------------------------------------
1 | package bulkCompare;
2 |
3 | import java.util.List;
4 |
5 | import minesweeper.gamestate.GameStateModel;
6 | import minesweeper.solver.bulk.GamePreListener;
7 | import minesweeper.structure.Action;
8 |
9 | public class PreActions extends GamePreListener {
10 |
11 | private List preActions;
12 |
13 | public PreActions(List preActions) {
14 | this.preActions = preActions;
15 | }
16 |
17 |
18 | @Override
19 | public void preAction(GameStateModel game) {
20 |
21 | for (Action a: preActions) {
22 | game.doAction(a);
23 | if (game.getGameState() == GameStateModel.LOST || game.getGameState() == GameStateModel.WON) {
24 | break;
25 | }
26 | }
27 |
28 | }
29 |
30 | }
31 |
--------------------------------------------------------------------------------
/MinesweeperBulkCompare/src/bulkCompare/StartStrategy.java:
--------------------------------------------------------------------------------
1 | package bulkCompare;
2 |
3 | import java.util.List;
4 |
5 | import minesweeper.gamestate.GameStateModel;
6 | import minesweeper.solver.bulk.GamePreListener;
7 | import minesweeper.structure.Action;
8 |
9 | public class StartStrategy extends GamePreListener {
10 |
11 | private List preActions;
12 | private int requiredZeros;
13 |
14 | /**
15 | * Play the strategy until the required number of zeros has been found
16 | */
17 | public StartStrategy(List preActions, int requiredZeros) {
18 | this.preActions = preActions;
19 | this.requiredZeros = requiredZeros;
20 | }
21 |
22 |
23 | @Override
24 | public void preAction(GameStateModel game) {
25 |
26 | int zeros = 0;
27 | int actionCount = 0;
28 |
29 | for (Action a: preActions) {
30 | game.doAction(a);
31 | actionCount++;
32 |
33 | if (game.getGameState() == GameStateModel.LOST || game.getGameState() == GameStateModel.WON) {
34 | break;
35 | }
36 | if (game.query(a) == 0) {
37 | zeros++;
38 | if (zeros >= requiredZeros) {
39 | break;
40 | }
41 | }
42 | }
43 |
44 | }
45 |
46 | }
47 |
--------------------------------------------------------------------------------
/MinesweeperBulkCompare/src/bulkCompare/StartStrategyResign.java:
--------------------------------------------------------------------------------
1 | package bulkCompare;
2 |
3 | import java.util.List;
4 |
5 | import minesweeper.gamestate.GameStateModel;
6 | import minesweeper.solver.bulk.GamePreListener;
7 | import minesweeper.structure.Action;
8 |
9 | public class StartStrategyResign extends GamePreListener {
10 |
11 | private List preActions;
12 | private int requiredZeros;
13 |
14 | /**
15 | * Play the strategy until the required number of zeros has been found. If first guess isn't a zero then resign
16 | */
17 | public StartStrategyResign(List preActions, int requiredZeros) {
18 | this.preActions = preActions;
19 | this.requiredZeros = requiredZeros;
20 | }
21 |
22 |
23 | @Override
24 | public void preAction(GameStateModel game) {
25 |
26 | int zeros = 0;
27 | int actionCount = 0;
28 |
29 | for (Action a: preActions) {
30 | game.doAction(a);
31 | actionCount++;
32 |
33 | if (game.getGameState() == GameStateModel.LOST || game.getGameState() == GameStateModel.WON) {
34 | break;
35 | }
36 | if (actionCount == 1 && game.query(a) != 0) {
37 | game.resign();
38 | break;
39 | }
40 | if (game.query(a) == 0) {
41 | zeros++;
42 | if (zeros >= requiredZeros) {
43 | break;
44 | }
45 | }
46 | }
47 |
48 | }
49 |
50 | }
51 |
--------------------------------------------------------------------------------
/MinesweeperBulkCompare/src/bulkCompare/TwoWayCompare.java:
--------------------------------------------------------------------------------
1 | package bulkCompare;
2 |
3 | import java.io.File;
4 |
5 | import minesweeper.gamestate.GameStateModel;
6 | import minesweeper.solver.bulk.BulkRequest;
7 | import minesweeper.solver.bulk.BulkRequestGame;
8 | import minesweeper.solver.bulk.GamePostListener;
9 |
10 | public class TwoWayCompare extends GamePostListener {
11 |
12 | private int oneWon = 0;
13 | private int twoWon = 0;
14 | private int played = 0;
15 | private int bothWon = 0;
16 | private int bothLost = 0;
17 | private int oneWonTwoLost = 0;
18 | private int oneLostTwoWon = 0;
19 |
20 | @Override
21 | public void postAction(BulkRequest request) {
22 |
23 | if (request.getRequestGames().length < 2) {
24 | System.out.println("This Monitor must have 2 players");
25 | System.exit(1);
26 | }
27 | played++;
28 |
29 | GameStateModel game1 = request.getRequestGames()[0].getGame();
30 | GameStateModel game2 = request.getRequestGames()[1].getGame();
31 |
32 | if (game1.getGameState() == GameStateModel.WON) {
33 | oneWon++;
34 | if (game2.getGameState() == GameStateModel.WON) {
35 | twoWon++;
36 | bothWon++;
37 | } else {
38 | oneWonTwoLost++;
39 |
40 | //savePosition(game2, "Advanced pseudo lost");
41 | }
42 | } else {
43 | if (game2.getGameState() == GameStateModel.WON) {
44 | twoWon++;
45 | oneLostTwoWon++;
46 | } else {
47 | bothLost++;
48 | }
49 | }
50 |
51 |
52 |
53 | // every 100th set of games show the comparison
54 | if (played % 100 == 0) {
55 | String sb = "Played " + played + ", One wins " + oneWon + ", One wins & Two loses " + oneWonTwoLost + ", One loses & Two wins " + oneLostTwoWon + ", Difference " + (oneLostTwoWon - oneWonTwoLost);
56 | System.out.println(sb);
57 | }
58 |
59 |
60 | }
61 |
62 | public void postResults() {
63 |
64 | System.out.println("Final Result:-");
65 | String sb = "Played " + played + ", One wins " + oneWon + ", One wins & Two loses " + oneWonTwoLost + ", One loses & Two wins " + oneLostTwoWon + ", Difference " + (oneLostTwoWon - oneWonTwoLost);
66 | System.out.println(sb);
67 |
68 | }
69 |
70 | void savePosition(GameStateModel game, String text) {
71 |
72 | File saveFile = new File("C:\\Users\\david\\Documents\\Minesweeper\\Positions\\Saved", "Pos_" + game.getSeed() + "_lost.mine");
73 | if (saveFile.exists()) {
74 | return;
75 | }
76 | try {
77 | System.out.println("Saving position in file " + saveFile.getAbsolutePath());
78 | game.savePosition(saveFile, text);
79 | } catch (Exception e) {
80 | System.out.println("Save position failed: " + e.getMessage());
81 | }
82 |
83 | }
84 |
85 | }
86 |
--------------------------------------------------------------------------------
/MinesweeperExplorer/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/MinesweeperExplorer/.gitignore:
--------------------------------------------------------------------------------
1 | /bin/
2 |
--------------------------------------------------------------------------------
/MinesweeperExplorer/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | MinesweeperExplorer
4 |
5 |
6 |
7 |
8 |
9 | org.eclipse.jdt.core.javabuilder
10 |
11 |
12 |
13 |
14 | org.eclipse.xtext.ui.shared.xtextBuilder
15 |
16 |
17 |
18 |
19 |
20 | org.eclipse.xtext.ui.shared.xtextNature
21 | org.eclipse.jdt.core.javanature
22 |
23 |
24 |
--------------------------------------------------------------------------------
/MinesweeperExplorer/.settings/org.eclipse.jdt.core.prefs:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5 | org.eclipse.jdt.core.compiler.compliance=1.8
6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate
8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
11 | org.eclipse.jdt.core.compiler.source=1.8
12 |
--------------------------------------------------------------------------------
/MinesweeperExplorer/build.fxbuild:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/MinesweeperExplorer/src/minesweeper/explorer/busy/BusyScreen.fxml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
19 |
24 |
25 |
26 |
--------------------------------------------------------------------------------
/MinesweeperExplorer/src/minesweeper/explorer/busy/MonitorTask.java:
--------------------------------------------------------------------------------
1 | package minesweeper.explorer.busy;
2 |
3 | import minesweeper.solver.utility.ProgressMonitor;
4 |
5 | public class MonitorTask implements Runnable {
6 |
7 | private final ProgressMonitor progress;
8 | private final BusyController controller;
9 | private boolean running = true;
10 |
11 | public MonitorTask(ProgressMonitor progress, BusyController controller) {
12 | this.progress = progress;
13 | this.controller = controller;
14 | }
15 |
16 | @Override
17 | public void run() {
18 |
19 | while (running) {
20 |
21 | controller.update(progress);
22 |
23 | try {
24 | Thread.sleep(500);
25 | } catch (InterruptedException e) {
26 | e.printStackTrace();
27 | running = false;
28 | break;
29 | }
30 | }
31 |
32 | System.out.println("Monitor task ending");
33 | }
34 |
35 | protected void stop() {
36 | running = false;
37 | }
38 |
39 |
40 | }
41 |
--------------------------------------------------------------------------------
/MinesweeperExplorer/src/minesweeper/explorer/busy/ParallelTask.java:
--------------------------------------------------------------------------------
1 | package minesweeper.explorer.busy;
2 |
3 | import java.util.concurrent.CountDownLatch;
4 |
5 |
6 | public abstract class ParallelTask implements Runnable {
7 |
8 | private CountDownLatch executionCompleted = new CountDownLatch(1);
9 | private BusyController controller;
10 |
11 | @Override
12 | public void run() {
13 | doExecute();
14 | executionCompleted.countDown();
15 | controller.finished();
16 | }
17 |
18 | abstract public void doExecute();
19 |
20 | public void await() {
21 | try {
22 | executionCompleted.await();
23 | } catch (InterruptedException e) {
24 | e.printStackTrace();
25 | }
26 | controller.finished();
27 |
28 | }
29 |
30 | abstract public T getResult();
31 |
32 | abstract public int getMaxProgress();
33 | abstract public int getProgress();
34 |
35 | protected void setController(BusyController controller) {
36 | this.controller = controller;
37 | }
38 |
39 | }
40 |
--------------------------------------------------------------------------------
/MinesweeperExplorer/src/minesweeper/explorer/main/BoardMonitor.java:
--------------------------------------------------------------------------------
1 | package minesweeper.explorer.main;
2 |
3 | import java.math.BigInteger;
4 |
5 | import minesweeper.explorer.gamestate.GameStateExplorer;
6 | import minesweeper.explorer.structure.Board;
7 | import minesweeper.gamestate.GameStateModel;
8 | import minesweeper.solver.Solver;
9 | import minesweeper.solver.settings.SettingsFactory;
10 | import minesweeper.solver.settings.SolverSettings;
11 | import minesweeper.solver.settings.SettingsFactory.Setting;
12 |
13 | public class BoardMonitor implements Runnable {
14 |
15 | private final MainScreenController controller;
16 | private int lastHash = 1;
17 |
18 | public BoardMonitor(MainScreenController controller) {
19 | this.controller = controller;
20 | }
21 |
22 | @Override
23 | public void run() {
24 |
25 | System.out.println("Starting monitor thread");
26 |
27 | String msg = "";
28 | boolean activeButtons = true;
29 | while (true) {
30 |
31 |
32 | Board board = controller.getCurrentBoard();
33 |
34 | int hash = board.getHashValue();
35 |
36 | // if we haven't placed too many mines
37 | if (hash != lastHash) {
38 |
39 | controller.removeIndicators();
40 |
41 | if (board.getFlagsPlaced() <= controller.getTotalMines()) {
42 |
43 | lastHash = hash;
44 |
45 | GameStateModel gs = null;
46 | try {
47 | gs = GameStateExplorer.build(board, controller.getTotalMines());
48 | SolverSettings settings = SettingsFactory.GetSettings(Setting.VERY_LARGE_ANALYSIS);
49 | Solver solver = new Solver(gs, settings, false);
50 |
51 | System.out.println("Checking solution count");
52 | BigInteger solutionCount = solver.getSolutionCount();
53 |
54 | if (solutionCount.signum() == 0) {
55 | msg = "There are no solutions for this board";
56 | activeButtons = false;
57 | } else {
58 | //System.out.println(solutionCount.bitLength());
59 | activeButtons = true;
60 | if (solutionCount.bitLength() > 50) {
61 | msg = MainScreenController.EXPONENT_DISPLAY.format(solutionCount) + " solutions remain";
62 | } else {
63 | msg = MainScreenController.NUMBER_DISPLAY.format(solutionCount) + " solutions remain";
64 | }
65 |
66 | }
67 |
68 | //System.out.println(solutionCount);
69 |
70 | } catch (Exception e) {
71 | msg = "Unable to calculate solution count: " + e.getMessage();
72 | activeButtons = false;
73 | e.printStackTrace();
74 | }
75 |
76 | } else {
77 | msg = "Invalid number of mines";
78 | activeButtons = false;
79 | //System.out.println("Too many mines placed");
80 | }
81 | }
82 |
83 |
84 | controller.setSolutionLine(msg);
85 | controller.setButtonsEnabled(activeButtons);
86 | board = null;
87 |
88 | try {
89 | Thread.sleep(1000);
90 | } catch (InterruptedException e) {
91 | e.printStackTrace();
92 | }
93 |
94 | }
95 |
96 |
97 | }
98 |
99 | }
100 |
--------------------------------------------------------------------------------
/MinesweeperExplorer/src/minesweeper/explorer/main/Explorer.java:
--------------------------------------------------------------------------------
1 | package minesweeper.explorer.main;
2 |
3 | import java.io.IOException;
4 | import java.text.DecimalFormat;
5 |
6 | import javafx.application.Application;
7 | import javafx.application.Platform;
8 | import javafx.event.EventHandler;
9 | import javafx.fxml.FXMLLoader;
10 | import javafx.scene.Parent;
11 | import javafx.scene.Scene;
12 | import javafx.scene.layout.Background;
13 | import javafx.scene.layout.BackgroundFill;
14 | import javafx.scene.paint.Color;
15 | import javafx.stage.Stage;
16 | import javafx.stage.WindowEvent;
17 |
18 | public class Explorer extends Application {
19 |
20 | public static String APPLICATION_NAME = "Minesweeper explorer";
21 | public static String VERSION = "0.0";
22 |
23 | {
24 | System.out.println("Java version " + System.getProperty("java.runtime.version"));
25 | System.out.println("Java vendor " + System.getProperty("java.vendor"));
26 | }
27 |
28 | public static final Background BACKGROUND_PINK = new Background(new BackgroundFill(Color.PINK, null, null));
29 | public static final Background BACKGROUND_SILVER = new Background(new BackgroundFill(Color.SILVER, null, null));
30 |
31 | //private final int tileSize = 24;
32 |
33 | public static final DecimalFormat PERCENT = new DecimalFormat("#0.000%");
34 | public static final DecimalFormat TWO_DP = new DecimalFormat("#0.00");
35 | public static final Background GREY_BACKGROUND = new Background(new BackgroundFill(Color.LIGHTGREY, null, null));
36 |
37 | private static Stage primaryStage;
38 |
39 | //private static final Graphics graphics = new Graphics();
40 |
41 | @Override
42 | public void start(Stage primaryStage) {
43 |
44 | System.out.println("Starting...");
45 |
46 | if (Explorer.class.getResource("MainScreen.fxml") == null) {
47 | System.out.println("MainScreen.fxml not found");
48 | }
49 |
50 | // create the helper screen
51 | FXMLLoader loader = new FXMLLoader(Explorer.class.getResource("MainScreen.fxml"));
52 |
53 | Parent root = null;
54 | try {
55 | root = (Parent) loader.load();
56 | } catch (IOException ex) {
57 | System.err.println(ex.getMessage());
58 | }
59 |
60 | Explorer.primaryStage = primaryStage;
61 |
62 | try {
63 | //BorderPane root = new BorderPane();
64 | Scene scene = new Scene(root,900,550);
65 | scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
66 | primaryStage.setScene(scene);
67 | primaryStage.setTitle(APPLICATION_NAME);
68 | primaryStage.getIcons().add(Graphics.ICON);
69 | primaryStage.show();
70 | } catch(Exception e) {
71 | e.printStackTrace();
72 | }
73 |
74 | System.out.println("Main screen running...");
75 |
76 | MainScreenController mainScreenController = loader.getController();
77 |
78 | if (mainScreenController == null) {
79 | System.out.println("MainScreenController not found");
80 | }
81 |
82 | mainScreenController.setGraphicsSet(new Graphics());
83 | mainScreenController.newExpertBoard();
84 |
85 | BoardMonitor monitor = new BoardMonitor(mainScreenController);
86 |
87 | Thread monitorThread = new Thread(monitor, "Monitor");
88 | monitorThread.setDaemon(true);
89 | monitorThread.start();
90 |
91 | // actions to perform when a close request is received
92 | primaryStage.setOnCloseRequest(new EventHandler() {
93 | @Override
94 | public void handle(WindowEvent event) {
95 | System.out.println("Minesweeper explorer has received a close request");
96 | //mainScreenController.kill();
97 | Platform.exit();
98 | }
99 | });
100 |
101 | double x = primaryStage.getX();
102 | double y = primaryStage.getY();
103 |
104 | mainScreenController.getTileValueController().getStage().setX(x + primaryStage.getWidth());
105 | mainScreenController.getTileValueController().getStage().setY(y);
106 |
107 | System.out.println("...Startup finished.");
108 | }
109 |
110 | public static void main(String[] args) {
111 | launch(args);
112 | }
113 |
114 | public static void setSubTitle(String subTitle) {
115 |
116 | if (subTitle == null || subTitle.trim().isEmpty()) {
117 | primaryStage.setTitle(APPLICATION_NAME);
118 | } else {
119 | primaryStage.setTitle(APPLICATION_NAME + " - " + subTitle);
120 | }
121 |
122 |
123 | }
124 |
125 | }
126 |
--------------------------------------------------------------------------------
/MinesweeperExplorer/src/minesweeper/explorer/main/TileValueData.java:
--------------------------------------------------------------------------------
1 | package minesweeper.explorer.main;
2 |
3 | import javafx.beans.property.SimpleStringProperty;
4 | import javafx.beans.property.StringProperty;
5 | import minesweeper.solver.constructs.InformationLocation.ByValue;
6 |
7 | public class TileValueData {
8 |
9 | private final StringProperty value;
10 | private final StringProperty probability;
11 | private final StringProperty clears;
12 |
13 | public TileValueData(ByValue bv) {
14 |
15 | this.value = new SimpleStringProperty(String.valueOf(bv.value));
16 | this.probability = new SimpleStringProperty(Explorer.PERCENT.format(bv.probability));
17 | this.clears = new SimpleStringProperty(String.valueOf(bv.clears));
18 |
19 | }
20 |
21 | public TileValueData(String value, String prob, String clears) {
22 |
23 | this.value = new SimpleStringProperty(value);
24 | this.probability = new SimpleStringProperty(prob);
25 | this.clears = new SimpleStringProperty(clears);
26 |
27 | }
28 |
29 | public StringProperty probabilityProperty() {
30 | return probability;
31 | }
32 |
33 | public StringProperty clearsProperty() {
34 | return clears;
35 | }
36 |
37 | public StringProperty valueProperty() {
38 | return value;
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/MinesweeperExplorer/src/minesweeper/explorer/main/TileValuesScreen.fxml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/MinesweeperExplorer/src/minesweeper/explorer/main/application.css:
--------------------------------------------------------------------------------
1 | /* JavaFX CSS - Leave this comment until you have at least create one rule which uses -fx-Property */
--------------------------------------------------------------------------------
/MinesweeperExplorer/src/minesweeper/explorer/structure/Expander.java:
--------------------------------------------------------------------------------
1 | package minesweeper.explorer.structure;
2 |
3 | import javafx.event.EventHandler;
4 | import javafx.geometry.Point2D;
5 | import javafx.scene.input.MouseEvent;
6 | import javafx.scene.paint.Color;
7 | import javafx.scene.shape.Circle;
8 | import javafx.scene.text.Font;
9 | import javafx.scene.text.Text;
10 | import javafx.stage.Popup;
11 |
12 | public class Expander extends Circle {
13 |
14 | // generic mouse click event for tiles
15 | private final EventHandler DRAGGED = new EventHandler() {
16 | @Override
17 | public void handle(MouseEvent event) {
18 |
19 | Expander expander = (Expander) event.getSource();
20 |
21 | Point2D xy = expander.localToParent(event.getX(), event.getY());
22 |
23 | expander.setCenterX(xy.getX());
24 | expander.setCenterY(xy.getY());
25 |
26 | toolTip.setX(event.getScreenX() + 10);
27 | toolTip.setY(event.getScreenY() - 10);
28 |
29 | int boardX = (int) (xy.getX() / size);
30 | int boardY = (int) (xy.getY() / size);
31 |
32 | String text = "(" + boardX + "," + boardY + ")";
33 |
34 | popupText.setText(text);
35 |
36 | toolTip.show(me().getScene().getWindow());
37 |
38 | /*
39 | if (event.getEventType() == MouseEvent.MOUSE_EXITED) {
40 | toolTip.hide();
41 | } else if (event.getEventType() == MouseEvent.MOUSE_ENTERED) {
42 | toolTip.show(window.getScene().getWindow());
43 | }
44 | */
45 |
46 | }
47 | };
48 |
49 | // generic mouse click event for tiles
50 | private final EventHandler DRAG_ENDED = new EventHandler() {
51 | @Override
52 | public void handle(MouseEvent event) {
53 |
54 | toolTip.hide();
55 |
56 | }
57 | };
58 |
59 | // generic mouse click event for tiles
60 | private final EventHandler ENTERED = new EventHandler() {
61 | @Override
62 | public void handle(MouseEvent event) {
63 |
64 | toolTip.setX(event.getScreenX() + 10);
65 | toolTip.setY(event.getScreenY() - 10);
66 |
67 | toolTip.show(me().getScene().getWindow());
68 |
69 | }
70 | };
71 |
72 | private Popup toolTip = new Popup();
73 | private Text popupText = new Text();
74 | private double size;
75 |
76 |
77 | public Expander(int x, int y, int radius, double size, Color fill) {
78 | super(x * size, y * size, radius, fill);
79 |
80 | toolTip.getContent().addAll(popupText);
81 | popupText.setText("Test");
82 | popupText.setFont(new Font(20));
83 | this.size = size;
84 | this.setOnMouseDragged(DRAGGED);
85 | this.setOnMouseReleased(DRAG_ENDED);
86 | //this.setOnMouseEntered(ENTERED);
87 | //this.setOnMouseExited(DRAG_ENDED);
88 |
89 | }
90 |
91 | private Expander me() {
92 | return this;
93 | }
94 |
95 | }
96 |
--------------------------------------------------------------------------------
/MinesweeperExplorer/src/minesweeper/explorer/structure/LedDigit.java:
--------------------------------------------------------------------------------
1 | package minesweeper.explorer.structure;
2 |
3 | import javafx.event.EventHandler;
4 | import javafx.scene.image.ImageView;
5 | import javafx.scene.input.ScrollEvent;
6 | import minesweeper.explorer.main.Graphics;
7 |
8 | public class LedDigit extends ImageView {
9 |
10 | // generic mouse scroll event for led digits
11 | private final static EventHandler SCROLLED = new EventHandler() {
12 |
13 | @Override
14 | public void handle(ScrollEvent event) {
15 |
16 | LedDigit digit = (LedDigit) event.getSource();
17 |
18 | if (digit.owner.isLocked()) {
19 | return;
20 | }
21 |
22 | //System.out.println("Scroll detected on digit " + event.getTextDeltaY());
23 |
24 | int delta;
25 | if (event.getTextDeltaY() < 0) {
26 | delta = 1;
27 | } else {
28 | delta = -1;
29 | }
30 |
31 | digit.rotateValue(delta);
32 |
33 | }
34 |
35 | };
36 |
37 | private final LedDigits owner;
38 | private int size;
39 |
40 | private int value = 0;
41 |
42 |
43 | public LedDigit(LedDigits owner, int size) {
44 |
45 | this.owner = owner;
46 | this.size = size;
47 |
48 | this.setOnScroll(SCROLLED);
49 | this.setPickOnBounds(true);
50 |
51 | doDraw();
52 |
53 | }
54 |
55 |
56 | private void doDraw() {
57 | setImage(Graphics.getLed(value));
58 | }
59 |
60 | public int getValue() {
61 | return this.value;
62 | }
63 |
64 |
65 | public void setValue(int value) {
66 | this.value = value;
67 | doDraw();
68 | }
69 |
70 | private void rotateValue(int delta) {
71 |
72 | int value = owner.getValue() + delta * this.size;
73 |
74 | if (value < 0) {
75 | value = 0;
76 | } else if (value > 999) {
77 | value = 999;
78 | }
79 |
80 | //int newValue = (value + delta) % 10;
81 | //if (newValue < 0) {
82 | // newValue = newValue + 10;
83 | //}
84 |
85 | owner.setValue(value);
86 | }
87 |
88 |
89 | }
90 |
--------------------------------------------------------------------------------
/MinesweeperExplorer/src/minesweeper/explorer/structure/LedDigits.java:
--------------------------------------------------------------------------------
1 | package minesweeper.explorer.structure;
2 |
3 | import javafx.beans.property.ReadOnlyIntegerProperty;
4 | import javafx.beans.value.ChangeListener;
5 | import javafx.beans.value.ObservableValue;
6 | import javafx.scene.layout.HBox;
7 |
8 | public class LedDigits extends HBox {
9 |
10 | ChangeListener VALUE_LISTENER = new ChangeListener() {
11 | @Override
12 | public void changed(ObservableValue observable, Object oldValue, Object newValue) {
13 | setValue((Integer) newValue);
14 | }
15 | };
16 | ReadOnlyIntegerProperty monitoredProperty;
17 |
18 | private final int numberOfDigits;
19 | private final LedDigit[] digits;
20 |
21 | private final int maxValue;
22 |
23 | private boolean locked;
24 | //private int value;
25 |
26 | public LedDigits(int numberOfDigits) {
27 | this(numberOfDigits, false);
28 | }
29 |
30 | public LedDigits(int numberOfDigits, boolean locked) {
31 |
32 | this.numberOfDigits = numberOfDigits;
33 | this.digits = new LedDigit[numberOfDigits];
34 |
35 | this.locked = locked;
36 |
37 | int size = 1;
38 |
39 | for (int i=0; i < numberOfDigits; i++) {
40 | digits[numberOfDigits - i - 1] = new LedDigit(this, size);
41 | size = size * 10;
42 | }
43 |
44 | this.maxValue = size - 1;
45 | //System.out.println(this.maxValue);
46 |
47 | // add the digits to the container
48 | this.getChildren().addAll(digits);
49 |
50 | }
51 |
52 |
53 | private void doDraw() {
54 |
55 | }
56 |
57 | public void setValueListener(ReadOnlyIntegerProperty valueProperty) {
58 | monitoredProperty = valueProperty;
59 | monitoredProperty.addListener(VALUE_LISTENER);
60 | }
61 |
62 | public void removeValueListener() {
63 | if (monitoredProperty == null) {
64 | return;
65 | }
66 |
67 | monitoredProperty.removeListener(VALUE_LISTENER);
68 | monitoredProperty = null;
69 | }
70 |
71 | public void setValue(int value) {
72 |
73 | if (value < 0) {
74 | value = 0;
75 | } else if (value > this.maxValue) {
76 | value = this.maxValue;
77 | }
78 |
79 | int work = value;
80 | for (int i=numberOfDigits - 1; i >= 0; i-- ) {
81 | int digitValue = work % 10;
82 | work = (work - digitValue) / 10;
83 | digits[i].setValue(digitValue);
84 | }
85 |
86 | doDraw();
87 | }
88 |
89 | public int getValue() {
90 |
91 | int work = 0;
92 | int exponent = 1;
93 | for (int i=numberOfDigits - 1; i >= 0; i-- ) {
94 | work = work + digits[i].getValue() * exponent;
95 | exponent = exponent * 10;
96 | }
97 |
98 | return work;
99 | }
100 |
101 | public boolean isLocked() {
102 | return locked;
103 | }
104 |
105 | }
106 |
--------------------------------------------------------------------------------
/MinesweeperExplorer/src/resources/images/0.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/DavidNHill/Minesweeper/0806bb80042184e4f2b1413f4092c4e09526c237/MinesweeperExplorer/src/resources/images/0.png
--------------------------------------------------------------------------------
/MinesweeperExplorer/src/resources/images/1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/DavidNHill/Minesweeper/0806bb80042184e4f2b1413f4092c4e09526c237/MinesweeperExplorer/src/resources/images/1.png
--------------------------------------------------------------------------------
/MinesweeperExplorer/src/resources/images/2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/DavidNHill/Minesweeper/0806bb80042184e4f2b1413f4092c4e09526c237/MinesweeperExplorer/src/resources/images/2.png
--------------------------------------------------------------------------------
/MinesweeperExplorer/src/resources/images/3.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/DavidNHill/Minesweeper/0806bb80042184e4f2b1413f4092c4e09526c237/MinesweeperExplorer/src/resources/images/3.png
--------------------------------------------------------------------------------
/MinesweeperExplorer/src/resources/images/4.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/DavidNHill/Minesweeper/0806bb80042184e4f2b1413f4092c4e09526c237/MinesweeperExplorer/src/resources/images/4.png
--------------------------------------------------------------------------------
/MinesweeperExplorer/src/resources/images/5.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/DavidNHill/Minesweeper/0806bb80042184e4f2b1413f4092c4e09526c237/MinesweeperExplorer/src/resources/images/5.png
--------------------------------------------------------------------------------
/MinesweeperExplorer/src/resources/images/6.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/DavidNHill/Minesweeper/0806bb80042184e4f2b1413f4092c4e09526c237/MinesweeperExplorer/src/resources/images/6.png
--------------------------------------------------------------------------------
/MinesweeperExplorer/src/resources/images/7.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/DavidNHill/Minesweeper/0806bb80042184e4f2b1413f4092c4e09526c237/MinesweeperExplorer/src/resources/images/7.png
--------------------------------------------------------------------------------
/MinesweeperExplorer/src/resources/images/8.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/DavidNHill/Minesweeper/0806bb80042184e4f2b1413f4092c4e09526c237/MinesweeperExplorer/src/resources/images/8.png
--------------------------------------------------------------------------------
/MinesweeperExplorer/src/resources/images/exploded.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/DavidNHill/Minesweeper/0806bb80042184e4f2b1413f4092c4e09526c237/MinesweeperExplorer/src/resources/images/exploded.png
--------------------------------------------------------------------------------
/MinesweeperExplorer/src/resources/images/flagged.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/DavidNHill/Minesweeper/0806bb80042184e4f2b1413f4092c4e09526c237/MinesweeperExplorer/src/resources/images/flagged.png
--------------------------------------------------------------------------------
/MinesweeperExplorer/src/resources/images/flaggedWrong.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/DavidNHill/Minesweeper/0806bb80042184e4f2b1413f4092c4e09526c237/MinesweeperExplorer/src/resources/images/flaggedWrong.png
--------------------------------------------------------------------------------
/MinesweeperExplorer/src/resources/images/flaggedWrong_thin.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/DavidNHill/Minesweeper/0806bb80042184e4f2b1413f4092c4e09526c237/MinesweeperExplorer/src/resources/images/flaggedWrong_thin.png
--------------------------------------------------------------------------------
/MinesweeperExplorer/src/resources/images/hidden.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/DavidNHill/Minesweeper/0806bb80042184e4f2b1413f4092c4e09526c237/MinesweeperExplorer/src/resources/images/hidden.png
--------------------------------------------------------------------------------
/MinesweeperExplorer/src/resources/images/led0.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/DavidNHill/Minesweeper/0806bb80042184e4f2b1413f4092c4e09526c237/MinesweeperExplorer/src/resources/images/led0.png
--------------------------------------------------------------------------------
/MinesweeperExplorer/src/resources/images/led1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/DavidNHill/Minesweeper/0806bb80042184e4f2b1413f4092c4e09526c237/MinesweeperExplorer/src/resources/images/led1.png
--------------------------------------------------------------------------------
/MinesweeperExplorer/src/resources/images/led2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/DavidNHill/Minesweeper/0806bb80042184e4f2b1413f4092c4e09526c237/MinesweeperExplorer/src/resources/images/led2.png
--------------------------------------------------------------------------------
/MinesweeperExplorer/src/resources/images/led3.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/DavidNHill/Minesweeper/0806bb80042184e4f2b1413f4092c4e09526c237/MinesweeperExplorer/src/resources/images/led3.png
--------------------------------------------------------------------------------
/MinesweeperExplorer/src/resources/images/led4.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/DavidNHill/Minesweeper/0806bb80042184e4f2b1413f4092c4e09526c237/MinesweeperExplorer/src/resources/images/led4.png
--------------------------------------------------------------------------------
/MinesweeperExplorer/src/resources/images/led5.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/DavidNHill/Minesweeper/0806bb80042184e4f2b1413f4092c4e09526c237/MinesweeperExplorer/src/resources/images/led5.png
--------------------------------------------------------------------------------
/MinesweeperExplorer/src/resources/images/led6.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/DavidNHill/Minesweeper/0806bb80042184e4f2b1413f4092c4e09526c237/MinesweeperExplorer/src/resources/images/led6.png
--------------------------------------------------------------------------------
/MinesweeperExplorer/src/resources/images/led7.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/DavidNHill/Minesweeper/0806bb80042184e4f2b1413f4092c4e09526c237/MinesweeperExplorer/src/resources/images/led7.png
--------------------------------------------------------------------------------
/MinesweeperExplorer/src/resources/images/led8.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/DavidNHill/Minesweeper/0806bb80042184e4f2b1413f4092c4e09526c237/MinesweeperExplorer/src/resources/images/led8.png
--------------------------------------------------------------------------------
/MinesweeperExplorer/src/resources/images/led9.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/DavidNHill/Minesweeper/0806bb80042184e4f2b1413f4092c4e09526c237/MinesweeperExplorer/src/resources/images/led9.png
--------------------------------------------------------------------------------
/MinesweeperExplorer/src/resources/images/led_blank.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/DavidNHill/Minesweeper/0806bb80042184e4f2b1413f4092c4e09526c237/MinesweeperExplorer/src/resources/images/led_blank.png
--------------------------------------------------------------------------------
/MinesweeperExplorer/src/resources/images/mine.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/DavidNHill/Minesweeper/0806bb80042184e4f2b1413f4092c4e09526c237/MinesweeperExplorer/src/resources/images/mine.png
--------------------------------------------------------------------------------
/MinesweeperExplorer/src/resources/images/mine_transparent.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/DavidNHill/Minesweeper/0806bb80042184e4f2b1413f4092c4e09526c237/MinesweeperExplorer/src/resources/images/mine_transparent.png
--------------------------------------------------------------------------------
/MinesweeperGameState/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/MinesweeperGameState/.gitignore:
--------------------------------------------------------------------------------
1 | /bin/
2 |
--------------------------------------------------------------------------------
/MinesweeperGameState/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | MinesweeperGameState
4 |
5 |
6 |
7 |
8 |
9 | org.eclipse.jdt.core.javabuilder
10 |
11 |
12 |
13 |
14 |
15 | org.eclipse.jdt.core.javanature
16 |
17 |
18 |
--------------------------------------------------------------------------------
/MinesweeperGameState/.settings/org.eclipse.jdt.core.prefs:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5 | org.eclipse.jdt.core.compiler.compliance=1.8
6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate
8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
11 | org.eclipse.jdt.core.compiler.source=1.8
12 |
--------------------------------------------------------------------------------
/MinesweeperGameState/src/minesweeper/gamestate/GameFactory.java:
--------------------------------------------------------------------------------
1 | package minesweeper.gamestate;
2 |
3 | import minesweeper.settings.GameSettings;
4 | import minesweeper.settings.GameType;
5 |
6 | public class GameFactory {
7 |
8 | private GameFactory() {
9 |
10 | }
11 |
12 | public static GameStateModelViewer create(GameType type, GameSettings settings, long gameSeed) {
13 |
14 | GameStateModelViewer gsm;
15 |
16 | if (type == GameType.EASY) {
17 | if (gameSeed != 0) {
18 | gsm = new GameStateEasy(settings, gameSeed);
19 | } else {
20 | gsm = new GameStateEasy(settings);
21 | }
22 | } else if (type == GameType.STANDARD) {
23 | if (gameSeed != 0) {
24 | gsm = new GameStateStandard(settings, gameSeed);
25 | } else {
26 | gsm = new GameStateStandard(settings);
27 | }
28 | } else if (type == GameType.HARD) {
29 | if (gameSeed != 0) {
30 | gsm = new GameStateHard(settings, gameSeed);
31 | } else {
32 | gsm = new GameStateHard(settings);
33 | }
34 | } else {
35 | throw new RuntimeException("Unexpected values in Game generation");
36 | }
37 |
38 | return gsm;
39 |
40 | }
41 |
42 | }
43 |
--------------------------------------------------------------------------------
/MinesweeperGameState/src/minesweeper/gamestate/GameStateModelViewer.java:
--------------------------------------------------------------------------------
1 | package minesweeper.gamestate;
2 |
3 | import minesweeper.settings.GameSettings;
4 | import minesweeper.structure.Location;
5 |
6 | /**
7 | * This contains a method to allow the viewer to see where the mines are
8 | * @author David
9 | *
10 | */
11 | abstract public class GameStateModelViewer extends GameStateModel {
12 |
13 | public GameStateModelViewer(GameSettings gameSettings) {
14 | this(gameSettings, 0);
15 | }
16 |
17 | public GameStateModelViewer(GameSettings gameSettings, long seed) {
18 | super(gameSettings, seed);
19 | }
20 |
21 | // can be used by the display to get the mines
22 | abstract public int privilegedQuery(Location m, boolean showMines);
23 |
24 |
25 | }
26 |
--------------------------------------------------------------------------------
/MinesweeperGameState/src/minesweeper/gamestate/MoveMethod.java:
--------------------------------------------------------------------------------
1 | package minesweeper.gamestate;
2 |
3 | public enum MoveMethod {
4 |
5 | HUMAN("Human"),
6 | BOOK("Opening Book"),
7 | CORRECTION("Correction"),
8 | TRIVIAL("Trivial analysis"),
9 | LOCAL("Local analysis"),
10 | PROBABILITY_ENGINE("Probability Engine"),
11 | BRUTE_FORCE("Brute Force"),
12 | BRUTE_FORCE_DEEP_ANALYSIS("Brute Force Deep Analysis"),
13 | GUESS("Guess"),
14 | ROLLOUT("Rollout"),
15 | UNAVOIDABLE_GUESS("Unavoidable Guess"),
16 | CHEAT("Cheat");
17 |
18 | public final String description;
19 |
20 |
21 | private MoveMethod(String description) {
22 | this.description = description;
23 | }
24 |
25 | }
26 |
--------------------------------------------------------------------------------
/MinesweeperGameState/src/minesweeper/random/DefaultRNG.java:
--------------------------------------------------------------------------------
1 | package minesweeper.random;
2 |
3 | public class DefaultRNG {
4 |
5 | static private Class extends RNG> defaultRNG = RNGJava.class;
6 |
7 |
8 | /**
9 | * Set the default RNG implementation used when creating the mine sweeper boards
10 | * @param rngClass
11 | */
12 | public static void setDefaultRNGClass(Class extends RNG> rngClass) {
13 | defaultRNG = rngClass;
14 | }
15 |
16 |
17 | /**
18 | * Get the default RNG implementation used when creating the mine sweeper boards
19 | * @param rngClass
20 | */
21 | public static Class extends RNG> getDefaultRNGClass() {
22 | return defaultRNG;
23 | }
24 |
25 | /**
26 | * Return an instance of the default random number generator with seed
27 | * @return
28 | */
29 | public static RNG getRNG(long seed) {
30 |
31 | RNG rng = null;
32 | try {
33 | rng = defaultRNG.newInstance();
34 | rng.seed(seed);
35 | } catch (InstantiationException e) {
36 | e.printStackTrace();
37 | } catch (IllegalAccessException e) {
38 | e.printStackTrace();
39 | } catch (IllegalArgumentException e) {
40 | e.printStackTrace();
41 | } catch (SecurityException e) {
42 | e.printStackTrace();
43 | }
44 |
45 | return rng;
46 |
47 | }
48 |
49 | }
50 |
--------------------------------------------------------------------------------
/MinesweeperGameState/src/minesweeper/random/RNG.java:
--------------------------------------------------------------------------------
1 | package minesweeper.random;
2 |
3 | public interface RNG {
4 |
5 | public void seed(long seed);
6 |
7 | public long random(int in);
8 |
9 | public String name();
10 |
11 | public String shortname();
12 |
13 | }
14 |
--------------------------------------------------------------------------------
/MinesweeperGameState/src/minesweeper/random/RNGJSF.java:
--------------------------------------------------------------------------------
1 | package minesweeper.random;
2 |
3 |
4 | public class RNGJSF implements RNG {
5 |
6 |
7 | private int[] s;
8 |
9 | public RNGJSF() {
10 | }
11 |
12 | public RNGJSF(long seed) {
13 | seed(seed);
14 | }
15 |
16 | public void seed(long seed) {
17 |
18 | int seed1 = (int) Math.abs(seed);
19 | int seed2 = (int) ((Math.abs(seed) >>> 32) & 0x001FFFFFl);
20 |
21 | //System.out.println(seed1 + " " + seed2);
22 |
23 | s = new int[] {0xf1ea5eed, seed1, seed2, seed1};
24 | for (int i = 0; i < 20; i++) random(1);
25 |
26 | }
27 |
28 | @Override
29 | public long random(int in) {
30 |
31 | int e = s[0] - (s[1] << 27 | s[1] >>> 5);
32 |
33 | s[0] = s[1] ^ (s[2] << 17 | s[2] >>> 15);
34 | s[1] = s[2] + s[3];
35 | s[2] = s[3] + e;
36 | s[3] = s[0] + e;
37 |
38 | //System.out.println(e + " " + s[0] + " " + s[1] + " " + s[2] + " " + s[3]);
39 |
40 | return ((s[3] & 0xFFFFFFFFl) * in) >>> 32;
41 |
42 | }
43 |
44 | private long bit32(long x) {
45 | return x & 0xFFFFFFFFl;
46 | }
47 |
48 | @Override
49 | public String name() {
50 | return "JSF random numbers";
51 | }
52 |
53 | @Override
54 | public String shortname() {
55 | return "JSF";
56 | }
57 |
58 |
59 | }
60 |
--------------------------------------------------------------------------------
/MinesweeperGameState/src/minesweeper/random/RNGJava.java:
--------------------------------------------------------------------------------
1 | package minesweeper.random;
2 |
3 | import java.util.Random;
4 |
5 | public class RNGJava implements RNG {
6 |
7 | static String shortName = "Java RNG";
8 |
9 | private Random rng = new Random();
10 |
11 | public RNGJava() {
12 | }
13 |
14 | public RNGJava(long seed) {
15 | seed(seed);
16 | }
17 |
18 | @Override
19 | public void seed(long seed) {
20 | rng = new Random(seed);
21 | }
22 |
23 | @Override
24 | public long random(int in) {
25 |
26 | if (in == 0) {
27 | return rng.nextLong();
28 | } else {
29 | return rng.nextInt(in);
30 | }
31 | //return (long) Math.floor(rng.nextDouble() * in);
32 |
33 | }
34 |
35 | @Override
36 | public String name() {
37 | return "Standard Java random numbers";
38 | }
39 |
40 | @Override
41 | public String shortname() {
42 | return "Java RNG";
43 | }
44 |
45 |
46 | }
47 |
--------------------------------------------------------------------------------
/MinesweeperGameState/src/minesweeper/random/RNGKiss64.java:
--------------------------------------------------------------------------------
1 | package minesweeper.random;
2 |
3 |
4 | public class RNGKiss64 implements RNG {
5 |
6 |
7 | private long kiss64_x = 1234567890987654321l;
8 | private long kiss64_c = 123456123456123456l;
9 | private long kiss64_y = 362436362436362436l;
10 | private long kiss64_z = 1066149217761810l;
11 | private long kiss64_t = 0;
12 |
13 | public RNGKiss64() {
14 | }
15 |
16 | public RNGKiss64(long seed) {
17 | seed(seed);
18 | }
19 |
20 | public void seed(long seed) {
21 |
22 | kiss64_x = seed | 1;
23 | kiss64_c = seed | 2;
24 | kiss64_y = seed | 4;
25 | kiss64_z = seed | 8;
26 | kiss64_t = 0;
27 |
28 | }
29 |
30 | @Override
31 | public long random(int in) {
32 |
33 | // multiply with carry
34 | kiss64_t = (kiss64_x << 58) + kiss64_c;
35 | kiss64_c = (kiss64_x >>> 6); // unsigned right shift
36 | kiss64_x += kiss64_t;
37 |
38 |
39 | //kiss64_c += (kiss64_x < kiss64_t)?1l:0l;
40 |
41 | kiss64_c += Long.compareUnsigned(kiss64_x, kiss64_t) < 0 ? 1l:0l;
42 |
43 | // XOR shift
44 | kiss64_y ^= (kiss64_y << 13);
45 | kiss64_y ^= (kiss64_y >>> 17); // unsigned right shift
46 | kiss64_y ^= (kiss64_y << 43);
47 |
48 | // Congruential
49 | kiss64_z = 6906969069l * kiss64_z + 1234567l;
50 | long rand = kiss64_x + kiss64_y + kiss64_z;
51 |
52 | if (in == 0) {
53 | return rand;
54 | } else {
55 | return ((rand & 0xFFFFFFFFl) * in) >>> 32; // unsigned right shift;
56 | }
57 |
58 | }
59 |
60 | @Override
61 | public String name() {
62 | return "KISS64 random numbers";
63 | }
64 |
65 | @Override
66 | public String shortname() {
67 | return "KISS64";
68 | }
69 |
70 |
71 | }
72 |
--------------------------------------------------------------------------------
/MinesweeperGameState/src/minesweeper/settings/GameSettings.java:
--------------------------------------------------------------------------------
1 | package minesweeper.settings;
2 |
3 | import java.util.Arrays;
4 | import java.util.List;
5 |
6 | public class GameSettings {
7 |
8 | /**
9 | * 9x9/10
10 | */
11 | final static public GameSettings BEGINNER = new GameSettings(9, 9, 10, "Beginner");
12 | /**
13 | * 16x16/40
14 | */
15 | final static public GameSettings ADVANCED = new GameSettings(16, 16, 40, "Advanced");
16 | /**
17 | * 30x16/99
18 | */
19 | final static public GameSettings EXPERT = new GameSettings(30, 16, 99, "Expert");
20 |
21 | final static private List standardSettings = Arrays.asList(BEGINNER, ADVANCED, EXPERT);
22 |
23 | final public int width;
24 | final public int height;
25 | final public int mines;
26 | final public String name;
27 |
28 | private GameSettings(int width, int height, int mines) {
29 | this(width, height, mines, "Custom");
30 | }
31 |
32 | private GameSettings(int width, int height, int mines, String name) {
33 | this.width = width;
34 | this.height = height;
35 | this.mines = mines;
36 | this.name = name;
37 | }
38 |
39 | public static GameSettings create(int width, int height, int mines) {
40 |
41 | for (GameSettings game: standardSettings) {
42 | if (game.width == width && game.height == height && game.mines == mines) {
43 | return game;
44 | }
45 | }
46 |
47 | return new GameSettings(width, height, mines, "Custom");
48 |
49 | }
50 |
51 | public String description() {
52 |
53 | return name + " (" + width + "," + height + "," + mines + ")";
54 |
55 | }
56 |
57 | @Override
58 | public String toString() {
59 |
60 | return width + "x" + height + "/" + mines;
61 |
62 | }
63 |
64 | }
65 |
--------------------------------------------------------------------------------
/MinesweeperGameState/src/minesweeper/settings/GameType.java:
--------------------------------------------------------------------------------
1 | package minesweeper.settings;
2 |
3 | public enum GameType {
4 |
5 | /**
6 | * Game starts with a guaranteed zero
7 | */
8 | EASY("Zero"),
9 |
10 | /**
11 | * Game starts with a guaranteed safe position (which could be a zero)
12 | */
13 | STANDARD("Safe"),
14 |
15 | /**
16 | * No guaranteed safe start (could be a mine)
17 | */
18 | HARD("Unsafe");
19 |
20 |
21 | public final String name;
22 |
23 | private GameType(String name) {
24 | this.name = name;
25 | }
26 |
27 |
28 | }
29 |
--------------------------------------------------------------------------------
/MinesweeperGameState/src/minesweeper/structure/Action.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this template, choose Tools | Templates
3 | * and open the template in the editor.
4 | */
5 | package minesweeper.structure;
6 |
7 | import java.math.BigDecimal;
8 | import java.text.DecimalFormat;
9 | import java.util.Comparator;
10 |
11 | import minesweeper.gamestate.MoveMethod;
12 |
13 |
14 | /**
15 | *
16 | * @author David
17 | */
18 | public class Action extends Location {
19 |
20 | public static final int CLEAR = 1;
21 | public static final int CLEARALL = 2;
22 | public static final int FLAG = 3;
23 |
24 | public static final DecimalFormat FORMAT_2DP = new DecimalFormat("#0.00");
25 |
26 | private final static String[] ACTION = {"", "Clear", "Clear around", "Place flag"};
27 | private final static BigDecimal MINUS_ONE = new BigDecimal("-1");
28 | private final static BigDecimal ONE_HUNDRED = BigDecimal.valueOf(100);
29 |
30 | static long globalUID = 0;
31 |
32 | private final long myUID;
33 | private final int action;
34 | private final BigDecimal bigProb;
35 |
36 | private final boolean certainty;
37 |
38 | //private int type = 0;
39 | private final MoveMethod moveMethod;
40 |
41 | String comment;
42 |
43 | // used by the human player
44 | public Action(int x, int y, int a) {
45 | this(new Location(x, y), a);
46 | }
47 |
48 | // used by the human player
49 | public Action(Location l, int a) {
50 | this(l, a, MoveMethod.HUMAN, "", MINUS_ONE);
51 | }
52 |
53 | // used by the computer coach
54 | public Action(Location l, int a, MoveMethod moveMethod, String comment, BigDecimal bigProb) {
55 | this(l, a, moveMethod, comment, bigProb, globalUID++);
56 |
57 | }
58 |
59 | // used by the computer coach to force a move to be earlier in the list - e.g. when we need to remove a flag placed by the human player before we can clear the square
60 | public Action(Location l, int a, MoveMethod moveMethod, String comment, BigDecimal bigProb, long uid) {
61 | super(l.x, l.y);
62 |
63 | this.action = a;
64 | this.comment = comment;
65 | this.bigProb = bigProb;
66 | this.moveMethod = moveMethod;
67 |
68 | this.myUID = uid;
69 | if (bigProb.compareTo(BigDecimal.ONE) == 0) {
70 | this.certainty = true;
71 | } else {
72 | this.certainty = false;
73 | }
74 |
75 | }
76 |
77 | public int getAction() {
78 | return this.action;
79 | }
80 |
81 | /**
82 | * Returns true when this action is 100% certain
83 | */
84 | public boolean isCertainty() {
85 | return this.certainty;
86 | }
87 |
88 | public BigDecimal getBigProb() {
89 | return bigProb;
90 | }
91 |
92 | public MoveMethod getMoveMethod() {
93 | return this.moveMethod;
94 | }
95 |
96 | @Override
97 | public String toString() {
98 |
99 | String result = Action.ACTION[this.action] + " at " + super.toString() + " by " + moveMethod.description + " " + comment;
100 |
101 | if (bigProb.compareTo(BigDecimal.ONE) < 0) {
102 | result = result + " with a probability of " + FORMAT_2DP.format(bigProb.multiply(ONE_HUNDRED)) + "%";
103 | }
104 |
105 | return result;
106 |
107 | }
108 |
109 |
110 | /**
111 | * sort by the UID field which is a sequence of when the move was found
112 | */
113 | static public final Comparator SORT_BY_MOVE_NUMBER = new Comparator() {
114 | @Override
115 | public int compare(Action o1, Action o2) {
116 |
117 | return (int) (o1.myUID - o2.myUID);
118 |
119 | }
120 | };
121 |
122 | }
123 |
--------------------------------------------------------------------------------
/MinesweeperGameState/src/minesweeper/structure/Area.java:
--------------------------------------------------------------------------------
1 | package minesweeper.structure;
2 |
3 | import java.util.Collection;
4 | import java.util.Collections;
5 | import java.util.HashSet;
6 | import java.util.Set;
7 |
8 | /**
9 | * Collection of locations and some operations
10 | * @author David
11 | *
12 | */
13 | public class Area {
14 |
15 | public final static Area EMPTY_AREA = new Area(new HashSet<>());
16 |
17 | private final Set area;
18 | private final Set readOnlyArea;
19 |
20 | /**
21 | * Build an area based on a collection of locations
22 | */
23 | public Area(Collection area) {
24 | this.area = new HashSet<>(area);
25 | this.readOnlyArea = Collections.unmodifiableSet(this.area);
26 | }
27 |
28 | /**
29 | * wrap a pre-built set
30 | */
31 | public Area(Set area) {
32 | this.area = area;
33 | this.readOnlyArea = Collections.unmodifiableSet(this.area);
34 | }
35 |
36 | /**
37 | * Returns true if this area contains the location
38 | */
39 | public boolean contains(Location loc) {
40 | return area.contains(loc);
41 | }
42 |
43 | /**
44 | * returns true if this area contains all the locations in the subset
45 | */
46 | public boolean supersetOf(Area subset) {
47 | return area.containsAll(subset.area);
48 | }
49 |
50 | public Collection getLocations() {
51 | return readOnlyArea;
52 | }
53 |
54 | public int size() {
55 | return area.size();
56 | }
57 |
58 | public Area add(Location add) {
59 | if (area.contains(add)) {
60 | return this;
61 | }
62 | Set result = new HashSet<>(this.size());
63 | result.addAll(area);
64 | result.add(add);
65 | return new Area(result);
66 | }
67 |
68 | public Area remove(Location remove) {
69 | if (!area.contains(remove)) {
70 | return this;
71 | }
72 | Set result = new HashSet<>(this.size());
73 | result.addAll(area);
74 | result.remove(remove);
75 | return new Area(result);
76 | }
77 |
78 | public Area merge(Area with) {
79 | Set result = new HashSet<>(this.size() + with.size());
80 | result.addAll(area);
81 | result.addAll(with.area);
82 | return new Area(result);
83 | }
84 |
85 | }
86 |
--------------------------------------------------------------------------------
/MinesweeperGameState/src/minesweeper/structure/Location.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this template, choose Tools | Templates
3 | * and open the template in the editor.
4 | */
5 | package minesweeper.structure;
6 |
7 | /**
8 | *
9 | * @author David
10 | */
11 | public class Location implements Comparable {
12 |
13 | public final int x;
14 | public final int y;
15 |
16 | protected final int sortOrder;
17 |
18 | public Location(int x, int y) {
19 |
20 | this.x = x;
21 | this.y = y;
22 |
23 | this.sortOrder = y * 10000 + x;
24 |
25 | }
26 |
27 | @Override
28 | public String toString() {
29 | return "(" + x + "," + y + ")";
30 | }
31 |
32 | /**
33 | * target is one if the 8 squares surrounding this location
34 | * @param target
35 | * @return
36 | */
37 | public boolean isAdjacent(Location target) {
38 |
39 | int dx = Math.abs(this.x - target.x);
40 | int dy = Math.abs(this.y - target.y);
41 |
42 | if (dx > 1 || dy > 1 || (dx == 0 && dy == 0)) {
43 | return false;
44 | } else {
45 | return true;
46 | }
47 |
48 | }
49 |
50 | @Override
51 | /**
52 | * Returns true if m describes the same location
53 | */
54 | public boolean equals(Object m) {
55 |
56 | if (m instanceof Location) {
57 | if (this.x == ((Location) m).x && this.y == ((Location) m).y) {
58 | return true;
59 | } else {
60 | return false;
61 | }
62 | } else {
63 | return false;
64 | }
65 |
66 | }
67 |
68 | @Override
69 | public int hashCode() {
70 | return sortOrder;
71 | }
72 |
73 | @Override
74 | public int compareTo(Location arg0) {
75 | return this.sortOrder - arg0.sortOrder;
76 | }
77 |
78 | }
79 |
--------------------------------------------------------------------------------
/WindowController/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/WindowController/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | WindowController
4 |
5 |
6 |
7 |
8 |
9 | org.eclipse.jdt.core.javabuilder
10 |
11 |
12 |
13 |
14 |
15 | org.eclipse.jdt.core.javanature
16 |
17 |
18 |
--------------------------------------------------------------------------------
/WindowController/.settings/org.eclipse.jdt.core.prefs:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5 | org.eclipse.jdt.core.compiler.compliance=1.8
6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate
8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
11 | org.eclipse.jdt.core.compiler.source=1.8
12 |
--------------------------------------------------------------------------------
/WindowController/bin/window/controller/GetWindowRect$GetWindowRectException.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/DavidNHill/Minesweeper/0806bb80042184e4f2b1413f4092c4e09526c237/WindowController/bin/window/controller/GetWindowRect$GetWindowRectException.class
--------------------------------------------------------------------------------
/WindowController/bin/window/controller/GetWindowRect$MyUser32.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/DavidNHill/Minesweeper/0806bb80042184e4f2b1413f4092c4e09526c237/WindowController/bin/window/controller/GetWindowRect$MyUser32.class
--------------------------------------------------------------------------------
/WindowController/bin/window/controller/GetWindowRect$WindowNotFoundException.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/DavidNHill/Minesweeper/0806bb80042184e4f2b1413f4092c4e09526c237/WindowController/bin/window/controller/GetWindowRect$WindowNotFoundException.class
--------------------------------------------------------------------------------
/WindowController/bin/window/controller/GetWindowRect.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/DavidNHill/Minesweeper/0806bb80042184e4f2b1413f4092c4e09526c237/WindowController/bin/window/controller/GetWindowRect.class
--------------------------------------------------------------------------------
/WindowController/jna-4.2.1.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/DavidNHill/Minesweeper/0806bb80042184e4f2b1413f4092c4e09526c237/WindowController/jna-4.2.1.jar
--------------------------------------------------------------------------------
/WindowController/jna-platform-4.2.1.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/DavidNHill/Minesweeper/0806bb80042184e4f2b1413f4092c4e09526c237/WindowController/jna-platform-4.2.1.jar
--------------------------------------------------------------------------------
/WindowController/src/window/controller/GetWindowRect.java:
--------------------------------------------------------------------------------
1 | package window.controller;
2 |
3 | import com.sun.jna.Native;
4 | import com.sun.jna.platform.win32.User32;
5 | import com.sun.jna.platform.win32.WinDef;
6 | import com.sun.jna.platform.win32.WinDef.DWORD;
7 | import com.sun.jna.platform.win32.WinDef.HWND;
8 | //import com.sun.jna.*;
9 | //import com.sun.jna.platform.win32.WinDef.HWND;
10 | import com.sun.jna.win32.*;
11 |
12 | public class GetWindowRect {
13 |
14 | public interface MyUser32 extends StdCallLibrary {
15 | MyUser32 INSTANCE = (MyUser32) Native.loadLibrary("user32", MyUser32.class,
16 | W32APIOptions.DEFAULT_OPTIONS);
17 |
18 | HWND FindWindow(String lpClassName, String lpWindowName);
19 |
20 | int GetWindowRect(HWND handle, int[] rect);
21 | }
22 |
23 | public static int[] getRect(String windowName) throws WindowNotFoundException,
24 | GetWindowRectException {
25 |
26 |
27 | HWND hwnd = MyUser32.INSTANCE.FindWindow(null, windowName);
28 |
29 | /*
30 | char[] buffer = new char[1000];
31 |
32 | //HWND next = User32.INSTANCE.GetForegroundWindow();
33 | HWND next = User32.INSTANCE.FindWindow(null, null);
34 |
35 | //HWND next = User32.INSTANCE.GetWindow(all, new DWORD(2));
36 |
37 | while (next != null) {
38 |
39 | User32.INSTANCE.GetWindowText(next, buffer, buffer.length);
40 |
41 | if (next != null) {
42 | System.out.println("found something - " + String.valueOf(buffer));
43 | }
44 |
45 | next = User32.INSTANCE.GetWindow(next, new DWORD(2));
46 |
47 | }
48 | */
49 |
50 |
51 | if (hwnd == null) {
52 | throw new WindowNotFoundException("", windowName);
53 | }
54 |
55 | int[] rect = {0, 0, 0, 0};
56 | int result = MyUser32.INSTANCE.GetWindowRect(hwnd, rect);
57 | if (result == 0) {
58 | throw new GetWindowRectException(windowName);
59 | }
60 | return rect;
61 | }
62 |
63 | @SuppressWarnings("serial")
64 | public static class WindowNotFoundException extends Exception {
65 | public WindowNotFoundException(String className, String windowName) {
66 | super(String.format("Window null for className: %s; windowName: %s",
67 | className, windowName));
68 | }
69 | }
70 |
71 | @SuppressWarnings("serial")
72 | public static class GetWindowRectException extends Exception {
73 | public GetWindowRectException(String windowName) {
74 | super("Window Rect not found for " + windowName);
75 | }
76 | }
77 | }
--------------------------------------------------------------------------------