Question: So, We need to turn in two classes for this one. A BotController and BotStatement. BotController will take input that the user supplies, validate it,
So, We need to turn in two classes for this one. A BotController and BotStatement. BotController will take input that the user supplies, validate it, and then give the model object what it needs to do its work. What the model needs is a description of the robot's world and a list of the program statements that the robot should follow.
BotController
Your controller will have the ability to read a new description of the robot's world, the ability to read in a new program, and the ability to start a simulation. Write a class called BotController with the following public methods (it should have no other public methods except a constructor if you want it).
public void readWorld(Scanner in)
readWorld should read lines from the Scanner until there are no more lines to read. Each line will represent a row in the robot's world. The line is allowed to have whitespace characters (spaces and tabs) and the four characters (r, w, b, t). The total number of (r, w, b, t) characters found should be the same on each line, and this number indicates the number of columns in the robot's world. The input lines should have at least one row and at least one column. There should be exactly one r. If any of these specifications are violated or in is null an IllegalArgumentException should be thrown.
- In this method, we read a file which has the chars for making a 2D array and store each line into a String variable. Process that string and make a 2D array from it, for as long as it has the character r,w,b,t. Basically, we need to do an error checking afterwards.
File would look like this: wwwt We also need to assume that we don't know how many lines there would be.
wbwr
public void readProgram(Scanner in)
readProgram should read lines from the Scanner until there are no more lines to read. For each line, you should first remove any comment and then any whitespace from the beginning and end of the de-commented string. (A comment begins with the first '#' and goes to the end of the line.) If the resulting string is empty the line can be ignored. Otherwise, the modified line should be used to construct a new BotStatement object. If the modified line is an illegal program statement, the construction of the BotStatement will fail with an IllegalArgumentException. Your readProgram method should not catch this exception and instead should allow it to continue to the caller of readProgram. You should document that an IllegalArgumentException is thrown if a program is illegally formatted.
Note that readWorld and readProgram can each be called more than once, but it is always the most recent call to either of these methods that defines what the robot's world and program.
public void start()
When start is called, a BotModel object should be created and the most recently read world and program should be passed to its constructor. If not both a world and program have been read, an IllegalStateException should be thrown.
The world should be represented as a two-dimensional array of char, following the same conventions used in Project 1 to represent the robot's world. The program should be represented as an array of BotStatement with the first element of the array being the first statement of the program and the last element of the array being the last statement of the program.
BotStatement
Each BotStatement represents a single Bot program statement (eg, 0 x*** -> n 0). Write a class called BotStatement with the following public methods (it should have no other public methods).
public BotStatement(String s)
If you consider each grouping of non-whitespace characters in the string to be "tokens", then the sequence of tokens in the string should be: an integer (the trigger state), a four-character string (the trigger sensors), the string "->" (unused), a single character (the action), and an integer (the new state). The four-character token should have the first character one of n/x/*, the second one of e/x/*, the third one of w/x/*, and the fourth one of s/x/*. The single action character should be one of n/e/w/s/x. Any amount of whitespace is allowed before, between and after each token, but these should be the only tokens in the string and they should occur in this order. If any of these expectations are broken, an IllegalArgumentException should be thrown.
public boolean match(int state, boolean n, boolean e, boolean w, boolean s)
This method will be used to match a robot's current state and sensor readings against a program statement to see if they are a match. The parameters n/e/w/s will each be true if there is a wall in the immediately adjacent square. If state does not match "the trigger state" return false. If n is true and the first character of the trigger sensors is x, or n is false and the first character of the trigger sensors is n, return false. The remaining parameters should behave similarly to n except with the second, third and fourth characters of the trigger sensors. If none of these tests causes the method to return false, then true should be returned.
public int nextState()
This method should return the new state value found by the constructor.
public char nextAction()
This method should return the action value found by the constructor.
You will write a complete BotModel class in a later project, but you will need a BotModel class in this project to allow your BotController to create a BotModel object. Use the following class to allow compilation (but don't turn it in).
public class BotModel {
public BotModel(char[][] world, BotStatement[] program) {
}
}
Any hints or help is much appreciated. Thank you.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
