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);
......
...@@ -5,21 +5,34 @@ ...@@ -5,21 +5,34 @@
*/ */
package com.utclo23.data.structure; package com.utclo23.data.structure;
import com.utclo23.data.configuration.Configuration;
import java.rmi.server.UID; import java.rmi.server.UID;
import java.util.List;
import java.util.Random;
import javafx.util.Pair;
/** /**
* *
* @author lucillefargeau * @author lucillefargeau
*/ */
public class ComputerPlayer extends Player{ public class ComputerPlayer extends Player {
private Coordinate focus; private Coordinate focus;
private Coordinate oldFocus;
public void loseFocus()
{
this.oldFocus = null;
this.focus = null;
}
public ComputerPlayer() { public ComputerPlayer() {
super(LightPublicUser.generateComputerProfile()); super(LightPublicUser.generateComputerProfile());
System.out.println("new Com player");
this.focus = null; this.focus = null;
this.oldFocus = null;
this.setComputer(true); this.setComputer(true);
} }
public Coordinate getFocus() { public Coordinate getFocus() {
...@@ -27,14 +40,221 @@ public class ComputerPlayer extends Player{ ...@@ -27,14 +40,221 @@ public class ComputerPlayer extends Player{
} }
public void setFocus(Coordinate focus) { public void setFocus(Coordinate focus) {
this.oldFocus = this.focus;
this.focus = 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.WIDTH || focus.getY() - 1 < 0) || tab[focus.getX()][focus.getY() - 1] != 0) {
this.focus = null;
this.oldFocus = null;
Random r = new Random();
do {
valid = true;
//choose a new location until empty
x = r.nextInt(Configuration.HEIGHT - 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]+" ");
} while (!valid);
} else {
x = focus.getX();
y = focus.getY() - 1;
}
} else {
x = focus.getX();
y = focus.getY() + 1;
}
} else {
x = focus.getX() - 1;
y = focus.getY();
}
} else {
x = focus.getX() + 1;
y = focus.getY();
}
}
Coordinate coordinate = new Coordinate(x, y);
System.out.println("IA chooses " + x + "," + y);
Mine mine = new Mine(this, coordinate);
this.getMines().add(mine);
return mine;
}
} }
...@@ -35,6 +35,7 @@ public class Player extends SerializableEntity{ ...@@ -35,6 +35,7 @@ public class Player extends SerializableEntity{
} }
public void setShips(List<Ship> ships) { public void setShips(List<Ship> ships) {
this.ships = ships; this.ships = ships;
} }
......
...@@ -303,7 +303,7 @@ public class GameListController extends AbstractController{ ...@@ -303,7 +303,7 @@ public class GameListController extends AbstractController{
private void goIntoGame(){ private void goIntoGame(){
if(isRunning()){ if(isRunning()){
if(receivedGame != null){ if(receivedGame != null){
getFacade().iIHMTableToIHMMain.showGame(receivedGame); getFacade().iIHMTableToIHMMain.showGame();
}else{ }else{
showErrorPopup("Connection Impossible","","Your connection request failed."); showErrorPopup("Connection Impossible","","Your connection request failed.");
refresh(); refresh();
......
...@@ -8,14 +8,11 @@ package com.utclo23.ihmtable; ...@@ -8,14 +8,11 @@ package com.utclo23.ihmtable;
import com.utclo23.data.structure.Coordinate; import com.utclo23.data.structure.Coordinate;
import com.utclo23.data.structure.StatGame; import com.utclo23.data.structure.StatGame;
import com.utclo23.data.facade.IDataIHMTable; import com.utclo23.data.facade.IDataIHMTable;
import com.utclo23.data.structure.Game; import com.utclo23.data.structure.Ship;
import com.utclo23.data.structure.LightPublicUser;
import com.utclo23.ihmmain.controller.AbstractController;
import com.utclo23.ihmmain.facade.IHMMainToIhmTable; import com.utclo23.ihmmain.facade.IHMMainToIhmTable;
import com.utclo23.ihmtable.controller.InGameGUIController; import com.utclo23.ihmtable.controller.InGameGUIController;
import java.io.IOException; import java.io.IOException;
import java.rmi.server.UID;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import javafx.fxml.FXMLLoader; import javafx.fxml.FXMLLoader;
...@@ -132,14 +129,12 @@ public class IHMTableFacade implements IIHMTableToIHMMain, IIHMTableToData { ...@@ -132,14 +129,12 @@ public class IHMTableFacade implements IIHMTableToIHMMain, IIHMTableToData {
/** /**
* Join a current game. * Join a current game.
* @param guid : id of the game.
*/ */
@Override @Override
public void showGame(Game game) { public void showGame() {
//Créer la fenêtre //Créer la fenêtre
FXMLLoader paneLoader = new FXMLLoader(getClass().getResource(FXML_PATH)); FXMLLoader paneLoader = new FXMLLoader(getClass().getResource(FXML_PATH));
Parent pane; Parent pane;
InGameGUIController controller = null;
Stage primaryStage = facadeIHMMain.getPrimaryStage(); Stage primaryStage = facadeIHMMain.getPrimaryStage();
try { try {
controller = new InGameGUIController(); controller = new InGameGUIController();
...@@ -177,6 +172,7 @@ public class IHMTableFacade implements IIHMTableToIHMMain, IIHMTableToData { ...@@ -177,6 +172,7 @@ public class IHMTableFacade implements IIHMTableToIHMMain, IIHMTableToData {
*/ */
@Override @Override
public void notifyGameReady() { public void notifyGameReady() {
System.out.println("notifygameready");
gameReady = true; gameReady = true;
// Notify the controller the game has started. // Notify the controller the game has started.
controller.startGame(); controller.startGame();
...@@ -194,20 +190,32 @@ public class IHMTableFacade implements IIHMTableToIHMMain, IIHMTableToData { ...@@ -194,20 +190,32 @@ public class IHMTableFacade implements IIHMTableToIHMMain, IIHMTableToData {
/** /**
* Show on the board if the shot has hit or not a ship. * Show on the board if the shot has hit or not a ship.
* @param coord : the coordinates of the hit. * @param coord : the coordinates of the hit.
* @param bool : true if a ship is hit. * @param touched : true if a ship is hit.
* @param destroyedShip : destroyed ship or null.
*/ */
@Override @Override
public void feedBack(Coordinate coord, boolean bool) { public void feedBack(Coordinate coord, boolean touched, Ship destroyedShip) {
controller.displayOpponentAttack(coord, touched, destroyedShip);
controller.timeToAttack(); controller.timeToAttack();
} }
/** /**
* Display the new stats of the player. * Display the new stats of the player.
* @param stGame : ths stats * @param stGame : the stats
*/ */
@Override @Override
public void finishGame(StatGame stGame) { public void finishGame(StatGame stGame) {
throw new UnsupportedOperationException(EXCEPTION_MESSAGE); String sMessage;
// Game lost.
if(!stGame.getWinner().getPlayerName().equals(facadeData.getMyPublicUserProfile().getPlayerName()))
{
sMessage = "Defeat! You should train against AI! Hahahah!";
} else {
// Game won.
sMessage = "Victory! I'm proud of you General!";
}
// Display popup.