I need help for making readworld and comment. import java.util.ArrayList; import
ID: 3604942 • Letter: I
Question
I need help for making readworld and comment.
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class SimpleBotController {
public void readWorld(Scanner in) {
List> charArrList = new ArrayList<>();
int lineLength = 0;
int countB = 0;
int countW = 0;
int countT = 0;
boolean firstLine = true;
while (in.hasNextLine()) { // check for next line
String text = in.nextLine(); // read next line
char[] arr = text.toCharArray();
List carr = new ArrayList<>();
int loopCountR = 0;
int loopCountB = 0;
int loopCountW = 0;
int loopCountT = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] == 'r' || arr[i] == 'b' || arr[i] == 'w' || arr[i] == 't' || arr[i] == ' ' || arr[i] == ' ') {
if (arr[i] == 'r' && loopCountR == 1) {
throw new IllegalArgumentException("Found more than 1 r");
}
if (arr[i] != ' ' && arr[i] != ' ') {
if (arr[i] == 'b') loopCountB++;
if (arr[i] == 'w') loopCountW++;
if (arr[i] == 't') loopCountT++;
if (arr[i] == 'r') loopCountR++;
carr.add(arr[i]);
}
}
else {
throw new IllegalArgumentException("Char other than r, b, w, t found");
}
}
charArrList.add(carr);
if (!firstLine && (countB != loopCountB || countW != loopCountW || countT != loopCountT)) {
throw new IllegalArgumentException("Different length line found");
}
else {
firstLine = false;
countB = loopCountB;
countW = loopCountW;
countT = loopCountT;
}
}
int size = charArrList.size();
lineLength = 1 + countB + countT + countW;
char[][] array = new char[size][lineLength];
for (int i = 0; i < size; i++) {
List temp = charArrList.get(i);
for (int j = 0; j < lineLength; j++) {
array[i][j] = temp.get(j);
}
}
}
public void readProgram(Scanner in) {
while (in.hasNextLine()) {
String statement = in.nextLine();
int index = statement.indexOf('#');
if (index != -1)
statement = statement.substring(0, index);
statement = statement.trim();
if (!statement.isEmpty()) {
try {
SimpleBotStatement sb = new SimpleBotStatement(statement);
}
catch(IllegalArgumentException e) {
System.out.println("IllegalArgumentException is thrown as a program is illegally formatted");
throw e;
}
}
}
}
public void start() {
if(world==null||program==null){
throw new IllegalStatement("must load legal world and state");}
SimpleBotModel sbm= new SimpleBotModel(world,program);
}
-----------------------------------------------------------------------------
public class SimpleBotStatement {
private int state;
private String sensors;
private int nextState;
private char action;
public SimpleBotStatement(String s)
{
String[] tokens = s.split("(\s)+"); //split tokens using 1 or more whitespaces
try{
state = Integer.parseInt(tokens[0]);
sensors = tokens[1];
checkSensors(); //call the private method to check if the trigger sensors are valid
if(!tokens[2].equals("->"))
throw new IllegalArgumentException("Expected -> after trigger sensors . Found " + tokens[2]);
//next token should be single char action, check if its valid value
String validAction = "newsx";
if(tokens[3].length() != 1 || validAction.indexOf(tokens[3]) == -1 )
throw new IllegalArgumentException("Action should be a single character and one of n/e/w/s/x");
action = tokens[3].charAt(0);
nextState = Integer.parseInt(tokens[4]);
}
catch(Exception e)
{
throw new IllegalArgumentException(e.getMessage());
}
}
public boolean match(int state, boolean n, boolean e, boolean w, boolean s)
{
if(this.state != state)
return false;
char ch = sensors.charAt(0);
if((n == true && ch == 'x' ) || (n == false && ch =='n'))
return false;
ch = sensors.charAt(1);
if((e == true && ch == 'x' ) || (e == false && ch =='e'))
return false;
ch = sensors.charAt(2);
if((w == true && ch == 'x' ) || (w == false && ch =='w'))
return false;
ch = sensors.charAt(3);
if((n == true && ch == 'x' ) || (s == false && ch =='s'))
return false;
return true;
}
public int nextState()
{
return nextState;
}
public char nextAction()
{
return action;
}
private void checkSensors()
{
boolean valid = true;
String err = "";
if(sensors.length() == 4)
{
String[] allowed = {"nx*", "ex*", "wx*", "sx*"}; //allowed characters for each of character positions
//if position is 0, then only n, x * are allowed
//if position is 1, then only e, x and * are allowed and so on
//now check if each character has valid character according to its position
for(int pos = 0; valid && pos < sensors.length(); pos++)
{
char ch = sensors.charAt(pos);
if(allowed[pos].indexOf(ch) == -1)
{
err = "Trigger Sensors invalid. Expected one of the characters in " + allowed[pos]+" at index "
+ pos + ". Found "+ ch;
valid = false;
}
}
}
else
{
err = "Trigger sensors be only 4 characters long";
valid = false;
}
if(!valid)
throw new IllegalArgumentException(err);
}
}
-------------------------------------------------------------------
import java.util.Scanner;
import java.util.Arrays;
public class SimpleBotControllerTester {
public static void main(String[] args) {
boolean pass = true;
SimpleBotController c = new SimpleBotController();
try { c.start(); pass = false; }
catch(IllegalStateException e) { }
catch(Exception e) { pass = false; }
try { c.readWorld(null); pass = false; }
catch(IllegalArgumentException e) { }
catch(Exception e) { pass = false; }
try { c.readWorld(new Scanner("bb wt ")); pass = false; }
catch(IllegalArgumentException e) { }
catch(Exception e) { pass = false; }
try { c.readWorld(new Scanner("rb wr ")); pass = false; }
catch(IllegalArgumentException e) { }
catch(Exception e) { pass = false; }
try { c.readWorld(new Scanner("rb wx ")); pass = false; }
catch(IllegalArgumentException e) { }
catch(Exception e) { pass = false; }
try { c.readWorld(new Scanner("rb www ")); pass = false; }
catch(IllegalArgumentException e) { }
catch(Exception e) { pass = false; }
c.readWorld(new Scanner(" t b w t w r "));
c.readProgram(new Scanner("# 0 **** -> x 0 0 **** -> x 0 # Hello # Hello"));
c.start();
pass = pass && Arrays.deepToString(SimpleBotModel.world).equals("[[t, b], [w, t], [w, r]]");
pass = pass && SimpleBotModel.program.length==2;
try { c.readProgram(new Scanner("dkfjhsdkjfh")); pass = false; }
catch(IllegalArgumentException e) { }
catch(Exception e) { pass = false; }
System.out.println(pass);
}
}
-------------------------------------------------------------
public class SimpleBotModel {
public static char[][] world;
public static SimpleBotStatement[] program;
public SimpleBotModel(char[][] world, SimpleBotStatement[] program) {
SimpleBotModel.world = world;
SimpleBotModel.program = program;
}
}
more information is here
http://krovetz.net/20/project2/SimpleBotController.html
Explanation / Answer
Below is the updated code of "readWorld" method:
public void readWorld(Scanner in) {
if (!in.hasNextLine()) {
throw new IllegalArgumentException("There is no line to read");
} else {
int countB, countW, countT;
String text = in.nextLine();
for (int i = 0; i < text.length(); i++) {
int loopCountR, loopCountB, loopCountW, loopCountT = 0;
char c = text.charAt(i);
switch (c) {
case 'r':
loopCountR++;
break;
case 'b':
loopCountB++;
break;
case 'w':
loopCountW++;
break;
case 't':
loopCountT++;
break;
case ' ':
case ' ':
break;
default:
throw new IllegalArgumentException("Char other than r, b, w, t found");
}
if (loopCountR == 2) {
throw new IllegalArgumentException("Found more than 1 r");
}
if (i == 0) {
countB = loopCountB;
countT = loopCountT;
countW = loopCountW;
if (countB == 0 && countT == 0 && countW == 0) {
throw new IllegalArgumentException("There should be atleast one row and one column");
}
} else if ((countB != loopCountB) || (countT != loopCountT) || (countW != loopCountW)) {
throw new IllegalArgumentException("Different length line found");
}
}
}
}
Please like the answer and test with this code and let me know if you need help with your doubts.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.