Skip to content
Open
Show file tree
Hide file tree
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
57 changes: 56 additions & 1 deletion src/Search.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import java.util.*;

public class Search {
/**
* Finds the location of the nearest reachable cheese from the rat's position.
Expand Down Expand Up @@ -29,6 +31,59 @@ public class Search {
* @throws HungryRatException if there is no reachable cheese
*/
public static int[] nearestCheese(char[][] maze) throws EscapedRatException, CrowdedMazeException, HungryRatException {
return null;
boolean[][] visited = new boolean[maze.length][maze[0].length];

Queue<int[]> que = new LinkedList<>();
que.add(ratLocation(maze));
while(!que.isEmpty()){
int[] current = que.poll();
if(visited[current[0]][current[1]]) continue;

visited[current[0]][current[1]] = true;

if(maze[current[0]][current[1]] == 'c') return current;
que.addAll(getNeighbors(maze, current));
// for(int[] neighbor: getNeighbors(maze, current)){
// que.add(neighbor);
// }

}

throw new HungryRatException();
}

public static List<int[]> getNeighbors(char[][] maze, int[] current){
int curR = current[0];
int curC = current[1];
int[][] directions = {{-1,0}, {1,0}, {0,-1}, { 0,1}}; //up down left right
List<int[]> moves = new ArrayList<>();
for(int[] dir: directions){
int changeR = dir[0];
int changeC = dir[1];

int newR = curR + changeR;
int newC = curC + changeC;

if(newR>=0 && newR < maze.length &&
newC >= 0 && newC < maze[newR].length &&
maze[newR][newC] != 'w'){
moves.add(new int[] {newR, newC});
}
}
return moves;
}

public static int[] ratLocation(char[][] maze) throws EscapedRatException, CrowdedMazeException{
int [] location = null;
for(int r=0; r<maze.length; r++){
for(int c=0; c<maze[r].length; c++){
if(maze[r][c] == 'R'){
if(location != null) throw new CrowdedMazeException();
location = new int[] {r, c};
}
}
}
if(location == null) throw new EscapedRatException();
return location;
}
}
77 changes: 77 additions & 0 deletions src/SearchTest.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,80 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;

import org.junit.Test;

public class SearchTest {
@Test
public void testFindRat(){
char [][] maze = {
{'o', 'o', 'o', 'o', 'c', 'w', 'c', 'o'},
{'w', 'o', 'o', 'w', 'w', 'c', 'w', 'o'},
{'o', 'o', 'o', 'o', 'R', 'w', 'o', 'o'},
{'o', 'o', 'w', 'w', 'w', 'o', 'o', 'o'},
{'o', 'o', 'o', 'o', 'c', 'o', 'o', 'o'}
};
int[] expected = {2, 4};
assertEquals(expected[0], Search.ratLocation(maze)[0]);
assertEquals(expected[1], Search.ratLocation(maze)[1]);
}

@Test
public void testFindRat_noRat(){
char [][] maze = {
{'o', 'o', 'o', 'o', 'c', 'w', 'c', 'o'},
{'w', 'o', 'o', 'w', 'w', 'c', 'w', 'o'},
{'o', 'o', 'o', 'o', 'w', 'w', 'o', 'o'},
{'o', 'o', 'w', 'w', 'w', 'o', 'o', 'o'},
{'o', 'o', 'o', 'o', 'c', 'o', 'o', 'o'}
};

assertThrows(EscapedRatException.class, () ->{
Search.ratLocation(maze);
});
}

@Test
public void testFindRat_TwoRats(){
char [][] maze = {
{'o', 'o', 'o', 'o', 'c', 'w', 'c', 'o'},
{'w', 'o', 'o', 'w', 'w', 'c', 'w', 'o'},
{'o', 'o', 'o', 'o', 'R', 'w', 'o', 'o'},
{'o', 'o', 'w', 'w', 'w', 'o', 'o', 'o'},
{'o', 'o', 'o', 'o', 'c', 'o', 'o', 'R'}
};

assertThrows(CrowdedMazeException.class, () ->{
Search.ratLocation(maze);
});
}

@Test
public void testNearestCheese_noCheese(){
char [][] maze = {
{'o', 'o', 'o', 'o', 'o', 'w', 'o', 'o'},
{'w', 'o', 'o', 'w', 'w', 'o', 'w', 'o'},
{'o', 'o', 'o', 'o', 'R', 'w', 'o', 'o'},
{'o', 'o', 'w', 'w', 'w', 'o', 'o', 'o'},
{'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o'}
};

assertThrows(HungryRatException.class, () ->{
Search.nearestCheese(maze);
});
}

@Test
public void testNearestCheese_twoCheese(){
char [][] maze = {
{'o', 'o', 'o', 'o', 'o', 'w', 'o', 'o'},
{'w', 'o', 'o', 'w', 'c', 'o', 'w', 'o'},
{'o', 'o', 'o', 'c', 'R', 'w', 'o', 'o'},
{'o', 'o', 'w', 'w', 'w', 'o', 'o', 'o'},
{'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o'}
};

assertEquals(1, Search.nearestCheese(maze)[0]);
assertEquals(4, Search.nearestCheese(maze)[1]);

}
}