I have a android application called Islands and bridges also known as Hashiwokakero
The application uses A 2 Dimensional array that spawns the Islands randomly everytime the user restarts the game It form a Matrix with number from 0 to 4 where 0=null and 1-4 = Island There can be 2 bridges comming out of one Island to connect the other , The map at the moment is not solvable. To solve the game the user needs to connect the islands using bridges so if an island = 4 it needs 4 connection to it if an island = 2 it needs 2 connection and so on..
in my research i found out that the best algorithm to solve the game is to use Depth first search - article
I have looked at different question on here but can't seem to find a solution as my array is of type String
rather than integer
.
QUESTION how can apply a DFS algorithm to connect the islands?
here is a screenshot of my application.
This the function to create a easy map 4x4 matrix:
private void InitializeEasy() {
Random rand = new Random();
String[][] debug_board_state = new String[4][4];
setCurrentState(new State(WIDTH_EASY));
for (int row = 0; row < debug_board_state.length; row++) {
for (int column = 0; column < debug_board_state[row].length; column++) {
debug_board_state[row][column] = String.valueOf(rand.nextInt(5));
}
}
for (int row = 0; row < debug_board_state.length; row++) {
for (int column = 0; column < debug_board_state[row].length; column++) {
System.out.print(debug_board_state[row][column] + " ");
}
System.out.println();
}
for (int row = 0; row < WIDTH_EASY; ++row) {
for (int column = 0; column < WIDTH_EASY; ++column) {
for (int colNum = column - 1; colNum <= (column + 1); colNum += 1) {
getCurrentState().board_elements[row][column] = new BoardElement();
getCurrentState().board_elements[row][column].max_connecting_bridges = Integer.parseInt(debug_board_state[row][column]);
getCurrentState().board_elements[row][column].row = row;
getCurrentState().board_elements[row][column].col = column;
if (getCurrentState().board_elements[row][column].max_connecting_bridges > 0) {
getCurrentState().board_elements[row][column].is_island = true;
}
}
}
}
}
DFS could be applied to the game state.
Pseudo algorithm:
As I mentioned, this is a piece of pseudo-code. You will need to refine it to handle edge-cases. You should also think about strategies to prevent the branching factor from becoming too large.
example (not thoroughly tested, not thoroughly debugged):
I also wrote a utility class that handles the common operations on the piece of land on which bridges need to be built (like finding the next available island, checking whether a bridge can be built, etc)