diff --git a/src/battleship/Main.java b/src/battleship/Main.java index 2de0852..49aae5d 100644 --- a/src/battleship/Main.java +++ b/src/battleship/Main.java @@ -54,6 +54,8 @@ public class Main { view = new Terminal(game); else view = new Window(game); + } else { + view = new Window(game); } } else throw new NoSuchElementException("Pas assez d'arguments"); diff --git a/src/battleship/model/Game.java b/src/battleship/model/Game.java index 3efd11d..7516869 100644 --- a/src/battleship/model/Game.java +++ b/src/battleship/model/Game.java @@ -28,43 +28,31 @@ public class Game { } public void changeCurrentPlayer(){ - currentPlayer = (currentPlayer == players[1])? players[0] : players[1]; + currentPlayer = getOtherPlayer(); } public void checkDrownedShips(){ - Player otherPlayer = (currentPlayer == players[1])? players[0] : players[1]; + Player otherPlayer = getOtherPlayer(); for(Ship ship : currentPlayer.getShips()){ if(!ship.isDrown()) - otherPlayer.isItDrown(ship); + otherPlayer.updateIsDrown(ship); } } public Player getWinner(){ - int cpt = 0; - for(Ship ship : players[0].getShips()){ - if(!ship.isDrown()) - break; - else - cpt ++; - } - if(cpt == 5) + Ship remainingShip = players[0].getShips().parallelStream().filter(ship -> !ship.isDrown()).findFirst().orElse(null); + if(remainingShip == null) + return players[1]; + remainingShip = players[1].getShips().parallelStream().filter(ship -> !ship.isDrown()).findFirst().orElse(null); + if(remainingShip == null) return players[1]; - cpt = 0; - for(Ship ship : players[1].getShips()){ - if(!ship.isDrown()) - break; - else - cpt ++; - } - if(cpt == 5) - return players[0]; return null; } public void move(Pair move){ boolean bool = false; - Player player = (currentPlayer == players[1])? players[0] : players[1]; - for (Ship ship : player.getShips()) { + Player otherPlayer = getOtherPlayer(); + for (Ship ship : otherPlayer.getShips()) { for(Pair pair : ship.getCoordsArray()){ if ((pair.getRight().equals(move.getRight())) && (pair.getLeft().equals(move.getLeft()))) { bool = true; @@ -72,22 +60,21 @@ public class Game { } } } - currentPlayer.addMove(new Triplet<>(move.getLeft(),move.getRight(),bool)); - + currentPlayer.addMove(new Triplet<>(move, bool)); } - public Player Play(View view){ + public void Play(View view){ view.setShips(players[0]); view.setShips(players[1]); - System.out.println(view.toString()); - while(getWinner() == null) { - System.out.println(view); - Pair move = currentPlayer.chooseMove(); - move(move); + Player winner = null; + while(winner == null) { + view.displayBoard(); + move(currentPlayer.chooseMove()); changeCurrentPlayer(); checkDrownedShips(); + winner = getWinner(); } - return getWinner(); + view.displayWinner(winner); } diff --git a/src/battleship/model/player/Human.java b/src/battleship/model/player/Human.java index a5272a1..c0bc5c1 100644 --- a/src/battleship/model/player/Human.java +++ b/src/battleship/model/player/Human.java @@ -22,7 +22,8 @@ public class Human extends Player { } return new Pair<>(x,y); } - public boolean areValid(int x,int y){ + + public boolean areValid(int x, int y){ if(x <0 || x >10 || y <0 || y >10) return false; for(Triplet move : moves){ diff --git a/src/battleship/model/player/Player.java b/src/battleship/model/player/Player.java index 325c578..e3df71c 100644 --- a/src/battleship/model/player/Player.java +++ b/src/battleship/model/player/Player.java @@ -46,23 +46,20 @@ public abstract class Player { return this; } - public boolean isItDrown(Ship ship) { + public void updateIsDrown(Ship ship) { int cpt = 0; for(Triplet move : moves){ for(int i = 1; i <= ship.getSize(); i++){ - int x = ship.getCoords().getLeft()+ i * ship.getDirection().getDirection().getLeft(); + int x = ship.getCoords().getLeft() + i * ship.getDirection().getDirection().getLeft(); int y = ship.getCoords().getRight()+ i * ship.getDirection().getDirection().getRight(); - if((move.getLeft() == x) && (move.getMiddle() == y)){ + if(move.getLeft() == x && move.getMiddle() == y){ cpt += 1; break; } } } - if(cpt == ship.getSize()) { + if(cpt == ship.getSize()) ship.setDrown(); - return true; - } - return false; } public ArrayList getShips(){ return this.ships; @@ -76,10 +73,11 @@ public abstract class Player { public ArrayList> validMoves() { ArrayList> validMovesList = new ArrayList<>(); - for(Integer i = 0; i<10;i++){ - for(Integer y = 0;y<10;y++) { - if(!moves.contains(new Triplet<>(i,y,true)) ||!moves.contains(new Triplet<>(i,y,false))){ - validMovesList.add(new Pair<>(i,y)); + for(int x = 0; x < 10; x++){ + for(int y = 0; y < 10; y++) { + Pair coords = new Pair<>(x,y); + if(!moves.contains(new Triplet<>(coords, true)) || !moves.contains(new Triplet<>(coords, false))){ + validMovesList.add(new Pair<>(x,y)); } } } diff --git a/src/battleship/utils/Triplet.java b/src/battleship/utils/Triplet.java index 3903a5e..a24f088 100644 --- a/src/battleship/utils/Triplet.java +++ b/src/battleship/utils/Triplet.java @@ -20,6 +20,10 @@ public class Triplet { this.right = right; } + public Triplet(Pair pair, V right) { + this(pair.getLeft(), pair.getRight(), right); + } + public U getLeft() { return left; } @@ -44,7 +48,7 @@ public class Triplet { return false; } final Triplet other = (Triplet) obj; - return this.left.equals(other.getLeft()) && this.middle.equals(other.getMiddle()) && this.left.equals(other.getRight()); + return this.left.equals(other.getLeft()) && this.middle.equals(other.getMiddle()) && this.right.equals(other.getRight()); } @Override diff --git a/src/battleship/view/Terminal.java b/src/battleship/view/Terminal.java index 6096dfe..8c24674 100644 --- a/src/battleship/view/Terminal.java +++ b/src/battleship/view/Terminal.java @@ -65,4 +65,9 @@ public class Terminal extends View { System.out.println(toString()); } + @Override + public void displayWinner(Player winner) { + System.out.println("Le joueur " + winner.getId() + " a gagné"); + } + } diff --git a/src/battleship/view/View.java b/src/battleship/view/View.java index 284fc30..38b89e3 100644 --- a/src/battleship/view/View.java +++ b/src/battleship/view/View.java @@ -41,7 +41,10 @@ public abstract class View { for(Ship ship : ships) { if(isShipPosition(ship, pair)) { isPosition = true; - if(isPositionDrowned(game.players[u == 0 ? 1 : 0], ship, pair)) { + int result = isPositionDrowned(game.players[u == 0 ? 1 : 0], ship, pair); + if(result == 1) { + chain += " X"; + } else if (result == 2){ chain += " !"; } else { chain += " ."; @@ -49,8 +52,14 @@ public abstract class View { break; } } - if(!isPosition) - chain += " _"; + if(!isPosition) { + if(isPositionDrowned(game.players[u == 0 ? 1 : 0], pair) == 2) { + chain += " ?"; + } else { + chain += " _"; + } + } + } chain += " |\n"; } @@ -72,19 +81,27 @@ public abstract class View { } - private boolean isPositionDrowned(Player other, Pair pair) { + private int isPositionDrowned(Player other, Pair pair) { for(Triplet move : other.getMoves()) { - if(move.getRight() && pair.getLeft().equals(move.getLeft()) && pair.getRight().equals(move.getMiddle())) { - return true; + if(pair.getLeft().equals(move.getLeft()) && pair.getRight().equals(move.getMiddle())) { + return 2; } } - return false; + return 0; } - private boolean isPositionDrowned(Player other, Ship ship, Pair pair) { + /** + * + * @param other other than the current player + * @param ship check if this ship at this position is touch + * @param pair coords + * @return 1 if ship fully drowned, 2 if only damaged, 0 if not + */ + private int isPositionDrowned(Player other, Ship ship, Pair pair) { if(ship.isDrown()) - return true; + return 1; return isPositionDrowned(other, pair); } - + + public abstract void displayWinner(Player winner); } diff --git a/src/battleship/view/Window.java b/src/battleship/view/Window.java index 0d474c2..0053e15 100644 --- a/src/battleship/view/Window.java +++ b/src/battleship/view/Window.java @@ -3,31 +3,40 @@ package battleship.view; import battleship.model.Game; import battleship.model.player.Player; -import java.awt.Color; import java.awt.Graphics; -import java.awt.Graphics2D; -import java.awt.geom.Line2D; import javax.swing.*; import java.awt.*; public class Window extends View { private JFrame frame; - private final int hauteur = 600; - private final int largeur = 1200; + + private final int height = 600; + private final int width = 1200; public Window(Game game) { super(game); this.frame = new JFrame("Battleship"); - frame.setSize(largeur+largeur/38,hauteur+hauteur/13); + frame.setSize(width + width / 13, height + height / 4); frame.setContentPane(new Draw()); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.setVisible(true); + } @Override public void setShips(Player player) { + } + + @Override + public void displayBoard() { + frame.paintComponents(frame.getGraphics()); + } + + @Override + public void displayWinner(Player winner) { + // TODO: 07/04/2021 afficher un dialog } class Draw extends JPanel { @@ -36,29 +45,19 @@ public class Window extends View { area.setBounds(20,10,400,20); //area.append("A B C D E F G H I J"); frame.add(area);*/ - for (int abscisse=largeur/24; abscisse> 1)) { + abscisse += width / 24 ; + } } - for (int ordonnee=hauteur/6; ordonnee