Skip to content

Commit 02ea90d

Browse files
authored
Merge pull request #6 from Eric-Butcher/make_solvers
Make solvers
2 parents 81644db + 0f8af9d commit 02ea90d

32 files changed

+2023
-451
lines changed

.idea/workspace.xml

Lines changed: 56 additions & 46 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/controller/SolveAction.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@ public SolveAction(Model model, View view){
1818
@Override
1919
public void actionPerformed(ActionEvent e) {
2020
this.model.solve();
21+
this.view.updateView(this.model.updateView());
2122
}
2223
}

src/controller/SolverSelectAction.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package controller;
22

33
import model.Model;
4+
import model.solvers.Solver;
5+
import model.solvers.SolverAlgorithms;
46
import view.View;
57

68
import javax.swing.*;
@@ -18,7 +20,9 @@ public SolverSelectAction(Model model, View view){
1820
}
1921
@Override
2022
public void actionPerformed(ActionEvent e) {
21-
String algo = ((JComboBox)e.getSource()).getSelectedItem().toString();
22-
// this.model.changeSelectedSolvingAlgo(algo);
23+
String algoString = ((JComboBox)e.getSource()).getSelectedItem().toString();
24+
SolverAlgorithms algoEnum = SolverAlgorithms.valueOf(algoString);
25+
Class<Solver> algoClass = (Class<Solver>) algoEnum.getClazz();
26+
this.model.changeSelectedSolvingAlgo(algoClass);
2327
}
2428
}
Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
package model.generators;
1+
package model;
2+
3+
import controller.TileUpdate;
24

35
import java.util.Objects;
46

@@ -70,6 +72,15 @@ public void removeLeftBorder(){
7072
this.leftBorder = false;
7173
}
7274

75+
public void setTraversed(boolean traversed){
76+
this.isTraversed = traversed;
77+
}
78+
79+
public static TileUpdate makeTileUpdateFromCell(Cell cell, boolean isCurrent, boolean toHighlight){
80+
TileUpdate retVal = new TileUpdate(cell.getxPos(), cell.getyPos(), cell.isTopBorder(), cell.isRightBorder(), cell.isBottomBorder(), cell.isLeftBorder(), false, cell.isInitialized(), cell.isTraversed(), toHighlight, isCurrent);
81+
return retVal;
82+
}
83+
7384
@Override
7485
public boolean equals(Object o) {
7586
if (this == o) return true;

src/model/Grid.java

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
package model;
2+
3+
import utilities.Constants;
4+
5+
import java.util.ArrayList;
6+
import java.util.concurrent.ThreadLocalRandom;
7+
8+
public class Grid {
9+
private Cell[][] cellGrid = new Cell[Constants.mazeLength][Constants.mazeLength];
10+
public Grid(){
11+
for (int i = 0; i < 16; i++){
12+
for (int j = 0; j < 16; j++){
13+
Cell cell = new Cell(i, j);
14+
this.cellGrid[i][j] = cell;
15+
}
16+
}
17+
}
18+
19+
public Cell getCell(int xLoc, int yLoc){
20+
if (((xLoc < 0) || (xLoc > 16)) || ((yLoc < 0) || (yLoc > 16))){
21+
throw new IllegalArgumentException("Gave location(s) outside of maze bounds. ");
22+
};
23+
return this.cellGrid[xLoc][yLoc];
24+
}
25+
26+
public Cell getRandomGridCell() {
27+
28+
int from = 0;
29+
int to = 16;
30+
31+
int x = ThreadLocalRandom.current().nextInt(from, to);
32+
int y = ThreadLocalRandom.current().nextInt(from, to);
33+
34+
return this.getCell(x, y);
35+
}
36+
37+
public void createPathBetweenCells(Cell from, Cell to){
38+
39+
int fromX = from.getxPos();
40+
int fromY = from.getyPos();
41+
int toX = to.getxPos();
42+
int toY = to.getyPos();
43+
44+
if ((fromY == toY) && (fromX > toX)){
45+
from.removeLeftBorder();
46+
to.removeRightBorder();
47+
} else if ((fromY == toY) && (fromX < toX)){
48+
from.removeRightBorder();
49+
to.removeLeftBorder();
50+
} else if ((fromX == toX) && (fromY > toY)){
51+
from.removeTopBorder();
52+
to.removeBottomBorder();
53+
} else if ((fromX == toX) && (fromY < toY)){
54+
from.removeBottomBorder();
55+
to.removeTopBorder();
56+
} else {
57+
throw new IllegalStateException("Cells provided have malformed coordinates.");
58+
}
59+
60+
}
61+
62+
public ArrayList<Cell> getAdjacentCells(Cell centerCell){
63+
int centerX = centerCell.getxPos();
64+
int centerY = centerCell.getyPos();
65+
66+
int newXIndex;
67+
int newYIndex;
68+
69+
ArrayList<Cell> adjacentCells = new ArrayList<>(4);
70+
71+
for (int xBump = -1; xBump < 2; xBump++){
72+
for (int yBump = -1; yBump < 2; yBump++){
73+
74+
if (Math.abs(xBump) != Math.abs(yBump)){
75+
newXIndex = centerX + xBump;
76+
newYIndex = centerY + yBump;
77+
if ((newXIndex >= Constants.minCellIndex) &&
78+
(newXIndex <= Constants.maxCellIndex) &&
79+
(newYIndex >= Constants.minCellIndex) &&
80+
(newYIndex <= Constants.maxCellIndex)
81+
){
82+
Cell adjacent = this.getCell(newXIndex, newYIndex);
83+
adjacentCells.add(adjacent);
84+
}
85+
86+
}
87+
}
88+
}
89+
90+
return adjacentCells;
91+
}
92+
93+
public static boolean isTherePathBetweenCells(Cell from, Cell to){
94+
95+
int fromX = from.getxPos();
96+
int fromY = from.getyPos();
97+
int toX = to.getxPos();
98+
int toY = to.getyPos();
99+
100+
if (((fromY == toY) && (fromX == toX + 1)) && ((!from.isLeftBorder()) && (!to.isRightBorder()))){
101+
return true;
102+
} else if (((fromY == toY) && (fromX + 1 == toX)) && ((!from.isRightBorder()) && (!to.isLeftBorder()))){
103+
return true;
104+
} else if (((fromX == toX) && (fromY == toY + 1)) && ((!from.isTopBorder()) && (!to.isBottomBorder()))){
105+
return true;
106+
} else if (((fromX == toX) && (fromY + 1 == toY)) && ((!from.isBottomBorder()) && (!to.isTopBorder()))){
107+
return true;
108+
}
109+
return false;
110+
}
111+
112+
public void unSolveGrid(){
113+
for (int i = 0; i < Constants.mazeLength; i++){
114+
for (int j = 0; j < Constants.mazeLength; j++){
115+
this.cellGrid[i][j].setTraversed(false);
116+
}
117+
}
118+
119+
}
120+
121+
}

src/model/generators/AldousBroderGenerator.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import controller.TileUpdate;
44
import controller.ViewUpdatePacket;
5+
import model.Cell;
56
import utilities.Constants;
67

78
import java.util.ArrayList;
@@ -33,23 +34,23 @@ public ViewUpdatePacket makeViewUpdatePacket() {
3334
for (int x = Constants.minCellIndex; x <= Constants.maxCellIndex; x++){
3435
for (int y = Constants.minCellIndex; y <= Constants.maxCellIndex; y++){
3536

36-
Cell cell = this.getCell(x, y);
37-
TileUpdate tileUpdate = makeTileUpdateFromCell(cell, false, false);
37+
Cell cell = this.getGrid().getCell(x, y);
38+
TileUpdate tileUpdate = Cell.makeTileUpdateFromCell(cell, false, false);
3839
updatePacket.addTileUpdate(tileUpdate);
3940
}
4041
}
4142

4243
// Add the current cell at the end, will override its earlier addition
4344
if (currentCell != null){
44-
TileUpdate tileUpdate = makeTileUpdateFromCell(this.getCurrentCell(), true, false);
45+
TileUpdate tileUpdate = Cell.makeTileUpdateFromCell(this.getCurrentCell(), true, false);
4546
updatePacket.addTileUpdate(tileUpdate);
4647
}
4748

4849
return updatePacket;
4950
}
5051

5152
private void startStep(){
52-
currentCell = this.getRandomGridCell();
53+
currentCell = this.getGrid().getRandomGridCell();
5354
currentCell.initializeCell();
5455
cellsInitialized++;
5556
}
@@ -64,10 +65,10 @@ public void iterate(){
6465
this.startStep();
6566
}
6667
else {
67-
ArrayList<Cell> neighbors = this.getAdjacentCells(currentCell);
68+
ArrayList<Cell> neighbors = this.getGrid().getAdjacentCells(currentCell);
6869
Cell chosen = popRandomCellFromList(neighbors);
6970
if (!chosen.isInitialized()){
70-
this.clearPathBetweenCells(currentCell, chosen);
71+
this.getGrid().createPathBetweenCells(currentCell, chosen);
7172
chosen.initializeCell();
7273

7374
cellsInitialized++;

0 commit comments

Comments
 (0)