Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 122 additions & 1 deletion src/Search.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class Search {
/**
* Finds the location of the nearest reachable cheese from the rat's position.
Expand Down Expand Up @@ -27,8 +32,124 @@ public class Search {
* @throws EscapedRatException if there is no rat in the maze
* @throws CrowdedMazeException if there is more than one rat in the maze
* @throws HungryRatException if there is no reachable cheese
*
*
* First Step: Find the location of the rat by looping through the entire array (Throw exceptions if more than one or zero rats)
*
* Second Step: Find the directions that a rat can go: (Filters walls, )
*
* Third Step: Use breadth first search to find the CLOSEST cheese to the rat
*/

public static int[] nearestCheese(char[][] maze) throws EscapedRatException, CrowdedMazeException, HungryRatException {
return null;
boolean [][] visited = new boolean[maze.length][maze[0].length];
int[] start = ratLocation(maze);

// queue = []
Queue<int[]> queue = new LinkedList<>();
// queue.add(start)
queue.add(start);

// while queue isn't empty
while(!queue.isEmpty()) {

// current = queue.pop
int[] current = queue.poll();
int curRow = current[0];
int curCol = current[1];

// if visited: continue
if(visited[curRow][curCol]) continue;

// mark visited
visited[curRow][curCol] = true;


// if current is cheese: return current
if(maze[curRow][curCol] == 'c')
{
return current;
}

// add neighbors to the queue
queue.addAll(getNeighbors(maze, current));
}

// throw hungry
throw new HungryRatException();
}

//Getting the possible places the rat can go
public static List<int[]> getNeighbors(char[][] maze, int[] current)
{
int curRow = current[0];
int curCol = current[1];

//UP (Subtract Row and the Column stays the same) [-1,0]

//DOWN (Add one to the row and the Column stays the same) [1,0]

//LEFT (Row stays the same and Column gets subtracted by one) [0,-1]

//RIGHT (Row stays the same and Column gets added by one) [0,1]

int[][] directions = {
{-1,0}, //UP
{1,0}, //DOWN
{0,-1}, //LEFT
{0,1}, //RIGHT

};

List<int[]> possibleMoves = new ArrayList<>();

for(int [] direction : directions) //looping through the directions
{
int changeRow = direction[0];
int changeCol = direction[1];

int newRow = curRow + changeRow;
int newCol = curCol + changeCol;

if(newRow >= 0 && newRow < maze.length &&
newCol >= 0 && newCol < maze[newRow].length &&
maze[newRow][newCol] != 'w')
{
//valid move
int[] validMove = {newRow, newCol};
possibleMoves.add(validMove);
}
}
return possibleMoves;
}

//**Step 1**/
public static int[] ratLocation(char[][] maze) throws EscapedRatException, CrowdedMazeException {
int[] location = null;

//searching the maze...
for(int row = 0; row < maze.length; row++)
{
for(int col = 0; col < maze[0].length; col++)
{
if(maze[row][col] == 'R')
{
if(location != null) {
throw new CrowdedMazeException(); //if there's more than one rat found throw exception
}

location = new int[] {row, col}; //setting the location of the rat when it's found
}
}
}

if(location == null) //if the location is still null after going through the maze, throw exceptions
{
//Rat not found
throw new EscapedRatException();
}

return location;
}

}