From 1fc316b13244c11567d1b93bda18bed587b153b7 Mon Sep 17 00:00:00 2001 From: Antonin Boyon Date: Tue, 6 Apr 2021 10:31:54 +0200 Subject: [PATCH] moving placeShip to Player --- src/battleship/model/player/Player.java | 14 +++++++++++++- src/battleship/view/Window.java | 6 ++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/battleship/model/player/Player.java b/src/battleship/model/player/Player.java index cdf072d..fab9ec6 100644 --- a/src/battleship/model/player/Player.java +++ b/src/battleship/model/player/Player.java @@ -1,16 +1,19 @@ 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 { protected ArrayList ships = new ArrayList<>(); protected ArrayList> moves = new ArrayList<>(); protected int id; + protected final int[] bato = { 5, 4, 3, 3, 2}; public boolean setShips(Ship ship){ for(int i = 0; i < ship.getSize(); i++){ @@ -88,5 +91,14 @@ public abstract class Player { public int getId() { return id; } - + + public void placeShipRandomly(Player player) { + Random rand = new Random(); + for(int i : bato) { + 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)]); + } + } + } } diff --git a/src/battleship/view/Window.java b/src/battleship/view/Window.java index 72146f6..ca67a7c 100644 --- a/src/battleship/view/Window.java +++ b/src/battleship/view/Window.java @@ -3,6 +3,8 @@ package battleship.view; import battleship.model.Game; import battleship.model.player.Player; +import java.awt.Graphics; + import javax.swing.*; public class Window extends View { @@ -26,4 +28,8 @@ public class Window extends View { public void displayBoard() { } + + public void designBoard(Graphics g) { + //super.paintComponent(g); + } }