From 19bd363503dcfe95d7e6eba58ea4ecf4ee5058d2 Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Sun, 28 Mar 2021 14:35:10 +0200 Subject: [PATCH] Add Direction enum + fix Game.setShips(Player) --- src/battleship/model/Direction.java | 28 +++++++++++ src/battleship/model/Game.java | 63 +++++++++++-------------- src/battleship/model/Ship.java | 21 +++++---- src/battleship/model/player/Player.java | 31 ++++++------ 4 files changed, 84 insertions(+), 59 deletions(-) create mode 100644 src/battleship/model/Direction.java diff --git a/src/battleship/model/Direction.java b/src/battleship/model/Direction.java new file mode 100644 index 0000000..a66556b --- /dev/null +++ b/src/battleship/model/Direction.java @@ -0,0 +1,28 @@ +package battleship.model; + +import battleship.utils.Pair; + +public enum Direction { + + RIGHT(new Pair<>(1, 0), "D"), + LEFT(new Pair<>(-1,0), "G"), + UP(new Pair<>(0,-1), "H"), + DOWN(new Pair<>(0,1), "B"), + DEFAULT(new Pair<>(-1,-1), null); + + private final Pair direction; + private final String keyword; + + Direction(Pair ukPair, String keyword) { + this.direction = ukPair; + this.keyword = keyword; + } + + public Pair getDirection() { + return direction; + } + + public String getKeyword() { + return keyword; + } +} diff --git a/src/battleship/model/Game.java b/src/battleship/model/Game.java index b976dda..366f6ee 100644 --- a/src/battleship/model/Game.java +++ b/src/battleship/model/Game.java @@ -11,38 +11,36 @@ public class Game { public Player[] players; public Player currentPlayer; - private int[] ships = new int[5]; + private final int[] ships = { 5, 4, 3, 3, 2}; public Game(Player[] players) { this.players = players; this.currentPlayer = players[0]; players[0].setId(1); players[1].setId(2); - ships[0] = 5; - ships[1] = 4; - ships[2] = 3; - ships[3] = 3; - ships[4] = 2; } public Player getCurrentPlayer(){ return this.currentPlayer; } + public void changeCurrentPlayer(){ currentPlayer = (currentPlayer == players[1])? players[0] : players[1]; } + public void checkDrownedShips(){ Player otherPlayer = (currentPlayer == players[1])? players[0] : players[1]; for(Ship ship : currentPlayer.getShips()){ - if(!ship.hasDrown()) + if(!ship.isDrown()) otherPlayer.isItDrown(ship); } } + public Player getWinner(){ int cpt = 0; for(Ship ship : players[0].getShips()){ - if(!ship.hasDrown()) + if(!ship.isDrown()) break; else cpt ++; @@ -51,7 +49,7 @@ public class Game { return players[1]; cpt = 0; for(Ship ship : players[1].getShips()){ - if(!ship.hasDrown()) + if(!ship.isDrown()) break; else cpt ++; @@ -76,41 +74,36 @@ public class Game { } public void setShips(Player player){ - Ship ship = new Ship(new Pair<>(0,0),2,new Pair<>(-1,-1)); Scanner scanner = new Scanner(System.in); - int x,y; - String dir = "AB"; - Pair pair = new Pair<>(null,null); + int x, y; + String dir; for(int i : ships){ - while(!player.setShips(ship)){ - System.out.println("Placement du bateau de longueur "+i +" :\n"); + boolean valid = false; + Ship ship = new Ship(new Pair<>(0,0), i, Direction.DEFAULT); + while(!player.setShips(ship)) { + if(valid) { + System.out.println("Erreur"); + } + valid = true; + System.out.println("Placement du bateau de longueur "+ ship.getSize()); System.out.println("Veuillez indiquer la coordonée x de votre bateau"); x = scanner.nextInt(); System.out.println("Veuillez indiquer la coordonée y de votre bateau"); y = scanner.nextInt(); - ship.setCoords(new Pair<>(x,y)); - while(!dir.equals("D") || !dir.equals("H") || !dir.equals("B") || !dir.equals("G")){ + ship.setCoords(new Pair<>(x, y)); + boolean validDirection = false; + while(!validDirection){ System.out.println("Veuillez indiquer la direction de placement de votre bateau (d droite, h haut, b bas, g gauche)"); - dir = scanner.nextLine(); + dir = scanner.next().toUpperCase(); System.out.println(dir); - dir = (String.valueOf(dir.substring(0,1))).toUpperCase(); - + for(Direction direction : Direction.values()) { + if(direction.getKeyword() != null && direction.getKeyword().equals(dir)) { + ship.setDirection(direction); + validDirection = true; + break; + } + } } - switch (dir){ - case "D": - ship.setDirection(new Pair<>(1,0)); - break; - case "H": - ship.setDirection(new Pair<>(0,1)); - break; - case "B": - ship.setDirection(new Pair<>(0,-1)); - break; - case "G": - ship.setDirection(new Pair<>(-1,0)); - break; - } - } } diff --git a/src/battleship/model/Ship.java b/src/battleship/model/Ship.java index 305728c..921aa10 100644 --- a/src/battleship/model/Ship.java +++ b/src/battleship/model/Ship.java @@ -6,17 +6,16 @@ public class Ship { private Pair coords; private final int size; - private Pair direction; - // (0,-1) bas // (0,1) haut // (1,0) droite // (-1,0) gauche + private Direction direction; // (0,-1) bas // (0,1) haut // (1,0) droite // (-1,0) gauche private boolean isDrown; - public Ship(Pair coords, int size,Pair direction) { + public Ship(Pair coords, int size, Direction direction) { this.coords = coords; this.size = size; this.direction = direction; isDrown = false; } - public void setDirection(Pair d){ + public void setDirection(Direction d){ this.direction = d; } public void setCoords(Pair c){ @@ -27,23 +26,29 @@ public class Ship { public void setDrown(){ isDrown = true; } - public Boolean hasDrown(){ + + public Boolean isDrown(){ return isDrown; } + public int getSize(){ return this.size; } - public Pair getDirection(){ + + public Direction getDirection(){ return this.direction; } + public Pair getCoords(){ return this.coords; } + + @SuppressWarnings("unchecked") public Pair[] getCoordsArray(){ - Pair[] tab = new Pair[size]; + Pair[] tab = new Pair[size]; for(int i = 0; i(coords.getLeft()+i* direction.getLeft(),coords.getRight()+i* direction.getRight()); + tab[i] = new Pair<>(coords.getLeft()+i* direction.getDirection().getLeft(),coords.getRight()+i * direction.getDirection().getRight()); } } return tab; diff --git a/src/battleship/model/player/Player.java b/src/battleship/model/player/Player.java index 88251e6..c2b2a50 100644 --- a/src/battleship/model/player/Player.java +++ b/src/battleship/model/player/Player.java @@ -12,15 +12,12 @@ public abstract class Player { protected ArrayList> moves = new ArrayList<>(); protected int id; - public Player(){ - } - public boolean setShips(Ship ship){ - int x,y; - for(int i = 0; i 9 ||x<0||y>9||y<0) + int x, y; + for(int i = 0; i < ship.getSize(); i++){ + x = ship.getCoords().getLeft() + i * ship.getDirection().getDirection().getLeft(); + y = ship.getCoords().getRight()+ i * ship.getDirection().getDirection().getRight(); + if(x > 9 || x < 0 || y > 9 || y < 0) return false; } this.ships.add(ship); @@ -36,12 +33,13 @@ public abstract class Player { moves.add(move); return this; } - public Boolean isItDrown(Ship ship){ + + public boolean isItDrown(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().getLeft(); - int y = ship.getCoords().getRight()+i*ship.getDirection().getRight(); + 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; @@ -50,20 +48,21 @@ public abstract class Player { } if(cpt == ship.getSize()) { ship.setDrown(); - return Boolean.TRUE; + return true; } - return Boolean.FALSE; + return false; } public ArrayList getShips(){ return this.ships; } + public ArrayList> getMoves(){ return this.moves; } public abstract Pair chooseMove(); - public ArrayList> validMoves(){ + public ArrayList> validMoves() { ArrayList> validMovesList = new ArrayList<>(); for(Integer i = 0; i<10;i++){ for(Integer y = 0;y<10;y++){ @@ -75,7 +74,7 @@ public abstract class Player { return validMovesList; } - public void setId(int i ){ + public void setId(int i){ id = i; }