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
9005f2a5
Commit
9005f2a5
authored
Dec 19, 2017
by
dkonam
Browse files
IA
parents
a7a1ee43
c9c7089d
Changes
16
Expand all
Show whitespace changes
Inline
Side-by-side
.classpath
0 → 100644
View file @
9005f2a5
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry
kind=
"con"
path=
"org.eclipse.jdt.launching.JRE_CONTAINER"
/>
<classpathentry
kind=
"src"
path=
"Battleship/src/main/resources"
/>
<classpathentry
kind=
"src"
path=
"Battleship/src/test/java"
/>
<classpathentry
kind=
"src"
path=
"Battleship/src/main/java"
/>
<classpathentry
kind=
"output"
path=
"bin"
/>
</classpath>
.project
0 → 100644
View file @
9005f2a5
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>
lo23-project
</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>
org.eclipse.jdt.core.javabuilder
</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>
org.eclipse.jdt.core.javanature
</nature>
</natures>
</projectDescription>
Battleship/src/main/java/com/utclo23/com/ComFacade.java
View file @
9005f2a5
...
...
@@ -61,10 +61,12 @@ public class ComFacade {
public
void
sendShipsToEnnemy
(
List
<
Ship
>
listShips
,
List
<
LightPublicUser
>
recipients
)
{
M_PlaceShip
mPlaceship
=
new
M_PlaceShip
(
iDataCom
.
getMyPublicUserProfile
(),
listShips
);
for
(
LightPublicUser
recipient
:
recipients
)
{
if
(
kIpCtrl
.
getHashMap
().
get
(
recipient
.
getId
())
!=
null
)
{
Sender
os
=
new
Sender
(
kIpCtrl
.
getHashMap
().
get
(
recipient
.
getId
()).
getHostAddress
(),
kIpCtrl
.
getPort
(),
mPlaceship
);
new
Thread
(
os
).
start
();
}
}
}
/**
* Called to initialize attribute kIpCtrl's knownIp hashmap.
...
...
@@ -106,10 +108,12 @@ public class ComFacade {
public
void
notifyNewCoordinates
(
Mine
mine
,
List
<
LightPublicUser
>
recipients
)
{
M_PlaceMine
mPlaceMine
=
new
M_PlaceMine
(
iDataCom
.
getMyPublicUserProfile
(),
mine
);
for
(
LightPublicUser
recipient
:
recipients
)
{
if
(
kIpCtrl
.
getHashMap
().
get
(
recipient
.
getId
())
!=
null
)
{
Sender
os
=
new
Sender
(
kIpCtrl
.
getHashMap
().
get
(
recipient
.
getId
()).
getHostAddress
(),
kIpCtrl
.
getPort
(),
mPlaceMine
);
new
Thread
(
os
).
start
();
}
}
}
/**
* Called to notify everybody of the creation of a new game to update all
...
...
Battleship/src/main/java/com/utclo23/data/facade/DataFacade.java
View file @
9005f2a5
...
...
@@ -220,13 +220,13 @@ public class DataFacade implements IDataCom, IDataIHMTable, IDataIHMMain {
}
/**
* Add new game
<<<<<<< HEAD
* Add new game
*
* @param game =======
* @param game the game to add >>>>>>> Data/javadoc
* @param game
*/
@Override
public
void
addNewGame
(
StatGame
game
)
{
Logger
.
getLogger
(
this
.
getClass
().
getName
()).
log
(
Level
.
INFO
,
"Data | new game received"
);
this
.
gameMediator
.
addNewGame
(
game
);
...
...
@@ -246,7 +246,9 @@ public class DataFacade implements IDataCom, IDataIHMTable, IDataIHMMain {
*/
@Override
public
void
setEnnemyShips
(
List
<
Ship
>
ships
)
{
Logger
.
getLogger
(
DataFacade
.
class
.
getName
()).
log
(
Level
.
INFO
,
null
,
"data | set ennemy ships"
);
this
.
gameMediator
.
setEnnemyShips
(
ships
);
}
...
...
@@ -467,7 +469,7 @@ public class DataFacade implements IDataCom, IDataIHMTable, IDataIHMMain {
public
Pair
<
Integer
,
Ship
>
attack
(
Coordinate
coords
,
boolean
isAttack
)
{
if
(
isAttack
)
{
Logger
.
getLogger
(
D
ata
F
acade
.
class
.
getName
()).
log
(
Level
.
INFO
,
null
,
"data
| attack "
+
coords
.
getX
()+
"-"
+
coords
.
getY
());
System
.
out
.
println
(
"d
ata
f
acade | attack "
+
coords
.
getX
()+
"-"
+
coords
.
getY
());
}
...
...
Battleship/src/main/java/com/utclo23/data/module/GameFactory.java
View file @
9005f2a5
...
...
@@ -10,6 +10,7 @@ import java.util.List;
import
com.utclo23.data.structure.BelgianGame
;
import
com.utclo23.data.structure.ClassicGame
;
import
com.utclo23.data.structure.ComputerPlayer
;
import
com.utclo23.data.structure.Game
;
import
com.utclo23.data.structure.GameType
;
import
com.utclo23.data.structure.LightPublicUser
;
...
...
@@ -40,13 +41,17 @@ public class GameFactory {
if
(
computerMode
)
{
System
.
out
.
println
(
"Data | computer mode"
);
//add second player
Player
j2
=
new
Player
(
LightPublicUser
.
generate
ComputerP
rofile
()
);
Player
j2
=
new
ComputerP
layer
(
);
j2
.
setComputer
(
true
);
players
.
add
(
j2
);
}
System
.
out
.
println
(
"player number : "
+
players
.
size
());
List
<
LightPublicUser
>
spectators
=
new
ArrayList
<>();
spectators
.
add
(
creator
);
...
...
Battleship/src/main/java/com/utclo23/data/module/GameMediator.java
View file @
9005f2a5
This diff is collapsed.
Click to expand it.
Battleship/src/main/java/com/utclo23/data/structure/ComputerPlayer.java
View file @
9005f2a5
...
...
@@ -5,19 +5,32 @@
*/
package
com.utclo23.data.structure
;
import
com.utclo23.data.configuration.Configuration
;
import
java.rmi.server.UID
;
import
java.util.List
;
import
java.util.Random
;
import
javafx.util.Pair
;
/**
*
* @author lucillefargeau
*/
public
class
ComputerPlayer
extends
Player
{
public
class
ComputerPlayer
extends
Player
{
private
Coordinate
focus
;
private
Coordinate
oldFocus
;
public
void
loseFocus
()
{
this
.
oldFocus
=
null
;
this
.
focus
=
null
;
}
public
ComputerPlayer
()
{
super
(
LightPublicUser
.
generateComputerProfile
());
System
.
out
.
println
(
"new Com player"
);
this
.
focus
=
null
;
this
.
oldFocus
=
null
;
this
.
setComputer
(
true
);
}
...
...
@@ -27,14 +40,221 @@ public class ComputerPlayer extends Player{
}
public
void
setFocus
(
Coordinate
focus
)
{
this
.
oldFocus
=
this
.
focus
;
this
.
focus
=
focus
;
}
public
Coordinate
getOldFocus
()
{
return
oldFocus
;
}
public
void
setOldFocus
(
Coordinate
oldFocus
)
{
this
.
oldFocus
=
oldFocus
;
}
@Override
public
void
setShips
(
List
<
Ship
>
ships
)
{
System
.
out
.
println
(
"Data | set ships IA"
);
int
[][]
tab
=
new
int
[
10
][
10
];
for
(
int
i
=
0
;
i
<
Configuration
.
WIDTH
;
++
i
)
{
for
(
int
j
=
0
;
j
<
Configuration
.
WIDTH
;
++
j
)
{
tab
[
i
][
j
]
=
0
;
//empty
}
}
int
x
,
y
;
for
(
Ship
ship
:
ships
)
{
Random
r
=
new
Random
();
boolean
valid
=
true
;
do
{
//choose a new location until empty
x
=
r
.
nextInt
(
Configuration
.
HEIGHT
-
1
);
y
=
r
.
nextInt
(
Configuration
.
WIDTH
-
1
);
if
(
tab
[
x
][
y
]
!=
0
||
(
x
+
ship
.
getSize
()
>=
Configuration
.
WIDTH
&&
y
+
ship
.
getSize
()
>=
Configuration
.
WIDTH
))
{
// System.out.println("("+x+","+y+") NON VALIDE");
valid
=
false
;
}
else
{
//check witdh
for
(
int
i
=
0
;
i
<
ship
.
getSize
();
++
i
)
{
if
((
x
+
i
>=
Configuration
.
WIDTH
)
||
tab
[
x
+
i
][
y
]
!=
0
)
{
valid
=
false
;
//System.out.println("("+x+","+y+") NV"+valid);
}
}
if
(!
valid
)
{
valid
=
true
;
//check height
for
(
int
i
=
0
;
i
<
ship
.
getSize
();
++
i
)
{
if
((
y
+
i
>=
Configuration
.
WIDTH
)
||
tab
[
x
][
y
+
i
]
!=
0
)
{
valid
=
false
;
//System.out.println("("+x+","+y+") "+valid);
}
}
}
}
}
while
(!
valid
);
System
.
out
.
println
(
"Data | IA setting ships - start pos "
+
x
+
","
+
y
);
System
.
out
.
println
(
"Data | size : "
+
ship
.
getSize
());
//fill
if
((
x
+
ship
.
getSize
()
<
Configuration
.
WIDTH
))
{
for
(
int
i
=
0
;
i
<
ship
.
getSize
();
++
i
)
{
tab
[
x
+
i
][
y
]
=
1
;
Coordinate
coord
=
new
Coordinate
(
x
+
i
,
y
);
ship
.
getListCoord
().
add
(
coord
);
}
}
else
{
for
(
int
i
=
0
;
i
<
ship
.
getSize
();
++
i
)
{
tab
[
x
][
y
+
i
]
=
1
;
Coordinate
coord
=
new
Coordinate
(
x
,
y
+
i
);
ship
.
getListCoord
().
add
(
coord
);
}
}
System
.
out
.
println
(
"Data | IA add ship"
);
this
.
getShips
().
add
(
ship
);
//add ship
for
(
int
i
=
0
;
i
<
Configuration
.
WIDTH
;
++
i
)
{
for
(
int
j
=
0
;
j
<
Configuration
.
WIDTH
;
++
j
)
{
if
(
tab
[
j
][
i
]
==
0
)
{
System
.
out
.
print
(
"~ "
);
}
else
{
System
.
out
.
print
(
"@ "
);
}
}
System
.
out
.
println
();
}
}
System
.
out
.
println
(
"Data | IA finishes to set up"
);
for
(
int
i
=
0
;
i
<
Configuration
.
WIDTH
;
++
i
)
{
for
(
int
j
=
0
;
j
<
Configuration
.
WIDTH
;
++
j
)
{
if
(
tab
[
j
][
i
]
==
0
)
{
System
.
out
.
print
(
"~ "
);
}
else
{
System
.
out
.
print
(
"@ "
);
}
}
System
.
out
.
println
();
}
}
public
Mine
randomMine
()
{
System
.
out
.
println
(
"computer plays... "
+
this
.
getMines
().
size
());
int
[][]
tab
=
new
int
[
10
][
10
];
for
(
int
i
=
0
;
i
<
Configuration
.
WIDTH
;
++
i
)
{
for
(
int
j
=
0
;
j
<
Configuration
.
WIDTH
;
++
j
)
{
tab
[
i
][
j
]
=
0
;
//empty
}
}
for
(
Mine
mine
:
this
.
getMines
())
{
tab
[
mine
.
getCoord
().
getX
()][
mine
.
getCoord
().
getY
()]
=
1
;
}
System
.
out
.
println
(
"IA mines "
);
for
(
int
i
=
0
;
i
<
Configuration
.
WIDTH
;
++
i
)
{
for
(
int
j
=
0
;
j
<
Configuration
.
WIDTH
;
++
j
)
{
if
(
tab
[
j
][
i
]
==
0
)
{
System
.
out
.
print
(
"~ "
);
}
else
{
System
.
out
.
print
(
"X "
);
}
}
System
.
out
.
println
();
}
int
x
=
0
,
y
=
0
;
boolean
valid
=
true
;
if
(
focus
==
null
)
{
//System.out.println("random method");
Random
r
=
new
Random
();
do
{
valid
=
true
;
//choose a new location until empty
x
=
r
.
nextInt
(
Configuration
.
WIDTH
-
1
);
y
=
r
.
nextInt
(
Configuration
.
WIDTH
-
1
);
if
(
tab
[
x
][
y
]
!=
0
||
(
x
>=
Configuration
.
WIDTH
||
y
>=
Configuration
.
WIDTH
))
{
valid
=
false
;
}
//System.out.print(" ("+x+","+y+ ")="+tab[x][y]+" "+valid);
}
while
(!
valid
);
}
else
{
//System.out.println("focus method");
if
((
focus
.
getX
()
+
1
>=
Configuration
.
WIDTH
||
focus
.
getY
()
>=
Configuration
.
WIDTH
)
||
tab
[
focus
.
getX
()
+
1
][
focus
.
getY
()]
!=
0
)
{
if
((
focus
.
getX
()
-
1
<
0
||
focus
.
getY
()
>=
Configuration
.
WIDTH
)
||
tab
[
focus
.
getX
()
-
1
][
focus
.
getY
()]
!=
0
)
{
if
((
focus
.
getX
()
>=
Configuration
.
WIDTH
||
focus
.
getY
()
+
1
>=
Configuration
.
WIDTH
)||
tab
[
focus
.
getX
()][
focus
.
getY
()
+
1
]
!=
0
)
{
if
(
(
focus
.
getX
()
>=
Configuration
.
WIDTH
||
focus
.
getY
()
-
1
<
0
)
||
tab
[
focus
.
getX
()][
focus
.
getY
()
-
1
]
!=
0
)
{
this
.
focus
=
null
;
this
.
oldFocus
=
null
;
Random
r
=
new
Random
();
do
{
valid
=
true
;
//choose a new location until empty
x
=
r
.
nextInt
(
Configuration
.
HEIGHT
-
1
);
y
=
r
.
nextInt
(
Configuration
.
WIDTH
-
1
);
if
(
tab
[
x
][
y
]
!=
0
||
(
x
>=
Configuration
.
WIDTH
||
y
>=
Configuration
.
WIDTH
))
{
valid
=
false
;
}
//System.out.print(" ("+x+","+y+ ")="+tab[x][y]+" ");
}
while
(!
valid
);
}
else
{
x
=
focus
.
getX
();
y
=
focus
.
getY
()
-
1
;
}
}
else
{
x
=
focus
.
getX
();
y
=
focus
.
getY
()
+
1
;
}
}
else
{
x
=
focus
.
getX
()
-
1
;
y
=
focus
.
getY
();
}
}
else
{
x
=
focus
.
getX
()
+
1
;
y
=
focus
.
getY
();
}
}
Coordinate
coordinate
=
new
Coordinate
(
x
,
y
);
System
.
out
.
println
(
"IA chooses "
+
x
+
","
+
y
);
Mine
mine
=
new
Mine
(
this
,
coordinate
);
this
.
getMines
().
add
(
mine
);
return
mine
;
}
}
Battleship/src/main/java/com/utclo23/data/structure/Game.java
View file @
9005f2a5
...
...
@@ -20,7 +20,6 @@ import javafx.util.Pair;
import
java.util.Iterator
;
/**
*
* @author Davy
...
...
@@ -47,7 +46,6 @@ public abstract class Game extends SerializableEntity {
/* creation of caretaker */
this
.
caretaker
=
new
Caretaker
();
}
/**
...
...
@@ -62,30 +60,30 @@ public abstract class Game extends SerializableEntity {
role
=
role
.
toLowerCase
();
if
(
role
.
equals
(
"player"
))
{
if
(
role
.
equals
(
"player"
))
{
if
(
this
.
players
==
null
)
{
if
(
this
.
players
==
null
)
{
System
.
out
.
println
(
"players == null"
);
}
if
(
this
.
players
.
size
()
<=
1
)
{
if
(
this
.
players
.
size
()
<=
1
)
{
Player
player
=
new
Player
(
user
);
this
.
players
.
add
(
player
);
System
.
out
.
println
(
"player "
+
player
.
getLightPublicUser
().
getId
());
System
.
out
.
println
(
"player "
+
player
.
getLightPublicUser
().
getId
());
}
/*else
}
/*else
{
throw new DataException("Data : already two players in this "
+ "game, you can not add another one.");
}*/
}
else
if
(
role
.
equals
(
"spectator"
))
{
}
else
if
(
role
.
equals
(
"spectator"
))
{
this
.
spectators
.
add
(
user
);
System
.
out
.
println
(
"spectator "
+
user
.
getId
());
System
.
out
.
println
(
"spectator "
+
user
.
getId
());
}
else
{
throw
new
DataException
(
"Data : given role is not known."
);
...
...
@@ -109,18 +107,17 @@ public abstract class Game extends SerializableEntity {
return
currentPlayer
;
}
public
boolean
isReady
()
{
return
this
.
players
.
size
()
==
2
;
}
public
void
nextTurn
()
{
if
(
this
.
isReady
()
&&
this
.
currentPlayer
==
null
)
{
/*
if (this.isReady() && this.currentPlayer == null) {
this.currentPlayer = this.players.get(0);
} else {
this.currentPlayer = this.ennemyOf(currentPlayer);
}
}
*/
}
public
Player
ennemyOf
(
Player
player
)
{
...
...
@@ -134,16 +131,10 @@ public abstract class Game extends SerializableEntity {
return
ennemy
;
}
public
Player
getPlayer
(
String
id
)
{
public
Player
getPlayer
(
String
id
)
{
Player
player
=
null
;
for
(
Player
p
:
this
.
players
)
{
if
(
p
.
getLightPublicUser
().
getId
().
equals
(
id
))
{
for
(
Player
p
:
this
.
players
)
{
if
(
p
.
getLightPublicUser
().
getId
().
equals
(
id
))
{
player
=
p
;
}
}
...
...
@@ -154,12 +145,12 @@ public abstract class Game extends SerializableEntity {
public
List
<
LightPublicUser
>
getRecipients
()
{
List
<
LightPublicUser
>
listRecipients
=
new
ArrayList
<>();
for
(
int
i
=
0
;
i
<
this
.
getPlayers
().
size
();
++
i
)
{
if
(!(
this
.
getPlayers
().
get
(
i
).
isComputer
()))
{
for
(
int
i
=
0
;
i
<
this
.
getPlayers
().
size
();
++
i
)
{
if
(!(
this
.
getPlayers
().
get
(
i
).
isComputer
()))
{
listRecipients
.
add
(
this
.
getPlayers
().
get
(
i
).
getLightPublicUser
());
}
}
listRecipients
.
addAll
(
this
.
getSpectators
())
;
listRecipients
.
addAll
(
this
.
getSpectators
());
return
listRecipients
;
}
...
...
@@ -177,9 +168,9 @@ public abstract class Game extends SerializableEntity {
/**
* get templates of ships for a given game
*
* @return
*/
public
abstract
List
<
Ship
>
getTemplateShips
();
// This is for deep copy of a List
...
...
@@ -195,14 +186,14 @@ public abstract class Game extends SerializableEntity {
return
dest
;
}
public
Pair
<
Integer
,
Ship
>
attack
(
Player
player
,
Coordinate
coordinate
,
boolean
isTrueAttack
)
throws
DataException
,
IOException
,
ClassNotFoundException
{
public
Pair
<
Integer
,
Ship
>
attack
(
Player
player
,
Coordinate
coordinate
,
boolean
isTrueAttack
)
throws
DataException
,
IOException
,
ClassNotFoundException
{
System
.
out
.
println
(
"exec attack of "
+
player
.
getLightPublicUser
().
getPlayerName
()+
" "
+
isTrueAttack
);
//Create mine
Mine
mine
=
new
Mine
(
player
,
coordinate
);
//For see if the mine is in the place of a ship(succeed at attack)
// 0=fail . 1=succeed
int
succeedAtt
e
ck
=
0
;
int
succeedAtt
a
ck
=
0
;
// The ship if the mine is in the place of it
Ship
shipTouch
=
null
;
Ship
shipReturn
=
null
;
...
...
@@ -215,77 +206,75 @@ public abstract class Game extends SerializableEntity {
throw
new
DataException
(
"Data : player opponent doesn't exist"
);
}
List
<
Ship
>
shipsOpponent
=
playerOpponent
.
getShips
();
if
(
shipsOpponent
.
size
()==
0
){
if
(
shipsOpponent
.
size
()
==
0
)
{
throw
new
DataException
(
"Data : player opponent didn't set any ship"
);
}
for
(
int
i
=
0
;
i
<
shipsOpponent
.
size
();
i
++){
Ship
shipA
=
shipsOpponent
.
get
(
i
);
List
<
Coordinate
>
shipCoord
=
shipA
.
getListCoord
();
//See if the mine is put in the place of a ship
for
(
int
j
=
0
;
j
<
shipCoord
.
size
();
j
++)
{
if
(
shipCoord
.
get
(
j
).
getX
()
==
coordinate
.
getX
()
&&
shipCoord
.
get
(
j
).
getY
()
==
coordinate
.
getY
()){
succeedAtt
e
ck
=
1
;
for
(
Ship
shipA
:
shipsOpponent
)
{
for
(
Coordinate
shipCoord
:
shipA
.
getListCoord
())
{
if
(
shipCoord
.
getX
()
==
coordinate
.
getX
()
&&
shipCoord
.
getY
()
==
coordinate
.
getY
())
{
succeedAtt
a
ck
=
1
;
shipTouch
=
shipA
;
break
;
}
}
if
(
succeedAtteck
==
1
){
if
(
succeedAttack
==
1
)
{
System
.
out
.
println
(
"data => touched "
);
break
;
}
}
if
(
isTrueAttack
==
true
){
//When this is a real attack
//Change the state of MineResult according to value of succeedAtteck
if
(
succeedAtteck
==
1
){
if
(
isTrueAttack
)
{
//When this is a real attack
//Change the state of MineResult according to value of succeedAttack
if
(
succeedAttack
==
1
)
{
System
.
out
.
println
(
"Data | good attack"
);
mine
.
setResult
(
MineResult
.
SUCCESS
);
//Add mine to player
player
.
getMines
().
add
(
mine
);
//If is not in right place, shipReturn = null
if
(
isShipDestroyed
(
shipTouch
,
player
.
getMines
())
){
if
(
isShipDestroyed
(
shipTouch
,
player
.
getMines
()))
{
System
.
out
.
println
(
"Data | very good attack"
);
shipReturn
=
shipTouch
;
}
}
else
{
}
else
{
mine
.
setResult
(
MineResult
.
FAILURE
);
System
.
out
.
println
(
"Data | bad attack"
);
//Add mine to player
player
.
getMines
().
add
(
mine
);
}
//Test if this game is finished
boolean
gameFinished
=
this
.
isGameFinishedByCurrentPlayer
();
if
(
gameFinished
){
if
(
gameFinished
)
{