AMazeBot 2016 Workshop # 2// AMazeBot 2016 Sample bot - WorkShop#2 - Mapping package bots.WorkShopBot_2; import amazebot2016.*; import amazebot2016.utils.Compass; import java.awt.Point; public class Brain extends BotBrain { final int UNKNOWN = 0; final int OPEN = 1; final int WALL = 2; @Override public String getName() { return "WorkShopBot_2"; } @Override public void run() { int[][] map = new int[MAZE_SIZE][MAZE_SIZE]; Point goal = this.getGoalCorner(); while (true) { // Calculate the distance from a Point to the goal for // 4 different options Point pos = getPosition(); map[pos.x][pos.y] = OPEN; printMap(map); // for debugging Point eastPoint = new Point(pos.x + 1, pos.y); Point northPoint = new Point(pos.x, pos.y - 1); Point westPoint = new Point(pos.x - 1, pos.y); Point southPoint = new Point(pos.x, pos.y + 1); double distanceEast = goal.distance(eastPoint); double distanceNorth = goal.distance(northPoint); double distanceWest = goal.distance(westPoint); double distanceSouth = goal.distance(southPoint); boolean moved = false; if (distanceEast < distanceNorth && distanceEast < distanceWest && distanceEast < distanceSouth) { // East is best if (map[eastPoint.x][eastPoint.y] != WALL) { if (map[eastPoint.x][eastPoint.y] == OPEN || look(Compass.EAST)) { moved = true; move(Compass.EAST); } if (! moved) map[eastPoint.x][eastPoint.y] = WALL; } } if (!moved && distanceNorth < distanceWest && distanceNorth < distanceSouth) { if (map[northPoint.x][northPoint.y] == OPEN || look(Compass.NORTH)) { moved = move(Compass.NORTH); map[northPoint.x][northPoint.y] = OPEN; } else map[northPoint.x][northPoint.y] = WALL; } if (!moved && distanceWest < distanceSouth) { moved = move(Compass.WEST); if (! moved) map[westPoint.x][westPoint.y] = WALL; } else if (!moved) { moved = move(Compass.SOUTH); if (! moved) map[southPoint.x][southPoint.y] = WALL; } } } void printMap(int[][] map) { println("------ MAZE MAP -------"); for (int y = 0; y < MAZE_SIZE; y++) { for (int x = 0; x < MAZE_SIZE; x++) { if (map[x][y] == OPEN) { print(" "); } else if (map[x][y] == WALL) { print("X"); } else { print("."); } } println(); } } } Although we have planned topics for each workshop, we generally try to ensure some time is available for answering any student questions. |
|
AMazeBot is © 2003-2016
Mohawk
College. |