From ed6ab60fbbeef2e8f2871c5d63488021cadabe3d Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Mon, 3 May 2021 14:55:41 +0200 Subject: [PATCH] Add comments --- src/battleship/model/Ship.java | 28 ++++++++++++- .../model/player/AbstractPlayer.java | 11 +++-- src/battleship/model/player/Computer.java | 5 +++ src/battleship/model/player/Human.java | 4 ++ src/battleship/model/player/Player.java | 42 ++++++++++++++++++- src/battleship/model/player/Random.java | 3 ++ src/battleship/view/View.java | 4 +- 7 files changed, 90 insertions(+), 7 deletions(-) diff --git a/src/battleship/model/Ship.java b/src/battleship/model/Ship.java index 9a77311..92e196b 100644 --- a/src/battleship/model/Ship.java +++ b/src/battleship/model/Ship.java @@ -4,14 +4,30 @@ import battleship.model.player.Player; import battleship.utils.Pair; import battleship.utils.Triplet; +/** + * player's ship class + */ public class Ship { + /** + * base coordinates of the ship + */ private Pair coords; + /** + * ship size + */ private final int size; + /** + * ship full coordinates calculate thank to base coordinates, direction and size + */ Pair[] fullCoords; - private Direction direction; // (0,-1) bas // (0,1) haut // (1,0) droite // (-1,0) gauche + private Direction direction; + /** + * if true the ship is destroyed + */ private boolean isDrown; + @SuppressWarnings("unchecked") public Ship(Pair coords, int size, Direction direction) { this.coords = coords; this.size = size; @@ -29,6 +45,9 @@ public class Ship { this.coords = c; } + /** + * set {@link Ship#isDrown} to true + */ public void setDrown(){ isDrown = true; } @@ -49,6 +68,9 @@ public class Ship { return this.coords; } + /** + * recalculate all coords based on this base coords, direction and size + */ 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()); @@ -64,6 +86,10 @@ public class Ship { return super.toString() + ", coords=" + coords.toString() + ", size=" + size + ", direction=" + direction.toString(); } + /** + * update value {@link Ship#isDrown} to true if {@code player} hit all of ship boxes + * @param player the opponent of this ship owner + */ public void updateIsDrown(Player player) { int cpt = 0; for(Pair coords : getFullCoords()) { diff --git a/src/battleship/model/player/AbstractPlayer.java b/src/battleship/model/player/AbstractPlayer.java index 8bdff77..f3a1ad6 100644 --- a/src/battleship/model/player/AbstractPlayer.java +++ b/src/battleship/model/player/AbstractPlayer.java @@ -7,6 +7,13 @@ import battleship.utils.Triplet; import java.util.ArrayList; +/** + * Abstract player class see {@link Player} to know more about this code organisation + * @see Player + * @see Human + * @see Computer + * @see Random + */ public abstract class AbstractPlayer implements Player { ArrayList ships = new ArrayList<>(); @@ -33,11 +40,9 @@ public abstract class AbstractPlayer implements Player { /** * 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){ + public void addMove(Triplet move){ moves.add(move); - return this; } public ArrayList> validMoves() { diff --git a/src/battleship/model/player/Computer.java b/src/battleship/model/player/Computer.java index 68b24b0..ea9832a 100644 --- a/src/battleship/model/player/Computer.java +++ b/src/battleship/model/player/Computer.java @@ -6,6 +6,11 @@ import battleship.utils.Pair; import java.util.Random; +/** + *

Computer super class, all player object which use an algorithm to calculate coordinates to use should extend from + * this object.

+ *

Random is the only algorithm include here but another algorithm can be easily implemented

+ */ public abstract class Computer extends AbstractPlayer { public void placeShipRandomly() { diff --git a/src/battleship/model/player/Human.java b/src/battleship/model/player/Human.java index 799f146..0a94e8d 100644 --- a/src/battleship/model/player/Human.java +++ b/src/battleship/model/player/Human.java @@ -2,6 +2,10 @@ package battleship.model.player; import battleship.utils.Pair; +/** + * This object do nothing itself, it just an interface to know the type of player (Human or not), + * each view interact with the player with its methods + */ public class Human extends AbstractPlayer { @Override diff --git a/src/battleship/model/player/Player.java b/src/battleship/model/player/Player.java index 2a25c52..f97f2c8 100644 --- a/src/battleship/model/player/Player.java +++ b/src/battleship/model/player/Player.java @@ -6,25 +6,65 @@ import battleship.utils.Triplet; import java.util.ArrayList; +/** + *

Player interface, used as an API.

+ *

This model (interface -> abstract class(es) -> concrete classes) prevent hard code.

+ *

This is the only object which interact with other object
+ * Allows an outside person from the project to easily change the code or to add a view easily without modifying + * existing classes

+ * @see AbstractPlayer + * @see Computer + * @see Human + * @see Random + */ public interface Player { int[] shipSize = { 5, 4, 3, 3, 2 }; + /** + * Used by computer only + * @see Random#chooseMove() + * @return coords in its opponent grid to play a move + */ Pair chooseMove(); + /** + * check if ship position and direction are valides and does not overlap on other vessels + * add the ship to player {@link AbstractPlayer#ships} list and return {@code true} if valid + * {@code false} otherwise + * @param ship the ship instance we check + * @return {@code true} if ship data are valids, {@code false} otherwise + */ boolean setShips(Ship ship); int getId(); - Player addMove(Triplet move); + /** + * Adds coordinates of the {@code move} in the {@link AbstractPlayer#moves} list + * @param move the move chosen by the player + */ + void addMove(Triplet move); void setId(int i); + /** + * give a list of the player possible moves, used in {@link Player#chooseMove()} + * @return a list of playable move + */ ArrayList> validMoves(); + /** + * Used by {@link Computer} instances to place ships + */ void placeShips(); + /** + * check if coordinates from {@link battleship.view.View#chooseMove(Player)}are in valids position + * @param x the x-axis of the coordinates + * @param y the y-axis of the coordinates + * @return {@code true} if valid, {@code false} otherwise + */ boolean areValid(int x, int y); ArrayList getShips(); diff --git a/src/battleship/model/player/Random.java b/src/battleship/model/player/Random.java index c1e76f1..a6dfcfb 100644 --- a/src/battleship/model/player/Random.java +++ b/src/battleship/model/player/Random.java @@ -2,6 +2,9 @@ package battleship.model.player; import battleship.utils.Pair; +/** + * place its ship and choose moves randomly + */ public class Random extends Computer { @Override diff --git a/src/battleship/view/View.java b/src/battleship/view/View.java index 39d8851..12e2df2 100644 --- a/src/battleship/view/View.java +++ b/src/battleship/view/View.java @@ -4,8 +4,8 @@ import battleship.model.player.Player; import battleship.utils.Pair; /** - *

View interface, used as an API
- * This model (interface(s) -> abstract class(es) -> concrete classes) prevent hard code.

+ *

View interface, used as an API

+ *

This model (interface -> abstract class(es) -> concrete classes) prevent hard code.

*

This is the only object which interact with other object
* Allows an outside person easily change the code or add a view easily without modifying existing classes

* @see AbstractView