AnInterface Class

package uk.ac.reading.cs2ja16.rdrohan; import java.util.Scanner; public class AnInterface { AWorld myWorld = new AWorld(); AnEntity entityObj = new AnEntity(); char[] displayChars; int currRow; public AnInterface() { /* * constructor for RobotInterface * sets up scanner used for input and the arena * then has main loop allowing user to enter commands */ Scanner s = new Scanner(System.in); // set up scanner myWorld = new AWorld(10, 10, 5); // create arena of size 20*6 with 5 robots char ch = ' '; do { System.out.print("Enter (A)dd robot, (D)isplay or e(X)it > "); ch = s.next().charAt(0); s.nextLine(); switch (ch) { case 'A' : case 'a' : myWorld.addEntity(); // add a new robot to arena break; case 'D' : case 'd' : doDisplay(); // display current arena break; case 'x' : ch = 'X'; // when X detected program ends break; } } while (ch != 'X'); // test if end s.close(); // close scanner } void outChars (char pchar) { /* * function to output the displayChars array surrounded by pchar */ System.out.println(pchar + new String(displayChars) + pchar); } void padChars(char pchar, int csize) { /* * function to fill displayChars array with character pchar */ for (int ct = 0; ct< csize; ct++) displayChars[ct] = pchar; } void showRobot(int x, int y) { /* * function to show robot at position x,y * It puts 'R' into x'th element in displayChars, if y is in the current row */ if (y==currRow) displayChars[x]= entityObj.getSymbol();//'E'; } void doDisplay() { /** * display the world on the console * Have border round world; Then display position of all robots */ int ax = myWorld.getxSize(); // find size of the arena int ay = myWorld.getySize(); displayChars = new char[ax]; // set size of array of chars used to display one row padChars('-', ax); // fill array with - outChars(' '); // then output it for (currRow = 0; currRow<ay; currRow++) { // for all rows in world + row before/after // note use of member integer currRow padChars(' ', ax); // fill array with spaces myWorld.showRobots(this); // search arena and add all robots in this row to the array outChars('|'); // output array } padChars('-', ax); // fill array with - outChars(' '); // and output } public static void main(String[] args) { // TODO Auto-generated method stub //AnInterface Interface; AnInterface r = new AnInterface(); // just call the interface } }
Class AnInterface

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.