From f2607f1268f1971c59465509b1bec0b32d0e1e93 Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Tue, 6 Apr 2021 09:55:02 +0200 Subject: [PATCH] Add View.placeShipRandomly --- src/battleship/model/Game.java | 4 +-- src/battleship/view/Terminal.java | 58 ++++++++++++++++++------------- src/battleship/view/View.java | 14 ++++++++ 3 files changed, 50 insertions(+), 26 deletions(-) diff --git a/src/battleship/model/Game.java b/src/battleship/model/Game.java index 430bb26..9d4f190 100644 --- a/src/battleship/model/Game.java +++ b/src/battleship/model/Game.java @@ -73,8 +73,8 @@ public class Game { } public Player Play(View view){ - //view.setShips(players[0]); - //view.setShips(players[1]); + view.setShips(players[0]); + view.setShips(players[1]); while(getWinner() == null) { System.out.println(view); Pair move = currentPlayer.chooseMove(); diff --git a/src/battleship/view/Terminal.java b/src/battleship/view/Terminal.java index af9fb3a..96f6554 100644 --- a/src/battleship/view/Terminal.java +++ b/src/battleship/view/Terminal.java @@ -3,6 +3,7 @@ package battleship.view; import battleship.model.Direction; import battleship.model.Game; import battleship.model.Ship; +import battleship.model.player.Human; import battleship.model.player.Player; import battleship.utils.Pair; @@ -21,37 +22,46 @@ public class Terminal extends View { System.out.println("Joueur " + player.getId() + ", placez vos navires"); int x, y; String dir; - for(int i : ships) { - boolean valid = false; - Ship ship = new Ship(new Pair<>(-1, -1), 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)); - 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.next().toUpperCase(); - for (Direction direction : Direction.values()) { - if (direction.getKeyword() != null && direction.getKeyword().equals(dir)) { - ship.setDirection(direction); - System.out.println(direction); - validDirection = true; - break; + if(player instanceof Human) { + for(int i : ships) { + boolean valid = false; + Ship ship = new Ship(new Pair<>(-1, -1), 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)); + 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.next().toUpperCase(); + for (Direction direction : Direction.values()) { + if (direction.getKeyword() != null && direction.getKeyword().equals(dir)) { + ship.setDirection(direction); + System.out.println(direction); + validDirection = true; + break; + } } } } } + } else { + // Random + placeShipRandomly(player); } + + } + + @Override public void displayBoard() { System.out.println(toString()); diff --git a/src/battleship/view/View.java b/src/battleship/view/View.java index 4e96a36..2697562 100644 --- a/src/battleship/view/View.java +++ b/src/battleship/view/View.java @@ -1,10 +1,14 @@ package battleship.view; +import battleship.model.Direction; 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; +import java.util.Random; import java.util.concurrent.atomic.AtomicInteger; public abstract class View { @@ -21,6 +25,16 @@ public abstract class View { public abstract void displayBoard(); + protected void placeShipRandomly(Player player) { + Random rand = new Random(); + for(int i : ships) { + Ship ship = null; + while(ship == null || !player.setShips(ship)) { + ship = new Ship(new Pair<>(rand.nextInt(10), rand.nextInt(10)), i, Direction.values()[rand.nextInt(Direction.values().length)]); + } + } + } + @Override public String toString() { ArrayList> moves = game.currentPlayer.getMoves();