A.I

Run Settings
LanguageJava
Language Version
Run Command
import java.util.*; import java.io.*; class Main { public static void main(String[] args) { printWorld(); } public static String[][] buildWorld(){ //the function used to read the robotdata.txt file String [][] world = new String[8][10]; //store row/column values in the world[][] array try{ File f = new File("robotdata.txt"); Scanner s = new Scanner(f); for(int row=0; row<8; row++){ //nested for loop to read the file in 8 rows and 10 columns for(int col=0; col<10; col++){ world[row][col]=s.next(); } } } catch(FileNotFoundException f){ System.out.println("No Files Exist!"); }catch(NoSuchElementException e){ System.out.println("No Elements Exist!"); } return world; //return the array world[][] for later usage } public static void printWorld(){//this function will print out the world with the path String[][] world = buildWorld();//use the function buildWorld() for the array world[][] String[][] robotPos = new String [8][10];//store robot position in the robotPos[][] array int x =0; int y =0; int moves =0; System.out.println("==========PRINTING WORLD=========="); try{ for(int row=0; row<8; row++){ for(int col=0; col<10; col++){ if(world[row][col].equals("A") || world[row][col].equals("B")){// make the computer detect the letter A and B System.out.print("[" + world[row][col]+"]");//highlight robot position robotPos[row][col] = world[row][col]; x =row+1; y =col+1; System.out.print(" Robot position is: Row "+ (x)+" Column "+(y)); }else if(world[row][col].equals("X")){// detect the X letter moves++;//moves counter if(world[row][col-1].equals("X")){//horizontal moves System.out.print(" H "); }else{//vertical moves System.out.print(" V "); } }else{ System.out.print(" ");//the O change to spaces } } System.out.println();//print next line } System.out.println("The robot has moved: "+moves+" moves"); } catch(ArrayIndexOutOfBoundsException a){ System.out.println("\ncomplete."); } } }
O O O O O A O O O O O O O O O X X O O O O O O O O O X O O O O O O O X X X O O O O O O O X O O O O O O O O O X X X X O O O O O O O O O X O O O O O O O O O B O O
Editor Settings
Theme
Key bindings
Full width
Lines