JAVA - Go game algorithm - java

I am trying to implement an algorithm to clear dead stones in my Go game.
I hear that floodfill is the best to achieve this as using it recursively would be most effiecient and easier to implement.
I am having trouble using it within my code and was wondering how I should go about implementing it.
This is one of my classes, it is pretty self explanatory.
import java.io.*;
public class GoGame implements Serializable {
int size;
char[][] pos; // This is the array that stores whether a Black (B) or White (W) piece is stored, otherwise its an empty character.
public GoGame(int s){
size = s;
}
public void init() {
pos = new char[size][size];
for (int i=0;i<size;i++) {
for (int j=0;j<size;j++) {
pos[i][j] = ' ';
}
}
}
public void ClearAll() {
for (int i=0;i<size;i++) {
for (int j=0;j<size;j++) {
pos[i][j] = ' ';
}
}
}
public void clear(int x, int y) {
pos[x][y]=' ';
}
public void putB(int x, int y) { //places a black stone on the board+array
pos[x][y]='B';
floodfill(x,y,'B','W');
}
public void putW(int x, int y) { //places a white stone on the board+array
pos[x][y]='W';
floodfill(x,y,'W','B');
}
public char get(int x, int y) {
return pos[x][y];
}
public void floodfill(int x, int y, char placed, char liberty){
floodfill(x-1, y, placed, liberty);
floodfill(x+1, y, placed, liberty);
floodfill(x, y-1, placed, liberty);
floodfill(x, y+1, placed, liberty);
}
}
x and y are the coordinates of the square, placed is the character of the stone put down, liberty is the other character
Any help would be amazing!

while the other answers are technically correct, you are also missing a lot more logic related to go. what you need to do is, i think (on a B move):
for each W neighbour of the move:
check that W group to see if it has any liberties (spaces)
remove it if not
flood fill is useful for finding the extent of a group of stones, but your routine needs a lot more than that (i'm simplifying here, and also trying to guess what this routine is used for - see comments below this answer).
given the above, a flood fill that identifies all the stones in a group would be something like this (note that it uses a second array for the fill, because you don't want to be changing pos just to find a group):
public void findGroup(int x, int y, char colour, char[][] mask) {
// if this square is the colour expected and has not been visited before
if (pos[x][y] == colour && mask[x][y] == ' ') {
// save this group member
mask[x][y] = pos[x][y];
// look at the neighbours
findGroup(x+1, y, colour, mask);
findGroup(x-1, y, colour, mask);
findGroup(x, y+1, colour, mask);
findGroup(x, y-1, colour, mask);
}
}
you can call that to identify a single group (and copy it into mask), so it will help you identify the members of a W group that neighbour a B move (for example), but it is only a small part of the total logic you need.
finally, note that if you want to do something with every stone in a group you have two options. you can call a routine like the one above, and then loop over mask to find the group, or you can put the action you want to do directly inside the routine (in which case you still use mask to control the extent of the flood fill in the test && mask[x][y] == ' ' but you don't use it as a result - all the work is done by the time the routine returns).
(programming something to handle go correctly, following all the rules, is actually quite complex - you've got a lot of work ahead... :o)

I'd use false proof for that. Here is how I find captured stones:
private static final int SIZE = 8;
private static final int VACANT = 0; //empty point
private static final int MY_COLOR = 1; //Black
private static final int ENEMY_COLOR = 2; //White
private static final int CHECKED = 50; //Mark for processed points
private static final int OUT = 100; //points out of the board
private static boolean isCaptured(int col, int row, int[][] board) {
boolean result = !isNotCaptured(col, row, board);
cleanBoard(board);
return result;
}
private static boolean isNotCaptured(int col, int row, int[][] board) {
int value = board[col][row];
if (!(value == MY_COLOR || value == CHECKED))
return true;
int top = row < SIZE - 1 ? board[col][row + 1] : OUT;
int bottom = row > 0 - 1 ? board[col][row - 1] : OUT;
int left = col > 0 ? board[col - 1][row] : OUT;
int right = col < SIZE - 1 ? board[col + 1][row] : OUT;
if (top == VACANT || right == VACANT || left == VACANT || bottom == VACANT)
return true;
board[col][row] = CHECKED;
return (top == MY_COLOR && isNotCaptured(col, row + 1, board))
|| (bottom == MY_COLOR && isNotCaptured(col, row - 1, board))
|| (left == MY_COLOR && isNotCaptured(col - 1, row, board))
|| (right == MY_COLOR && isNotCaptured(col + 1, row, board));
}
private static void cleanBoard(int[][] board) {
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
if (board[i][j] == CHECKED)
board[i][j] = MY_COLOR;
}
}
}
Then you can call method like this:
isCaptured(5, 4, board)

I think that BFS will be better for this case because you need to explore the neighbors first, so that if any of them is captured then the point is captured.

As others pointed out, there is also a "ko rule" in Go which roughly means that you are not allowed to capture back immediately when a single stone is captured (simplified). In summary, you may want to use an existing library for this.
I recommend the brugo repository, which is available in maven.
<!-- https://mvnrepository.com/artifact/be.brugo/brugo -->
<dependency>
<groupId>be.brugo</groupId>
<artifactId>brugo</artifactId>
<version>0.1.0</version>
</dependency>
It roughly works like this.
(warning: code not tested)
// create a starting position
Position position = new Position(boardSize, komi);
// play a move
Intersection whereToPlay = Intersection.valueOf(4,4);
IntStatus colorToPlay = IntStatus.BLACK;
Position position2 = position.play(whereToPlay, colorToPlay);
// watch the result.
IntStatus[][] matrix = position2.getMatrix()
It also contains objects to export to Load/Save SGF. The loading of SGF files does not only support UTF-8 but also Asian encodings. Here is a screenshot that shows how difficult this is to implement yourself:
If you also plan to use javafx, then run this demo: brugo.go.ui.javafx.goban.GobanComponentDemo
Enough to get you started.

Related

Using BFS to find number of possible paths for an object on a grid

I have a matrix that represents a grid and would like to find out all possible places an object can move to.
An object can only move horizontally or vertically.
Let's assume that the example below is the grid I'm looking at, which is represented as a 2d matrix. The object is the *, the 0s are empty spaces that an object can move to, and the 1s are walls which the object cannot jump over or go on to.
What is the best way to find all possible movements of this object provided that it can only move horizontally or vertically?
I'd like to print a message saying: "There are 9 places the object can go to." The 9 is for the example below, but I would like it to work for any configuration of the below grid. So all I have to do is give the current coordinates of the * and it will give me the number of possible positions it can move to.
A thing to note is that the *'s original position is not considered in the calculations, which is why for the example below the message would print 9 and not 10.
I have a isaWall method that tells me if the cell is a wall or not. The isaWall method is in a Cell class. Each cell is represented by its coordinates. I looked into using Algorithms like BFS or DFS, but I didn't quite understand how to implement them in this case, as I am not too familiar with the algorithms. I thought of using the Cells as nodes of the graph, but wasn't too sure how to traverse the graph because from the examples I saw online of BFS and DFS, you would usually have a destination node and source node (the source being the position of the *), but I don't really have a destination node in this case. I would really appreciate some help.
00111110
01000010
100*1100
10001000
11111000
EDIT: I checked the website that was recommend in the comments and tried to implement my own version. It unfortunately didn't work. I understand that I have to expand the "frontier" and I basically just translated the expansion code to Java, but it still doesn't work. The website continues explaining the process, but in my case, there is no destination cell to go to. I'd really appreciate an example or a clearer explanation pertaining to my case.
EDIT2: I'm still quite confused by it, can someone please help?
While BFS/DFS are commonly used to find connections between a start and end point, that isn't really what they are. BFS/DFS are "graph traversal algorithms," which is a fancy way of saying that they find every point reachable from a start point. DFS (Depth First Search) is easier to implement, so we'll use that for your needs (note: BFS is used when you need to find how far away any point is from the start point, and DFS is used when you only need to go to every point).
I don't know exactly how your data is structured, but I'll assume your map is an array of integers and define some basic functionality (for simplicity's sake I made the start cell 2):
Map.java
import java.awt.*;
public class Map {
public final int width;
public final int height;
private final Cell[][] cells;
private final Move[] moves;
private Point startPoint;
public Map(int[][] mapData) {
this.width = mapData[0].length;
this.height = mapData.length;
cells = new Cell[height][width];
// define valid movements
moves = new Move[]{
new Move(1, 0),
new Move(-1, 0),
new Move(0, 1),
new Move(0, -1)
};
generateCells(mapData);
}
public Point getStartPoint() {
return startPoint;
}
public void setStartPoint(Point p) {
if (!isValidLocation(p)) throw new IllegalArgumentException("Invalid point");
startPoint.setLocation(p);
}
public Cell getStartCell() {
return getCellAtPoint(getStartPoint());
}
public Cell getCellAtPoint(Point p) {
if (!isValidLocation(p)) throw new IllegalArgumentException("Invalid point");
return cells[p.y][p.x];
}
private void generateCells(int[][] mapData) {
boolean foundStart = false;
for (int i = 0; i < mapData.length; i++) {
for (int j = 0; j < mapData[i].length; j++) {
/*
0 = empty space
1 = wall
2 = starting point
*/
if (mapData[i][j] == 2) {
if (foundStart) throw new IllegalArgumentException("Cannot have more than one start position");
foundStart = true;
startPoint = new Point(j, i);
} else if (mapData[i][j] != 0 && mapData[i][j] != 1) {
throw new IllegalArgumentException("Map input data must contain only 0, 1, 2");
}
cells[i][j] = new Cell(j, i, mapData[i][j] == 1);
}
}
if (!foundStart) throw new IllegalArgumentException("No start point in map data");
// Add all cells adjacencies based on up, down, left, right movement
generateAdj();
}
private void generateAdj() {
for (int i = 0; i < cells.length; i++) {
for (int j = 0; j < cells[i].length; j++) {
for (Move move : moves) {
Point p2 = new Point(j + move.getX(), i + move.getY());
if (isValidLocation(p2)) {
cells[i][j].addAdjCell(cells[p2.y][p2.x]);
}
}
}
}
}
private boolean isValidLocation(Point p) {
if (p == null) throw new IllegalArgumentException("Point cannot be null");
return (p.x >= 0 && p.y >= 0) && (p.y < cells.length && p.x < cells[p.y].length);
}
private class Move {
private int x;
private int y;
public Move(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
}
}
Cell.java
import java.util.LinkedList;
public class Cell {
public final int x;
public final int y;
public final boolean isWall;
private final LinkedList<Cell> adjCells;
public Cell(int x, int y, boolean isWall) {
if (x < 0 || y < 0) throw new IllegalArgumentException("x, y must be greater than 0");
this.x = x;
this.y = y;
this.isWall = isWall;
adjCells = new LinkedList<>();
}
public void addAdjCell(Cell c) {
if (c == null) throw new IllegalArgumentException("Cell cannot be null");
adjCells.add(c);
}
public LinkedList<Cell> getAdjCells() {
return adjCells;
}
}
Now to write our DFS function. A DFS recursively touches every reachable cell once with the following steps:
Mark current cell as visited
Loop through each adjacent cell
If the cell has not already been visited, DFS that cell, and add the number of cells adjacent to that cell to the current tally
Return the number of cells adjacent to the current cell + 1
You can see a visualization of this here. With all the helper functionality we wrote already, this is pretty simple:
MapHelper.java
class MapHelper {
public static int countReachableCells(Map map) {
if (map == null) throw new IllegalArgumentException("Arguments cannot be null");
boolean[][] visited = new boolean[map.height][map.width];
// subtract one to exclude starting point
return dfs(map.getStartCell(), visited) - 1;
}
private static int dfs(Cell currentCell, boolean[][] visited) {
visited[currentCell.y][currentCell.x] = true;
int touchedCells = 0;
for (Cell adjCell : currentCell.getAdjCells()) {
if (!adjCell.isWall && !visited[adjCell.y][adjCell.x]) {
touchedCells += dfs(adjCell, visited);
}
}
return ++touchedCells;
}
}
And that's it! Let me know if you need any explanations about the code.

JAVA 2D Map using String? i.e. "+" and "C"

so I've been looking but I simply just don't know how to state my problem.
So I'm just going to break an egg, and if you can link to the correct answer anyhow then please don't be afraid to, this is a long shot and I know this exists many places, I am just unable to find it.
I am looking at making a 2D map, based off on PLUS signs (+) and ONE (C), the C is the characters current location.
It would look like this
C+++++++++++++++++++
++++++++++++++++++++
++++++++++++++++++++
++++++++++++++++++++
++++++++++++++++++++
When printed.
Notice C is based off of integers, namely currentX and currentY (1 & 1).
This is my current code in bp_Map.class
public class bp_Map {
// Map
public static String mapP = "+";
public static String mapC = "C";
public static int sizeY = 19;
public static int sizeX = 19;
public static void drawMap(int currX, int currY) {
int currentY = 0;
while (currentY <= sizeY) {
drawX();
System.out.print("\n");
currentY ++;
}
}
public static void drawX() {
int currentX = 0;
while (currentX <= sizeX) {
System.out.print(mapP);
currentX++;
}
}
I could use an array, instead of mapP and mapC and just do
public static final String mapChar[] = {"+", "C"}
But I don't feel the need to do this atm.
My current problem is I don't want 20 if statements (or 1 if and 19 if else statements) to check the location of X, and then print correspondingly Y.
I am new to java and still learning, I have used while, but should I use for? I'm a bit lost, hope you guys can help me. This is for a text-based rpg, and I'm working on it alongside my studies.
You don't need if-else cases - this is a perfect usage example for loops.
First of all, define things which will never change as final fields in your class:
private static final String EMPTY = "+";
private static final String CHARACTER = "C";
private static final int SIZE_X = 20;
private static final int SIZE_Y = 5;
For this example, I'll be using fields for the current X and Y coordinates too, but you may want to change this since I assume they come from elsewhere in your program:
private static int currentX = 7;
private static int currentY = 3;
Now, think of how a TV draws pixels on a screen: from the top to the bottom and from left to right, pixel by pixel, at least 30 times a second. Let's try and do the same, and draw one row at a time:
public static void main(String[] args) {
if(currentX > SIZE_X - 1 || currentY > SIZE_Y - 1) {
throw new IllegalStateException("Out of bounds");
}
for (int y = 0; y < SIZE_Y; y++) {
drawRow(y);
}
}
What would the drawRow() function look like? One possible implementation is below:
private static void drawRow(int i) {
// Use a StringBuilder, ~30 times faster than String concatenation!
StringBuilder row = new StringBuilder();
if(i == currentY) {
// Create this row differently, as it contains the character.
for (int x = 0; x < SIZE_X; x++) {
if(x == currentX) {
row.append(CHARACTER);
} else {
row.append(EMPTY);
}
}
} else {
// Create an empty row.
for (int x = 0; x < SIZE_X; x++) {
row.append(EMPTY);
}
}
// "Draw" the row by printing it to the console.
System.out.println(row.toString());
}
This produces:
++++++++++++++++++++
++++++++++++++++++++
++++++++++++++++++++
+++++++C++++++++++++
++++++++++++++++++++
Try playing around with the coordinates and run main again. This is just one of many possible solutions - the neat thing about the above code is that no Map or even array is needed, but it may be that you do need them eventually and the code would have to change to accommodate this (e.g. keep a bit matrix, make a nested for loop over it and draw the set bit as the character). Let us know if you would like an example of this.
An approach I would use in this case is the following in pseudo-code:
Create a character matrix with the dimensions of sizeX by sizeY
Use the java.util.Arrays.fill builtin to fill the entire matrix with the character '+'
Replace the character at position {currX, currY} (1-indexed) with character 'C'
Pretty-print the matrix
Here a possible implementation of what I described above:
/*
* Prints a block of sizeX by sizeY of the filler character,
* where the character at position {posX, posY} (1-indexed) is replaced with the replacement character
*
* TODO: Validation checks. Currently assumes posX and posY are always within range of the matrix
*/
public void drawMap(int sizeX, int sizeY, char fillerChar, char replacementChar, int posX, int posY){
// Create a char-matrix of dimensions sizeX by sizeY
char[][] matrix = new char[sizeX][sizeY];
// Fill this matrix initially with the filler-character
for(char[] row : matrix)
java.util.Arrays.fill(row, fillerChar);
// Replace the character at position {currX, currY} (1-indexed) with the replacement-character
matrix[posX-1][posY-1] = replacementChar;
// Print the matrix
prettyPrintMatrix(matrix);
}
private void prettyPrintMatrix(char[][] matrix){
for(char[] row : matrix){
for(char ch : row)
System.out.print(ch);
System.out.println();
}
}
This could then be called with:
drawMap(10, 10, '+', 'C', 4, 2);
Which will output:
++++++++++
++++++++++
++++++++++
+C++++++++
++++++++++
++++++++++
++++++++++
++++++++++
++++++++++
++++++++++
Try it online.
Some things to note:
I've added the size and characters as parameter to the method. In my TIO-link above you can see a call with different sizes or characters also works (i.e. m.drawMap(5, 5, 'a', 'B', 5, 5);).
I've added a TODO for validation checks. If the given posX or poxY are larger than the sizeX or sizeY respectively, it will of course give an ArrayOutOfBoundsException. So perhaps a check at the top of the method to see if the given pos-parameters are valid is in order depending on how you want to use it.

How to count a cell's neighbors in a cellular automaton with wraparound

So I'm making a program that simulates Life-like cellular automata, but I'm having some trouble with the method used to count a cell's live neighbors. The problem is that I want to be able to change how the grid wraps around -- that is, whether it wraps around from left to right (i.e., cylindrical), from top to bottom and left to right (i.e., toroidal), or not at all (i.e., flat) -- and I can't figure out how to make my method account for that. Here's what I have so far:
public int getLiveNeighbors(int row, int col)
{
int count = 0;
// "topology" is an int that represents wraparound:
// 0 = flat; 1 = cylindrical; 2 = toroidal
int top = topology != 2 ? row - 1 : (row + ROWS - 1) % ROWS;
int bottom = topology != 2 ? row + 1 : (row + 1) % ROWS;
int left = topology != 0 ? (col + COLS - 1) % COLS : col - 1;
int right = topology != 0 ? (col + 1) % COLS : col + 1;
for (int r = top; r < bottom + 1; r++)
for (int c = left; c < right + 1; c++)
if (!(r == row && c == col) && getCell(r, c).equals(LIVE))
count++;
}
The key, I think, is the if-statement in the for-loop -- there has to be some way to check whether r and c are within the bounds of the grid, while keeping in mind that the definition of "bounds" will vary depending on whether/how the grid wraps around. In the past I've gotten around this by having three different sets (one for each wraparound setting) of eight different if-statements to individually check each of the eight cells comprising the original cell's neighborhood; as you can imagine, it was not very pretty, but at least it worked.
I'm not so great at explaining my own code, so I hope that wasn't too confusing -- I'm feeling a little loopy myself (ha). If anyone has any questions, feel free to ask!
You probably already have a class like Board with a method like getCell(x, y) (at least a method of this kind is present in your code).
I'd just make this method lenient in a sense that it would accept negative x and y or x and y greater or equal to COLS and ROWS. Thus you could just iterate over col - 1 to col + 1 and row - 1 to row + 1 (minus col and row) and not care that these coordinates go "over the board". It's the task of the Board to do coordinate lookups correctly.
What makes your code harder is also that you handle different topologies in one place. It's quite hard to follow.
You could make it simpler by implementing different subclasses of Board like CylindricalBoard, ToroidalBoard and FlatBoard. Each of the subclasses would implement getCell differently, but in the context of the subclass it will be clearly understandable.
You're looking for the Strategy Pattern:
There are common situations when classes differ only in their behavior. For this cases is a good idea to isolate the algorithms in separate classes in order to have the ability to select different algorithms at runtime.
In this case you'd want something like this (abbreviated for clarity):
class Point {
int x;
int y;
}
interface WrapStrategy {
Point moveUp(Point p);
Point moveDown(Point p);
Point moveLeft(Point p);
Point moveRight(Point p);
}
class CylinderWrapping implements WrapStrategy {
int height;
int circumference;
Point moveUp(Point p) {
if (p.y <= 0)
return null; // cannot move up
return new Point(p.x, p.y - 1);
}
Point moveDown(Point p) {
if (p.y >= height - 1)
return null; // cannot move down
return new Point(p.x, p.y + 1);
}
Point moveLeft(Point p) {
if (p.x <= 0)
return new Point(circumference - 1, p.y);
return new Point(p.x - 1, p.y);
}
Point moveRight(Point p) {
if (p.x >= circumference - 1)
return new Point(0, p.y);
return new Point(p.x + 1, p.y);
}
}
Try this:
import java.awt.Point;
public class Neighbours {
public static void main(String[] args) {
Neighbours inst=new Neighbours();
int r=3;//<ROWS
int c=3;//<COLS
for(int i :new int[]{0,1,2}){
inst.type=i;
System.out.format("There are %d neighbours of point (%d,%d), topography type %d\n", inst.countLiveNeighbours(r, c), c, r,i);
}
}
int ROWS=4;
int COLS=4;
int type=0;//0=flat, 1=cylinder, 2=toroid
/**
* Is x,y a neighbour of r,c?
* #return coordinates of neighbour or null
*/
Point neighbour(int x, int y, int r, int c){
if((x==c)&&(y==r))
return null;
switch (type){
/*this is wrong for the reasons explained below
case 0: return ((x<COLS)&&(y<ROWS)) ? new Point (x,y) : null;
case 1: return y<ROWS ? new Point(x%COLS,y) : null;
case 2: return new Point(x%COLS,y%ROWS);
*/
//replacement statements produce the correct behaviour
case 0: return ((x<COLS)&&(x>-1)&&(y<ROWS)&&(y>-1)) ? new Point (x,y) : null;
case 1: return ((y<ROWS)&&(y>-1)) ? new Point(Math.floorMod(x,COLS),y) : null;
case 2: return new Point(Math.floorMod(x,COLS),Math.floorMod(y,ROWS));
}
return null;
}
int countLiveNeighbours(int r, int c){
int result=0;
for(int x=c-1; x<c+2; x++)
for(int y=r-1; y<r+2; y++){
Point p=neighbour(x,y,r,c);
if(live(p)){
System.out.format("\tpoint (%d,%d)\n",(int)p.getX(),(int)p.getY());
result++;
}
}
return result;
}
boolean live(Point p){
boolean result=true;
if(p==null)
return false;
//perform tests for liveness here and set result
return result;
}
}

Checking for game objects position

I'm making a game where a game object, a squirrel, is trying to evade another object, the terriers. Here is what I have so far:
Squirrel.java
import java.io.File;
import java.io.FileNotFoundException;
import java.lang.Math;
import java.util.Scanner;
public class Squirrel implements AnimalInterface {
//
// DO NOT MODIFY BELOW
//
private int currentRow;
private int currentCol;
private int previousRow = -1;
private int previousCol = -1;
private int closestRow;
private int closestCol;
private char[][] field;
// Initializes position and field
public Squirrel(int row, int col, char[][] field){
this.currentRow = row;
this.currentCol = col;
this.field = field;
}
// Getters
public int getCurrentRow(){ return currentRow; }
public int getCurrentCol(){ return currentCol; }
public int getPreviousRow(){ return previousRow; }
public int getPreviousCol(){ return previousCol; }
public int getClosestRow(){ return closestRow; }
public int getClosestCol(){ return closestCol; }
//
// DO NOT MODIFY ABOVE
//
// Find closest terrier
public void findClosest(){
int rows = 0; int cols = 0;
//rows = field[0][0]; cols = field[1][0];
// TO DO: Replace with code to find closest
closestRow = -1;
closestCol = -1;
rows = field.length;
cols = field[0].length;
double dist = 0;
double newDist = 0;
for(int i = currentRow; i < rows; i++) {
for(int j =0; j < cols; j++) {
//if its 'D' for Terrier
if(i != currentRow && j != currentCol && field[i][j] == 'D') {
//find Euclidean distance
newDist = Math.sqrt( Math.pow(currentRow - i, 2)+Math.pow(currentCol - j, 2));
if(dist == 0) {
dist = newDist;
closestRow = i;
closestCol = j;
} else if(newDist < dist) {
dist = newDist;
closestRow = i;
closestCol = j;
}
}
}
}
}
// Move squirrel according to the rules
public void moveAnimal() {
eMove move;
// Store previous position
previousRow = currentRow;
previousCol = currentCol;
// TO DO: replace with code to select move (Step 1)
move = eMove.RIGHT;
// TO DO: replace with code to adjust move (Step 2)
move = move;
// TO DO: replace with code to make move (Step 3)
currentCol++;
}
//
// Private Methods, if you need them
//
}
I need to finish these two steps, but I'm not sure how to check for the position of the terrier so I can make the squirrel move in the opposite direction:
STEP 1) Select the move (of type eMove) which is in the exact opposite direction from the closest Terrier. For example, if the Terrier is left on the same row, move right. If the Terrier is below on the same column, move up. If the Terrier is above and right, move down and left. If the Terrier is below and right, move up and left, and so on.
STEP 2) Adjust the selected move (of type eMove) to avoid going off the board, running into a Terrier, or running into another Squirrel by carefully implementing the following behavior (in the order shown). You probably will want implement a private method to see if a particular move is valid.
If you are planning on moving DOWN_LEFT, but that move is not valid for one of the reasons above, move LEFT instead.
If you are planning on moving LEFT, but that move is not valid for one of the reasons above, move UP_LEFT instead.
If you are planning on moving UP_LEFT, but that move is not valid for one of the reasons above, move UP instead.
If you are planning on moving UP, but that move is not valid for one of the reasons above, move UP_RIGHT instead.
If you are planning on moving UP_RIGHT, but that move is not valid for one of the reasons above, move RIGHT instead.
If you are planning on moving RIGHT, but that move is not valid for one of the reasons above, move DOWN_RIGHT instead.
If you are planning on moving DOWN_RIGHT, but that move is not valid for one of the reasons above, move DOWN instead.
If you are planning on moving DOWN, but that move is not valid for one of the reasons above, move DOWN_LEFT instead.
If you get through adjusting the move as specified above, and the move is still not valid, set the move to NO_MOVE and return without updating the position.
If anymore information is needed let me know, thank you!
Add setters
public void setCurrentRow(int newRow){ currentRow=newRow; }
public void setCurrentCol(int currentCol){ currentCol=newCol; }
If the terrier has a higher column location than you, move lower (set the column to a lower value), if it's higher, move lower. Same for the row.
if (mySquirrel.getCurrentCol() > aTerrior.getCurrentColumn())
{
mySquirrel.setCurrentCol(mySquirrel.getCurrentCol()+1)
}
//...etc (one for < aTerrior and two for the rows)...

When navigating a 2D array, check neighboring elements for valid path relative to point of entry?

The nature of this problem has changed since submission, but the question isn't fit for deletion. I've answered the problem below and marked it as a community post.
I'm writing a recursive path-navigating function and the final piece I need involves knowing which cell you came from, and determining where to go next.
The Stage
You are given a 2d array where 0's denote an invalid path and 1's denote a valid path. As far as I know, you are allowed to manipulate the data of the array you're navigating, so I mark a traveled path with 2's.
The Goal
You need to recursively find and print all paths from origin to exit. There are four mazes, some with multiple paths, dead ends, or loops.
I've written code that can correctly handle all three cases, except the method for finding the next path is flawed in that it starts at a fixed location relative to your current index, and checks for a travelled path; If you encounter it, it's supposed to retreat.
While this works in most cases, it fails in a case when the first place it checks happens to be the place you came from. At this point, it returns out and ends prematurely.
Because of this, I need to find a way to intelligently start scanning (clockwise or anti-clockwise) based on where you came from, so that that place is always the last place checked.
Here is some code describing the process (note: edge cases are handled prior to this, so we don't need to worry about that):
private static void main()
{
int StartX = ;//Any arbitrary X
int StartY = ;//Any arbitrary Y
String Path = ""; //Recursive calls will tack on their location to this and print only when an exit path is found.
int[][] myArray = ;//We are given this array, I just edit it as I go
Navigator(StartX, StartY, Path, myArray);
}
private static void Navigator(int locX, int locY, String Path, int[][] myArray)
{
int newX = 0; int newY = 0;
Path = Path.concat("["+locX+","+locY+"]");
//Case 1: You're on the edge of the maze
boolean bIsOnEdge = (locX == 0 || locX == myArray.length-1 || locY == 0 || locY == myArray[0].length-1);
if (bIsOnEdge)
{
System.out.println(Path);
return;
}
int[][] Surroundings = surroundingsFinder(locX, locY, myArray);
for (int i = 0; i <= 7; i++)
{
//Case 2: Path encountered
if (Surroundings[0][i] == 1)
{
myArray[locX][locY] = 2;
newX = Surroundings[1][i];
newY = Surroundings[2][i];
Navigator(newX, newY, myArray, Path);
}
//Case 3: Breadcrumb encountered
if (Surroundings[0][i] == 2)
{
myArray[locX][locY] = 1;
return;
}
}
}
//generates 2D array of your surroundings clockwise from N to NW
//THIS IS THE PART THAT NEEDS TO BE IMPROVED, It always starts at A.
//
// H A B
// G - C
// F E D
//
static int[][] surroundingsFinder(int locX, int locY, int[][] myArray)
{
int[][] Surroundings = new int[3][8];
for (int i = -1; i <= 1; i++)
{
for (int j = -1; j <= 1; j++)
{
}
}
//Can be done simpler, is done this way for clarity
int xA = locX-1; int yA = locY; int valA = myArray[xA][yA];
int xB = locX-1; int yB = locY+1; int valB = myArray[xB][yB];
int xC = locX; int yC = locY+1; int valC = myArray[xC][yC];
int xD = locX+1; int yD = locY+1; int valD = myArray[xD][yD];
int xE = locX+1; int yE = locY; int valE = myArray[xE][yE];
int xF = locX+1; int yF = locY-1; int valF = myArray[xF][yF];
int xG = locX; int yG = locY-1; int valG = myArray[xG][yG];
int xH = locX-1; int yH = locY-1; int valH = myArray[xH][yH];
int[][] Surroundings = new int[3][8];
Surroundings[0][0] = valA; Surroundings[1][0] = xA; Surroundings[2][0] = yA;
Surroundings[0][1] = valB; Surroundings[1][1] = xB; Surroundings[2][1] = yB;
Surroundings[0][2] = valC; Surroundings[1][2] = xC; Surroundings[2][2] = yC;
Surroundings[0][3] = valD; Surroundings[1][3] = xD; Surroundings[2][3] = yD;
Surroundings[0][4] = valE; Surroundings[1][4] = xE; Surroundings[2][4] = yE;
Surroundings[0][5] = valF; Surroundings[1][5] = xF; Surroundings[2][5] = yF;
Surroundings[0][6] = valG; Surroundings[1][6] = xG; Surroundings[2][6] = yG;
Surroundings[0][7] = valH; Surroundings[1][7] = xH; Surroundings[2][7] = yH;
return Surroundings;
}
Can anyone help me with this? As you can see, surroundingsFinder always finds A first, then B all the way to H. This is fine if and only if you entered from H. But if fails on cases where you entered from A, so I need to make a way to intelligently determine where to start finding. Once I know this, I can probably adapt the logic so I no longer use a 2D array of values, as well. But so far I can't come up with the logic for the smart searcher!
NOTE: I am aware that Java does not optimize middle-recursion. It seems impossible to get tail recursion working for a problem like this.
The Solution
The initial goal was to print, from start to end, all of the paths that exit the array.
An earlier rendition of the script wrote "0" on treaded locations rather than "2", but for some reason I imagined that I needed the "2" and I needed to differentiate between "treaded path" and "invalid path".
In fact, due to the recursive nature of the problem, I discovered that you can in fact solve the problem writing only 0's as you go. Also, I no longer needed to keep track of where I came from and instead of checking clockwise over a matrix, I was iterating from left to right down the 3x3 matrix surrounding me, skipping my own cell.
Here is the completed code for such a solution. It prints to console upon finding an exit (edge) and otherwise traces itself around the maze, complete with recursion. To start the function, you are given a square 2D array of 0's and 1's where 1 is a valid path and 0 is invalid. You are also given a set of coordinates where you are "dropped in" (locX, locY) and an empty string that accumulates coordinates, forming a path that is later printed out (String Path = "")
Here is the code:
static void Navigator(int locX, int locY, int[][] myArray, String Path)
{
int newX = 0;
int newY = 0;
Path = Path.concat("["+locX+","+locY+"]");
if ((locX == 0 || locX == myArray.length-1 || locY == 0 || locY == myArray[0].length-1))
{//Edge Found
System.out.println(Path);
pathCnt++;
myArray[locX][locY] = 1;
return;
}
for (int row = -1; row <= 1; row++)
{
for (int col = -1; col <= 1; col++)
{
if (!(col == 0 && row == 0) && (myArray[locX+row][locY+col] == 1))
{ //Valid Path Found
myArray[locX][locY] = 0;
Navigator(locX+row, locY+col, myArray, Path);
}
}
}
//Dead End Found
myArray[locX][locY] = 1;
return;
} System.out.println(Path);
pathCnt++;
swamp[locX][locY] = 1;
return;
}
for (int row = -1; row <= 1; row++)
{
for (int col = -1; col <= 1; col++)
{
if (!(col == 0 && row == 0) && (swamp[locX+row][locY+col] == 1))
{ //Valid Path Found
swamp[locX][locY] = 0;
Navigator(locX+row, locY+col, swamp, Path);
}
}
}
//Dead End Found
swamp[locX][locY] = 1;
return;
}
As you may determine yourself, every time we "enter" a cell, we have 8 neighbors to check for validity. First, to save on run time and to avoid going out of the array during our for loop (it can't find myArray[i][j] if i or j point it outside, and it will error out), we check for edges. Since we're given the area of our swamp we use a truth comparison statement that essentially says ("(am I on the top or left edge?) or (am I on the bottom or right edge?)"). If we ARE on an edge, we print out the Path we're holding (thanks to deep copy, we have a unique copy of the original Path that only prints if we're on an edge, and includes our full set of coordinates).
If we aren't on an edge, then we start looking around us. We start at top left and move horizontally to bottom right, with a special check to make sure we're not checking where we're standing.:
A B C
D . E
F G H
This loop checks only for 1's and only calls the function up again should that happen. Why? Because it is the second-to-last case. There is only one extra situation that will occur, and if we reach the end of the function it means we hit that case. Why write extra code (checking for 0's to specifically recognize it?
So, as I just mentioned, if we exit the for loop, it means we didn't encounter any 1's at all. It means we're surrounded by zeros! It means we've hit a dead end, and that means that all we have to do is error our away out of that instance of the function, ergo the final return;.
All in all, the final function is simple. But coming from no background and having to realize the patterns and meanings of these cases, and after several failed attempts at this, it can take quite a bit of work. I was several days at work on perfecting this.
Happy coding, Everyone!
Your issue seems to be with:
if (Surroundings[0][i] == 2)
{
myArray[locX][locY] = 1;
return;
}
Perhaps this should be changed to:
if (Surroundings[0][i] == 2)
{
// not sure why you need this if it's already 1
myArray[locX][locY] = 1;
// go to next iteration of the "i" loop
// and keep looking for next available path
continue;
}
Your recursive method will automatically return when none of the surrounding cells satisfy the condition if (Surroundings[0][i] == 1).
PS: It's conventional to name your variables using small letter as the first character. For example: surroundings, path, startX or myVar

Categories