Add comments
This commit is contained in:
parent
5d2aa4edd5
commit
ed6ab60fbb
@ -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<Integer, Integer> coords;
|
||||
/**
|
||||
* ship size
|
||||
*/
|
||||
private final int size;
|
||||
/**
|
||||
* ship full coordinates calculate thank to base coordinates, direction and size
|
||||
*/
|
||||
Pair<Integer,Integer>[] 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<Integer, Integer> 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<Integer, Integer> coords : getFullCoords()) {
|
||||
|
@ -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<Ship> 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<Integer,Integer,Boolean> move){
|
||||
public void addMove(Triplet<Integer,Integer,Boolean> move){
|
||||
moves.add(move);
|
||||
return this;
|
||||
}
|
||||
|
||||
public ArrayList<Pair<Integer,Integer>> validMoves() {
|
||||
|
@ -6,6 +6,11 @@ import battleship.utils.Pair;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* <p>Computer super class, all player object which use an algorithm to calculate coordinates to use should extend from
|
||||
* this object.</p>
|
||||
* <p>Random is the only algorithm include here but another algorithm can be easily implemented</p>
|
||||
*/
|
||||
public abstract class Computer extends AbstractPlayer {
|
||||
|
||||
public void placeShipRandomly() {
|
||||
|
@ -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
|
||||
|
@ -6,25 +6,65 @@ import battleship.utils.Triplet;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* <p>Player interface, used as an API.</p>
|
||||
* <p>This model (interface -> abstract class(es) -> concrete classes) prevent hard code.</p>
|
||||
* <p>This is the only object which interact with other object<br>
|
||||
* Allows an outside person from the project to easily change the code or to add a view easily without modifying
|
||||
* existing classes</p>
|
||||
* @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<Integer,Integer> 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<Integer,Integer,Boolean> move);
|
||||
/**
|
||||
* Adds coordinates of the {@code move} in the {@link AbstractPlayer#moves} list
|
||||
* @param move the move chosen by the player
|
||||
*/
|
||||
void addMove(Triplet<Integer,Integer,Boolean> 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<Pair<Integer,Integer>> 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<Ship> getShips();
|
||||
|
@ -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
|
||||
|
@ -4,8 +4,8 @@ import battleship.model.player.Player;
|
||||
import battleship.utils.Pair;
|
||||
|
||||
/**
|
||||
* <p>View interface, used as an API<br>
|
||||
* This model (interface(s) -> abstract class(es) -> concrete classes) prevent hard code.</p>
|
||||
* <p>View interface, used as an API</p>
|
||||
* <p>This model (interface -> abstract class(es) -> concrete classes) prevent hard code.</p>
|
||||
* <p>This is the only object which interact with other object<br>
|
||||
* Allows an outside person easily change the code or add a view easily without modifying existing classes</p>
|
||||
* @see AbstractView
|
||||
|
Loading…
Reference in New Issue
Block a user