// Autor: Antonín Daněk import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; import java.net.UnknownHostException; import java.util.Scanner; import javax.swing.JOptionPane; public class client { public static int PORT=3555; public static String IP; String robotsName; String input,fromServer; Socket sck; BufferedReader br; PrintWriter pw; Scanner sc = new Scanner(System.in); // Its used for finding out rotation of Karel Point current=null; int rotation; public client(String ip, String port){ IP=ip; PORT=Integer.valueOf(port); // connects to the server try { sck = new Socket(IP, PORT); } catch (UnknownHostException e) { JOptionPane.showMessageDialog(null,"There is no server running on "+ IP); } catch (IOException e) { JOptionPane.showMessageDialog(null,"There is no server running on "+ IP +" (I/O error)"); } // creates object for data send / recieve try{ br = new BufferedReader(new InputStreamReader(sck.getInputStream())); pw = new PrintWriter(sck.getOutputStream(), true); } catch (IOException ex) { System.err.print("Connection creation failed on port " + PORT); } // waits until server sends legend and the name of robot try{ while(true){ if((robotsName = br.readLine()) != null){ System.out.println(robotsName); robotsName = parseName(robotsName); if(robotsName.equals("")) continue; else break; }}} catch (IOException e) { e.printStackTrace(); } System.out.println(); System.out.println("Robots name is: '" + robotsName+"'"); System.out.println(); int state; while(true){ System.out.print(robotsName +" "); //loads order from the command line input = sc.nextLine(); //if user insert command NAJDI (which is not included in SERVERs protocol) //program automatically finds the secret if(input.equals("NAJDI")) { getRotation(); find(); System.out.println(fromServer); //If one of these codes come, program ends, robot found secret or its destroied state = getNumberFromMessage(fromServer); if(state==221 || state==530 || state==550 || state==571 || state==572){ end(); break; } end(); break; } // If user doesn't use NAJDI for autosearch, user can send standard commands else { pw.print(robotsName + " " + input + "\r\n"); pw.flush(); // waits for data from server and save it into member fromServer waitForData(); } System.out.println(fromServer); //If one of these codes come, program ends, robot found secret or its destroied state = getNumberFromMessage(fromServer); if(state==221 || state==530 || state==550 || state==571 || state==572){ end(); break; } } } //used for autosearch, it moves Karel to coordinates (0,0) void find(){ System.out.println("rotace:" + rotation); //turns Karel to correct way on axe x if(current.x>0){turnToTheLeft();} else if(current.x<0){turnToTheRight();} //make steps until x coordinate is 0 while(current.x!=0){ pw.print(robotsName + " KROK" + "\r\n"); System.out.println((robotsName + " KROK")); pw.flush(); // waits for data from server and save it into member fromServer waitForData(); System.out.println(fromServer); if(getNumberFromMessage(fromServer)==250){ current=getCoordinatesFromMessage(fromServer); } // if robot is broken, repair it else if(getNumberFromMessage(fromServer)==570) { repairRobotFromMessage(fromServer); } //this should never happend else System.out.println("Algorithm error (" + fromServer + ")"); } //turns Karel to correct way on axe y if(current.y<0){turnToTheUp();} else if(current.y>0){turnToTheDown();} //make steps until y coordinate is 0 while(current.y!=0){ pw.print(robotsName + " KROK" + "\r\n"); System.out.println(robotsName + " KROK"); pw.flush(); // waits for data from server and save it into member fromServer waitForData(); System.out.println(fromServer); if(getNumberFromMessage(fromServer)==250){ current=getCoordinatesFromMessage(fromServer); } // if robot is broken, repair it else if(getNumberFromMessage(fromServer)==570) { repairRobotFromMessage(fromServer); } //this should never happend else System.out.println("Algorithm error (" + fromServer + ")"); } // At this point, robot is on coordinates (0,0) so it can read the secret pw.print(robotsName + " ZVEDNI" + "\r\n"); System.out.println(robotsName + " ZVEDNI"); pw.flush(); waitForData(); } // Turns robot to the left (from outside it is independentl on current rotation) void turnToTheLeft(){ if(rotation==0){ pw.print(robotsName + " VLEVO" + "\r\n");System.out.println(robotsName + " VLEVO"); pw.flush(); waitForData();System.out.println(fromServer); } if(rotation==1){ pw.print(robotsName + " VLEVO" + "\r\n");System.out.println(robotsName + " VLEVO"); pw.flush(); waitForData();System.out.println(fromServer); pw.print(robotsName + " VLEVO" + "\r\n");System.out.println(robotsName + " VLEVO"); pw.flush(); waitForData();System.out.println(fromServer);} if(rotation==2){ pw.print(robotsName + " VLEVO" + "\r\n");System.out.println(robotsName + " VLEVO"); pw.flush(); waitForData();System.out.println(fromServer); pw.print(robotsName + " VLEVO" + "\r\n");System.out.println(robotsName + " VLEVO"); pw.flush(); waitForData();System.out.println(fromServer); pw.print(robotsName + " VLEVO" + "\r\n");System.out.println(robotsName + " VLEVO"); pw.flush(); waitForData();System.out.println(fromServer);} if(rotation==3){ } rotation=3; System.out.println("I am turned LEFT now."); } // Turns robot to the right (from outside it is independentl on current rotation) void turnToTheRight(){ if(rotation==0){ pw.print(robotsName + " VLEVO" + "\r\n");System.out.println(robotsName + " VLEVO"); pw.flush(); waitForData();System.out.println(fromServer); pw.print(robotsName + " VLEVO" + "\r\n");System.out.println(robotsName + " VLEVO"); pw.flush(); waitForData();System.out.println(fromServer); pw.print(robotsName + " VLEVO" + "\r\n");System.out.println(robotsName + " VLEVO"); pw.flush(); waitForData();System.out.println(fromServer);} if(rotation==1){ } if(rotation==2){ pw.print(robotsName + " VLEVO" + "\r\n");System.out.println(robotsName + " VLEVO"); pw.flush(); waitForData();System.out.println(fromServer); } if(rotation==3){ pw.print(robotsName + " VLEVO" + "\r\n");System.out.println(robotsName + " VLEVO"); pw.flush(); waitForData();System.out.println(fromServer); pw.print(robotsName + " VLEVO" + "\r\n");System.out.println(robotsName + " VLEVO"); pw.flush(); waitForData();System.out.println(fromServer); } rotation=1; System.out.println("I am turned RIGHT now."); } // Turns robot up (from outside it is independentl on current rotation) void turnToTheUp(){ if(rotation==0){ } if(rotation==1){ pw.print(robotsName + " VLEVO" + "\r\n");System.out.println(robotsName + " VLEVO"); pw.flush(); waitForData();System.out.println(fromServer); } if(rotation==2){ pw.print(robotsName + " VLEVO" + "\r\n");System.out.println(robotsName + " VLEVO"); pw.flush(); waitForData();System.out.println(fromServer); pw.print(robotsName + " VLEVO" + "\r\n");System.out.println(robotsName + " VLEVO"); pw.flush(); waitForData();System.out.println(fromServer); } if(rotation==3){ pw.print(robotsName + " VLEVO" + "\r\n");System.out.println(robotsName + " VLEVO"); pw.flush(); waitForData();System.out.println(fromServer); pw.print(robotsName + " VLEVO" + "\r\n");System.out.println(robotsName + " VLEVO"); pw.flush(); waitForData();System.out.println(fromServer); pw.print(robotsName + " VLEVO" + "\r\n");System.out.println(robotsName + " VLEVO"); pw.flush(); waitForData();System.out.println(fromServer); } rotation=0; System.out.println("I am turned UP now."); } // Turns robot down (from outside it is independentl on current rotation) void turnToTheDown(){ if(rotation==0){ pw.print(robotsName + " VLEVO" + "\r\n");System.out.println(robotsName + " VLEVO"); pw.flush(); waitForData();System.out.println(fromServer); pw.print(robotsName + " VLEVO" + "\r\n");System.out.println(robotsName + " VLEVO"); pw.flush(); waitForData();System.out.println(fromServer); } if(rotation==1){ pw.print(robotsName + " VLEVO" + "\r\n");System.out.println(robotsName + " VLEVO"); pw.flush(); waitForData();System.out.println(fromServer); pw.print(robotsName + " VLEVO" + "\r\n");System.out.println(robotsName + " VLEVO"); pw.flush(); waitForData();System.out.println(fromServer); pw.print(robotsName + " VLEVO" + "\r\n");System.out.println(robotsName + " VLEVO"); pw.flush(); waitForData();System.out.println(fromServer); } if(rotation==2){ } if(rotation==3){ pw.print(robotsName + " VLEVO" + "\r\n");System.out.println(robotsName + " VLEVO"); pw.flush(); waitForData();System.out.println(fromServer); } System.out.println("I am turned DOWN now."); rotation=2; } // it makes one turn and one step to find out current rotation of Karel // 0-up;1-right;2-down;3-left void getRotation(){ Point last=null; //get coorinates of start position while(last==null) { pw.print(robotsName + " VLEVO" + "\r\n"); pw.flush(); System.out.println(robotsName + " VLEVO"); // waits for data from server and save it into member fromServer waitForData(); System.out.println(fromServer); if(getNumberFromMessage(fromServer)==250){ last=getCoordinatesFromMessage(fromServer); } //this should never happend else System.out.println("Algorithm error (" + fromServer + ")"); } //get coordinates of position after one step while(current==null) { pw.print(robotsName + " KROK" + "\r\n"); pw.flush(); System.out.println(robotsName + " KROK"); // waits for data from server and save it into member fromServer waitForData(); System.out.println(fromServer); if(getNumberFromMessage(fromServer)==250){ current=getCoordinatesFromMessage(fromServer); } // if robot is broken, repair it else if(getNumberFromMessage(fromServer)==570) { repairRobotFromMessage(fromServer); } //this should never happend else System.out.println("Algorithm error (" + fromServer + ")"); } //finding rotation from two obtained coordinates if(last.x==current.x) { if(last.y "); else new client(args[0],args[1]); //new client("dsnlab1.felk.cvut.cz"); //new client("localhost"); } } //represents coordinates class Point{ int x; int y; public Point(int xx,int yy){ this.x=xx; this.y=yy; } boolean equal(Point b){ if((this.x==b.x) && (this.y==b.y)) return true; else return false; } }