Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Description: On this assignment, you need to program a robot that has been dropped into a grid of square rooms. Each wall of each room

Description: On this assignment, you need to program a robot that has been dropped into a grid of square rooms. Each wall of each room has been painted a different color: the North wall is Neon, the East wall is Eggplant, the South wall is Sandy, and the West wall is Walnut. Walls between rooms have doors in them, but walls on the edge do not. All of the doors are closed initially.Description:

The robot knows how to do the following:

  1. Check N (or Check E, or S, or W) to see if there is a door on that wall of its current room;
  2. Go N (or E or S or W) to move one room over;
  3. Do basic math and remember numbers.
  4. Say something (a pop-up window with the specified text shows up)

If you ask the robot to Go through a wall that does not have a door, it isnt smart enough to know it cant so it will walk into the wall and damage itself.

The Robot needs to complete three challenges as described below. At the end of each challenge, the robot is asked to say something. This creates a pop-up window and causes the program to pause until the message is closed.

Getting Started: Download the zip file: Assignment Robot.zip Unzip it. Copy the unzipped folder in your 1400 Assignment folder and use it as a starter project for this assignment. The folder includes 3 files: Robot.java, DoorRoom.java, and RobotApp.java. Each of the three challenges will be implemented as a method in class RobotApp. The files Robot and DoorRoom should not be modified. To help you get started an additional demo method is included. It shows you how the robot can be controlled. The demo method should be removed before turning in your assignment code. Use best practices like proper indentation, descriptive names, following Java naming conventions, avoiding code duplication, etc. ( max 5 points )

Challenge 1: ( max 7 points )

Suppose the robot is dropped into a square grid of rooms and starts in the north-east corner of the grid. Come up with an algorithm that the robot can use to figure out how many rooms are in the grid.

  • An algorithm is a deterministic set of steps that do not require any outside information.
  • Remember, your robot doesnt know how many squares there are in advance; if you refer to the size of the grid in your instructions to the robot, it wont work.

Once you have a plan, use it to implement the method challenge1 in RobotApp.

Make sure it works for different size grids. ( 2 out of 7 points ) If you change the size of the grid to 1 or 10 the robot should still return the correct number of rooms.

Minimize the number of moves: ( 1 out of 7 points ) In a 3-by-3 grid, how many times will the robot have to move through a door to run your algorithm? How about a 4-by-4? An n-by-n? Assume that we want to save on robot fuel. Try to make an algorithm that minimizes the moves it takes for the robot to solve the challenge (to figure out the number of rooms in the grid).

One more thing to do: ( 1 out of 7 points ) Once you have an algorithm you think is general (works for all size squares) and efficient (uses few moves), consider through how many doors the robot has to enter in order to figure out the total number of rooms. It should be an expression based on n e.g.for an n-by-n grid the robot moves through 2n+5 doors or something like that. In the end, the robot should say the number of rooms and the expression describing the number of moves. The number of rooms is dynamically calculated with the help of the robot. The expression is something you figured out and part of the hard-coded text. Use the following format: {numberOfRooms} rooms {expression} moves E.g.: 3 rooms 2n+5 moves Notice: neither 3 nor 2n+5 are the right answer but just an example of the expected format

Challenge 2: ( max 7 points )

This time the robot is dropped into a rectangular (not necessarily square) grid of rooms. Again, the robot starts in the north-east corner of the grid and again the robot is tasked to figure out the total number of rooms. Solve the analogous challenges described above but this time for a rectangular grid.

This time implement the method challenge2 in RobotApp.

Make sure it works for different grid sizes. ( 2 out of 7 points ) Minimize the number of moves to save robot fuel: ( 1 out of 7 points ) Expression based on width(w) and height(h): ( 1 out of 7 points ) Come up with an expression that describes the number of moves needed for a w-by-h grid. In the end, the robot should say the number of rooms and the expression based on w and h

Challenge 3: ( max 7 points )

In this third challenge, the robot is dropped into a square grid of rooms. However, this time the robot starts in any arbitrary room in the grid.

Solve the analogous challenges described above by implementing the method challenge3 in RobotApp.

Make sure the robot returned the correct number of rooms regardless of the grid size. ( 2 out of 7 points ) Minimize the number of moves to save robot fuel: ( 1 out of 7 points ) Expression based on width(w) and height(h): ( 1 out of 7 points ) Come up with an expression that describes the number of moves needed for an n-by-n grid. Since this will probably depend on the starting location of the robot, assume the robot starts in the worst possible room (the room that requires the robot to open the most doors) In the end, the robot should say the number of rooms and the expression based on n

Challenge 4: ( max 7 points )

In this third challenge, the robot is dropped into a rectangular (not necessarily square) grid of rooms. However, this time the robot starts in any arbitrary room in the grid.

Solve the analogous challenges described above by implementing the method challenge4 in RobotApp.

Make sure the robot returned the correct number of rooms regardless of the grid size. ( 2 out of 7 points ) Minimize the number of moves to save robot fuel: ( 1 out of 7 points ) Expression based on width(w) and height(h): ( 1 out of 7 points ) Come up with an expression that describes the number of moves needed for a w-by-h grid. Since this will probably depend on the starting location of the robot, assume the robot starts in the worst possible room (the room that requires the robot to open the most doors) In the end, the robot should say the number of rooms and the expression based on w and h

image text in transcribedimage text in transcribedimage text in transcribed

RobotApp solved three challenges where robot needs to figure out the number of rooms in a given grid The code still needs to wor when you change the value of the variable, n, h, and r public class RobotApp public static roid main (String args f // square 4x4 grid to demo how the robotcan be controlled Robot robot F new Robotl4. 4, 3, 0) demo (robot) int n=5; int h = 4; int r 6; // square nxn grid with the robot in the north-east corner robot new Robot (n, n, n-1, 0 challengel (robot) // rectangular grid with width w and height h / robot in the north-east corne:r robot ne Robot (, h,l,0) challenge2 (robot) // square nxn grid // robot's starting position: l over, 2 down robot= new Robot(n, n, 1, 2); challenge3 (robot) // rectangular grid with width w and height h // robot '5 starting position: 3 over, 1 down robot new Robot (m, h, 3. 1) challenge4 (robot) private static void challengel (Robot robot) TODO: Solve challenge1 and update the text below say (3" rooms lexpression of n moves" private static void challenge2 (Robot robot) /TODO: Solve challenge 2 and update text belorw robot.say(3" rooms lexpression of and h moves" private static void challenge 3 Robot robot} {// TODO: Solve challenge 3 and update text below robot.say(+rooms lexpression of n) moves" private static void challenge4 (Robot robot) / TODO: 3olve challenge and update text below robot say (3rooms (expression of w and h moves" // This method briefly demonstrates how the robot can be controlled private static void demo (Robot robot) int x = 3; robot.go' robot.go' if (robot-check (*N' } } robot.go(' robot.go'E // robot.go'E'); // uncomment this line and see what happens robot.sayl x + "xooms 3n5 moves" //eparam sx the starting x position of the robot Oparam sy the starting y position of the robot public Robot(DoorRoom room) {ths(room, o, 0 ) ; //eparam x thestarting position of the robot param sy the starting y position of the robot public Robot (boolean[][] grid, int x, int sy} {this (new DoorRoom grid), sy);} public Robot (boolean grid) ehin (new DoorRoom (grid)) //eparam sx the starting x position of the robot @param sy the starting y position of the robot pubiic Robot (ering grid, int,int sy) (chis (ne DoozRoom (grid),,sy) // param x the arting xponition of the robot Oparam sy the tarting y ponieion of the robot public Robot (String grid) this (new DoorRoom (grid)); //epazamthe starting position of the robot pazam y the starting y position of the zobot public Robot (int w, int h) this (new DoorRoom (,h)) public boolean check (Sering dir return chis.check (dir.charAt (o)) public void go (cha dir} {if (!this . check (dir)) {2tch (dir) { default: throw new IllegalArgumentException ("Unknown direction: dir) 0 default: throw new IllegalArgumentException ("Untnown direction: +dir) public void go (String dir this.go(dir charat(0)) private void sho tehis.oom.dra Bot (ehis. chis-y //Show a message in Eront of the rooms Oparam mag The message to show JLabel 1 = new JLabel (String. valueOf (nsg)) ; JoptionPane.shonessageDialog (this.room.window,, The robot says:". JOptionPane PLAIN MESSAGE) OSuppressarnings (serial private seatic class Hitkall extends RuntimeException DoozRoom.java / Do NOI modity import jara.art.Color import java.awt.RenderingHints mport jara, awt.inage. Buffered.mage; import Jarax.awing. ImageIcon import javax.swing.JPrame; inport jaTAx.Sing.JLabel; publie class DoozRoon f Frame window; BufferedImage content Graphies2D painebrush public static final Color E ne Color (50, ,B0) public static final ColoreColor (120. 0 10) public 5atie final Color new Color (210, 255, 0); public static final Color new Color (255, 220 50) public seatic final Color DooRe Color (o. 19. 191) public stasic tinal int RS9e; boolean) open; public DoosRoom (boolean[][] g id} {this, open = gzid; this, init(); private void iniso this.window new JErameGrid of Doors his .contene = nav Bufferedsaage (this.open-length"RS, this. open [O].1ength.RS, Bufferedimage. TYPE INT ARGE); this-paintbrush (Graphics2D) this.content.getGraphics 0 this.paintbrush.setRenderingHint RenderingHints.KEY ANTIALIA3NG, RenderingHints.VALUE ANTIALIAS_) this .window. sescontent Pane new JLabel (new ImageIcon (this . content) } ); this.window-pack O this.window. setVisible (true) ehin.window.seDeEaulecleseoperasion (JEam.DISPOSE o CLOSE) public DoorRoom (String map) 3eringos map.splien for (String this. open rowrows (cols Math.max (cols, row.lengthO)h now boolean [colm] [zows. length); public DoorRoom(int w, int h) this.opene boolean h this.inito public boolean isDoor line int y char direction) if (x0 11 y . this . open-length 11 y >-this . open [O].length) return alse; if (lopenx Iy)) return false; vitch(direceion) case .w": case .w": x--1; break ; case -e": case .E": x +-1; break ; case .n": case H': y default: throw ne 1legalArgumentException ("Unknown direction: +direction); 1; break ; case .s': case .3., y +-1; break; return open[x]ty]: public void dravBot (double x. double this paintbrush.setColor (Color. BLACK)this paintbrush.fillRect (0. 0. this.content.getWidth this.content.getHeight O) if (thi5.15Door (1, j, e')) {this.paintbrush-setColor (DOOR);this.paintbrush.fillRect (1.RS+RS-R3/8, J.RS+R3/4, R3/8, RS/2); }} chis-paintbrush. fii1Rec R RS/RS RS/R-R3/4,RS-R3/) this-paintbrush.setColer (Coloz HITE) this.paintbrush.filloval ((int) (x R3) +RS/4, int y R3) R/4, RS/2, R3/2); ths paintbrush.setColor (Color.BLACE) this.window.repaint // necessary to see any changes to the screen

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