Update
I was able to get my algorithm working by increasing the thread size to a few gigabytes and was able to solve a 1803x1803 maze in a second or two.
---------------
I started teaching myself recursion yesterday in Java. I created an algorithm that takes a photo of a maze and solves it. However, I get a stack overflow answer when doing mazes that are larger than about 200x200 px because I think the stacks of this algorithm get too long. How can I better this algorithm so that I can input images up to possibly 1000x1000?
Additionally, can you tell me what kind of algorithm I am currently using? I believe this is either DFS, but I am unsure.
Please explain why your solution is more efficient and the idea that it uses.
This is the main class for solving
public class BlackWhiteSolver {
static int[][] solutionSet = new int[203][203];
static int width, height;
static String originalImage;
static int correctX, correctY;
public static void convert() {
try {
BufferedImage original = ImageIO.read(new File(originalImage));
int red;
int threshold = 2;
width = original.getWidth();
height = original.getHeight();
for(int i=0; i<original.getWidth(); i++) {
for(int j=0; j<original.getHeight(); j++) {
red = new Color(original.getRGB(i, j)).getRed();
// 1 = white, 0 = black, 9 = tried, 5 = solved
if(red > threshold) { solutionSet[i][j] = 1; }
else { solutionSet[i][j] = 0; }
}
}
} catch (IOException e) {e.printStackTrace();}
}
public BlackWhiteSolver(int solvedX, int solvedY, String pic) {
correctX = solvedX;
correctY = solvedY;
originalImage = pic;
}
public boolean solve (int row, int column) {
boolean completed = false;
if (validPoint(row, column)) {
solutionSet[row][column] = 9;
if (row == correctX && column == correctY) {
completed = true;
} else {
completed = solve (row+1, column);
if (!completed) {
completed = solve (row, column+1);
}
if (!completed) {
completed = solve (row-1, column);
}
if (!completed) {
completed = solve (row, column-1);
}
}
if (completed) {
solutionSet[row][column] = 5;
}
}
return completed;
}
private boolean validPoint (int row, int column) {
boolean isValid = false;
if (row < height-1 && column < width-1 && row >= 1 && column >= 1 ) {
if (solutionSet[row][column] == 1) {
isValid = true;
}
}
return isValid;
}
public static void solvedFile() {
BufferedImage binarized = new BufferedImage(width, height,BufferedImage.TYPE_3BYTE_BGR);
int newPixel = 0;
int rgb = new Color(255, 0, 0).getRGB();
for(int i=0; i<width; i++){
for(int j=0; j<height; j++)
{
if (solutionSet[i][j] == 0) {
newPixel = 0;
newPixel = colorToRGB(1, newPixel, newPixel, newPixel);
} else if (solutionSet[i][j] == 1 || solutionSet[i][j] == 9) {
newPixel = 255;
newPixel = colorToRGB(1, newPixel, newPixel, newPixel);
} else if (solutionSet[i][j] == 5) {
newPixel = 16711680;
}
binarized.setRGB(i, j, newPixel);
}
}
try { ImageIO.write(binarized, "gif",new File("maze-complete") );} catch (IOException e) {e.printStackTrace();}
}
private static int colorToRGB(int alpha, int red, int green, int blue) {
int newPixel = 0;
newPixel += alpha;
newPixel = newPixel << 8;
newPixel += red; newPixel = newPixel << 8;
newPixel += green; newPixel = newPixel << 8;
newPixel += blue;
return newPixel;
}
}
This is the class that runs the maze
public class BlackWhiteInterface
{
public static void main (String[] args) {
BlackWhiteSolver puzzle = new BlackWhiteSolver(60, 202, "maze-4.gif");
System.out.println();
puzzle.convert();
if (puzzle.solve(0,34)) {
System.out.println("completed");
puzzle.solvedFile();
} else {
System.out.println("not possible");
}
}
}
Generates correct maze with start and end point
public class MazeBuilder {
static String start = "left";
static String end = "down";
public static void main(String[] args)
{
try
{
BufferedImage original = ImageIO.read(new File("mazeInput1.gif"));
BufferedImage binarized = new BufferedImage(original.getWidth(), original.getHeight(),BufferedImage.TYPE_BYTE_BINARY);
int red;
int redRightPixel;
int redUpPixel;
int newPixel;
int threshold = 2;
for(int i=0; i<original.getWidth(); i++)
{
for(int j=0; j<original.getHeight(); j++)
{
red = new Color(original.getRGB(i, j)).getRed();
int alpha = new Color(original.getRGB(i, j)).getAlpha();
if(red > threshold) { newPixel = 255; }
else { newPixel = 0; }
if (i == 0 || j == 0 || i == original.getWidth()-1 || j == original.getHeight() - 1){
newPixel = 0;
if (end == "left") {
} else if (end == "right") {
} else if (end == "up") {
} else if (end == "down") {
}
/*if (i == 1 || j == 1 || i == original.getWidth()-2 || j == original.getHeight() - 2 && red > 2) {
System.out.println("Start Point: (" + i + ", " + j + ")");
}
if (i == 0 && j > 0 && j < original.getHeight()-1) {
redRightPixel = new Color(original.getRGB(i+1, j)).getRed();
if (i == 0 && redRightPixel > 2) {
System.out.println("Start Point: (" + i + ", " + j + ")");
newPixel = 255;
}
}*/
/*if (j == original.getHeight()-1 && i > 0 && i < original.getWidth()-1) {
redUpPixel = new Color(original.getRGB(i, j-1)).getRed();
if (redUpPixel > 2) {
System.out.println("End Point: (" + i + ", " + j + ")");
newPixel = 255;
}
}*/
}
if (start == "left") {
if (i == 1 && j != 0 && j != original.getHeight()-1 && red > 2) {
System.out.println("Start Point: (" + i + ", " + j + ")");
}
} else if (start == "right") {
if (i == original.getHeight()-2 && j != 0 && j != original.getHeight()-1 && red > threshold) {
System.out.println("Start Point: (" + i + ", " + j + ")");
}
} else if (start == "up") {
if (j == 1 && i != 0 && i != original.getWidth()-1 && red > threshold) {
System.out.println("Start Point: (" + i + ", " + j + ")");
}
} else if (start == "down") {
if (j == original.getHeight()-2 && i != 0 && i != original.getWidth()-1 && red > threshold) {
System.out.println("Start Point: (" + i + ", " + j + ")");
}
}
if (end == "left") {
if (i == 1 && j != 0 && j != original.getHeight()-1 && red > 2) {
System.out.println("End Point: (" + i + ", " + j + ")");
}
} else if (end == "right") {
if (i == original.getHeight()-2 && j != 0 && j != original.getHeight()-1 && red > threshold) {
System.out.println("End Point: (" + i + ", " + j + ")");
}
} else if (end == "up") {
if (j == 1 && i != 0 && i != original.getWidth()-1 && red > threshold) {
System.out.println("End Point: (" + i + ", " + j + ")");
}
} else if (end == "down") {
if (j == original.getHeight()-2 && i != 0 && i != original.getWidth()-1 && red > threshold) {
System.out.println("End Point: (" + i + ", " + j + ")");
}
}
newPixel = colorToRGB(alpha, newPixel, newPixel, newPixel);
binarized.setRGB(i, j, newPixel);
}
}
ImageIO.write(binarized, "gif",new File("maze-4") );
}
catch (IOException e)
{
e.printStackTrace();
}
}
private static int colorToRGB(int alpha, int red, int green, int blue) {
int newPixel = 0;
newPixel += alpha;
newPixel = newPixel << 8;
newPixel += red; newPixel = newPixel << 8;
newPixel += green; newPixel = newPixel << 8;
newPixel += blue;
return newPixel;
}
}
Example output of a 203 x 203 maze
One simple way that's only slightly more efficient is to not store the path you've followed so far in the stack with recursion. Instead store the path you've followed so far in either a java.util.BitSet (where you store each path pixel in element y*width + x of the BitSet) or you can simply use the red area of the picture that you've colored in to store the path.
This avoids stack overflows.
The basic algorithm is to start at the start point and go in one of the four cardinal directions unless you've already visited that direction (either trying it and finding it a dead end or having come from that direction to get here). When you go in a direction, you do the same thing there. It's a simple nonrecursive loop.
When you hit a dead end, you figure out how you got there originally by checking all four directions from where you are to see where the path came from. You remove the red from the spot where you're standing and go back in the direction you came from. If there is no red path in any direction, you're at the starting point again and you've tried everything, so there's no solution to the maze.
When you backtrack, you try the next direction you haven't tried yet at the older square on the path until all directions are dead ends.
If you ever reach the end point, you're done.
Here's some pseudocode that can't generally handle cycles (paths that go in a "circle"), that's grossly inefficient (for example, it should use a BitSet instead of a boolean[][]), and that probably has some bugs, but it gives the general idea:
public class MazeSolver {
private static enum Direction { UP, RIGHT, DOWN, LEFT }
// Return array's element is true if that's part of the path
public static boolean[][] solve(final boolean[][] mazeWallHere,
int x, int y,
final int endX, final int endY) {
final int width = mazeWallHere.length;
final int height = mazeWallHere[0].length;
final boolean[][] path = new boolean[width][height];
Direction nextDirection = Direction.UP;
boolean backtrack = false;
while (true) {
// If this spot is a dead end in all new directions, head back
if (backtrack) {
backtrack = false;
// Unmark where we are
path[x][y] = false;
// Find where we came from and what direction we took to get here
// Then switch to the next direction
// If all directions have been tried, backtrack again
// If we can't backtrack, return null because there's no solution
// If we went up to get here, go back down and try going right.
if (y != 0 && path[x][y - 1]) {
y--;
nextDirection = Direction.RIGHT;
continue;
}
// If we went right to get here, go back left and try going down.
else if (x != 0 && path[x - 1][y]) {
x--;
nextDirection = Direction.DOWN;
continue;
}
// If we went down to get here, go back up and try going left.
else if (y < height && path[x][y + 1]) {
y++;
nextDirection = Direction.LEFT;
continue;
}
// If we went left to get here, go back right and backtrack again.
else if (x < width && path[x + 1][y]) {
x++;
backtrack = true;
continue;
}
// If we didn't come from anywhere, we're at the starting point
// All possible paths are dead ends
else return null;
}
// Mark where we are
path[x][y] = true;
// If we've solved it, return the solution
if (x == endX && y == endY) return path;
// Move unless we:
// * hit the edge of the maze
// * it's the direction we originally got here from
// * hit a wall
// If we can't go a certain direction, try the next direction
// If we're out of directions to try, backtrack
switch (nextDirection) {
case UP: if (y == height
|| path[x][y + 1]
|| mazeWallHere[x][y + 1]) {
nextDirection = Direction.RIGHT;
continue;
}
else y++;
break;
case RIGHT: if (x == width
|| path[x + 1][y]
|| mazeWallHere[x + 1][y]) {
nextDirection = Direction.DOWN;
continue;
}
else x++;
break;
case DOWN: if (y == 0
|| path[x][y - 1]
|| mazeWallHere[x][y - 1]) {
nextDirection = Direction.LEFT;
continue;
}
else y--;
break;
case LEFT: if (x == 0
|| path[x - 1][y]
|| mazeWallHere[x - 1][y]) {
backtrack = true;
continue;
}
else x--;
break;
}
}
}
}
If you want to handle cycles properly, make path an int[][] and store the move number instead of true so that you know which path is older.
Related
How do I prevent the same tic tac toe coordinate from being inputted by the user?
The user input is taken at the main method in the Game Class.
The tic tac toe cells with [x, y] coordinates ranging from (0-2) can be either:
0(_), 1 (X) or 2 (O)
Grid Class with alpha beta search tree pruning algorithm
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
class Grid {
List<Cell> availableCells;
int[][] board = new int[3][3];
Scanner scan = new Scanner(System.in);
// Set limit to search tree depth
int treeDepth = 9;
List<CellsAndScores> rootsChildrenScore = new ArrayList<>();
public int score() {
int score = 0;
// Check all columns
for (int j = 0; j < 3; ++j) {
int X = 0;
int O = 0;
for (int i = 0; i < 3; ++i) {
if (board[i][j] == 0) {
} else if (board[i][j] == 1) {
X++;
} else {
O++;
}
}
score += changeInScore(X, O);
}
// Check all rows
for (int i = 0; i < 3; ++i) {
int X = 0;
int O = 0;
for (int j = 0; j < 3; ++j) {
if (board[i][j] == 0) {
} else if (board[i][j] == 1) {
X++;
} else {
O++;
}
}
score += changeInScore(X, O);
}
int X = 0;
int O = 0;
// Check diagonal (first)
for (int i = 0, j = 0; i < 3; ++i, ++j) {
if (board[i][j] == 1) {
X++;
} else if (board[i][j] == 2) {
O++;
} else {
}
}
score += changeInScore(X, O);
X = 0;
O = 0;
// Check Diagonal (Second)
for (int i = 2, j = 0; i > -1; --i, ++j) {
if (board[i][j] == 1) {
X++;
} else if (board[i][j] == 2) {
O++;
} else {
}
}
score += changeInScore(X, O);
return score;
}
private int changeInScore(int X, int O) {
int change;
if (X == 3) {
change = 100;
} else if (X == 2 && O == 0) {
change = 10;
} else if (X == 1 && O == 0) {
change = 1;
} else if (O == 3) {
change = -100;
} else if (O == 2 && X == 0) {
change = -10;
} else if (O == 1 && X == 0) {
change = -1;
} else {
change = 0;
}
return change;
}
public int alphaBetaMinimax(int alpha, int beta, int depth, int turn) {
if (beta <= alpha) {
System.out.println("Pruning at tree depth = " + depth + " alpha: " + alpha + " beta: " + beta);
if (turn == 1)
return Integer.MAX_VALUE;
else
return Integer.MIN_VALUE;
}
if (depth == treeDepth || gameOver()) {
return score();
}
List<Cell> cellsAvailable = getAvailableStates();
if (cellsAvailable.isEmpty()) {
return 0;
}
if (depth == 0) {
rootsChildrenScore.clear();
}
int maxValue = Integer.MIN_VALUE, minValue = Integer.MAX_VALUE;
for (int i = 0; i < cellsAvailable.size(); ++i) {
Cell cell = cellsAvailable.get(i);
int currentScore = 0;
if (turn == 1) {
placeAMove(cell, 1);
currentScore = alphaBetaMinimax(alpha, beta, depth + 1, 2);
maxValue = Math.max(maxValue, currentScore);
// Set alpha
alpha = Math.max(currentScore, alpha);
if (depth == 0) {
rootsChildrenScore.add(new CellsAndScores(currentScore, cell));
}
} else if (turn == 2) {
placeAMove(cell, 2);
currentScore = alphaBetaMinimax(alpha, beta, depth + 1, 1);
minValue = Math.min(minValue, currentScore);
// Set beta
beta = Math.min(currentScore, beta);
}
// reset board
board[cell.x][cell.y] = 0;
// Do not evaluate the rest of the branches after search tree is pruned
if (currentScore == Integer.MAX_VALUE || currentScore == Integer.MIN_VALUE)
break;
}
return turn == 1 ? maxValue : minValue;
}
public boolean gameOver() {
// Game is over is someone has won, or board is full (draw)
return (hasXWon() || hasOWon() || getAvailableStates().isEmpty());
}
public boolean hasXWon() {
if ((board[0][0] == board[1][1] && board[0][0] == board[2][2] && board[0][0] == 1)
|| (board[0][2] == board[1][1] && board[0][2] == board[2][0] && board[0][2] == 1)) {
// System.out.println("X Diagonal Win");
return true;
}
for (int i = 0; i < 3; ++i) {
if (((board[i][0] == board[i][1] && board[i][0] == board[i][2] && board[i][0] == 1)
|| (board[0][i] == board[1][i] && board[0][i] == board[2][i] && board[0][i] == 1))) {
// System.out.println("X Row or Column win");
return true;
}
}
return false;
}
public boolean hasOWon() {
if ((board[0][0] == board[1][1] && board[0][0] == board[2][2] && board[0][0] == 2)
|| (board[0][2] == board[1][1] && board[0][2] == board[2][0] && board[0][2] == 2)) {
// System.out.println("O Diagonal Win");
return true;
}
for (int i = 0; i < 3; ++i) {
if ((board[i][0] == board[i][1] && board[i][0] == board[i][2] && board[i][0] == 2)
|| (board[0][i] == board[1][i] && board[0][i] == board[2][i] && board[0][i] == 2)) {
// System.out.println("O Row or Column win");
return true;
}
}
return false;
}
public List<Cell> getAvailableStates() {
availableCells = new ArrayList<>();
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
if (board[i][j] == 0) {
availableCells.add(new Cell(i, j));
}
}
}
return availableCells;
}
public void placeAMove(Cell Cell, int player) {
board[Cell.x][Cell.y] = player; // player = 1 for X, 2 for O
}
public Cell returnBestMove() {
int MAX = -100000;
int best = -1;
for (int i = 0; i < rootsChildrenScore.size(); ++i) {
if (MAX < rootsChildrenScore.get(i).score) {
MAX = rootsChildrenScore.get(i).score;
best = i;
}
}
return rootsChildrenScore.get(best).cell;
}
public void displayBoard() {
System.out.println();
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
if (board[i][j] == 0)
System.out.print("_" + " ");
if (board[i][j] == 1)
System.out.print("X" + " ");
if (board[i][j] == 2)
System.out.print("O" + " ");
}
System.out.println("");
}
System.out.println();
}
public void resetGrid() {
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
board[i][j] = 0;
}
}
}
}
Cell class
class Cell {
int x, y;
public Cell(int x, int y) {
this.x = x;
this.y = y;
}
public String toString() {
return "[" + x + ", " + y + "]";
}
}
class CellsAndScores {
int score;
Cell cell;
CellsAndScores(int score, Cell cell) {
this.score = score;
this.cell = cell;
}
}
Game Class with main method - takes user input
import java.util.Random;
public class Game {
public static void main(String[] args) {
Grid grid = new Grid();
Random random = new Random();
grid.displayBoard();
System.out.print("Who moves first? [1]Computer(X) [2]User(O): ");
int turn = grid.scan.nextInt();
if (turn == 1) {
Cell p = new Cell(random.nextInt(3), random.nextInt(3));
grid.placeAMove(p, 1);
grid.displayBoard();
}
while (!grid.gameOver()) {
int x = 0, y = 0;
System.out.print("Please enter an x coordinate [0-2]: ");
x = grid.scan.nextInt();
System.out.print("Please enter an y coordinate [0-2]: ");
y = grid.scan.nextInt();
Cell userMove = new Cell(y, x);
grid.placeAMove(userMove, 2); // 2 for O and O is the user
grid.displayBoard();
if (grid.gameOver())
break;
grid.alphaBetaMinimax(Integer.MIN_VALUE, Integer.MAX_VALUE, 0, 1);
for (CellsAndScores pas : grid.rootsChildrenScore)
System.out.println("Cell: " + pas.cell + " Score: " + pas.score);
grid.placeAMove(grid.returnBestMove(), 1);
grid.displayBoard();
}
if (grid.hasXWon()) {
System.out.println("Unfortunately, you lost!");
grid.resetGrid();
} else if (grid.hasOWon()) {
System.out.println("You win!");
grid.resetGrid();
} else {
System.out.println("It's a draw!");
grid.resetGrid();
}
}
}
My answer would be to add a boolean check method into your Grid.java class and then in your main method - call this boolean check method before the placeAMove() method.
For example, in your Grid.java class, adding the following method:
/*
* Return true if space is ok to use.
*/
public boolean isMoveOK(Cell cell) {
return board[cell.x][cell.y] == 0;
}
This way, using your pre-existing 0/1/2 values that keep track of empty/X/O space values, you may provide a check to see if the space value is zero or not.
This would be one way to use it in your main method, to answer your question of, 'How do I prevent the same tic tac toe coordinate from being inputted by the user?'
Cell userMove = new Cell(y, x);
if (grid.isMoveOK(userMove)) {
grid.placeAMove(userMove, 2); // 2 for O and O is the user
} else {
System.out.println("Please try a different space/cell");
continue;
}
grid.displayBoard();
if (grid.gameOver())
break;
In this way, I am skipping the remaining loop code in your main method loop, until there's a valid open space. (When there is, then the program should proceed to check for winning values or whether to proceed playing)
Hope this answers your question! :)
Cheers
I'm struggling with a minimax exercise, I'm just trying to make a connect four ai with it. Mine works when only exploring one node deep but I can't figure out why it messes up once it goes deeper.
private int minimax(Gameboard gameBoard, int alpha, int depth, char color) {
Gameboard gb = new Gameboard(gameBoard);
int value = 0;
int bestChoice = 0;
int bestValue = alpha;
// determine which use the value is for
if (gb.computerColor == color) {
value = 1;
} else {
value = -1;
}
if (gb.CheckForWinner(gb.lastSpacePlayed)) {
if(gb.winner == gb.computerColor){
bestValue = (1000000000 - depth);
}else{
bestValue = (-1000000000 - depth);
}
}
// get the bestValue at our maximum recrusion depth
else if (depth == maxDepth) {
int moveWeight = (threatLevel(gb, color));
if (moveWeight != 0) {
bestValue = value * (moveWeight - depth);
} else {
bestValue = moveWeight;
}
} else {
// Generate moves for each col and find the best score from each of
// the generated moves.
for (int c = 0; c < 7; c++) {
Gameboard game = new Gameboard(gb);
int selectedPlace = game.PlacePiece(c, color);
// Recursive call the generated game grid and compare the
// current value to move value
// If move is higher, make it the new current value.
if (selectedPlace != -1) {
char tempColor;
// change the user for the child node after a piece is played
if (color == 'Y') {
tempColor = 'R';
} else {
tempColor = 'Y';
}
// call the function so we can explore to maximum depth
if (depth < maxDepth) {
int v = minimax(new Gameboard(game), -1000000, depth + 1, tempColor);
if (v > bestValue) {
bestChoice = c;
bestValue = v;
}
System.out.println(v);
}
}
}
}
if (depth == 0) {
if (threatLevel(gb, gb.lastSpacePlayed.getColor()) == 0) {
return gb.spaces.get(0).get(3).getColor() == gb.playerColor ? 2
: 3;
} else {
return bestChoice;
}
} else {
return bestValue;
}
}
I'm starting it off as so return minimax(gameBoard, -1000000, 0, gameBoard.computerColor);
My understanding is just looping over all children and returning a value a maximum value if the nodes are the same as the parent and a minimum value if the nodes aren't. Any direction would be appreciated.
private int minimax(Gameboard gameBoard, int depth, char color) {
Gameboard gb = new Gameboard(gameBoard);
int bestChoice = 0;
int bestValue = 0;
//If we've won, return highest possible value. If we've lost, return lowest.
if (gb.CheckForWinner(gb.lastSpacePlayed)) {
if(gb.winner == color){
return Integer.MAX_VALUE
}else{
return Integer.MIN_VALUE
}
}
//if we hit maximum depth, resort to our heuristic method.
else if (depth == maxDepth) {
return threatLevel(gb, color);
} else {
// Generate moves for each col and find the best score from each of
// the generated moves. Keep track of the worst one.
int worstBestResponse = Integer.MAX_INT
boolean tie = true;
for (int c = 0; c < 7; c++) {
Gameboard game = new Gameboard(gb);
int selectedPlace = game.PlacePiece(c, color);
// Recursive call the generated game grid and compare the
// current value to move value
// If move is higher, make it the new current value.
if (selectedPlace != -1) {
tie = false;
char tempColor;
// change the user for the child node after a piece is played
if (color == 'Y') {
tempColor = 'R';
} else {
tempColor = 'Y';
}
// call the function so we can explore to maximum depth
if (depth < maxDepth) {
int v = minimax(new Gameboard(game), depth + 1, tempColor);
if (v < worstBestResponse) {
worstBestResponse = v;
}
}
}
}
if(tie) {
//if game is a tie, we return 0, to show no favour.
return 0;
} else {
//After determining the value of the opponents best response, we return the negative value of it. That is, what's bad for them is good for us and visa versa.
return -worstBestResponse;
}
}
}
I believe something like this is more what you're looking for. This is assuming that threatLevel is a heuristic method for determining approximately who is winning in a given game.
I've taken out any knowledge the method may have about who it's rooting for. It should only be rooting for whoever "color" is. I've also cleaned up your arbitrarily large integers to show winning and losing. MAX_VALUE and MIN_VALUE is much more elegant.
Anyway, try this out, and call it with depth of 0. Let me know if you have questions
If your approach isn't working you could try to mimic the basic structure of the pseudocode on wiki or here. In java I have:
/**
* initialize MiniMax(0, player1) where player1 is computer
* #param deg depth
* #param player either MAX or MIN
* #return int {score,column}
*/
int[] MiniMax(int deg, char player) {
// list of possible columns to place a checker
List<int[]> moves = nextMoves();
int v;
char prev;
if (player==player1) prev = player2;
else prev = player1;
// IF depth = 0 OR there is a connect 4 OR the board is full (draw) THEN
// return your static evaluation (heuristic)
// if the current position has a connect 4, return -inf or inf
// depending on if player is MIN or MAX respectively
if (checkPos(prev) || (deg == this.deg) || moves.isEmpty())
return new int[] {eval(),bestMove};
//MAX
else if (player==player1) {
v = -inf;
for (int[] move : moves) { // move = {row,column}
board[move[0]][move[1]] = player1; // make move
int score = MiniMax(deg+1,player2)[0];
board[move[0]][move[1]] = ' '; // undo move
if (score>v) {
v = score;
bestMove = move[1];
}
}
return new int[] {v,bestMove};
}
//MIN
else {
v = inf;
for (int[] move : moves) { // move = {row,column}
board[move[0]][move[1]] = player2; // make move
int score = MiniMax(deg+1,player1)[0];
board[move[0]][move[1]] = ' '; // undo move
if (v>score) {
v = score;
bestMove = move[1];
}
}
return new int[] {v,bestMove};
}
}
It could be useful to use a character array or something, rather than a class, to represent the board. The most efficient way though is to represent the board as a long as you can check if there is a connect four using 4 bit shifts. Here's some sloppy java that shows the above stuff working:
import java.util.ArrayList;
import java.util.List;
public class Connect4 {
static final int WIDTH = 7;
static final int HEIGHT = 6;
private static final int inf = 9999999;
private static final char player1 = 'X';
private static final char player2 = 'O';
char[][] board = new char[WIDTH][HEIGHT];
private int deg;
int bestMove;
static char[][] copy(char[][] aArray) {
char[][] copy = new char[aArray.length][aArray[0].length];
for (int idy = 0; idy < aArray.length; ++idy) {
for (int idx = 0; idx < aArray[0].length; ++idx) {
copy[idy][idx] = aArray[idy][idx];
}
}
return copy;
}
void prints() {
System.out.println();
for (int i = 0; i < 6; i++) {
System.out.println("=============================");
for (int j = 0; j < 7; j++) {
if (j == (WIDTH - 1)) {
if (board[i][j]==' ') {
System.out.println("| |");
}
else {
if(board[i][j]==player1 ) System.out.println("| " +"\u001B[32m"+board[i][j]+"\u001B[0m" + " |");
else System.out.println("| " +"\u001B[34m"+board[i][j]+"\u001B[0m" + " |");
}
}
else {
if (board[i][j]==' ') {
System.out.print("| ");
}
else {
if(board[i][j]==player1 ) System.out.print("| " +"\u001B[32m"+board[i][j]+"\u001B[0m" + " ");
else System.out.print("| " +"\u001B[34m"+board[i][j]+"\u001B[0m" + " ");
}
}
}
}
System.out.println("=============================");
}
//STATIC EVALUATION
int eval3(char player) {
if (checkPos(player)) {
return inf;
}
int count;
int open;
int evaluation = 0;
//evaluation = number of open 3 in rows
//go through all possible 4in rows and check
//horz
for (int i = 0; i < HEIGHT; i++) {
for (int j = 0; j < 4; j++) {
count = 0;
open = 0;
if (board[i][j]==player) count++;
else if(board[i][j]==' ') open++;
if (board[i][j + 1]==player) count++;
else if(board[i][j + 1]==' ') open++;
if (board[i][j + 2]==player) count++;
else if(board[i][j + 2]==' ') open++;
if (board[i][j + 3]==player) count++;
else if(board[i][j + 3]==' ') open++;
if ((count == 3) && (open == 1)) evaluation++;
}
}
//vert
for (int j = 0; j < WIDTH; j++) {
for (int i = 0; i < 3; i++) {
count = 0;
open = 0;
if (board[i][j]==player) count++;
else if (board[i][j]==' ') open++;
if (board[i + 1][j]==player) count++;
else if (board[i+1][j]==' ') open++;
if (board[i + 2][j]==player) count++;
else if (board[i + 2][j]==' ') open++;
if (board[i + 3][j]==player) count++;
else if (board[i + 3][j]==' ') open++;
if ((count == 3) && (open == 1)) evaluation++;
}
}
// pos slope diag
for (int j = 0; j < 4; j++) {
for (int i = 3; i < HEIGHT; i++) {
count = 0;
open = 0;
if (board[i][j]==player) count++;
else if (board[i][j]==' ') open++;
if (board[i - 1][j + 1]==player) count++;
else if (board[i - 1][j + 1]==' ') open++;
if (board[i - 2][j + 2]==player) count++;
else if (board[i - 2][j + 2]==' ') open++;
if (board[i - 3][j + 3]==player) count++;
else if (board[i - 3][j + 3]==' ') open++;
if ((count == 3) && (open == 1)) evaluation++;
}
}
// neg slope diag
for (int j = 0; j < 4; j++) {
for (int i = 0; i < (3); i++) {
count = 0;
open = 0;
if (board[i][j]==player) count++;
else if (board[i][j]==' ') open++;
if (board[i + 1][j + 1]==player) count++;
else if (board[i + 1][j + 1]==' ') open++;
if (board[i + 2][j + 2]==player) count++;
else if (board[i + 2][j + 2]==' ') open++;
if (board[i + 3][j + 3]==player) count++;
else if (board[i + 3][j + 3]==' ') open++;
if ((count == 3) && (open == 1)) evaluation++;
}
}
return evaluation;
}
int eval() {return eval3(player1) - eval3(player2);}
boolean checkPos(char cur) {
//horz
for (int i = 0; i < HEIGHT; i++) {
for (int j = 0; j < 4; j++) {
if ( board[i][j]==cur &&
board[i][j + 1]==cur &&
board[i][j + 2]==cur &&
board[i][j + 3]==cur) {
return true;
}
}
}
//vert
for (int j = 0; j < WIDTH; j++) {
for (int i = 0; i < 3; i++) {
if ( board[i][j]==cur &&
board[i + 1][j]==cur &&
board[i + 2][j]==cur &&
board[i + 3][j]==cur) {
return true;
}
}
}
// pos slope diag
for (int j = 0; j < 4; j++) {
for (int i = 3; i < HEIGHT; i++) {
if ( board[i][j]==cur &&
board[i - 1][j + 1]==cur &&
board[i - 2][j + 2]==cur &&
board[i - 3][j + 3]==cur) {
return true;
}
}
}
// neg slope diag
for (int j = 0; j < 4; j++) {
for (int i = 0; i < 3; i++) {
if ( board[i][j]==cur &&
board[i + 1][j + 1]==cur &&
board[i + 2][j + 2]==cur &&
board[i + 3][j + 3]==cur) {
return true;
}
}
}
return false;
}
List<int[]> nextMoves() {
List<int[]> result = new ArrayList<>();
for (int j = 0; j < WIDTH; j++) {
//if column j isnt full then add
if (board[0][j]==' ') result.add(new int[] {findY(j),j});
}
return result;
}
int findY(int col) {
int i = board.length - 1;
while (i > -1) {
if (board[i][col]==' ') break;
i--;
}
return i;
}
/**
* #param deg depth
* #param player either MAX or MIN
* #return int {score,column}
*/
int[] MiniMax(int deg, char player) {
// list of possible columns to place a checker
List<int[]> moves = nextMoves();
int v;
char prev;
if (player==player1) prev = player2;
else prev = player1;
// IF depth = 0 OR there is a connect 4 OR the board is full (draw) THEN
// return your static evaluation (heuristic)
// if the current position has a connect 4, return -inf or inf
// depending on if player is MIN or MAX respectively
if (checkPos(prev) || (deg == this.deg) || moves.isEmpty())
return new int[] {eval(),bestMove};
//MAX
else if (player==player1) {
v = -inf;
for (int[] move : moves) { // move = {row,column}
board[move[0]][move[1]] = player1; // make move
int score = MiniMax(deg+1,player2)[0];
board[move[0]][move[1]] = ' '; // undo move
if (score>v) {
v = score;
bestMove = move[1];
}
}
return new int[] {v,bestMove};
}
//MIN
else {
v = inf;
for (int[] move : moves) { // move = {row,column}
board[move[0]][move[1]] = player2; // make move
int score = MiniMax(deg+1,player1)[0];
board[move[0]][move[1]] = ' '; // undo move
if (v>score) {
v = score;
bestMove = move[1];
}
}
return new int[] {v,bestMove};
}
}
public static void main(String[] args) {
Connect4 c = new Connect4();
c.deg = 5;
char[][] b = {
{' ',' ',' ',' ',' ',' ',' '},
{' ',' ',' ',' ',' ',' ',' '},
{' ',' ',' ',' ',' ',' ',' '},
{' ',' ',' ',' ',' ',' ',' '},
{' ','X','X','X',' ',' ',' '},
{' ','O','X','X','X',' ',' '}
};
c.board = copy(b);
c.prints();
System.out.println("best value = " + c.MiniMax(0, player1)[1]);
}
}
I wrote a pathfinding algorithm for android. It seems to be running very slowly and I can not figure out why. I have asked a similar question before, but I didn't get the answers I was looking for (And I have changed code since then). Here is my path finding class :
public class Pathfinding {
private static Node[][] grid;
private static NodeComparator nodeComparator;
static{
nodeComparator = new NodeComparator();
}
public static class NodeComparator implements Comparator<Node> {
#Override
public int compare(Node node1, Node node2) {
if(node1.F > node2.F){
return 1;
}
else if(node1.F < node2.F){
return -1;
}
else{
return 0;
}
}
}
public static Array<Node> findPath(Node start, Node finish, Node[][] _grid) {
Array<Node> path = new Array<Node>();
Array<Node> openList = new Array<Node>();
Array<Node> closedList = new Array<Node>();
grid = _grid;
if(start == null){
return path;
}
if(finish == null){
return path;
}
Node currentNode = start;
currentNode.G = 0;
currentNode.H = getHeuristic(currentNode, finish);
currentNode.parent = null;
openList.add(currentNode);
System.out.println("PATHFINDING STARTED ||| startPos : " + start.position + " finishPos : " + finish.position);
while (openList.size > 0) {
//Sorts open nodes lowest F value to heighest
openList.sort(nodeComparator);
currentNode = openList.first();
//If path is found, return
if (currentNode == finish) {
System.out.println("PATH FOUND...RETURNING -gat5");
return constructPath(currentNode);
}
openList.removeValue(currentNode, true);
closedList.add(currentNode);
int counter = 0;
for (Node neighbor : getNeighbors(currentNode)) {
if (!closedList.contains(neighbor, true)) { //If neighbor not in closed list
if(neighbor != null) { //If neighbor not wall
if(counter == 4){
counter++;
}
int movementCost = checkMovementCost(counter);
if (openList.contains(neighbor, true)) {
if (currentNode.G + movementCost < neighbor.G) {
neighbor.parent = currentNode;
}
} else {
openList.add(neighbor);
neighbor.parent = currentNode;
neighbor.H = getHeuristic(currentNode, finish);
neighbor.G = neighbor.parent.G + movementCost;
}
counter++;
}
}
}
System.out.println(counter);
}
System.out.println("NO FINAL");
System.out.println("NO PATH FOUND RETURNING...");
path.add(start);
return path;
}
private static int checkMovementCost(int neighbor) {
int movementCost = 0;
switch (neighbor) {
//Diagonal
case 0:
case 2:
case 6:
case 8:
movementCost = 16;
break;
//Not Diagonal
case 1:
case 3:
case 5:
case 7:
movementCost = 10;
break;
}
return movementCost;
}
public static Array<Node> constructPath(Node finish) {
Array<Node> pathNodes = new Array<Node>();
Node currentNode = finish;
pathNodes.add(currentNode);
while (currentNode.parent != null) {
currentNode = currentNode.parent;
pathNodes.add(currentNode);
}
return pathNodes;
}
private static float getHeuristic(Node start, Node finish){
int H = 0;
H += Math.abs(start.position.x - finish.position.x);
H += Math.abs(start.position.y - finish.position.y);
return H;
}
private static Array<Node> getNeighbors(Node node){
Array<Node> neighbors = new Array<Node>();
int x = (int)node.position.x;
int y = (int)node.position.y;
if(x - 1 > 0 && x - 1 < grid.length && y + 1 < grid.length && y + 1 > 0){
neighbors.add(grid[x - 1][y + 1]);
}
else{
neighbors.add(null);
}
if(x > 0 && x < grid.length && y + 1 < grid.length && y + 1 > 0){
neighbors.add(grid[x][y + 1]);
}
else{
neighbors.add(null);
}
if(x + 1 > 0 && x + 1 < grid.length && y + 1 < grid.length && y + 1 > 0){
neighbors.add(grid[x + 1][y + 1]);
}
else{
neighbors.add(null);
}
if(x - 1 > 0 && x - 1 < grid.length && y < grid.length && y > 0){
neighbors.add(grid[x - 1][y]);
}
else{
neighbors.add(null);
}
if(x > 0 && x < grid.length && y < grid.length && y > 0){
neighbors.add(grid[x][y]);
}
else{
neighbors.add(null);
}
if(x + 1 > 0 && x + 1 < grid.length && y < grid.length && y > 0){
neighbors.add(grid[x + 1][y]);
}
else{
neighbors.add(null);
}
if(x - 1 > 0 && x - 1 < grid.length && y - 1 < grid.length && y - 1> 0){
neighbors.add(grid[x - 1][y - 1]);
}
else{
neighbors.add(null);
}
if(x > 0 && x < grid.length && y - 1 < grid.length && y - 1 > 0){
neighbors.add(grid[x][y - 1]);
}
else{
neighbors.add(null);
}
if(x + 1 > 0 && x + 1 < grid.length && y - 1 < grid.length && y - 1 > 0){
neighbors.add(grid[x + 1][y - 1]);
}
else{
neighbors.add(null);
}
return neighbors;
}
}
Thank you so much for your help!
**Some more information : ** When I run this algorithm only once, it works fine. But once I run it 3+ times, it starts lose framerate fast. My grid I am using is 200x200.
If your evaluation of the paths is as simple as you are pointing out, probably the slowness of the algorithm has something do do with the ordering that you do in each iteration of the algorithm.
//Sorts open nodes lowest F value to heighest
openList.sort(nodeComparator);
Just using PriorityQueue to order the nodes to be expanded by the algorithm results in a much more efficient implementation. If you want, take a look to the implementation details of the A* algorithm implemented in the library hipster4j. This works in Android, too.
Hope my answer helps.
public static void main(String[] args) {
Picture myPic = new Picture(600, 600);
Graphics canvas = myPic.getOffScreenGraphics();
canvas.setColor(Color.WHITE);
canvas.fillRect(0, 0, 600, 600);
canvas.setColor(Color.BLACK);
int x1;
int y1;
int x2;
int y2;
int heading;
x1 = 300;
y1 = 300;
x2 = 300;
y2 = 300;
heading = 0;
String keyboard = JOptionPane.showInputDialog(null,
"Enter your input");
if (keyboard != null) {
if (keyboard.isEmpty() || keyboard.contains(" ")) {
JOptionPane.showMessageDialog(
null,
"Please enter an input",
"Error",
JOptionPane.ERROR_MESSAGE);
main(args);
}
int counth = 0;
int countg = 0;
int countPlus = 0;
int countMinus = 0;
int countK = 0;
int countR = 0;
int countG = 0;
int countB = 0;
int countC = 0;
int countO = 0;
for (int i = 0; i < keyboard.length(); i++) {
if (keyboard.charAt(i) == '+') {
if (heading == 0) {
heading = 1;
} else if (heading == 1) {
heading = 2;
} else if (heading == 2) {
heading = 3;
} else if (heading == 3) {
heading = 0;
}
}
}
countPlus++;
for (int i = 0; i < keyboard.length(); i++) {
if (keyboard.charAt(i) == '-') {
if (heading == 0) {
heading = 3;
} else if (heading == 1) {
heading = 0;
} else if (heading == 2) {
heading = 1;
} else if (heading == 3) {
heading = 2;
}
}
}
countMinus++;
for (int i = 0; i < keyboard.length(); i++) {
if (keyboard.charAt(i) == 'h' || keyboard.charAt(i) == 'f') {
if (heading == 0) {
counth++;
int k = 10 * (counth);
canvas.drawLine(x1, y1, x2, y2 + k);
} else if (heading == 1) {
counth++;
int k = 10 * (counth);
canvas.drawLine(x1, y1, x2 + k, y2);
} else if (heading == 2) {
counth++;
int k = 10 * (counth);
canvas.drawLine(x1, y1, x2, y2 - k);
} else if (heading == 3) {
counth++;
int k = 10 * (counth);
canvas.drawLine(x1, y1, x2 - k, y2);
} else if (heading > 3 || heading < 0) {
JOptionPane.showMessageDialog(
null,
"Your heading is greater than 3 or less than 0",
"Error",
JOptionPane.ERROR_MESSAGE);
main(args);
}
}
}
for (int i = 0; i < keyboard.length(); i++) {
if (keyboard.charAt(i) == 'K') {
canvas.setColor(Color.BLACK);
}
}
countK++;
for (int i = 0; i < keyboard.length(); i++) {
if (keyboard.charAt(i) == 'R') {
canvas.setColor(Color.RED);
}
}
countR++;
for (int i = 0; i < keyboard.length(); i++) {
if (keyboard.charAt(i) == 'G') {
canvas.setColor(Color.GREEN);
}
}
countG++;
for (int i = 0; i < keyboard.length(); i++) {
if (keyboard.charAt(i) == 'B') {
canvas.setColor(Color.BLUE);
}
}
countB++;
for (int i = 0; i < keyboard.length(); i++) {
if (keyboard.charAt(i) == 'C') {
canvas.setColor(Color.CYAN);
}
}
countC++;
for (int i = 0; i < keyboard.length(); i++) {
if (keyboard.charAt(i) == 'O') {
canvas.setColor(Color.BLACK);
}
}
countO++;
System.out.println("h = " + counth);
System.out.println("g = " + countg);
System.out.println("+ = " + countPlus);
System.out.println("- = " + countMinus);
System.out.println("K = " + countK);
System.out.println("R = " + countR);
System.out.println("G = " + countG);
System.out.println("B = " + countB);
System.out.println("C = " + countC);
System.out.println("O = " + countO);
myPic.repaint();
} else {
System.exit(0);
}
}
I'm trying to draw some lines based on user input.
The inputs are h and f for moving forward 10 pixels (both do the same thing) and + or - to change the direction the line will move 90 degrees either clockwise for + or counterclockwise for - (default is moving up).
For example, if a user inputs: "hhh" the line will move up 30 pixels.
The problem I am having is I want the user to be able to input hhh+hhh and it draw a line 30 pixels upwards then turns and draws 30 more pixels but to the right. However in my program when I input hhh+hhh it just makes one straight line 60 pixels to the right.
So my question is: how can I get it to draw a line turn when it sees either + or - in the specified direction and draw another line.
Code completely untested but I suggest you:
Delete the two loops for + and -.
for(int i =0; i < keyboard.length(); i++)
{
if(keyboard.charAt(i) == '+')
heading++;
if(keyboard.charAt(i) == '-')
heading--;
if (heading < 0)
heading = 3;
if (heading > 3)
heading = 0;
if(keyboard.charAt(i) == 'h' || keyboard.charAt(i) == 'f')
//Snip rest of heading code here
}
The issue you are having is that you are looping through the code multiple times independent of processing the line drawing code.
While this may not resolve your issue it may get you on the right track.
The problem is to find the shortest path on a grid from a start point to a finish point. the grid is a 2 dimensional array filled with 0's and 1's. 1's are the path. I have a method that checks the neighbors of a given coordinate to see if its a path. The problem im having is with the boundaries of the grid. The right and bottom boundary can just be checked using the arrays length and the length of a column. But how would i check to make sure that i dont try to check a point thats to the left of the grid or above the grid?
This is my method
public static void neighbors(coordinate current, int[][] grid, Queue q)
{
int row = current.getRow();
int col = current.getCol();
if(grid[row-1][col] == 1)
{
if(grid[row][col] == -1)
{
grid[row-1][col] = grid[row][col] + 2;
}
else
{
grid[row-1][col] = grid[row][col] + 1;
}
coordinate x = new coordinate(row-1,col);
q.enqueue(x);
}
else if(grid[row+1][col] == 1)
{
if(grid[row][col] == -1)
{
grid[row+1][col] = grid[row][col] + 2;
}
else
{
grid[row+1][col] = grid[row][col] + 1;
}
coordinate x = new coordinate(row+1,col);
q.enqueue(x);
}
else if(grid[row][col-1] == 1)
{
if(grid[row][col] == -1)
{
grid[row][col-1] = grid[row][col] + 2;
}
else
{
grid[row][col-1] = grid[row][col] + 1;
}
coordinate x = new coordinate(row, col - 1);
q.enqueue(x);
}
else if(grid[row][col+1] == 1)
{
if(grid[row][col+1] == -1)
{
grid[row][col+1] = grid[row][col] + 1;
}
else
{
grid[row][col+1] = grid[row][col] + 1;
}
coordinate x = new coordinate(row, col + 1);
q.enqueue(x);
}
else
{
}
q.dequeue();
}
I assume that the leftmost and topmost indexes are 0 in your arrays, so just make sure that index-1 >= 0 before indexing into the appropriate array.