mardi 28 juillet 2015

Java Scanner command System

I have to create a java class where I can read some commands from standard console. It's like to simulate a movement into a grid. I have some difficult to create exactly what I want. Let's say that I have this commands:

  • START X,Y,DIRECTION
  • STEP

"X" and "Y" are coordinates of a matrix 6x6. "DIRECTION" can be "UP","DOWN","LEFT","RIGHT". If I write "STEP" I'll do one step.

The program should discard STEP command until a valid START command has been executed. After that, I can use STEP or another valid START command that it will put "1" using the new coordinates removing the first one.

Examples:

a)START 1,4,UP ---> OK! ANOTHER START OR STEP COMMAND
  STEP ---> OK MOVE!

b)START 3,5,UP ---> OK! ANOTHER START OR STEP COMMAND
  START 5,2,LEFT ---> DONE! OK NEW POSITION!
  STEP ---> OK MOVE!

c)STEP ---> NO! I NEED START
  OWINEVEIVNEW ---> NO! I NEED START
  START 3,2,RIGHT ---> OK! ANOTHER START OR STEP COMMAND

I have to catch also coordinates (X,Y) and DIRECTION if I have a START command.

My idea is:

public static void main(String[] args) {
    Movement grid = new Movement(6); --> call constructor. 6 is dimension square grid
    System.out.println("**********INSERT COMMANDS**********");
    while(true){ --> loop to continue to insert commands
        if (grid.startCommand()) {
           System.out.println("OK START RECEIVED! I CAN USE STEP");
           grid.stepForward();
        } else {
           System.out.println("I can't go ahead without valid START command. Try again!");
        };
    }   
}

public boolean stepForward() {
    if (start.equals("STEP") {
        System.out.println("OK LET'S DO A STEP!!");
    }
    return true;
}

public boolean startCommand() {
    Scanner sc = new Scanner(System.in);
    String start = sc.next();
    sc.useDelimiter("\\s+");
    String[] coordinates = sc.next().split(",");
    X = Integer.parseInt(coordinates[0]);
    Y = Integer.parseInt(coordinates[1]);
    direction = coordinates[2];
    while(start.equals("START")) {
        if ((0<=X && X<dim) && (0<=Y && Y<dim)){
            if (direction.equals("UP")||direction.equals("DOWN")||direction.equals("LEFT")||direction.equals("RIGHT")){
                cleanGrid(); --> this is just a method that put everything at 0
                matrix[X][Y] = 1;
                return true;
            } else {
                System.out.println("Your direction doesn't exist. Use \"UP\",\"DOWN\",\"LEFT\",\"RIGHT\".Try again!");
                return false;
            }
        } else {
            System.out.println("Check range of X and Y. Try again!");
            return false;
        }
    } 
    System.out.println("Insert command like ex. \"START 1,2,UP\". Try again!");
    return false;
}

Yes, I'm lost between IF,WHILE, etc... I tried different solutions but or I lose the possibility to insert another START or I can't recognize STEP command or other kind of problems. Can someone help me to figure out with this please?? Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire