A. Introduction
At the beginning of this course, we discussed about the importance of developing an algorithm in pseudo code before writing code. We reiterate the same point when discussing multiple phases of programming. Now it is a good time to review this process and keep in mind: programming is a multi-step process.
Have you worked on the pseudo code of this robot problem before? Can you turn the pseudo code into Java now?
B. Some Assumptions:
Let's assume the room size is 20x20 units, surrounded by four walls labeled #1-4. Use a random number to generate 1, 2 or 3 windows to install on the walls. All windows will have a width of 2 units, and at most 1 window can be installed on each wall. No window will wrap around a corner.
Write a program to count the number of windows. To start off, your program will first set up the windows, then ask the user " Where is the robot?". Instruct the user to enter 2 integers between 1 and 20 to tell the robot's location.
C. Your output should print:
The numbers of windows the robot found in the room.
The location of those windows.
import java.util.Scanner;
class beepboop {
public static void main(String[] args) {
int WindowCount = 0;
int Windows = (int) Math.ceil(Math.random()*3);
int[] WindowWalls = new int[4]; //wall 1 is bottom, wall 2 is right, wall 3 is top, and wall 4 is left
int count = 0;
while(Windows != count){
WindowWalls[(int) Math.floor(Math.random()*4)]++;
count = 0;
for(int i=0; i<4; i++){
if(WindowWalls[i]>0){
count++;
}
}
}
int[] WindowPosition = new int[4];
for(int i=0; i<4; i++){
if(WindowWalls[i]>0){
WindowPosition[i] = (int) Math.ceil(Math.random()*19); //indicates the bottom of the window or the left of the window
}
}
Scanner Andrew = new Scanner(System.in);
System.out.println("What is the robot's x coordinate? (must be between 1 and 20 (inclusive))");
int x = Andrew.nextInt();
if(x>20 || x<1){
System.out.println("WHAT HAVE YOU DONE?? YOU PUT THE ROBOT OUTSIDE ITS CAGE. NOW IT HAS ESCAPED. WE'RE DOOMED.");
System.exit(0);
}
System.out.println("What is the robot's y coordinate? (must be between 1 and 20 (inclusive))");
int y = Andrew.nextInt();
if(y>20 || y<1){
System.out.println("WHAT HAVE YOU DONE?? YOU PUT THE ROBOT OUTSIDE ITS CAGE. NOW IT HAS ESCAPED. WE'RE DOOMED.");
System.exit(0);
}
int[] Position = {x,y};
for(; Position[0]>1; Position[0]--){}
for(; Position[1]>1; Position[1]--){}
for(; Position[0]<20; Position[0]++){
if(WindowPosition[0] == Position[0]){
WindowCount++;
System.out.println("There is a window from (" + Position[0] + ",1) to (" + (Position[0]+1) + ",1)");
}
}
for(; Position[1]<20; Position[1]++){
if(WindowPosition[1] == Position[1]){
WindowCount++;