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,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
......
......@@ -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,12 +146,9 @@ public class GameMediator {
/**
* Check if the ship has the right amount of coordinates
*/
for (Ship s : player.getShips()) {
if(s.getSize() != s.getListCoord().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();
}
}
......@@ -199,7 +191,7 @@ 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));
}
}
......@@ -220,42 +212,42 @@ public class GameMediator {
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)) {
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);
}
}
......@@ -265,8 +257,6 @@ public class GameMediator {
return returnList;
}
/**
*
* @param coordinate
......@@ -278,6 +268,8 @@ public class GameMediator {
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");
......@@ -286,6 +278,8 @@ public class GameMediator {
//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) {
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) {
......@@ -300,98 +294,96 @@ 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());
//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();
//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
// 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);
}
return pairReturn;
}
} else {
throw new DataException("Data : player dosn't existe");
}
}
Mine m = this.currentGame.getComputerPlayer().randomMine();
this.forwardCoordinates(m);
public void attackIA() throws DataException {
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;
//check if current game
if (this.currentGame != null) {
if (this.currentGame.isShipDestroyed(ship, this.currentGame.getComputerPlayer().getMines())) {
this.currentGame.getComputerPlayer().loseFocus();
System.out.println("IA loses total focus");
ComputerPlayer computerPlayer = (ComputerPlayer) this.currentGame.getCurrentPlayer();
Coordinate coord = null; //location of shoot
}
}
}
//random mode, no focus on location
if (true) {
if (!check) {
System.out.println("IA loses focus");
this.currentGame.getComputerPlayer().setFocus(null);
}
//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();
} 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);
}
} 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("id " + id);
if (user == null) {
System.out.println("user is null");
}
if(this.currentGame == 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) {
......@@ -417,7 +409,7 @@ public class GameMediator {
//send game
//
System.out.println(" ROLE : "+role);
System.out.println(" ROLE : " + role);
this.dataFacade.getComfacade().connectionToGame(game, role);
}
......@@ -472,24 +464,20 @@ public class GameMediator {
/**
* Exit current game.
*/
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;
......@@ -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,25 +516,23 @@ 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.
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;
}
}
}
/**
......@@ -561,21 +548,23 @@ public class GameMediator {
* Win if the game has no winner yet.
*/
public void defWin() throws DataException {
if(this.currentGame == null) {
if (this.currentGame == null) {
throw new DataException("Pas de partie en cours.");
}
if(this.getCurrentGame().getWinner() == null) {
if (this.getCurrentGame().getWinner() == null) {
this.win();
}
}
/**
* Check if there is no current game or there is one but it already has a winner.
* Check if there is no current game or there is one but it already has a
* winner.
*
* @return
*/
public boolean isFinishedGame() {
boolean finished = true;
if((this.currentGame != null) && (this.currentGame.getWinner() == null)) {
if ((this.currentGame != null) && (this.currentGame.getWinner() == null)) {
finished = false;
}
return finished;
......@@ -589,7 +578,8 @@ public class GameMediator {
}
/**
* Return the status of the owner in the current game (player, spectator or null).
* Return the status of the owner in the current game (player, spectator or
* null).
*
* @return
*/
......@@ -599,14 +589,15 @@ public class GameMediator {
}
/**
* Return the status of user in the current game (player, spectator or null).
* Return the status of user in the current game (player, spectator or
* null).
*
* @param user
* @return
*/
public String getUserStatus(LightPublicUser user) {
String status = null;
if(this.currentGame.isPlayer(user)) {
if (this.currentGame.isPlayer(user)) {
status = "player";
} else if (this.currentGame.isSpectator(user)) {
status = "spectator";
......@@ -617,8 +608,8 @@ public class GameMediator {
public void setEnnemyShips(List<Ship> ships) {
// Check game is instanciated
if (this.currentGame != null){
if(!ships.isEmpty()){
if (this.currentGame != null) {
if (!ships.isEmpty()) {
// retrieve the id of the player that put the ships
String player_id = ships.get(0).getOwner().getLightPublicUser().getId();
......@@ -627,7 +618,51 @@ public class GameMediator {
// Set the ships
p.setShips(ships);
checkPlayersReady();
}
}
}
/**