diff --git a/src/controller/Communication.java b/src/controller/Communication.java new file mode 100755 index 0000000..40fcd22 --- /dev/null +++ b/src/controller/Communication.java @@ -0,0 +1,83 @@ +package controller; + +import java.util.ArrayList; + +import model.Location; +import model.Move; + +/** + * Responsible for mapping our representation and moves to the server's representation + * and expected input for moves. + * @author Ayamin + * + */ +public class Communication { + + /** + * Converts a string representation of a move sequence to a + * Move type object usable by this client's representation. + * + * @param stringMove The string representation of a move sequence, sent by the server. + * @return An arraylist of Move objects constructed from the string representation. + */ + public static ArrayList stringToMove(String stringMoves) { + String[] splitMoves = stringMoves.split(":"); + + ArrayList moveIndexes = new ArrayList(); + + for (String move : splitMoves) { + String index = move.replaceAll("[^0-9]", ""); + if (!index.equals("")) { + moveIndexes.add(Integer.parseInt(index)); + } + } + + ArrayList moves = new ArrayList(); + + for (int i = 0; i < moveIndexes.size() - 3; i+=2) { + Location from = new Location(moveIndexes.get(i), moveIndexes.get(i+1)); + Location to = new Location(moveIndexes.get(i+2), moveIndexes.get(i+3)); + Move move = new Move(from, to); + moves.add(move); + } + + return moves; + + } + + /** + * Converts a arraylist of Move type object to the + * string representation understood by the server. + * + * @param moves The list of Move objects, representing a move sequence. + * @return A string representation of the Move object. + */ + public static String moveToString(ArrayList moves) { + StringBuilder sb = new StringBuilder(); + + Move firstmove = moves.get(0); + sb.append("("); + sb.append(firstmove.source.row); + sb.append(":"); + sb.append(firstmove.source.column); + sb.append("):("); + sb.append(firstmove.destination.row); + sb.append(":"); + sb.append(firstmove.destination.column); + sb.append(")"); + for (int i = 1; i < moves.size(); ++i) { + Move move = moves.get(i); + sb.append(":("); + sb.append(move.destination.row); + sb.append(":"); + sb.append(move.destination.column); + sb.append(")"); + } + return sb.toString(); + } + + public Communication() { + // TODO Auto-generated constructor stub + } + +} diff --git a/src/test/CommunicationTest.java b/src/test/CommunicationTest.java new file mode 100755 index 0000000..87278dc --- /dev/null +++ b/src/test/CommunicationTest.java @@ -0,0 +1,87 @@ +package test; + +import java.util.ArrayList; + +import model.Location; +import model.Move; +import controller.Communication; + +public class CommunicationTest { + + public CommunicationTest() { + // TODO Auto-generated constructor stub + } + + public boolean moveExistsInSet(Move move, ArrayList set) { + for (Move m : set) { + if ( move.destination.row == m.destination.row && + move.destination.column == m.destination.column && + move.source.row == m.source.row && + move.source.column == m.source.column) { + return true; + } + } + return false; + } + + /** + * Input: "(0:0):(2:2):(4:0)" + * + * Output: { Move( from=(0,0), to=(2,2) ), + * Move( from=(2,2), to=(4,0) ) } + */ + public void stringToMoveTest() { + String moveString = "(0:0):(2:2):(4:0)"; + + ArrayList moves = Communication.stringToMove(moveString); + + Move move1 = new Move(new Location(0, 0), new Location(2, 2)); + Move move2 = new Move(new Location(2, 2), new Location(4, 0)); + + assert(moveExistsInSet(move1, moves) && moveExistsInSet(move2, moves)) : "FAILED: String --> Move"; + + System.out.println("PASSED: String --> Move"); + } + + /** + * Input: { Move( from=(0,0), to=(2,2) ), + * Move( from=(2,2), to=(4,0) ) } + * + * Output: "(0:0):(2:2):(4:0)" + */ + public void moveToStringTest() { + ArrayList moves = new ArrayList(); + Move move1 = new Move(new Location(0, 0), new Location(2, 2)); + Move move2 = new Move(new Location(2, 2), new Location(4, 0)); + moves.add(move1); + moves.add(move2); + + String moveString = Communication.moveToString(moves); + + assert(moveString.equals("(0:0):(2:2):(4:0)")) : "PASSED: Move --> String"; + + System.out.println("PASSED: Move --> String"); + } + + /** + * Test the inverse property. + */ + public void bidirectionalTest() { + String moveString = "(0:0):(2:2):(4:0)"; + + ArrayList moves = Communication.stringToMove(moveString); + + assert(Communication.moveToString(moves).equals(moveString)) : "FAILED: String --> Move --> String"; + + System.out.println("PASSED: String --> Move --> String"); + } + + public static void main(String[] args) { + CommunicationTest ct = new CommunicationTest(); + ct.stringToMoveTest(); + ct.moveToStringTest(); + ct.bidirectionalTest(); + + } + +}