Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need to implement JAVA code in order to move a robot around a square room (grid) and the size of the room can change

I need to implement JAVA code in order to move a "robot" around a square room (grid) and the size of the room can change size. 6 x 6 is the size I was given. I'm assuming that i need to write some type of algorithm using loops to guide it in the patterns described but Im really confused as to how or what to do. The assignment description is below... thanks for your help

Description

This time the robot needs to walk three different patterns as described below. In addition, you are going to use a random number generator to generate random grid sizes to work with.

Resist the temptation to rely on any specific grid size. All three algorithms need to work for any square grid - regardless of the size.

Challenge 1: image text in transcribed

At the beginning of challenge 1, the robot is in the North-East corner of the grid. The robot should walk left and right and left and right etc. as shown on in the image. If the grid size is odd the robot will end up at the South-West corner. If the grid size is even, the robot will end up at the South-East corner. When the robot arrives in its final position it should say "done"

Challenge 2: image text in transcribed

At the beginning of challenge 2, the robot is in the North-East corner of the grid. The robot should walk down, left, up, right, down ... like a clockwise spiral - as shown in the picture. The robot needs to stop when the last room has been entered. No room on the grid should be skipped and no room should be entered twice on the path to the center.When the robot arrives in its final position it should say "did it"

Challenge 3: image text in transcribed

At the beginning of challenge 3, the robot is in the North-East corner of the grid. The robot starts heading south and it distances itself more and more from the North-East corner as shown in the picture. No room on the grid should be skipped and no room should be entered twice. When the robot arrives in its final position it should say "finished"

Generate Random Grids:

Modify the method main. Currently, it generates grids of size 6x6. Make the necessary changes so that the grids will be randomly assigned one of the following sizes: 2, 3, 5, or 6 All grids are square but the three grids are independent of each other. They don't necessarily have the same sizes.

The 3 Java files are included but i didn't include one because it generated this graphic.... image text in transcribed

1 *************************************************/ 2 public class RobotPattern { 3 public static void main(String[] args) { 4 int n = 6; 5 // square nxn grid with the robot in the north west corner 6 Robot robot = new Robot(n, n, n-1, 0); 7 challenge1(robot); 8 9 robot = new Robot(n, n, n-1, 0); 10 challenge2(robot); 11 12 robot = new Robot(n, n, n-1, 0); 13 challenge3(robot); 14 } 15 private static void challenge1(Robot robot) { 16 // TODO: Make the robot walk the pattern described in challenge 1 17 // When done it should say the specified message 18 } 19 private static void challenge2(Robot robot) { 20 // TODO: Make the robot walk the pattern described in challenge 2 21 // When done it should say the specified message 22 } 23 private static void challenge3(Robot robot) { 24 // TODO: Make the robot walk the pattern described in challenge 3 25 // When done it should say the specified message 26 } 27 } END 28 *************************************************/

ROBOT CLASS JAVA CODE 2 * Do NOT modify 3 *******************/ 4 5 import java.awt.Font; 6 import java.util.ArrayList; 7 import javax.swing.JLabel; 8 import javax.swing.JOptionPane; 9 10 public class Robot { 11 12 private DoorRoom room; 13 private int x; 14 private int y; 15 16 public Robot(DoorRoom room, int sx, int sy) { 17 this.room = room; 18 this.x = sx; 19 this.y = sy; 20 this.show(); 21 try { Thread.sleep(500); } catch (InterruptedException e) {} 22 } 23 public Robot(DoorRoom room) { 24 this(room,0,0); 25 } 26 public Robot(boolean[][] grid, int sx, int sy) { 27 this(new DoorRoom(grid), sx, sy); 28 } 29 public Robot(boolean[][] grid) { 30 this(new DoorRoom(grid)); 31 } 32 public Robot(String grid, int sx, int sy) { 33 this(new DoorRoom(grid), sx, sy); 34 } 35 public Robot(String grid) { 36 this(new DoorRoom(grid)); 37 } 38 public Robot(int w, int h, int sx, int sy) { 39 this(new DoorRoom(w, h), sx, sy); 40 } 41 public Robot(int w, int h) { 42 this(new DoorRoom(w, h)); 43 } 44 public boolean check(char dir) { 45 try { Thread.sleep(125); } catch (InterruptedException e) {} 46 return room.isDoor(this.x, this.y, dir); 47 } 48 public boolean check(String dir) { return this.check(dir.charAt(0)); } 49 50 public void go(char dir) { 51 if (!this.check(dir)) { 52 switch(dir) { 53 case 'w': case 'W': for(double i=0; i

START END

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions