Commit 66bf55bc authored by Paul Jeannot's avatar Paul Jeannot Committed by Quentin DRUAULT-AUBIN
Browse files

[Ship] Refactor coordinates computing + tests

parent 24db309e
......@@ -13,6 +13,7 @@ import com.utclo23.data.structure.Mine;
import com.utclo23.data.structure.Player;
import com.utclo23.data.structure.Ship;
import com.utclo23.data.structure.ShipType;
import com.utclo23.ihmtable.structure.CoordinatesGenerator;
import com.utclo23.ihmtable.structure.InGameStats;
import java.io.IOException;
import java.util.logging.Level;
......@@ -1014,20 +1015,7 @@ public class InGameGUIController {
// Ship founded.
suitableShip = true;
// Add the coordinates.
ship.getListCoord().add(startPosition);
ship.getListCoord().add(endPosition);
// Add the coordinates between the two cases.
Coordinate diff = new Coordinate(endPosition.getX()-startPosition.getX(),endPosition.getY()-startPosition.getY());
int xDirection = (diff.getX() >= 0) ? 1 : -1;
int yDirection = (diff.getY() >= 0) ? 1 : -1;
for(int x = 1; x < Math.abs(diff.getX()); ++x) {
ship.getListCoord().add(new Coordinate(startPosition.getX()+xDirection*x,startPosition.getY()));
}
for(int y = 1; y < Math.abs(diff.getY()); ++y) {
ship.getListCoord().add(new Coordinate(startPosition.getX(),startPosition.getY()+yDirection*y));
}
CoordinatesGenerator.generate(startPosition, endPosition, ship.getListCoord());
// Send the ship.
facade.getFacadeData().setShip(ship);
......
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.utclo23.ihmtable.structure;
import com.utclo23.data.structure.Coordinate;
import java.util.List;
/**
*
* @author gum
*/
public class CoordinatesGenerator {
/**
* This function adds coordinates between a starting/ending point
* @param startPosition Start position of the ship
* @param endPosition End position of the ship
* @param list List in which coordinates have to be added
*/
public static void generate(Coordinate startPosition, Coordinate endPosition, List<Coordinate> list) {
list.add(startPosition);
// Add the coordinates between the two cases.
Coordinate diff = new Coordinate(endPosition.getX() - startPosition.getX(), endPosition.getY() - startPosition.getY());
int xDirection = (diff.getX() >= 0) ? 1 : -1;
int yDirection = (diff.getY() >= 0) ? 1 : -1;
for (int x = 1; x < Math.abs(diff.getX()); ++x) {
list.add(new Coordinate(startPosition.getX() + xDirection * x, startPosition.getY()));
}
for (int y = 1; y < Math.abs(diff.getY()); ++y) {
list.add(new Coordinate(startPosition.getX(), startPosition.getY() + yDirection * y));
}
list.add(endPosition);
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.utclo23.ihmtable;
import com.utclo23.data.structure.Coordinate;
import com.utclo23.ihmtable.structure.CoordinatesGenerator;
import java.util.ArrayList;
import static org.junit.Assert.*;
/**
*
* @author gum
*/
public class CoordinatesGenerationTest {
private static CoordinatesGenerator generator = new CoordinatesGenerator();
@org.junit.Test
public void testCoordinateGenerator1() throws Exception {
Coordinate startPoint = new Coordinate(1, 1);
Coordinate endPoint = new Coordinate(5, 1);
ArrayList<Coordinate> coordinates = new ArrayList<Coordinate>();
CoordinatesGenerator.generate(startPoint, endPoint, coordinates);
assertTrue("Must contains (1,1)", equal(new Coordinate(1, 1), coordinates.get(0)));
assertTrue("Must contains (2,1)", equal(new Coordinate(2, 1), coordinates.get(1)));
assertTrue("Must contains (3,1)", equal(new Coordinate(3, 1), coordinates.get(2)));
assertTrue("Must contains (4,1)", equal(new Coordinate(4, 1), coordinates.get(3)));
assertTrue("Must contains (5,1)", equal(new Coordinate(5, 1), coordinates.get(4)));
}
@org.junit.Test
public void testCoordinateGenerator2() throws Exception {
Coordinate startPoint = new Coordinate(5, 1);
Coordinate endPoint = new Coordinate(1, 1);
ArrayList<Coordinate> coordinates = new ArrayList<Coordinate>();
CoordinatesGenerator.generate(startPoint, endPoint, coordinates);
assertTrue("Must contains (5,1)", equal(new Coordinate(5, 1), coordinates.get(0)));
assertTrue("Must contains (4,1)", equal(new Coordinate(4, 1), coordinates.get(1)));
assertTrue("Must contains (3,1)", equal(new Coordinate(3, 1), coordinates.get(2)));
assertTrue("Must contains (2,1)", equal(new Coordinate(2, 1), coordinates.get(3)));
assertTrue("Must contains (1,1)", equal(new Coordinate(1, 1), coordinates.get(4)));
}
@org.junit.Test
public void testCoordinateGenerator3() throws Exception {
Coordinate startPoint = new Coordinate(1, 5);
Coordinate endPoint = new Coordinate(1, 1);
ArrayList<Coordinate> coordinates = new ArrayList<Coordinate>();
CoordinatesGenerator.generate(startPoint, endPoint, coordinates);
assertTrue("Must contains (1,5)", equal(new Coordinate(1, 5), coordinates.get(0)));
assertTrue("Must contains (1,4)", equal(new Coordinate(1, 4), coordinates.get(1)));
assertTrue("Must contains (1,3)", equal(new Coordinate(1, 3), coordinates.get(2)));
assertTrue("Must contains (1,2)", equal(new Coordinate(1, 2), coordinates.get(3)));
assertTrue("Must contains (1,1)", equal(new Coordinate(1, 1), coordinates.get(4)));
}
@org.junit.Test
public void testCoordinateGenerator4() throws Exception {
Coordinate startPoint = new Coordinate(1, 1);
Coordinate endPoint = new Coordinate(1, 5);
ArrayList<Coordinate> coordinates = new ArrayList<Coordinate>();
CoordinatesGenerator.generate(startPoint, endPoint, coordinates);
assertTrue("Must contains (1,1)", equal(new Coordinate(1, 1), coordinates.get(0)));
assertTrue("Must contains (1,2)", equal(new Coordinate(1, 2), coordinates.get(1)));
assertTrue("Must contains (1,3)", equal(new Coordinate(1, 3), coordinates.get(2)));
assertTrue("Must contains (1,4)", equal(new Coordinate(1, 4), coordinates.get(3)));
assertTrue("Must contains (1,5)", equal(new Coordinate(1, 5), coordinates.get(4)));
}
@org.junit.Test
public void testCoordinateGenerator5() throws Exception {
Coordinate startPoint = new Coordinate(1, 1);
Coordinate endPoint = new Coordinate(2, 1);
ArrayList<Coordinate> coordinates = new ArrayList<Coordinate>();
CoordinatesGenerator.generate(startPoint, endPoint, coordinates);
assertTrue("Must contains (1,1)", equal(new Coordinate(1, 1), coordinates.get(0)));
assertTrue("Must contains (2,1)", equal(new Coordinate(2, 1), coordinates.get(1)));
}
@org.junit.Test
public void testCoordinateGenerator6() throws Exception {
Coordinate startPoint = new Coordinate(2, 1);
Coordinate endPoint = new Coordinate(1, 1);
ArrayList<Coordinate> coordinates = new ArrayList<Coordinate>();
CoordinatesGenerator.generate(startPoint, endPoint, coordinates);
assertTrue("Must contains (2,1)", equal(new Coordinate(2, 1), coordinates.get(0)));
assertTrue("Must contains (1,1)", equal(new Coordinate(1, 1), coordinates.get(1)));
}
@org.junit.Test
public void testCoordinateGenerator7() throws Exception {
Coordinate startPoint = new Coordinate(1, 2);
Coordinate endPoint = new Coordinate(1, 1);
ArrayList<Coordinate> coordinates = new ArrayList<Coordinate>();
CoordinatesGenerator.generate(startPoint, endPoint, coordinates);
assertTrue("Must contains (1,2)", equal(new Coordinate(1, 2), coordinates.get(0)));
assertTrue("Must contains (1,1)", equal(new Coordinate(1, 1), coordinates.get(1)));
}
@org.junit.Test
public void testCoordinateGenerator8() throws Exception {
Coordinate startPoint = new Coordinate(1, 1);
Coordinate endPoint = new Coordinate(1, 2);
ArrayList<Coordinate> coordinates = new ArrayList<Coordinate>();
CoordinatesGenerator.generate(startPoint, endPoint, coordinates);
assertTrue("Must contains (1,1)", equal(new Coordinate(1, 1), coordinates.get(0)));
assertTrue("Must contains (1,2)", equal(new Coordinate(1, 2), coordinates.get(1)));
}
public boolean equal(Coordinate c1, Coordinate c2) {
return c1.getX() == c2.getX() && c1.getY() == c2.getY();
}
}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment