diff --git a/src/battleship/Main.java b/src/battleship/Main.java index 6218167..4443464 100644 --- a/src/battleship/Main.java +++ b/src/battleship/Main.java @@ -3,6 +3,7 @@ package battleship; import battleship.model.Game; import battleship.model.player.AbstractPlayer; import battleship.model.player.Human; +import battleship.model.player.Player; import battleship.model.player.Random; import battleship.utils.Pair; import battleship.view.AbstractView; @@ -35,7 +36,7 @@ public class Main { } private static void parseArgs(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException { - AbstractPlayer[] players = new AbstractPlayer[2]; + Player[] players = new AbstractPlayer[2]; ArrayList>> playerClass = new ArrayList<>(2); playerClass.add(new Pair<>("human", Human.class)); playerClass.add(new Pair<>("random", Random.class)); diff --git a/src/battleship/model/Board.java b/src/battleship/model/Board.java deleted file mode 100644 index d35e80e..0000000 --- a/src/battleship/model/Board.java +++ /dev/null @@ -1,7 +0,0 @@ -package battleship.model; - -// TODO: 10/04/2021 -public class Board { - - -} diff --git a/src/battleship/model/Game.java b/src/battleship/model/Game.java index c375fa9..2ebf1df 100644 --- a/src/battleship/model/Game.java +++ b/src/battleship/model/Game.java @@ -1,19 +1,16 @@ package battleship.model; -import battleship.model.player.AbstractPlayer; import battleship.model.player.Player; import battleship.utils.Pair; import battleship.utils.Triplet; import battleship.view.AbstractView; -import java.util.Random; - public class Game { public Player[] players; public Player currentPlayer; - public Game(AbstractPlayer[] players) { + public Game(Player[] players) { this.players = players; this.currentPlayer = players[0]; players[0].setId(1); @@ -35,17 +32,17 @@ public class Game { public void checkDrownedShips(){ Player otherPlayer = getOtherPlayer(); - for(Ship ship : currentPlayer.ships){ + for(Ship ship : otherPlayer.getShips()){ if(!ship.isDrown()) - otherPlayer.updateIsDrown(ship); + ship.updateIsDrown(currentPlayer); } } public Player getWinner(){ - Ship remainingShip = players[0].ships.parallelStream().filter(ship -> !ship.isDrown()).findFirst().orElse(null); + Ship remainingShip = players[0].getShips().parallelStream().filter(ship -> !ship.isDrown()).findFirst().orElse(null); if(remainingShip == null) return players[1]; - remainingShip = players[1].ships.parallelStream().filter(ship -> !ship.isDrown()).findFirst().orElse(null); + remainingShip = players[1].getShips().parallelStream().filter(ship -> !ship.isDrown()).findFirst().orElse(null); if(remainingShip == null) return players[1]; return null; @@ -54,13 +51,15 @@ public class Game { public void move(Pair move){ boolean bool = false; Player otherPlayer = getOtherPlayer(); - for (Ship ship : otherPlayer.ships) { - for(Pair pair : ship.getCoordsArray()){ - if ((pair.getRight().equals(move.getRight())) && (pair.getLeft().equals(move.getLeft()))) { + for (Ship ship : otherPlayer.getShips()) { + for(Pair coords : ship.getFullCoords()){ + if ((coords.getRight().equals(move.getRight())) && (coords.getLeft().equals(move.getLeft()))) { bool = true; break; } } + if(bool) + break; } currentPlayer.addMove(new Triplet<>(move, bool)); } @@ -70,24 +69,14 @@ public class Game { view.setShips(players[1]); Player winner = null; while(winner == null) { + System.out.println("Au tour du joueur " + currentPlayer.getId()); view.displayBoard(); - move(currentPlayer.chooseMove()); - changeCurrentPlayer(); + move(view.chooseMove(currentPlayer)); checkDrownedShips(); + changeCurrentPlayer(); winner = getWinner(); } view.displayWinner(winner); } - - public void placeShipRandomly(Player player) { - Random rand = new Random(); - for(int i : player.ShipSize) { - Ship ship = new Ship(new Pair<>(-1, -1), i, Direction.DEFAULT); - while(!player.setShips(ship)) { - ship = new Ship(new Pair<>(rand.nextInt(10), rand.nextInt(10)), i, Direction.values()[rand.nextInt(Direction.values().length)]); - } - } - } - } diff --git a/src/battleship/model/Ship.java b/src/battleship/model/Ship.java index 45060e9..6ff2408 100644 --- a/src/battleship/model/Ship.java +++ b/src/battleship/model/Ship.java @@ -1,33 +1,41 @@ package battleship.model; +import battleship.model.player.Player; import battleship.utils.Pair; +import battleship.utils.Triplet; + +import java.util.Arrays; public class Ship { private Pair coords; private final int size; + Pair[] fullCoords; private Direction direction; // (0,-1) bas // (0,1) haut // (1,0) droite // (-1,0) gauche private boolean isDrown; public Ship(Pair coords, int size, Direction direction) { this.coords = coords; this.size = size; + this.fullCoords = new Pair[this.size]; this.direction = direction; isDrown = false; + recalculateFullCoords(); } + public void setDirection(Direction d){ this.direction = d; } + public void setCoords(Pair c){ this.coords = c; - } public void setDrown(){ isDrown = true; } - public Boolean isDrown(){ + public boolean isDrown(){ return isDrown; } @@ -43,21 +51,33 @@ public class Ship { return this.coords; } - @SuppressWarnings("unchecked") - public Pair[] getCoordsArray(){ - Pair[] tab = new Pair[size]; - for(int i = 0; i(coords.getLeft()+i* direction.getDirection().getLeft(),coords.getRight()+i * direction.getDirection().getRight()); - } + public void recalculateFullCoords() { + for(int i = 0; i < size; ++i){ + fullCoords[i] = new Pair<>(coords.getLeft() + i * direction.getDirection().getLeft(),coords.getRight() + i * direction.getDirection().getRight()); } - return tab; - + } + public Pair[] getFullCoords(){ + return fullCoords; } @Override public String toString() { return super.toString() + ", coords=" + coords.toString() + ", size=" + size + ", direction=" + direction.toString(); } + + public void updateIsDrown(Player player) { + int cpt = 0; + for(Pair coords : getFullCoords()) { + for(Triplet move : player.getMoves()) { + if(move.getRight() && move.getLeft().equals(coords.getLeft()) && move.getMiddle().equals(coords.getRight())){ + ++cpt; + break; + } + } + } + if(cpt == getSize()) { + setDrown(); + } + } } diff --git a/src/battleship/model/player/AbstractPlayer.java b/src/battleship/model/player/AbstractPlayer.java index 6f37f96..233c0ed 100644 --- a/src/battleship/model/player/AbstractPlayer.java +++ b/src/battleship/model/player/AbstractPlayer.java @@ -9,22 +9,19 @@ import java.util.ArrayList; public abstract class AbstractPlayer implements Player { + ArrayList ships = new ArrayList<>(); + ArrayList> moves = new ArrayList<>(); public int id; - public boolean setShips(Ship ship) { if(ship.getDirection() == Direction.DEFAULT) return false; - for(int i = 0; i < ship.getSize(); i++){ - int x = ship.getCoords().getLeft() + i * ship.getDirection().getDirection().getLeft(); - int y = ship.getCoords().getRight()+ i * ship.getDirection().getDirection().getRight(); - if(x > 9 || x < 0 || y > 9 || y < 0) + for(Pair coords : ship.getFullCoords()) { + if(coords.getLeft() > 9 || coords.getLeft() < 0 || coords.getRight() > 9 || coords.getRight() < 0) return false; for(Ship ship1 : this.ships) { - for (int j = 0; j < ship1.getSize(); j++) { - int x1 = ship1.getCoords().getLeft() + i * ship1.getDirection().getDirection().getLeft(); - int y1 = ship1.getCoords().getRight() + i * ship1.getDirection().getDirection().getRight(); - if (x1 == x && y1 == y) + for (Pair coords1 : ship1.getFullCoords()) { + if (coords1.getLeft().equals(coords.getLeft()) && coords1.getRight().equals(coords.getRight())) return false; } } @@ -43,22 +40,6 @@ public abstract class AbstractPlayer implements Player { return this; } - 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 y = ship.getCoords().getRight()+ i * ship.getDirection().getDirection().getRight(); - if(move.getLeft() == x && move.getMiddle() == y){ - cpt += 1; - break; - } - } - } - if(cpt == ship.getSize()) - ship.setDrown(); - } - public ArrayList> validMoves() { ArrayList> validMovesList = new ArrayList<>(); for(int x = 0; x < 10; x++){ @@ -72,6 +53,18 @@ public abstract class AbstractPlayer implements Player { return validMovesList; } + + public boolean areValid(int x, int y){ + if(x <0 || x >10 || y <0 || y >10) + return false; + for(Triplet move : moves){ + if((move.getLeft() == x) && (move.getMiddle() == y) ) + return false; + } + return true; + } + + @Override public void setId(int i) { id = i; } @@ -79,4 +72,19 @@ public abstract class AbstractPlayer implements Player { public int getId() { return id; } + + @Override + public String toString() { + return getClass().getSimpleName() + " " + id; + } + + @Override + public ArrayList getShips() { + return ships; + } + + @Override + public ArrayList> getMoves() { + return moves; + } } diff --git a/src/battleship/model/player/Computer.java b/src/battleship/model/player/Computer.java new file mode 100644 index 0000000..68b24b0 --- /dev/null +++ b/src/battleship/model/player/Computer.java @@ -0,0 +1,23 @@ +package battleship.model.player; + +import battleship.model.Direction; +import battleship.model.Ship; +import battleship.utils.Pair; + +import java.util.Random; + +public abstract class Computer extends AbstractPlayer { + + public void placeShipRandomly() { + java.util.Random rand = new Random(); + for(int i : shipSize) { + Ship ship = new Ship(new Pair<>(-1, -1), i, Direction.DEFAULT); + while(!setShips(ship)) { + ship.setCoords(new Pair<>(rand.nextInt(10), rand.nextInt(10))); + ship.setDirection(Direction.values()[rand.nextInt(Direction.values().length)]); + ship.recalculateFullCoords(); + } + } + } + +} diff --git a/src/battleship/model/player/Human.java b/src/battleship/model/player/Human.java index 053d7d4..799f146 100644 --- a/src/battleship/model/player/Human.java +++ b/src/battleship/model/player/Human.java @@ -1,40 +1,15 @@ package battleship.model.player; import battleship.utils.Pair; -import battleship.utils.Triplet; -import battleship.view.Terminal; - -import java.util.Scanner; public class Human extends AbstractPlayer { - - @Override public Pair chooseMove() { - int x = -1, y = -1; - Scanner scanner = Terminal.scanner; - while(!areValid(x,y)) { - System.out.println("Veuillez indiquer la coordonée x de votre coup"); - x = scanner.nextInt(); - System.out.println("Veuillez indiquer la coordonée y de votre coup"); - y = scanner.nextInt(); - } - return new Pair<>(x,y); + return null; } - public boolean areValid(int x, int y){ - if(x <0 || x >10 || y <0 || y >10) - return false; - for(Triplet move : moves){ - if((move.getLeft() == x) && (move.getMiddle() == y) ) - return false; - } - return true; - } @Override - public String toString(){ - return "Human " +id; - } + public void placeShips() {} } diff --git a/src/battleship/model/player/Player.java b/src/battleship/model/player/Player.java index 1fbd538..2a25c52 100644 --- a/src/battleship/model/player/Player.java +++ b/src/battleship/model/player/Player.java @@ -8,9 +8,7 @@ import java.util.ArrayList; public interface Player { - ArrayList ships = new ArrayList<>(); - ArrayList> moves = new ArrayList<>(); - int[] ShipSize = { 5, 4, 3, 3, 2}; + int[] shipSize = { 5, 4, 3, 3, 2 }; Pair chooseMove(); @@ -18,11 +16,18 @@ public interface Player { int getId(); - void updateIsDrown(Ship ship); - Player addMove(Triplet move); + void setId(int i); + ArrayList> validMoves(); + void placeShips(); + + boolean areValid(int x, int y); + + ArrayList getShips(); + + ArrayList> getMoves(); } diff --git a/src/battleship/model/player/Random.java b/src/battleship/model/player/Random.java index e188bfb..c1e76f1 100644 --- a/src/battleship/model/player/Random.java +++ b/src/battleship/model/player/Random.java @@ -2,15 +2,17 @@ package battleship.model.player; import battleship.utils.Pair; -public class Random extends AbstractPlayer { +public class Random extends Computer { @Override public Pair chooseMove() { java.util.Random rand = new java.util.Random(); return validMoves().get(rand.nextInt(validMoves().size())); } + @Override - public String toString(){ - return "Random " +id; + public void placeShips() { + placeShipRandomly(); } + } diff --git a/src/battleship/view/AbstractView.java b/src/battleship/view/AbstractView.java index a6babcb..c71f46f 100644 --- a/src/battleship/view/AbstractView.java +++ b/src/battleship/view/AbstractView.java @@ -23,8 +23,8 @@ public abstract class AbstractView implements View{ String chain = ""; for(int u = 0; u < 2; ++u) { Player player = game.players[u]; - ArrayList ships = game.players[u].ships; - chain += "Player " + (u + 1) + " :\n"; + ArrayList ships = game.players[u].getShips(); + chain += "Joueur " + player.getId() + " :\n"; chain += "+ - - - - - - - - - - +\n"; for(int x = 0; x < 10; ++x) { chain += "|"; @@ -63,24 +63,16 @@ public abstract class AbstractView implements View{ } private boolean isShipPosition(Ship ship, Pair boardsCoords) { - if(ship.getCoords().equals(boardsCoords)) - return true; - for(int a = 0; a < ship.getSize(); ++a) { - if(new Pair<>(ship.getCoords().getLeft() + a * ship.getDirection().getDirection().getLeft(), ship.getCoords().getRight() + a * ship.getDirection().getDirection().getRight()).equals(boardsCoords)) { + for(Pair coords : ship.getFullCoords()) { + if(boardsCoords.equals(coords)) return true; - } } return false; } - - private int isPositionDrowned(Player other, Pair pair) { - for(Triplet move : other.moves) { - if(pair.getLeft().equals(move.getLeft()) && pair.getRight().equals(move.getMiddle())) { - return 2; - } - } - return 0; + @Override + public void setShips(Player player) { + player.placeShips(); } /** @@ -95,4 +87,18 @@ public abstract class AbstractView implements View{ return 1; return isPositionDrowned(other, pair); } + + private int isPositionDrowned(Player other, Pair pair) { + for(Triplet move : other.getMoves()) { + if(pair.getLeft().equals(move.getLeft()) && pair.getRight().equals(move.getMiddle())) { + return 2; + } + } + return 0; + } + + @Override + public Pair chooseMove(Player player) { + return player.chooseMove(); + } } diff --git a/src/battleship/view/Terminal.java b/src/battleship/view/Terminal.java index ed5f779..af9d4e4 100644 --- a/src/battleship/view/Terminal.java +++ b/src/battleship/view/Terminal.java @@ -50,14 +50,13 @@ public class Terminal extends AbstractView { } } } + ship.recalculateFullCoords(); } } } else { - // Random - game.placeShipRandomly(player); + super.setShips(player); + // Computer } - - } @Override @@ -65,8 +64,25 @@ public class Terminal extends AbstractView { System.out.println(toString()); } + @Override + public Pair chooseMove(Player player) { + if(player instanceof Human) { + int x = -1, y = -1; + while(!player.areValid(x, y)) { + System.out.println("Veuillez indiquer la coordonée x de votre coup"); + x = scanner.nextInt(); + System.out.println("Veuillez indiquer la coordonée y de votre coup"); + y = scanner.nextInt(); + } + return new Pair<>(x,y); + } + return super.chooseMove(player); + + } + @Override public void displayWinner(Player winner) { + displayBoard(); System.out.println("Le joueur " + winner.getId() + " a gagné"); } diff --git a/src/battleship/view/View.java b/src/battleship/view/View.java index 84dbe8e..06641d2 100644 --- a/src/battleship/view/View.java +++ b/src/battleship/view/View.java @@ -1,6 +1,7 @@ package battleship.view; import battleship.model.player.Player; +import battleship.utils.Pair; public interface View { @@ -10,5 +11,7 @@ public interface View { void displayBoard(); + Pair chooseMove(Player player); + void displayWinner(Player winner); } diff --git a/src/battleship/view/Window.java b/src/battleship/view/Window.java index 913298a..5441758 100644 --- a/src/battleship/view/Window.java +++ b/src/battleship/view/Window.java @@ -2,13 +2,14 @@ package battleship.view; import battleship.model.Game; import battleship.model.player.Player; +import battleship.utils.Pair; import javax.swing.*; import java.awt.*; public class Window extends AbstractView { - private JFrame frame; + private final JFrame frame; private final int height = 600; private final int width = 1200; @@ -33,6 +34,12 @@ public class Window extends AbstractView { frame.paintComponents(frame.getGraphics()); } + @Override + public Pair chooseMove(Player player) { + + return null; + } + @Override public void displayWinner(Player winner) { // TODO: 07/04/2021 afficher un dialog