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 {
public void sendShipsToEnnemy(List<Ship> listShips, List<LightPublicUser> recipients) {
M_PlaceShip mPlaceship = new M_PlaceShip(iDataCom.getMyPublicUserProfile(), listShips);
for (LightPublicUser recipient : recipients) {
Sender os = new Sender(kIpCtrl.getHashMap().get(recipient.getId()).getHostAddress(), kIpCtrl.getPort(), mPlaceship);
new Thread(os).start();
if (kIpCtrl.getHashMap().get(recipient.getId()) != null) {
Sender os = new Sender(kIpCtrl.getHashMap().get(recipient.getId()).getHostAddress(), kIpCtrl.getPort(), mPlaceship);
new Thread(os).start();
}
}
}
......@@ -106,8 +108,10 @@ public class ComFacade {
public void notifyNewCoordinates(Mine mine, List<LightPublicUser> recipients) {
M_PlaceMine mPlaceMine = new M_PlaceMine(iDataCom.getMyPublicUserProfile(), mine);
for (LightPublicUser recipient : recipients) {
Sender os = new Sender(kIpCtrl.getHashMap().get(recipient.getId()).getHostAddress(), kIpCtrl.getPort(), mPlaceMine);
new Thread(os).start();
if (kIpCtrl.getHashMap().get(recipient.getId()) != null) {
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 {
}
/**
* 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(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;
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.generateComputerProfile());
Player j2 = new ComputerPlayer();
j2.setComputer(true);
players.add(j2);
}
System.out.println("player number : "+players.size());
List<LightPublicUser> spectators = new ArrayList<>();
spectators.add(creator);
......
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.data.module.DataException;
import com.utclo23.data.configuration.Configuration;
import com.utclo23.data.facade.DataFacade;
import com.utclo23.data.structure.ComputerPlayer;
......@@ -17,9 +14,7 @@ import com.utclo23.data.structure.Message;
import com.utclo23.data.structure.Mine;
import com.utclo23.data.structure.StatGame;
import com.utclo23.ihmtable.IIHMTableToData;
import java.io.File;
import java.io.IOException;
import java.rmi.server.UID;
import java.util.ArrayList;
import java.util.HashMap;
......@@ -50,7 +45,6 @@ public class GameMediator {
* Constructor
*/
public GameMediator(DataFacade dataFacade) {
Logger.getLogger(this.getClass().getName()).log(Level.INFO, "Création du Game Mediator");
this.dataFacade = dataFacade;
this.gamesMap = new HashMap<>();
......@@ -65,6 +59,7 @@ public class GameMediator {
}
public void setCurrentGame(Game currentGame) {
Logger.getLogger(this.getClass().getName()).log(Level.INFO, "Data | ");
this.currentGame = currentGame;
}
......@@ -96,9 +91,9 @@ public class GameMediator {
if (comFacade != null && game != null) {
System.out.println("notify");
comFacade.notifyNewGame(game.getStatGame());
} else {
System.out.println("no notify");
}
else System.out.println("no notify");
//set current game
Logger.getLogger(this.getClass().getName()).log(Level.INFO, "Création d'un Game");
......@@ -151,13 +146,10 @@ public class GameMediator {
/**
* Check if the ship has the right amount of coordinates
*/
for (Ship s : player.getShips()) {
if(s.getSize() != s.getListCoord().size()){
throw new DataException("Data : ship has more coordinates than its size");
}
if (ship.getSize() != ship.getListCoord().size()) {
throw new DataException("Data : ship has more coordinates than its size");
}
/**
* determine all positions taken *
*/
......@@ -171,7 +163,7 @@ public class GameMediator {
//test the ship isn't placed on coordinates that are already taken
for (Coordinate c : ship.getListCoord()) {
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 {
//last ship
if (this.currentGame.getTemplateShips().size() == player.getShips().size()) {
if (this.dataFacade.getComfacade() != null) {
this.dataFacade.getComfacade().sendShipsToEnnemy(player.getShips(), this.currentGame.getRecipients());
checkPlayersReady();
}
}
......@@ -192,23 +184,23 @@ public class GameMediator {
}
}
/**
*
*
*/
public void setComputerShips() {
Player cPlayer = this.currentGame.getComputerPlayer();
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));
}
}
/**
* Gives a random position to a ship.
*
*
* @param previousShips
* @param ship
* @param ship
*/
private void craftCoordinates(List<Ship> previousShips, Ship ship) {
List<List<Coordinate>> allCoords = this.createAvailableCoordinates(previousShips, ship);
......@@ -216,57 +208,55 @@ public class GameMediator {
int position = r.nextInt(allCoords.size());
ship.setListCoord(allCoords.get(position));
}
private List<List<Coordinate>> createAvailableCoordinates(List<Ship> previousShips, Ship ship) {
int size = ship.getSize();
List<List<Coordinate>> returnList = new ArrayList();
for(int x = 0; x < 10; x++) {
for(int y = 0; y < 10; y++) {
Coordinate c = new Coordinate(x,y);
if(c.isAllowed(previousShips)) {
List<List<Coordinate>> returnList = new ArrayList();
for (int x = 0; x < 10; x++) {
for (int y = 0; y < 10; y++) {
Coordinate c = new Coordinate(x, y);
if (c.isAllowed(previousShips)) {
//Crafting the West -> East coordinates.
List<Coordinate> coordsWE = new ArrayList();
coordsWE.add(new Coordinate(x,y));
coordsWE.add(new Coordinate(x, y));
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);
if(!cSuite.isAllowed(previousShips)) {
if (!cSuite.isAllowed(previousShips)) {
allowed = false;
break;
} else {
coordsWE.add(cSuite);
}
}
if(allowed) {
if (allowed) {
returnList.add(coordsWE);
}
//Crafting the South->North coordinates.
List<Coordinate> coordsSN = new ArrayList();
coordsSN.add(new Coordinate(x,y));
coordsSN.add(new Coordinate(x, y));
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);
if(!cSuite.isAllowed(previousShips)) {
if (!cSuite.isAllowed(previousShips)) {
allowed = false;
break;
} else {
coordsSN.add(cSuite);
}
}
if(allowed) {
if (allowed) {
returnList.add(coordsSN);
}
}
}
}
return returnList;
}
/**
*
* @param coordinate
......@@ -275,18 +265,22 @@ public class GameMediator {
* @throws DataException
*/
public Pair<Integer, Ship> attack(Coordinate coordinate, boolean isTrueAttack) throws DataException, IOException, ClassNotFoundException {
if (this.currentGame != null) {
Player player = this.currentGame.getCurrentPlayer();
System.out.println("ATTACK CURRENT PLAYER " + player.getLightPublicUser().getPlayerName());
Pair<Integer, Ship> pairReturn;
if (player == null) {
throw new DataException("Data : player not found for set player ship");
}
//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
System.out.println("--------------------------------------------------");
System.out.println("Data | True attack -------------------------------");
//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++) {
......@@ -300,102 +294,100 @@ public class GameMediator {
pairReturn = this.currentGame.attack(player, coordinate, isTrueAttack);
// Forward to other players.
dataFacade.getComfacade().notifyNewCoordinates(new Mine(player, coordinate), currentGame.getRecipients());
//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();
//if creator of the game
if (this.currentGame.getStatGame().getCreator().getId().equals(this.dataFacade.getUserMediator().getMyPublicUserProfile().getId())) {
//if computer mode ?
if (this.currentGame.isComputerGame()) {
//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 {
// 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
// is the right person to test the mine)
pairReturn = this.currentGame.attack(player, coordinate, isTrueAttack);
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();
//save with caretaker
//this.currentGame.getCaretaker().add(this.currentGame.saveStateToMemento());
}
}
//this.currentGame.nextTurn();
if (isTrueAttack) {
//Test if this game is finished
//If this game is finished, leave the game
if (this.currentGame.getStatGame().getWinner() != null) {
this.leaveGame();
}
}
public Coordinate generateRandomPosition() {
//reduce possible locations
int x = (int) (Math.random() * (Configuration.WIDTH));
int y = (int) (Math.random() * (Configuration.HEIGHT));
System.out.println("--------------------------------------------------");
Coordinate coordinate = new Coordinate(x, y);
return coordinate;
return pairReturn;
} else {
throw new DataException("Data : player dosn't existe");
}
}
/**
*
* Update current game's list as a new user has joined it.
*
* @param user the new user who has joined
* @param id id of the stat game
* @param role role of the new user
*/
public void updateGameList(LightPublicUser user, String id, String role) throws DataException {
System.out.println("id "+id);
if(user == null)
{
System.out.println("user is null");
}
if(this.currentGame == null)
{
System.out.println("current game is null");
}
System.out.println("id " + id);
if (user == null) {
System.out.println("user is null");
}
if (this.currentGame == null) {
System.out.println("current game is null");
}
if (this.currentGame.getId().compareTo(id) == 0) {
System.out.println("add Urole "+role);
System.out.println("add Urole " + role);
this.getCurrentGame().addUser(user, role);
if (this.dataFacade.getComfacade() != null) {
System.out.println("data join game resp");
this.dataFacade.getComfacade().joinGameResponse(true, user.getId(), this.currentGame.getStatGame());
......@@ -409,16 +401,16 @@ public class GameMediator {
public void gameConnectionRequestGame(String id, String role) {
role = role.toLowerCase();
if (this.dataFacade.getComfacade() != null) {
StatGame game = null;
if (this.gamesMap.containsKey(id)) {
game = this.gamesMap.get(id);
//send game
//
System.out.println(" ROLE : "+role);
System.out.println(" ROLE : " + role);
this.dataFacade.getComfacade().connectionToGame(game, role);
}
......@@ -462,39 +454,35 @@ public class GameMediator {
/**
* Set the winner of the current game.
*
* @param winner
*
* @param winner
*/
public void setWinner(LightPublicUser winner) {
this.currentGame.setWinner(winner);
}
/**
* Exit current game.
*/
public void leaveGame() {
public void leaveGame() {
//Sauvegarde à ajouter.
this.dataFacade.getUserMediator().addPlayedGame(this.currentGame.getStatGame());
//Sauvegarde à ajouter, que l'owner soit joueur ou pas.
String status = this.getOwnerStatus();
if(status == "player") {
if(this.currentGame.getStatGame().getWinner() == null) {
if (status == "player") {
if (this.currentGame.getStatGame().getWinner() == null) {
this.giveUp();
}
this.dataFacade.getUserMediator().addPlayedGame(this.currentGame.getStatGame());
}
this.currentGame = null;
// this.currentGame = null;
}
public void receptionGame(Game game) {
System.out.println("reception game ... ");
this.currentGame = game;
if (this.dataFacade.getIhmMainFacade() != null) {
System.out.println("give to ihm ... ");
this.dataFacade.getIhmMainFacade().receptionGame(game);
}
......@@ -514,6 +502,7 @@ public class GameMediator {
* @param mine the mine placed
*/
public void forwardCoordinates(Mine mine) {
System.out.println("forward coordinate..");
List<Ship> ships = this.currentGame.getCurrentPlayer().getShips();
Ship shipDestroyed = null;
boolean touched = false;
......@@ -527,27 +516,25 @@ public class GameMediator {
}
if (this.dataFacade.getIhmTablefacade() != null) {
//this.dataFacade.getIhmTablefacade().feedback(mine,touched,shipDestroyed) ;
this.dataFacade.getIhmTablefacade().feedBack(mine.getCoord(), touched, shipDestroyed);
}
if (this.currentGame.isGameFinishedByEnnemy()) {
//Sauvegarde à ajouter, que l'owner soit joueur ou pas.
//Sauvegarde à ajouter, que l'owner soit joueur ou pas.