Commit 330fd18d authored by dkonam's avatar dkonam
Browse files

merge conflict

parents 90268335 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>
<?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){
if (kIpCtrl.getHashMap().get(recipient.getId()) != null) {
Sender os = new Sender(kIpCtrl.getHashMap().get(recipient.getId()).getHostAddress(), kIpCtrl.getPort(), mPlaceship);
new Thread(os).start();
}
}
}
}
......@@ -112,13 +114,13 @@ 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
* users Data's module.
......
......@@ -142,7 +142,7 @@ public class DataFacade implements IDataCom, IDataIHMTable, IDataIHMMain {
*/
@Override
public void updatePlayername(String playername) throws DataException {
this.userMediator.updatePlayername(playername);
}
......@@ -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,10 @@ public class DataFacade implements IDataCom, IDataIHMTable, IDataIHMMain {
*/
@Override
public void setEnnemyShips(List<Ship> ships) {
//this.gameMediator.setEnnemyShips(ships);
Logger.getLogger(DataFacade.class.getName()).log(Level.INFO, null, "data | set ennemy ships");
this.gameMediator.setEnnemyShips(ships);
}
/**
......@@ -257,6 +260,7 @@ public class DataFacade implements IDataCom, IDataIHMTable, IDataIHMMain {
*/
@Override
public void forwardCoordinates(Mine mine) {
Logger.getLogger(DataFacade.class.getName()).log(Level.INFO, null, "data | forward coordinates");
this.gameMediator.forwardCoordinates(mine);
}
......@@ -266,8 +270,11 @@ public class DataFacade implements IDataCom, IDataIHMTable, IDataIHMMain {
*/
@Override
public void leaveGame() {
Logger.getLogger(DataFacade.class.getName()).log(Level.INFO, null, "data | leave game");
PublicUser user = this.userMediator.getMyPublicUserProfile();
//LightPublicUser user = this.userMediator.getMyLightPublicUserProfile(); com doit prendre en argument un LightPublicUser
this.comfacade.leaveGame();
this.gameMediator.leaveGame();
try {
......@@ -282,6 +289,8 @@ public class DataFacade implements IDataCom, IDataIHMTable, IDataIHMMain {
*/
@Override
public void opponentHasLeftGame() {
Logger.getLogger(DataFacade.class.getName()).log(Level.INFO, null, "data | opponent has left");
if (!this.gameMediator.isFinishedGame()) {
try {
this.gameMediator.defWin();
......@@ -298,7 +307,13 @@ public class DataFacade implements IDataCom, IDataIHMTable, IDataIHMMain {
*/
@Override
public void connectionLostWithOpponent() {
Logger.getLogger(DataFacade.class.getName()).log(Level.INFO, null, "data | connection lost");
//stats.
this.gameMediator.setWinner(null);
this.gameMediator.getCurrentGame().getStatGame().setGameAbandonned(true);
this.gameMediator.leaveGame();
this.ihmTablefacade.connectionLostWithOpponent();
}
......@@ -363,6 +378,8 @@ public class DataFacade implements IDataCom, IDataIHMTable, IDataIHMMain {
*/
@Override
public void forwardMessage(Message msg) {
Logger.getLogger(DataFacade.class.getName()).log(Level.INFO, null, "data | forward message");
this.gameMediator.forwardMessage(msg);
}
......@@ -402,8 +419,8 @@ public class DataFacade implements IDataCom, IDataIHMTable, IDataIHMMain {
*/
@Override
public void updateGameList(LightPublicUser user, String id, String role) throws DataException {
Logger.getLogger(DataFacade.class.getName()).log(Level.INFO, null, "data | update game list "+user.getPlayerName()+" "+id+" "+role);
this.gameMediator.updateGameList(user, id, role);
this.gameMediator.getCurrentGame().addUser(user, role);
}
/**
......@@ -414,6 +431,8 @@ public class DataFacade implements IDataCom, IDataIHMTable, IDataIHMMain {
*/
@Override
public List<Ship> getTemplateShips() throws DataException {
Logger.getLogger(DataFacade.class.getName()).log(Level.INFO, null, "data | get template ships");
if (this.gameMediator.getCurrentGame() != null) {
return this.gameMediator.getCurrentGame().getTemplateShips();
} else {
......@@ -428,7 +447,8 @@ public class DataFacade implements IDataCom, IDataIHMTable, IDataIHMMain {
*
*/
public void setShip(Ship ship) throws DataException {
Logger.getLogger(DataFacade.class.getName()).log(Level.INFO, null, "data | set ship");
this.gameMediator.setPlayerShip(ship);
}
......@@ -447,11 +467,20 @@ public class DataFacade implements IDataCom, IDataIHMTable, IDataIHMMain {
*/
@Override
public Pair<Integer, Ship> attack(Coordinate coords, boolean isAttack) {
if(isAttack)
{
System.out.println("datafacade | attack "+coords.getX()+"-"+coords.getY());
}
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);
return null;
}
}
......@@ -485,6 +514,9 @@ public class DataFacade implements IDataCom, IDataIHMTable, IDataIHMMain {
*/
@Override
public void sendMessage(String text) {
Logger.getLogger(DataFacade.class.getName()).log(Level.INFO, null, "data | send message "+text);
this.gameMediator.sendMessage(text);
}
......@@ -655,8 +687,10 @@ public class DataFacade implements IDataCom, IDataIHMTable, IDataIHMMain {
@Override
public void receptionGame(Game game) throws DataException {
Logger.getLogger(DataFacade.class.getName()).log(Level.INFO, null, "data | reception game");
if (game != null) {
Logger.getLogger(DataFacade.class.getName()).log(Level.INFO, null, "data | game not null");
this.gameMediator.receptionGame(game);
}
......@@ -676,4 +710,39 @@ public class DataFacade implements IDataCom, IDataIHMTable, IDataIHMMain {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
/**
*
* send number of victories
*
* @return int number of victories
* @throws DataException
*/
@Override
public int getNumberVictories() throws DataException {
return this.userMediator.getNumberVictories() ;
}
/**
*
* send number of defeats
*
* @return int number of defeats
* @throws DataException
*/
@Override
public int getNumberDefeats() throws DataException {
return this.userMediator.getNumberDefeats() ;
}
/**
*
* send number of abandons
*
* @return int number of abandons
* @throws DataException
*/
@Override
public int getNumberAbandons() throws DataException {
return this.userMediator.getNumberAbandons() ;
}
}
......@@ -47,6 +47,8 @@ public interface IDataIHMMain {
public void updateFileImage(String fileImage) throws DataException;
public void updatePassword(String password) throws DataException;
public int getNumberVictories() throws DataException;
public int getNumberDefeats() throws DataException;
public int getNumberAbandons() throws DataException;
}
\ No newline at end of file
......@@ -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,14 +14,13 @@ 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;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.util.Pair;
......@@ -49,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<>();
......@@ -64,6 +59,7 @@ public class GameMediator {
}
public void setCurrentGame(Game currentGame) {
Logger.getLogger(this.getClass().getName()).log(Level.INFO, "Data | ");
this.currentGame = currentGame;
}
......@@ -95,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");
......@@ -150,11 +146,10 @@ public class GameMediator {
/**
* Check if the ship has the right amount of coordinates
*/
if(ship.getSize() != ship.getListCoord().size()){
if (ship.getSize() != ship.getListCoord().size()) {
throw new DataException("Data : ship has more coordinates than its size");
}
/**
* determine all positions taken *
*/
......@@ -168,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());
}
}
......@@ -179,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();
}
}
......@@ -190,6 +185,78 @@ 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++) {
craftCoordinates(listShips, listShips.get(s));
}
}
/**
* Gives a random position to a ship.
*
* @param previousShips
* @param ship
*/
private void craftCoordinates(List<Ship> previousShips, Ship ship) {
List<List<Coordinate>> allCoords = this.createAvailableCoordinates(previousShips, ship);
Random r = new Random();
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)) {
//Crafting the West -> East coordinates.
List<Coordinate> coordsWE = new ArrayList();
coordsWE.add(new Coordinate(x, y));
boolean allowed = true;
for (int s = 0; s < previousShips.size(); s++) {
Coordinate cSuite = new Coordinate(x + s, y);
if (!cSuite.isAllowed(previousShips)) {
allowed = false;
break;
} else {
coordsWE.add(cSuite);
}
}
if (allowed) {
returnList.add(coordsWE);
}
//Crafting the South->North coordinates.
List<Coordinate> coordsSN = new ArrayList();
coordsSN.add(new Coordinate(x, y));
allowed = true;
for (int s = 0; s < previousShips.size(); s++) {
Coordinate cSuite = new Coordinate(x, y + s);
if (!cSuite.isAllowed(previousShips)) {
allowed = false;
break;
} else {
coordsSN.add(cSuite);
}
}
if (allowed) {
returnList.add(coordsSN);
}
}
}
}
return returnList;
}
/**
*
* @param coordinate
......@@ -198,121 +265,129 @@ 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");
}
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;
//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) {
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);
// Forward to other players.
dataFacade.getComfacade().notifyNewCoordinates(new Mine(player, coordinate), currentGame.getRecipients());
//save with caretaker
this.currentGame.getCaretaker().add(this.currentGame.saveStateToMemento());
return pairReturn;
//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);
}
}
}
} 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;
}