-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: First attempt to generalize the puzzle dimensions and available …
…moves
- Loading branch information
1 parent
23672bd
commit bfab4ba
Showing
8 changed files
with
300 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
src/com/greenmoonsoftware/eightpuzzle/ApplicationState.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package com.greenmoonsoftware.eightpuzzle; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public final class ApplicationState { | ||
public static final ApplicationState instance = new ApplicationState(); | ||
private int dimensionX; | ||
private int dimensionY; | ||
private String solutionState; | ||
private Map<String, Coordinates> solutionMap = new HashMap<String, Coordinates>(); | ||
private ApplicationState() {} | ||
|
||
public final void initialize(int dimX, int dimY, String solution) { | ||
dimensionX = dimX; | ||
dimensionY = dimY; | ||
solutionState = solution; | ||
String[] solutionValues = solutionState.split(" "); | ||
if (solutionValues.length != dimX*dimY) { | ||
throw new IllegalArgumentException("Solution values ["+solutionValues.length+"] do not match dimension size ["+dimX*dimY+"]"); | ||
} | ||
int spotCounter = 0; | ||
for (int x=0; x<dimensionX; x++) { | ||
for (int y=0; y<dimensionY; y++) { | ||
solutionMap.put(solutionValues[spotCounter], new Coordinates(x,y)); | ||
spotCounter++; | ||
} | ||
} | ||
} | ||
|
||
public int getDimensionX() { | ||
return dimensionX; | ||
} | ||
|
||
public int getDimensionY() { | ||
return dimensionY; | ||
} | ||
|
||
public int getXForValue(String val) { | ||
return solutionMap.get(val).x; | ||
} | ||
|
||
public int getYForValue(String val) { | ||
return solutionMap.get(val).y; | ||
} | ||
|
||
public String getSolutionState() { | ||
return solutionState; | ||
} | ||
|
||
private static class Coordinates { | ||
final int x; | ||
final int y; | ||
private Coordinates(int x, int y) { | ||
this.x = x; | ||
this.y = y; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package com.greenmoonsoftware.eightpuzzle; | ||
|
||
public class Spot { | ||
private int currentX; | ||
private int currentY; | ||
private int solutionX; | ||
private int solutionY; | ||
private int cost = -1; | ||
|
||
private String character; | ||
|
||
public Spot(String character, int currentX, int currentY, int solutionX, int solutionY) { | ||
this.character = character; | ||
this.currentX = currentX; | ||
this.currentY = currentY; | ||
this.solutionX = solutionX; | ||
this.solutionY = solutionY; | ||
} | ||
|
||
public Spot newMove(int newX, int newY) { | ||
return new Spot(this.character, newX, newY, solutionX, solutionY); | ||
} | ||
|
||
public int getCurrentX() { | ||
return currentX; | ||
} | ||
|
||
public int getCurrentY() { | ||
return currentY; | ||
} | ||
|
||
public int getSolutionX() { | ||
return solutionX; | ||
} | ||
|
||
public int getSolutionY() { | ||
return solutionY; | ||
} | ||
|
||
public String getCharacter() { | ||
return character; | ||
} | ||
|
||
public int calculateCost() { | ||
if (cost >= 0) { | ||
return cost; | ||
} | ||
if (character.equals("0")) { | ||
cost = 0; | ||
return cost; | ||
} | ||
cost = Math.abs(currentX - solutionX); | ||
cost += Math.abs(currentY - solutionY); | ||
return cost; | ||
} | ||
} |
Oops, something went wrong.