package org.amc.game.chess;

import org.amc.game.chess.ChessBoard.Coordinate;
import org.amc.game.chess.controller.InputParser;

import java.text.ParseException;

/**
 * Parses text in the form 'H1A2' to create a Move object
 * 
 * @author Adrian Mclaughlin
 *
 */
public class SimpleInputParser implements InputParser {

    private static final String errorMessage = "Move must be enter like so: A1B2";
    private static final int INPUT_LENGTH = 4;

    /**
     * @see InputParser#parseMoveString(String)
     */
    @Override
    public Move parseMoveString(String moveString) throws ParseException {
        if (inputCorrectLength(moveString)) {
            String startCoordinate = moveString.substring(0, 2);
            String endCoordinate = moveString.substring(2, 4);
            try {
                return createMove(startCoordinate, endCoordinate);
            } catch (IllegalArgumentException iae) {
                throw new ParseException(errorMessage, 0);
            }
        } else {
            throw new ParseException(errorMessage, 0);
        }
    }

    private boolean inputCorrectLength(String moveString) {
        return moveString.length() == INPUT_LENGTH;
    }

    private Move createMove(String startCoordinate, String endCoordinate) {
        Location startSquare = new Location(Coordinate.valueOf(String.valueOf(startCoordinate
                        .charAt(0))), Integer.parseInt(String.valueOf(startCoordinate.charAt(1))));
        Location endSquare = new Location(
                        Coordinate.valueOf(String.valueOf(endCoordinate.charAt(0))),
                        Integer.parseInt(String.valueOf(endCoordinate.charAt(1))));
        return new Move(startSquare, endSquare);
    }

}

@@ -349,24 +349,28 @@
 tring errorM
+oveM
 essage = %22Mo
@@ -402,16 +402,105 @@
  A1B2%22;%0A
+    private static final String errorLocationMessage = %22Move must be enter like so: A1%22;%0A
     priv
@@ -522,24 +522,29 @@
 l int INPUT_
+MOVE_
 LENGTH = 4;%0A
@@ -542,16 +542,72 @@
 GTH = 4;
+%0A    private static final int INPUT_LOCATION_LENGTH = 2;
 %0A%0A    /*
@@ -773,24 +773,28 @@
 inputCorrect
+Move
 Length(moveS
@@ -1108,32 +1108,36 @@
 Exception(errorM
+oveM
 essage, 0);%0A    
@@ -1206,16 +1206,20 @@
 n(errorM
+oveM
 essage, 
@@ -1271,16 +1271,20 @@
 tCorrect
+Move
 Length(S
@@ -1347,16 +1347,169 @@
 = INPUT_
+MOVE_LENGTH;%0A    %7D%0A    %0A    private boolean inputCorrectLocationLength(String locationString) %7B%0A        return locationString.length() == INPUT_LOCATION_
 LENGTH;%0A
@@ -1589,16 +1589,37 @@
 dinate) 
+throws ParseException
 %7B%0A      
@@ -1643,28 +1643,29 @@
 quare = 
-new 
+parse
 Location
 (Coordin
@@ -1660,48 +1660,85 @@
 tion
-(Coordinate.valueOf(String.valueOf(start
+String(startCoordinate);%0A        Location endSquare = parseLocationString(end
 Coor
@@ -1743,16 +1743,18 @@
 ordinate
+);
 %0A       
@@ -1758,116 +1758,178 @@
     
-   
+return new Move(startSquare, endSquare);%0A
     
+%7D%0A
     
+%0A
     
- .charAt(0))), Integer.parseInt(String.valueOf(startCoordinate.charAt(1))));%0A        
+public Location parseLocationString(String locationString) throws ParseException %7B%0A        if(inputCorrect
 Location
  end
@@ -1928,59 +1928,79 @@
 tion
- endSquare = new Location(%0A                        
+Length(locationString)) %7B%0A            Location location = new Location(
 Coor
@@ -2033,34 +2033,22 @@
 eOf(
-endCoordinate.charAt(0))),
+locationString
 %0A   
@@ -2067,16 +2067,34 @@
         
+     .charAt(0))),
  Integer
@@ -2118,29 +2118,30 @@
 valueOf(
-endCoordinate
+locationString
 .charAt(
@@ -2159,48 +2159,117 @@
     
+    
 return 
-new Move(startSquare, endSquare);
+location;%0A        %7D else %7B%0A            throw new ParseException(errorLocationMessage,0);%0A        %7D
 %0A   

add string parsing for location strings