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);
......
......@@ -5,21 +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() {
......@@ -27,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.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{
}
public void setShips(List<Ship> ships) {
this.ships = ships;
}
......
......@@ -303,7 +303,7 @@ public class GameListController extends AbstractController{
private void goIntoGame(){
if(isRunning()){
if(receivedGame != null){
getFacade().iIHMTableToIHMMain.showGame(receivedGame);
getFacade().iIHMTableToIHMMain.showGame();
}else{
showErrorPopup("Connection Impossible","","Your connection request failed.");
refresh();
......
......@@ -8,14 +8,11 @@ package com.utclo23.ihmtable;
import com.utclo23.data.structure.Coordinate;
import com.utclo23.data.structure.StatGame;
import com.utclo23.data.facade.IDataIHMTable;
import com.utclo23.data.structure.Game;
import com.utclo23.data.structure.LightPublicUser;
import com.utclo23.ihmmain.controller.AbstractController;
import com.utclo23.data.structure.Ship;
import com.utclo23.ihmmain.facade.IHMMainToIhmTable;
import com.utclo23.ihmtable.controller.InGameGUIController;
import java.io.IOException;
import java.rmi.server.UID;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.fxml.FXMLLoader;
......@@ -132,14 +129,12 @@ public class IHMTableFacade implements IIHMTableToIHMMain, IIHMTableToData {
/**
* Join a current game.
* @param guid : id of the game.
*/
@Override
public void showGame(Game game) {
public void showGame() {
//Créer la fenêtre
FXMLLoader paneLoader = new FXMLLoader(getClass().getResource(FXML_PATH));
Parent pane;
InGameGUIController controller = null;
Stage primaryStage = facadeIHMMain.getPrimaryStage();
try {
controller = new InGameGUIController();
......@@ -177,6 +172,7 @@ public class IHMTableFacade implements IIHMTableToIHMMain, IIHMTableToData {
*/
@Override
public void notifyGameReady() {
System.out.println("notifygameready");
gameReady = true;
// Notify the controller the game has started.
controller.startGame();
......@@ -194,20 +190,32 @@ public class IHMTableFacade implements IIHMTableToIHMMain, IIHMTableToData {
/**
* Show on the board if the shot has hit or not a ship.
* @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
public void feedBack(Coordinate coord, boolean bool) {
public void feedBack(Coordinate coord, boolean touched, Ship destroyedShip) {
controller.displayOpponentAttack(coord, touched, destroyedShip);
controller.timeToAttack();
}
/**
* Display the new stats of the player.
* @param stGame : ths stats
* @param stGame : the stats
*/
@Override
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.
controller.displayFinishPopup(sMessage);
}
/**
......@@ -215,7 +223,8 @@ public class IHMTableFacade implements IIHMTableToIHMMain, IIHMTableToData {
*/
@Override
public void opponentHasLeftGame() {
throw new UnsupportedOperationException(EXCEPTION_MESSAGE);
// Display popup.
controller.displayFinishPopup("Your opponent has left this game!");
}
/**
......@@ -223,7 +232,7 @@ public class IHMTableFacade implements IIHMTableToIHMMain, IIHMTableToData {
*/
@Override
public void connectionLostWithOpponent() {
throw new UnsupportedOperationException(EXCEPTION_MESSAGE);
controller.displayFinishPopup("Connection has been lost with your opponent");
}
/**
......
......@@ -6,6 +6,7 @@
package com.utclo23.ihmtable;
import com.utclo23.data.structure.Coordinate;
import com.utclo23.data.structure.Ship;
import com.utclo23.data.structure.StatGame;
/**
......@@ -15,7 +16,7 @@ import com.utclo23.data.structure.StatGame;
public interface IIHMTableToData {
public void notifyGameReady();
public void printMessage(String message);
public void feedBack(Coordinate coord, boolean bool);
public void feedBack(Coordinate coord, boolean bool, Ship destroyedShip);
public void finishGame(StatGame stGame);
public void opponentHasLeftGame();
public void connectionLostWithOpponent();
......
......@@ -5,8 +5,6 @@
*/
package com.utclo23.ihmtable;
import com.utclo23.data.structure.Game;
import java.rmi.server.UID;
import javafx.stage.Stage;
/**
......@@ -17,5 +15,5 @@ public interface IIHMTableToIHMMain {
public void showSavedGameWithId(int id);
public void createInGameGUI(Stage primaryStage);
public void stopTimer();
public void showGame(Game game);
public void showGame();
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.utclo23.ihmtable.structure;
import com.utclo23.data.structure.Coordinate;
import java.util.List;
/**
*
* @author gum
*/
public class CoordinatesGenerator {
/**
* This function adds coordinates between a starting/ending point
* @param startPosition Start position of the ship
* @param endPosition End position of the ship
* @param list List in which coordinates have to be added
*/
public static void generate(Coordinate startPosition, Coordinate endPosition, List<Coordinate> list) {
// For 1-cell ships.
if (startPosition.getX() == endPosition.getX() &&
startPosition.getY() == endPosition.getY()
) {
list.add(endPosition);
} else {
// For more-tan-two-cell ship.
list.add(startPosition);
// Add the coordinates between the two cases.
Coordinate diff = new Coordinate(endPosition.getX() - startPosition.getX(), endPosition.getY() - startPosition.getY());
int xDirection = (diff.getX() >= 0) ? 1 : -1;
int yDirection = (diff.getY() >= 0) ? 1 : -1;
for (int x = 1; x < Math.abs(diff.getX()); ++x) {
list.add(new Coordinate(startPosition.getX() + xDirection * x, startPosition.getY()));
}
for (int y = 1; y < Math.abs(diff.getY()); ++y) {
list.add(new Coordinate(startPosition.getX(), startPosition.getY() + yDirection * y));
}