diff --git a/src/battleship/Main.java b/src/battleship/Main.java index 49aae5d..6218167 100644 --- a/src/battleship/Main.java +++ b/src/battleship/Main.java @@ -1,12 +1,12 @@ 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; import battleship.view.Terminal; -import battleship.view.View; import battleship.view.Window; import java.lang.reflect.InvocationTargetException; @@ -15,7 +15,7 @@ import java.util.NoSuchElementException; public class Main { - public static View view; + public static AbstractView view; public static Game game; public static void main(String[] args) { @@ -35,13 +35,13 @@ public class Main { } private static void parseArgs(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException { - Player[] players = new Player[2]; - ArrayList>> playerClass = new ArrayList<>(2); + AbstractPlayer[] players = new AbstractPlayer[2]; + ArrayList>> playerClass = new ArrayList<>(2); playerClass.add(new Pair<>("human", Human.class)); playerClass.add(new Pair<>("random", Random.class)); if(args.length >= 2) { for(int i = 0; i < 2; ++i) { - for (Pair> pair : playerClass) { + for (Pair> pair : playerClass) { if(args[i].equalsIgnoreCase(pair.getLeft())) { players[i] = pair.getRight().getDeclaredConstructor().newInstance(); } diff --git a/src/battleship/model/Board.java b/src/battleship/model/Board.java new file mode 100644 index 0000000..d35e80e --- /dev/null +++ b/src/battleship/model/Board.java @@ -0,0 +1,7 @@ +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 7516869..c375fa9 100644 --- a/src/battleship/model/Game.java +++ b/src/battleship/model/Game.java @@ -1,17 +1,19 @@ package battleship.model; +import battleship.model.player.AbstractPlayer; import battleship.model.player.Player; import battleship.utils.Pair; import battleship.utils.Triplet; -import battleship.view.View; +import battleship.view.AbstractView; + +import java.util.Random; public class Game { public Player[] players; public Player currentPlayer; - private final int[] ships = { 5, 4, 3, 3, 2}; - public Game(Player[] players) { + public Game(AbstractPlayer[] players) { this.players = players; this.currentPlayer = players[0]; players[0].setId(1); @@ -33,17 +35,17 @@ public class Game { public void checkDrownedShips(){ Player otherPlayer = getOtherPlayer(); - for(Ship ship : currentPlayer.getShips()){ + for(Ship ship : currentPlayer.ships){ if(!ship.isDrown()) otherPlayer.updateIsDrown(ship); } } public Player getWinner(){ - Ship remainingShip = players[0].getShips().parallelStream().filter(ship -> !ship.isDrown()).findFirst().orElse(null); + Ship remainingShip = players[0].ships.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); + remainingShip = players[1].ships.parallelStream().filter(ship -> !ship.isDrown()).findFirst().orElse(null); if(remainingShip == null) return players[1]; return null; @@ -52,7 +54,7 @@ public class Game { public void move(Pair move){ boolean bool = false; Player otherPlayer = getOtherPlayer(); - for (Ship ship : otherPlayer.getShips()) { + for (Ship ship : otherPlayer.ships) { for(Pair pair : ship.getCoordsArray()){ if ((pair.getRight().equals(move.getRight())) && (pair.getLeft().equals(move.getLeft()))) { bool = true; @@ -63,7 +65,7 @@ public class Game { currentPlayer.addMove(new Triplet<>(move, bool)); } - public void Play(View view){ + public void Play(AbstractView view){ view.setShips(players[0]); view.setShips(players[1]); Player winner = null; @@ -78,4 +80,14 @@ public class Game { } + 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/player/AbstractPlayer.java b/src/battleship/model/player/AbstractPlayer.java new file mode 100644 index 0000000..6f37f96 --- /dev/null +++ b/src/battleship/model/player/AbstractPlayer.java @@ -0,0 +1,82 @@ +package battleship.model.player; + +import battleship.model.Direction; +import battleship.model.Ship; +import battleship.utils.Pair; +import battleship.utils.Triplet; + +import java.util.ArrayList; + +public abstract class AbstractPlayer implements Player { + + 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) + 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) + return false; + } + } + } + this.ships.add(ship); + return true; + } + + /** + * La methode retourne son objet afin d'avoir la possibilité de faire Player.addMove().addMove().etc... + * @param move + * @return Player + */ + public Player addMove(Triplet move){ + moves.add(move); + 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++){ + 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)); + } + } + } + return validMovesList; + + } + public void setId(int i) { + id = i; + } + + public int getId() { + return id; + } +} diff --git a/src/battleship/model/player/Human.java b/src/battleship/model/player/Human.java index c0bc5c1..053d7d4 100644 --- a/src/battleship/model/player/Human.java +++ b/src/battleship/model/player/Human.java @@ -6,7 +6,7 @@ import battleship.view.Terminal; import java.util.Scanner; -public class Human extends Player { +public class Human extends AbstractPlayer { diff --git a/src/battleship/model/player/Player.java b/src/battleship/model/player/Player.java index e3df71c..1fbd538 100644 --- a/src/battleship/model/player/Player.java +++ b/src/battleship/model/player/Player.java @@ -1,104 +1,28 @@ package battleship.model.player; -import battleship.model.Direction; import battleship.model.Ship; import battleship.utils.Pair; import battleship.utils.Triplet; import java.util.ArrayList; -import java.util.Random; -public abstract class Player { +public interface Player { - protected ArrayList ships = new ArrayList<>(); - protected ArrayList> moves = new ArrayList<>(); - protected int id; - protected final static int[] bato = { 5, 4, 3, 3, 2}; + ArrayList ships = new ArrayList<>(); + ArrayList> moves = new ArrayList<>(); + int[] ShipSize = { 5, 4, 3, 3, 2}; - 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) - 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) - return false; - } - } - } - this.ships.add(ship); - return true; - } + Pair chooseMove(); - /** - * La methode retourne son objet afin d'avoir la possibilité de faire Player.addMove().addMove().etc... - * @param move - * @return Player - */ - public Player addMove(Triplet move){ - moves.add(move); - return this; - } + boolean setShips(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 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 getShips(){ - return this.ships; - } + int getId(); - public ArrayList> getMoves(){ - return this.moves; - } + void updateIsDrown(Ship ship); + + Player addMove(Triplet move); - public abstract Pair chooseMove(); - public ArrayList> validMoves() { - ArrayList> validMovesList = new ArrayList<>(); - 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)); - } - } - } - return validMovesList; - } - public void setId(int i){ - id = i; - } - public int getId() { - return id; - } - - public void placeShipRandomly() { - Random rand = new Random(); - for(int i : bato) { - Ship ship = new Ship(new Pair<>(-1, -1), i, Direction.DEFAULT); - while(!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/player/Random.java b/src/battleship/model/player/Random.java index 6ba0f8c..e188bfb 100644 --- a/src/battleship/model/player/Random.java +++ b/src/battleship/model/player/Random.java @@ -2,7 +2,7 @@ package battleship.model.player; import battleship.utils.Pair; -public class Random extends Player { +public class Random extends AbstractPlayer { @Override public Pair chooseMove() { diff --git a/src/battleship/view/AbstractView.java b/src/battleship/view/AbstractView.java new file mode 100644 index 0000000..a6babcb --- /dev/null +++ b/src/battleship/view/AbstractView.java @@ -0,0 +1,98 @@ +package battleship.view; + +import battleship.model.Game; +import battleship.model.Ship; +import battleship.model.player.Player; +import battleship.utils.Pair; +import battleship.utils.Triplet; + +import java.util.ArrayList; + +public abstract class AbstractView implements View{ + + + protected Game game; + + public AbstractView(Game game) { + this.game = game; + } + + @Override + public String toString() { + // String chain = "A vous de joueur "+game.currentPlayer.toString()+ "\n+ - - - - - - - - - - +\n"; + 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"; + chain += "+ - - - - - - - - - - +\n"; + for(int x = 0; x < 10; ++x) { + chain += "|"; + for(int y = 0; y < 10; ++y) { + Pair pair = new Pair<>(x, y); + boolean isPosition = false; + for(Ship ship : ships) { + if(isShipPosition(ship, pair)) { + isPosition = true; + int result = isPositionDrowned(game.players[u == 0 ? 1 : 0], ship, pair); + if(result == 1) { + chain += " X"; + } else if (result == 2){ + chain += " !"; + } else { + chain += " ."; + } + break; + } + } + if(!isPosition) { + if(isPositionDrowned(game.players[u == 0 ? 1 : 0], pair) == 2) { + chain += " ?"; + } else { + chain += " _"; + } + } + + } + chain += " |\n"; + } + chain += "+ - - - - - - - - - - +\n"; + } + + return chain; + } + + 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)) { + 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; + } + + /** + * + * @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 1; + return isPositionDrowned(other, pair); + } +} diff --git a/src/battleship/view/Terminal.java b/src/battleship/view/Terminal.java index 8c24674..ed5f779 100644 --- a/src/battleship/view/Terminal.java +++ b/src/battleship/view/Terminal.java @@ -9,7 +9,7 @@ import battleship.utils.Pair; import java.util.Scanner; -public class Terminal extends View { +public class Terminal extends AbstractView { public static Scanner scanner = new Scanner(System.in); @@ -23,7 +23,7 @@ public class Terminal extends View { int x, y; String dir; if(player instanceof Human) { - for(int i : ships) { + for(int i : shipsSize) { boolean valid = false; Ship ship = new Ship(new Pair<>(-1, -1), i, Direction.DEFAULT); while (!player.setShips(ship)) { @@ -54,7 +54,7 @@ public class Terminal extends View { } } else { // Random - player.placeShipRandomly(); + game.placeShipRandomly(player); } diff --git a/src/battleship/view/View.java b/src/battleship/view/View.java index 38b89e3..84dbe8e 100644 --- a/src/battleship/view/View.java +++ b/src/battleship/view/View.java @@ -1,107 +1,14 @@ package battleship.view; -import battleship.model.Game; -import battleship.model.Ship; import battleship.model.player.Player; -import battleship.utils.Pair; -import battleship.utils.Triplet; -import java.awt.Graphics; -import java.awt.*; -import java.util.ArrayList; +public interface View { -public abstract class View { + int[] shipsSize = { 5, 4, 3, 3, 2}; + void setShips(Player player); - protected final Game game; - protected final int[] ships = { 5, 4, 3, 3, 2}; + void displayBoard(); - public View(Game game) { - this.game = game; - } - - public abstract void setShips(Player player); - - public abstract void displayBoard(); - - @Override - public String toString() { - // String chain = "A vous de joueur "+game.currentPlayer.toString()+ "\n+ - - - - - - - - - - +\n"; - String chain = ""; - for(int u = 0; u < 2; ++u) { - Player player = game.players[u]; - ArrayList ships = game.players[u].getShips(); - chain += "Player " + (u + 1) + " :\n"; - chain += "+ - - - - - - - - - - +\n"; - for(int x = 0; x < 10; ++x) { - chain += "|"; - for(int y = 0; y < 10; ++y) { - Pair pair = new Pair<>(x, y); - boolean isPosition = false; - for(Ship ship : ships) { - if(isShipPosition(ship, pair)) { - isPosition = true; - int result = isPositionDrowned(game.players[u == 0 ? 1 : 0], ship, pair); - if(result == 1) { - chain += " X"; - } else if (result == 2){ - chain += " !"; - } else { - chain += " ."; - } - break; - } - } - if(!isPosition) { - if(isPositionDrowned(game.players[u == 0 ? 1 : 0], pair) == 2) { - chain += " ?"; - } else { - chain += " _"; - } - } - - } - chain += " |\n"; - } - chain += "+ - - - - - - - - - - +\n"; - } - - return chain; - } - - 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)) { - return true; - } - } - return false; - } - - - 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; - } - - /** - * - * @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 1; - return isPositionDrowned(other, pair); - } - - public abstract void displayWinner(Player winner); + void displayWinner(Player winner); } diff --git a/src/battleship/view/Window.java b/src/battleship/view/Window.java index 0053e15..913298a 100644 --- a/src/battleship/view/Window.java +++ b/src/battleship/view/Window.java @@ -3,11 +3,10 @@ package battleship.view; import battleship.model.Game; import battleship.model.player.Player; -import java.awt.Graphics; import javax.swing.*; import java.awt.*; -public class Window extends View { +public class Window extends AbstractView { private JFrame frame;