CS 161 Homework 4 - Karol the Robot

Due Wed Feb 25 Thu Feb 26 at 11:59pm

Overview

In this assignment, you will be programming a simple "robot" named Karol with a mission to completely explore its environment--no matter what the environment looks like. The challenge is that Karol is very near-sighted, so you will only be able to use local sensing to determine what is nearby. You will be implementing methods that allow Karol to decide how to walk through environments based on this sensing.

The programming in this homework will give you a chance to practice working with if statements and booleans. The more difficult part of the assignment though involves practicing thinking through a logical problem like a computer scientist: how can you come up with a specific set of decisions that will work in a more general case (e.g., perform abstraction)? You'll need to come up with the logical solution for how to move the robot, and then convert that logic into the formal language of code.

This assignment should be completed individually. I encourage you to try and think through the logical problems on your own (it's good practice for future assignments and exams), but you are welcome to discuss algorithmic ideas with others--just remember the Gilligan's Island Rule.

Objectives

Necessary Files

You will need a copy of the Hwk4.zip file. The BlueJ project in this file includes two classes for you:

The project also contains the README.txt that you will need to fill out and turn in along with your assignment.

Remember to extract the files from the zip folder before proceeding!

Background: Karol

It may not look like much, but Karol (see below) is a robot. Karol's job is to search every inch of its environment--the exact same way a Roomba tries to automatically vacuum the entire floor.

Karol is the green square. The walls of the room are blue; and the empty area (the floor) is white. Each time Karol takes a step, it leaves a gray trail behind it. Karol stops automatically when it has completely explored the room, content in a job well done. Note that Karol starts at a random location in the room---you can't assume anything about the initial location!

Sensing Surroundings

Karol's Memory (State)

Moving Around

Online Version

Assignment Details

Your task is to complete the provided Karol class so that the robot is able to successfully navigate the "empty room" (map 0), the "maze" (map 1), and the "diamond" (map 2):

Remember that Karol will always begin in a random location, and your solution will need to account for that!

  1. The first thing you should notice is that the provided Karol class is completely without comments! You should remedy this shortcoming.
    1. Add in complete and properly formatted JavaDoc comments for the class comment and for each method, including the constructor. JavaDoc format is described on page 90 of the textbook.
    2. Also add inline comments for the provided code (e.g., the instance variables and the constructor). Remember that comments should explain the why of the code, not the what. Try to explain what the code is doing at a higher level of abstraction!
  2. You'll need to implement the stepEmptyMap() method so that Karol is able to navigate the empty map (map 0). To get you started, some hints are below:
    1. Start by just trying to make Karol move at all each step. You should be able to just change the returned value (use one of the provided constants).
    2. Next, can you add in logic (e.g., using if statements and the values of the state and the surroundings) to make Karol fill in a single grey line from top to bottom? You'll want to go all the way to the North, and then turn around and go all the way to the South.
      • Consider using two different state values: one for if you are going north, and one for if you are going south. You'll need to have your algorithm make a decision (e.g., with an if statement) about whether you should change state. You'll want to return one value if you want to go north, and a different value if you want to go south!
      • Helpful hint: Try printing out the state variable at different points in your method, and then use the provided "step" button to have Karol take a single step. This can help you trace what is going on!
    3. Finish up by adding further "rules" so that Karol can cover the entire map. Again, think about what states or "modes" Karol should have, what you should do in those states and with different surroundings, and when you want to change states.
    4. Be sure and test your method with different starting points for Karol (hit the "Reset" button to try again).
    5. Include a lot of comments explaining your logic. Why are you moving to each state? What are the "rules" you are following? A good rule of thumb would be to include a comment for each if statement explaining what condition that represents.
  3. You'll need to implement the stepMazeMap() method so that Karol is able to navigate the maze map (map 1). Hint: think about using a wall-following process, such as where you always turn to the right to explore the maze.
    • Think about "exploring" passages in a spiral manner--always going to the "right" first. That is, if Karol is "facing" north, it would like to explore "east" passages; if it's facing east, it would like to explore south passages, and so forth.
    • The trick is: you don't want to go as far in one direction as possible (you might miss a branching path!), but to always take the left-hand turn if you can. Use the state to represent Karol's current "facing" (e.g., 0 is north, 1 is east, etc), and think about taking the "left" path (relative to that facing) each time if you can. If you can't, go forward; if you can't, go right; if you can't, go back.
    • The maze is one of those pieces that you need to have most of the parts implemented in order to feel like you're making progress :(
  4. You'll need to implement the stepDiamondMap() method so that Karol is able to navigate the diamond map (map 2). Hint: can you use a similar process to clearing the empty map?
  5. You'll need to implement the stepStalactiteMap() method so that Karol is able to navigate the stalactite map (map 3). Note that this is a much harder map the than previous three!
    • Hint: you might try thinking about alternating between "sweeping" over the whole map from north to south, and "sweeping" over the whole map from east to west, so that you can get into each nook and cranny!
  6. And that's it! I have provided additional maps if you want the challenge, but solving them is not required. You can complete the stalactite map for extra credit.

Extensions

Submitting Your Assignment

There are a couple of details to keep in mind before you submit your assignment.

  1. Double-check that your methods are completed and work correctly.
    • If your program doesn't compile, I can't give you credit for it!
  2. Make sure that your code is commented. Give lots of inline comments: I should be able to understand your robot's logic just from the comments, without really reading the code itself!
  3. You will also need to fill out the README.txt file. You can open this file in BlueJ by double-clicking on the white paper icon in the upper left corner of the class window. You should place the answer to each question below the question box, replacing the "<Replace this with your response!>". Remember to save your changes (select "Class > Save" from the menu).

  4. You should compress the entire Hwk4 folder into a single zip file (like you did for Lab A), and then submit it to the Hwk4 Submission page on Moodle.
    • Be sure an upload the ENTIRE project folder--that is what includes all your work!
  5. This assignment is due at midnight on Wed, Feb 25 Thu, Feb 26. Important note: we have an exam the next day, so getting this done earlier will make your life less stressful!

Grading

This assignment will be graded out of 40 points (again, same worth as other homeworks, just higher precision). Map explorations are worth multiple points: you will earn partial credit depending on how much of the map you can explore!