-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Communications class maps our client representation of a movement to …
…the server's understanding of a move (its string representation), and vice-versa. Test class added, it's automated.
- Loading branch information
Showing
2 changed files
with
170 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Move> stringToMove(String stringMoves) { | ||
String[] splitMoves = stringMoves.split(":"); | ||
|
||
ArrayList<Integer> moveIndexes = new ArrayList<Integer>(); | ||
|
||
for (String move : splitMoves) { | ||
String index = move.replaceAll("[^0-9]", ""); | ||
if (!index.equals("")) { | ||
moveIndexes.add(Integer.parseInt(index)); | ||
} | ||
} | ||
|
||
ArrayList<Move> moves = new ArrayList<Move>(); | ||
|
||
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<Move> 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 | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Move> 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<Move> 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<Move> moves = new ArrayList<Move>(); | ||
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<Move> 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(); | ||
|
||
} | ||
|
||
} |