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);
......
......@@ -766,5 +766,69 @@ public class UserMediator {
}
/**
*
* send number of victories
*
* @return int number of victories
* @throws DataException
*/
public int getNumberVictories() throws DataException {
if(this.owner != null){
List<StatGame> games = this.getMyOwnerProfile().getPlayedGamesList() ;
LightPublicUser user = this.getMyLightPublicUserProfile() ;
int nbVictories = 0;
for (StatGame g : games) {
if ((g.getWinner() == user) && !(g.isGameAbandonned())) {
nbVictories++ ;
}
}
return nbVictories ;
}
return 0;
}
/**
*
* send number of defeats
*
* @return int number of defeats
* @throws DataException
*/
public int getNumberDefeats() throws DataException {
if(this.owner != null){
List<StatGame> games = this.getMyOwnerProfile().getPlayedGamesList() ;
LightPublicUser user = this.getMyLightPublicUserProfile() ;
int nbDefeats = 0;
for (StatGame g : games) {
if ((g.getWinner() != user) && !(g.isGameAbandonned())) {
nbDefeats++ ;
}
}
return nbDefeats ;
}
return 0;
}
/**
*
* send number of abandons
*
* @return int number of abandons
* @throws DataException
*/
public int getNumberAbandons() throws DataException {
if(this.owner!=null){
List<StatGame> games = this.getMyOwnerProfile().getPlayedGamesList() ;
LightPublicUser user = this.getMyLightPublicUserProfile() ;
int nbAbandons = 0;
for (StatGame g : games) {
if ((g.getWinner() == null) && (g.isGameAbandonned())) {
nbAbandons++ ;
}
}
return nbAbandons ;
}
return 0;
}
}
......@@ -5,20 +5,34 @@
*/
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);
}
public Coordinate getFocus() {
......@@ -26,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.