-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added scripts for grid feature showcase.
- Loading branch information
1 parent
5c25e01
commit 66adb5a
Showing
3 changed files
with
144 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
from traverseCraft.world import CreateGridWorld | ||
from traverseCraft.agent import GridAgent | ||
|
||
def solver(grid, n:int, m:int, i:int, j:int, agent:GridAgent)->int: | ||
# Base Case | ||
grid[i][j] = -1 | ||
# move top | ||
if(i > 0 and grid[i-1][j] == 1 and agent.moveAgent(i - 1, j, delay=0.28)): | ||
solver(grid, n, m, i - 1, j, agent) | ||
agent.moveAgent(i, j) | ||
# move down | ||
if(i < (n - 1) and grid[i+1][j] == 1 and agent.moveAgent(i + 1, j, delay=0.28)): | ||
solver(grid, n, m, i + 1, j, agent) | ||
agent.moveAgent(i, j) | ||
# move right | ||
if(j < (m - 1) and grid[i][j+1] == 1 and agent.moveAgent(i, j + 1, delay=0.28)): | ||
solver(grid, n, m, i, j + 1, agent) | ||
agent.moveAgent(i, j) | ||
# move left | ||
if(j > 0 and grid[i][j-1] == 1 and agent.moveAgent(i, j - 1, delay=0.28)): | ||
solver(grid, n, m, i, j - 1, agent) | ||
agent.moveAgent(i, j) | ||
|
||
def numIslands(grid, n:int, m:int, agent:GridAgent)->int: | ||
ans = 0 | ||
for i in range(n): | ||
for j in range(m): | ||
if(grid[i][j] == 1): | ||
ans += 1 | ||
if((i + j) != 0): | ||
agent.moveAgent(i, j) | ||
solver(grid, n, m, i, j, agent) | ||
return ans | ||
|
||
if __name__ == "__main__": | ||
n = 5 | ||
m = 7 | ||
grid = [[1, 1, 0, 0, 1, 1, 1], | ||
[1, 1, 0, 0, 1, 1, 1], | ||
[0, 0, 1, 1, 0, 0, 0], | ||
[1, 1, 0, 0, 0, 1, 1], | ||
[1, 1, 1, 0, 1, 1, 1]] | ||
water = [] | ||
land = [] | ||
for i in range(n): | ||
for j in range(m): | ||
if(grid[i][j] == 0): | ||
water.append([i, j]) | ||
else: | ||
land.append([i, j]) | ||
|
||
world = CreateGridWorld(worldName = "Leetcode Number of Islands", rows = n, cols = m, cellSize = 36) | ||
world.constructWorld() | ||
world.setBlockPath(water) | ||
world.setGoalState(land) | ||
agent = GridAgent(world, agentName = "Robot") | ||
def sim(): | ||
print("Starting the simulation!") | ||
print("Number of unique Islands: ", numIslands(grid, n, m, agent)) | ||
print("Simulation Completed!") | ||
world.setAgent(agent) | ||
agent.setAlgorithmCallBack(sim) | ||
world.showWorld() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from traverseCraft.world import CreateGridWorld | ||
from traverseCraft.agent import GridAgent | ||
|
||
def solver(n:int, m:int, i:int, j:int, agent:GridAgent)->int: | ||
# Base Case | ||
if(i == (n - 1) and j == (m - 1)): | ||
return 1 | ||
elif((i < 0) or (j < 0) or (i == n) or (j == m)): | ||
return 0 | ||
|
||
ans = 0 | ||
|
||
# move right | ||
if(agent.moveAgent(i, j + 1, delay=0.28)): | ||
ans += solver(n, m, i, j + 1, agent) | ||
agent.moveAgent(i, j) | ||
|
||
# move down | ||
if(agent.moveAgent(i + 1, j, delay=0.28)): | ||
ans += solver(n, m, i + 1, j, agent) | ||
agent.moveAgent(i, j) | ||
|
||
return ans | ||
|
||
def uniquePaths(m:int, n:int, agent:GridAgent)->int: | ||
return solver(m, n, 0, 0, agent) | ||
|
||
if __name__ == "__main__": | ||
m = 3 | ||
n = 7 | ||
world = CreateGridWorld(worldName = "Leetcode Unique Paths", rows = m, cols = n, cellSize = 36) | ||
world.constructWorld() | ||
agent = GridAgent(world, agentName = "Robot") | ||
def sim(): | ||
print("Starting the simulation!") | ||
print("Number of unique paths: ", uniquePaths(m, n, agent)) | ||
print("Simulation Completed!") | ||
world.setAgent(agent) | ||
agent.setAlgorithmCallBack(sim) | ||
world.showWorld() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
from traverseCraft.world import CreateGridWorld | ||
from traverseCraft.agent import GridAgent | ||
|
||
def solver(n:int, m:int, i:int, j:int, agent:GridAgent)->int: | ||
# Base Case | ||
if(i == (n - 1) and j == (m - 1)): | ||
return 1 | ||
elif((i < 0) or (j < 0) or (i == n) or (j == m)): | ||
return 0 | ||
|
||
ans = 0 | ||
|
||
# move right | ||
if(agent.moveAgent(i, j + 1, delay=0.28)): | ||
ans += solver(n, m, i, j + 1, agent) | ||
agent.moveAgent(i, j) | ||
|
||
# move down | ||
if(agent.moveAgent(i + 1, j, delay=0.28)): | ||
ans += solver(n, m, i + 1, j, agent) | ||
agent.moveAgent(i, j) | ||
|
||
return ans | ||
|
||
def uniquePaths(m:int, n:int, agent:GridAgent)->int: | ||
return solver(m, n, 0, 0, agent) | ||
|
||
if __name__ == "__main__": | ||
m = 4 | ||
n = 7 | ||
world = CreateGridWorld(worldName = "Leetcode Unique Paths 2", rows = m, cols = n, cellSize = 36) | ||
world.constructWorld() | ||
world.setBlockPath([[0,5], [2,2], [1,4], [3,5]]) | ||
agent = GridAgent(world, agentName = "Robot") | ||
def sim(): | ||
print("Starting the simulation!") | ||
print("Number of unique paths: ", uniquePaths(m, n, agent)) | ||
print("Simulation Completed!") | ||
world.setAgent(agent) | ||
agent.setAlgorithmCallBack(sim) | ||
world.showWorld() |