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
79a2df6f
Commit
79a2df6f
authored
Dec 18, 2017
by
dkonam
Browse files
Merge branch 'Xiaodan/WinAfterAttack' into Data/develop
parents
714d2092
8647d840
Changes
4
Hide whitespace changes
Inline
Side-by-side
Battleship/src/main/java/com/utclo23/data/facade/DataFacade.java
View file @
79a2df6f
...
...
@@ -449,7 +449,11 @@ public class DataFacade implements IDataCom, IDataIHMTable, IDataIHMMain {
@Override
public
Pair
<
Integer
,
Ship
>
attack
(
Coordinate
coords
,
boolean
isAttack
)
{
try
{
return
this
.
gameMediator
.
attack
(
coords
,
isAttack
);
Pair
<
Integer
,
Ship
>
pairReturn
=
this
.
gameMediator
.
attack
(
coords
,
isAttack
);
if
(
this
.
getComfacade
()
!=
null
){
this
.
getComfacade
().
notifyNewCoordinates
(
this
.
gameMediator
.
getCurrentGame
().
getRecentMine
(
coords
),
this
.
gameMediator
.
getCurrentGame
().
getRecipients
());
}
return
pairReturn
;
}
catch
(
Exception
ex
)
{
ex
.
printStackTrace
();
//Logger.getLogger(DataFacade.class.getName()).log(Level.SEVERE, null, ex);
...
...
Battleship/src/main/java/com/utclo23/data/module/GameMediator.java
View file @
79a2df6f
...
...
@@ -279,27 +279,33 @@ public class GameMediator {
if
(
player
==
null
)
{
throw
new
DataException
(
"Data : player not found for set player ship"
);
}
if
(
isTrueAttack
==
true
)
{
//return the result of the attack
//if isTrueAttack=1, then add mine to player ; otherwise, that is just a test, no stat of mine
if
(
isTrueAttack
==
true
)
{
//check if mine already used at current location
List
<
Mine
>
mines
=
player
.
getMines
();
if
(
mines
.
size
()
>
0
)
{
for
(
int
i
=
0
;
i
<
mines
.
size
();
i
++)
{
Mine
mine
=
mines
.
get
(
i
);
if
(
mine
.
getCoord
().
getX
()
==
coordinate
.
getX
()
&&
mine
.
getCoord
().
getY
()
==
coordinate
.
getY
())
{
Logger
.
getLogger
(
GameMediator
.
class
.
getName
()).
log
(
Level
.
WARNING
,
"Data : Mine places in the place where already has a mine"
);
return
null
;
List
<
Mine
>
mines
=
player
.
getMines
();
if
(
mines
.
size
()
>
0
)
{
for
(
int
i
=
0
;
i
<
mines
.
size
();
i
++)
{
Mine
mine
=
mines
.
get
(
i
);
if
(
mine
.
getCoord
().
getX
()
==
coordinate
.
getX
()
&&
mine
.
getCoord
().
getY
()
==
coordinate
.
getY
())
{
Logger
.
getLogger
(
GameMediator
.
class
.
getName
()).
log
(
Level
.
WARNING
,
"Data : Mine places in the place where already has a mine"
);
return
null
;
}
}
}
}
//return the result of the attack
//if isTrueAttack=1, then add mine to player ; otherwise, that is just a test, no stat of mine
pairReturn
=
this
.
currentGame
.
attack
(
player
,
coordinate
,
isTrueAttack
);
//save with caretaker
this
.
currentGame
.
getCaretaker
().
add
(
this
.
currentGame
.
saveStateToMemento
());
//Test if this game is finished
//If this game is finished, leave the game
if
(
this
.
currentGame
.
getStatGame
().
getWinner
()
!=
null
){
this
.
leaveGame
();
}
return
pairReturn
;
}
else
{
// In the case of a test, that's possible that the current player is not
...
...
@@ -441,6 +447,7 @@ public class GameMediator {
/**
* Exit current game.
*/
public
void
leaveGame
()
{
//Sauvegarde à ajouter.
...
...
Battleship/src/main/java/com/utclo23/data/structure/Game.java
View file @
79a2df6f
...
...
@@ -228,7 +228,11 @@ public abstract class Game extends SerializableEntity {
//Add mine to player
player
.
getMines
().
add
(
mine
);
}
//Test if this game is finished
boolean
gameFinished
=
this
.
isGameFinishedByCurrentPlayer
();
if
(
gameFinished
){
this
.
setWinner
(
player
.
getLightPublicUser
());
}
//Let opponent be the current player
nextTurn
();
}
...
...
@@ -293,6 +297,19 @@ public abstract class Game extends SerializableEntity {
}
}
/* to get the mine added in attack */
public
Mine
getRecentMine
(
Coordinate
coordinate
){
List
<
Player
>
players
=
this
.
players
;
for
(
Player
play
:
players
){
Mine
minePlayer
=
play
.
getMines
().
get
(
play
.
getMines
().
size
()-
1
);
Coordinate
coorMine
=
minePlayer
.
getCoord
();
if
(
coorMine
.
getX
()
==
coordinate
.
getX
()
&&
coorMine
.
getY
()
==
coordinate
.
getY
()){
return
minePlayer
;
}
}
return
null
;
}
public
Caretaker
getCaretaker
()
{
return
caretaker
;
...
...
Battleship/src/test/java/com/utclo23/data/AttackTest.java
View file @
79a2df6f
...
...
@@ -5,7 +5,11 @@
*/
package
com.utclo23.data
;
import
com.utclo23.com.ComFacade
;
import
com.utclo23.data.facade.DataFacade
;
import
com.utclo23.data.facade.IDataCom
;
import
com.utclo23.data.facade.IDataIHMMain
;
import
com.utclo23.data.facade.IDataIHMTable
;
import
com.utclo23.data.module.DataException
;
import
com.utclo23.data.module.GameMediator
;
import
com.utclo23.data.structure.ClassicGame
;
...
...
@@ -13,6 +17,7 @@ import com.utclo23.data.structure.Coordinate;
import
com.utclo23.data.structure.Game
;
import
com.utclo23.data.structure.GameType
;
import
com.utclo23.data.structure.LightPublicUser
;
import
com.utclo23.data.structure.Mine
;
import
com.utclo23.data.structure.Player
;
import
com.utclo23.data.structure.Ship
;
import
com.utclo23.data.structure.ShipType
;
...
...
@@ -24,14 +29,15 @@ import java.util.List;
import
java.util.logging.Level
;
import
java.util.logging.Logger
;
import
javafx.util.Pair
;
import
org.junit.Rule
;
import
org.junit.rules.ExpectedException
;
/**
* Test the function attack
* @author wuxiaodan
*/
public
class
AttackTest
{
@org
.
junit
.
Test
public
void
testCasePlayer
()
throws
DataException
{
...
...
@@ -84,19 +90,24 @@ public class AttackTest {
shipsB
.
add
(
shipB1
);
playerB
.
setShips
(
shipsB
);
//Test of player A and player B
Coordinate
coorTestA1
=
new
Coordinate
(
11
,
2
);
//case de B
Pair
<
Integer
,
Ship
>
attackA1
=
df
.
attack
(
coorTestA1
,
true
);
//test of function getRecentMine
Mine
mineTest
=
df
.
getGameMediator
().
getCurrentGame
().
getRecentMine
(
coorTestA1
);
Coordinate
coorTestB1
=
new
Coordinate
(
2
,
2
);
Pair
<
Integer
,
Ship
>
attackB1
=
df
.
attack
(
coorTestB1
,
true
);
Coordinate
coorTestA2
=
new
Coordinate
(
12
,
2
);
//case de B
Pair
<
Integer
,
Ship
>
attackA2
=
df
.
attack
(
coorTestA2
,
true
);
Coordinate
coorTestB2
=
new
Coordinate
(
9
,
2
);
Pair
<
Integer
,
Ship
>
attackB2
=
df
.
attack
(
coorTestB2
,
fals
e
);
Pair
<
Integer
,
Ship
>
attackB2
=
df
.
attack
(
coorTestB2
,
tru
e
);
Coordinate
coorTestA3
=
new
Coordinate
(
13
,
2
);
//case de B
Pair
<
Integer
,
Ship
>
attackA3
=
df
.
attack
(
coorTestA3
,
fals
e
);
Pair
<
Integer
,
Ship
>
attackA3
=
df
.
attack
(
coorTestA3
,
tru
e
);
Coordinate
coorTestB3
=
new
Coordinate
(
1
,
2
);
Pair
<
Integer
,
Ship
>
attackB3
=
df
.
attack
(
coorTestB3
,
fals
e
);
Pair
<
Integer
,
Ship
>
attackB3
=
df
.
attack
(
coorTestB3
,
tru
e
);
//Test of a spectator
// Coordinate coorTestU3 = new Coordinate(2,1);
...
...
@@ -106,6 +117,7 @@ public class AttackTest {
//Pair<Integer, Ship> attackA4 = df.attack(coorTestA3, true);
}
@org
.
junit
.
Test
...
...
@@ -164,6 +176,7 @@ public class AttackTest {
df
.
forwardCoordinates
(
mine
);
}
...
...
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