Commit 9005f2a5 authored by dkonam's avatar dkonam
Browse files

IA

parents a7a1ee43 c9c7089d
<?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>
<?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>
...@@ -61,8 +61,10 @@ public class ComFacade { ...@@ -61,8 +61,10 @@ public class ComFacade {
public void sendShipsToEnnemy(List<Ship> listShips, List<LightPublicUser> recipients) { public void sendShipsToEnnemy(List<Ship> listShips, List<LightPublicUser> recipients) {
M_PlaceShip mPlaceship = new M_PlaceShip(iDataCom.getMyPublicUserProfile(), listShips); M_PlaceShip mPlaceship = new M_PlaceShip(iDataCom.getMyPublicUserProfile(), listShips);
for (LightPublicUser recipient : recipients) { for (LightPublicUser recipient : recipients) {
Sender os = new Sender(kIpCtrl.getHashMap().get(recipient.getId()).getHostAddress(), kIpCtrl.getPort(), mPlaceship); if (kIpCtrl.getHashMap().get(recipient.getId()) != null) {
new Thread(os).start(); Sender os = new Sender(kIpCtrl.getHashMap().get(recipient.getId()).getHostAddress(), kIpCtrl.getPort(), mPlaceship);
new Thread(os).start();
}
} }
} }
...@@ -106,8 +108,10 @@ public class ComFacade { ...@@ -106,8 +108,10 @@ public class ComFacade {
public void notifyNewCoordinates(Mine mine, List<LightPublicUser> recipients) { public void notifyNewCoordinates(Mine mine, List<LightPublicUser> recipients) {
M_PlaceMine mPlaceMine = new M_PlaceMine(iDataCom.getMyPublicUserProfile(), mine); M_PlaceMine mPlaceMine = new M_PlaceMine(iDataCom.getMyPublicUserProfile(), mine);
for (LightPublicUser recipient : recipients) { for (LightPublicUser recipient : recipients) {
Sender os = new Sender(kIpCtrl.getHashMap().get(recipient.getId()).getHostAddress(), kIpCtrl.getPort(), mPlaceMine); if (kIpCtrl.getHashMap().get(recipient.getId()) != null) {
new Thread(os).start(); Sender os = new Sender(kIpCtrl.getHashMap().get(recipient.getId()).getHostAddress(), kIpCtrl.getPort(), mPlaceMine);
new Thread(os).start();
}
} }
} }
......
...@@ -220,13 +220,13 @@ public class DataFacade implements IDataCom, IDataIHMTable, IDataIHMMain { ...@@ -220,13 +220,13 @@ public class DataFacade implements IDataCom, IDataIHMTable, IDataIHMMain {
} }
/** /**
* Add new game <<<<<<< HEAD * Add new game
* *
* @param game ======= * @param game
* @param game the game to add >>>>>>> Data/javadoc
*/ */
@Override @Override
public void addNewGame(StatGame game) { public void addNewGame(StatGame game) {
Logger.getLogger(this.getClass().getName()).log(Level.INFO, "Data | new game received");
this.gameMediator.addNewGame(game); this.gameMediator.addNewGame(game);
...@@ -246,7 +246,9 @@ public class DataFacade implements IDataCom, IDataIHMTable, IDataIHMMain { ...@@ -246,7 +246,9 @@ public class DataFacade implements IDataCom, IDataIHMTable, IDataIHMMain {
*/ */
@Override @Override
public void setEnnemyShips(List<Ship> ships) { public void setEnnemyShips(List<Ship> ships) {
Logger.getLogger(DataFacade.class.getName()).log(Level.INFO, null, "data | set ennemy ships"); Logger.getLogger(DataFacade.class.getName()).log(Level.INFO, null, "data | set ennemy ships");
this.gameMediator.setEnnemyShips(ships); this.gameMediator.setEnnemyShips(ships);
} }
...@@ -467,7 +469,7 @@ public class DataFacade implements IDataCom, IDataIHMTable, IDataIHMMain { ...@@ -467,7 +469,7 @@ public class DataFacade implements IDataCom, IDataIHMTable, IDataIHMMain {
public Pair<Integer, Ship> attack(Coordinate coords, boolean isAttack) { public Pair<Integer, Ship> attack(Coordinate coords, boolean isAttack) {
if(isAttack) if(isAttack)
{ {
Logger.getLogger(DataFacade.class.getName()).log(Level.INFO, null, "data | attack "+coords.getX()+"-"+coords.getY()); System.out.println("datafacade | attack "+coords.getX()+"-"+coords.getY());
} }
......
...@@ -10,6 +10,7 @@ import java.util.List; ...@@ -10,6 +10,7 @@ import java.util.List;
import com.utclo23.data.structure.BelgianGame; import com.utclo23.data.structure.BelgianGame;
import com.utclo23.data.structure.ClassicGame; import com.utclo23.data.structure.ClassicGame;
import com.utclo23.data.structure.ComputerPlayer;
import com.utclo23.data.structure.Game; import com.utclo23.data.structure.Game;
import com.utclo23.data.structure.GameType; import com.utclo23.data.structure.GameType;
import com.utclo23.data.structure.LightPublicUser; import com.utclo23.data.structure.LightPublicUser;
...@@ -40,13 +41,17 @@ public class GameFactory { ...@@ -40,13 +41,17 @@ public class GameFactory {
if(computerMode) if(computerMode)
{ {
System.out.println("Data | computer mode");
//add second player //add second player
Player j2 = new Player(LightPublicUser.generateComputerProfile()); Player j2 = new ComputerPlayer();
j2.setComputer(true); j2.setComputer(true);
players.add(j2); players.add(j2);
} }
System.out.println("player number : "+players.size());
List<LightPublicUser> spectators = new ArrayList<>(); List<LightPublicUser> spectators = new ArrayList<>();
spectators.add(creator); spectators.add(creator);
......
package com.utclo23.data.module; package com.utclo23.data.module;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.utclo23.com.ComFacade; import com.utclo23.com.ComFacade;
import com.utclo23.data.module.DataException;
import com.utclo23.data.configuration.Configuration; import com.utclo23.data.configuration.Configuration;
import com.utclo23.data.facade.DataFacade; import com.utclo23.data.facade.DataFacade;
import com.utclo23.data.structure.ComputerPlayer; import com.utclo23.data.structure.ComputerPlayer;
...@@ -17,9 +14,7 @@ import com.utclo23.data.structure.Message; ...@@ -17,9 +14,7 @@ import com.utclo23.data.structure.Message;
import com.utclo23.data.structure.Mine; import com.utclo23.data.structure.Mine;
import com.utclo23.data.structure.StatGame; import com.utclo23.data.structure.StatGame;
import com.utclo23.ihmtable.IIHMTableToData; import com.utclo23.ihmtable.IIHMTableToData;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.rmi.server.UID;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
...@@ -50,7 +45,6 @@ public class GameMediator { ...@@ -50,7 +45,6 @@ public class GameMediator {
* Constructor * Constructor
*/ */
public GameMediator(DataFacade dataFacade) { public GameMediator(DataFacade dataFacade) {
Logger.getLogger(this.getClass().getName()).log(Level.INFO, "Création du Game Mediator");
this.dataFacade = dataFacade; this.dataFacade = dataFacade;
this.gamesMap = new HashMap<>(); this.gamesMap = new HashMap<>();
...@@ -65,6 +59,7 @@ public class GameMediator { ...@@ -65,6 +59,7 @@ public class GameMediator {
} }
public void setCurrentGame(Game currentGame) { public void setCurrentGame(Game currentGame) {
Logger.getLogger(this.getClass().getName()).log(Level.INFO, "Data | ");
this.currentGame = currentGame; this.currentGame = currentGame;
} }
...@@ -96,9 +91,9 @@ public class GameMediator { ...@@ -96,9 +91,9 @@ public class GameMediator {
if (comFacade != null && game != null) { if (comFacade != null && game != null) {
System.out.println("notify"); System.out.println("notify");
comFacade.notifyNewGame(game.getStatGame()); comFacade.notifyNewGame(game.getStatGame());
} else {
System.out.println("no notify");
} }
else System.out.println("no notify");
//set current game //set current game
Logger.getLogger(this.getClass().getName()).log(Level.INFO, "Création d'un Game"); Logger.getLogger(this.getClass().getName()).log(Level.INFO, "Création d'un Game");
...@@ -151,13 +146,10 @@ public class GameMediator { ...@@ -151,13 +146,10 @@ public class GameMediator {
/** /**
* Check if the ship has the right amount of coordinates * Check if the ship has the right amount of coordinates
*/ */
for (Ship s : player.getShips()) { if (ship.getSize() != ship.getListCoord().size()) {
if(s.getSize() != s.getListCoord().size()){ throw new DataException("Data : ship has more coordinates than its size");
throw new DataException("Data : ship has more coordinates than its size");
}
} }
/** /**
* determine all positions taken * * determine all positions taken *
*/ */
...@@ -171,7 +163,7 @@ public class GameMediator { ...@@ -171,7 +163,7 @@ public class GameMediator {
//test the ship isn't placed on coordinates that are already taken //test the ship isn't placed on coordinates that are already taken
for (Coordinate c : ship.getListCoord()) { for (Coordinate c : ship.getListCoord()) {
if (positionMap.containsKey("" + c.getX() + "-" + c.getY())) { if (positionMap.containsKey("" + c.getX() + "-" + c.getY())) {
throw new DataException("Data : position already taken"); throw new DataException("Data : position already taken " + c.getX() + "-" + c.getY());
} }
} }
...@@ -182,8 +174,8 @@ public class GameMediator { ...@@ -182,8 +174,8 @@ public class GameMediator {
//last ship //last ship
if (this.currentGame.getTemplateShips().size() == player.getShips().size()) { if (this.currentGame.getTemplateShips().size() == player.getShips().size()) {
if (this.dataFacade.getComfacade() != null) { if (this.dataFacade.getComfacade() != null) {
this.dataFacade.getComfacade().sendShipsToEnnemy(player.getShips(), this.currentGame.getRecipients()); this.dataFacade.getComfacade().sendShipsToEnnemy(player.getShips(), this.currentGame.getRecipients());
checkPlayersReady();
} }
} }
...@@ -192,23 +184,23 @@ public class GameMediator { ...@@ -192,23 +184,23 @@ public class GameMediator {
} }
} }
/** /**
* *
*/ */
public void setComputerShips() { public void setComputerShips() {
Player cPlayer = this.currentGame.getComputerPlayer(); Player cPlayer = this.currentGame.getComputerPlayer();
List<Ship> listShips = this.currentGame.getTemplateShips(); List<Ship> listShips = this.currentGame.getTemplateShips();
for(int s = 0; s < listShips.size(); s++) { for (int s = 0; s < listShips.size(); s++) {
craftCoordinates(listShips, listShips.get(s)); craftCoordinates(listShips, listShips.get(s));
} }
} }
/** /**
* Gives a random position to a ship. * Gives a random position to a ship.
* *
* @param previousShips * @param previousShips
* @param ship * @param ship
*/ */
private void craftCoordinates(List<Ship> previousShips, Ship ship) { private void craftCoordinates(List<Ship> previousShips, Ship ship) {
List<List<Coordinate>> allCoords = this.createAvailableCoordinates(previousShips, ship); List<List<Coordinate>> allCoords = this.createAvailableCoordinates(previousShips, ship);
...@@ -216,57 +208,55 @@ public class GameMediator { ...@@ -216,57 +208,55 @@ public class GameMediator {
int position = r.nextInt(allCoords.size()); int position = r.nextInt(allCoords.size());
ship.setListCoord(allCoords.get(position)); ship.setListCoord(allCoords.get(position));
} }
private List<List<Coordinate>> createAvailableCoordinates(List<Ship> previousShips, Ship ship) { private List<List<Coordinate>> createAvailableCoordinates(List<Ship> previousShips, Ship ship) {
int size = ship.getSize(); int size = ship.getSize();
List<List<Coordinate>> returnList = new ArrayList(); List<List<Coordinate>> returnList = new ArrayList();
for(int x = 0; x < 10; x++) { for (int x = 0; x < 10; x++) {
for(int y = 0; y < 10; y++) { for (int y = 0; y < 10; y++) {
Coordinate c = new Coordinate(x,y); Coordinate c = new Coordinate(x, y);
if(c.isAllowed(previousShips)) { if (c.isAllowed(previousShips)) {
//Crafting the West -> East coordinates. //Crafting the West -> East coordinates.
List<Coordinate> coordsWE = new ArrayList(); List<Coordinate> coordsWE = new ArrayList();
coordsWE.add(new Coordinate(x,y)); coordsWE.add(new Coordinate(x, y));
boolean allowed = true; boolean allowed = true;
for(int s = 0; s < previousShips.size(); s++) { for (int s = 0; s < previousShips.size(); s++) {
Coordinate cSuite = new Coordinate(x + s, y); Coordinate cSuite = new Coordinate(x + s, y);
if(!cSuite.isAllowed(previousShips)) { if (!cSuite.isAllowed(previousShips)) {
allowed = false; allowed = false;
break; break;
} else { } else {
coordsWE.add(cSuite); coordsWE.add(cSuite);
} }
} }
if(allowed) { if (allowed) {
returnList.add(coordsWE); returnList.add(coordsWE);
} }
//Crafting the South->North coordinates. //Crafting the South->North coordinates.
List<Coordinate> coordsSN = new ArrayList(); List<Coordinate> coordsSN = new ArrayList();
coordsSN.add(new Coordinate(x,y)); coordsSN.add(new Coordinate(x, y));
allowed = true; allowed = true;
for(int s = 0; s < previousShips.size(); s++) { for (int s = 0; s < previousShips.size(); s++) {
Coordinate cSuite = new Coordinate(x, y + s); Coordinate cSuite = new Coordinate(x, y + s);
if(!cSuite.isAllowed(previousShips)) { if (!cSuite.isAllowed(previousShips)) {
allowed = false; allowed = false;
break; break;
} else { } else {
coordsSN.add(cSuite); coordsSN.add(cSuite);
} }
} }
if(allowed) { if (allowed) {
returnList.add(coordsSN); returnList.add(coordsSN);
} }
} }
} }
} }
return returnList; return returnList;
} }
/** /**
* *
* @param coordinate * @param coordinate
...@@ -275,18 +265,22 @@ public class GameMediator { ...@@ -275,18 +265,22 @@ public class GameMediator {
* @throws DataException * @throws DataException
*/ */
public Pair<Integer, Ship> attack(Coordinate coordinate, boolean isTrueAttack) throws DataException, IOException, ClassNotFoundException { public Pair<Integer, Ship> attack(Coordinate coordinate, boolean isTrueAttack) throws DataException, IOException, ClassNotFoundException {
if (this.currentGame != null) { if (this.currentGame != null) {
Player player = this.currentGame.getCurrentPlayer(); Player player = this.currentGame.getCurrentPlayer();
System.out.println("ATTACK CURRENT PLAYER " + player.getLightPublicUser().getPlayerName());
Pair<Integer, Ship> pairReturn; Pair<Integer, Ship> pairReturn;
if (player == null) { if (player == null) {
throw new DataException("Data : player not found for set player ship"); throw new DataException("Data : player not found for set player ship");
} }
//return the result of the attack //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=1, then add mine to player ; otherwise, that is just a test, no stat of mine
if (isTrueAttack == true) { if (isTrueAttack == true) {
//check if mine already used at current location System.out.println("--------------------------------------------------");
System.out.println("Data | True attack -------------------------------");
//check if mine already used at current location
List<Mine> mines = player.getMines(); List<Mine> mines = player.getMines();
if (mines.size() > 0) { if (mines.size() > 0) {
for (int i = 0; i < mines.size(); i++) { for (int i = 0; i < mines.size(); i++) {
...@@ -300,102 +294,100 @@ public class GameMediator { ...@@ -300,102 +294,100 @@ public class GameMediator {
pairReturn = this.currentGame.attack(player, coordinate, isTrueAttack); pairReturn = this.currentGame.attack(player, coordinate, isTrueAttack);
// Forward to other players.
dataFacade.getComfacade().notifyNewCoordinates(new Mine(player, coordinate), currentGame.getRecipients());
//save with caretaker //save with caretaker
this.currentGame.getCaretaker().add(this.currentGame.saveStateToMemento()); this.currentGame.getCaretaker().add(this.currentGame.saveStateToMemento());
//if creator of the game
//Test if this game is finished if (this.currentGame.getStatGame().getCreator().getId().equals(this.dataFacade.getUserMediator().getMyPublicUserProfile().getId())) {
//If this game is finished, leave the game //if computer mode ?
if(this.currentGame.getStatGame().getWinner() != null){ if (this.currentGame.isComputerGame()) {
this.leaveGame(); //attack
// this.currentGame.nextTurn();
Mine m = this.currentGame.getComputerPlayer().randomMine();
this.forwardCoordinates(m);
System.out.println("verification ia bateaux de ... " + this.currentGame.ennemyOf(this.currentGame.getComputerPlayer()).getLightPublicUser().getPlayerName());
boolean check = false;
for (Ship ship : this.currentGame.ennemyOf(this.currentGame.getComputerPlayer()).getShips()) {
if (this.currentGame.isShipTouched(ship, m)) {
System.out.println("IA focus on location");
this.currentGame.getComputerPlayer().setFocus(m.getCoord());
check = true;
if (this.currentGame.isShipDestroyed(ship, this.currentGame.getComputerPlayer().getMines())) {
this.currentGame.getComputerPlayer().loseFocus();
System.out.println("IA loses total focus");
}
}
}
if (!check) {
System.out.println("IA loses focus");
this.currentGame.getComputerPlayer().setFocus(null);
}
}
} }
return pairReturn;
} else { } else {
// In the case of a test, that's possible that the current player is not // In the case of a test, that's possible that the current player is not
// the right player to test the mine (that means the enemy of the player // the right player to test the mine (that means the enemy of the player
// is the right person to test the mine) // is the right person to test the mine)
pairReturn = this.currentGame.attack(player, coordinate, isTrueAttack); pairReturn = this.currentGame.attack(player, coordinate, isTrueAttack);
if (pairReturn.getKey() == 0 && pairReturn.getValue() == null) { if (pairReturn.getKey() == 0 && pairReturn.getValue() == null) {
pairReturn = this.currentGame.attack(this.currentGame.ennemyOf(player), coordinate, isTrueAttack); //pairReturn = this.currentGame.attack(this.currentGame.ennemyOf(player), coordinate, isTrueAttack);
} }
return pairReturn;
}
} else {
throw new DataException("Data : player dosn't existe");
}
}
public void attackIA() throws DataException {
//check if current game
if (this.currentGame != null) {
ComputerPlayer computerPlayer = (ComputerPlayer) this.currentGame.getCurrentPlayer();
Coordinate coord = null; //location of shoot
//random mode, no focus on location
if (true) {
//make a new shot not already chosen
coord = this.generateRandomPosition();
boolean alreadyDone = false;
do {
for (Mine mine : computerPlayer.getMines()) {
if (mine.getCoord().getX() == coord.getX() && mine.getCoord().getY() == coord.getY()) {
alreadyDone = true;
break;
}
}
coord = this.generateRandomPosition();
} while (alreadyDone);
} }
this.currentGame.nextTurn(); //this.currentGame.nextTurn();
//save with caretaker if (isTrueAttack) {
//this.currentGame.getCaretaker().add(this.currentGame.saveStateToMemento()); //Test if this game is finished
} //If this game is finished, leave the game