Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Quentin DRUAULT-AUBIN
lo23-project
Commits
66bf55bc
Commit
66bf55bc
authored
Dec 12, 2017
by
Paul Jeannot
Committed by
Quentin DRUAULT-AUBIN
Dec 15, 2017
Browse files
[Ship] Refactor coordinates computing + tests
parent
24db309e
Changes
3
Hide whitespace changes
Inline
Side-by-side
Battleship/src/main/java/com/utclo23/ihmtable/controller/InGameGUIController.java
View file @
66bf55bc
...
...
@@ -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
);
...
...
Battleship/src/main/java/com/utclo23/ihmtable/structure/CoordinatesGenerator.java
0 → 100644
View file @
66bf55bc
/*
* 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
);
}
}
Battleship/src/test/java/com/utclo23/ihmtable/CoordinatesGenerationTest.java
0 → 100644
View file @
66bf55bc
/*
* 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
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment