Add Ship.java, Pair.java, fix Player.java, add ArrayList of ships in Battleship.java

This commit is contained in:
Quentin Legot 2021-03-23 10:37:51 +01:00
parent 2eb488f14a
commit 48ba657ef1
4 changed files with 75 additions and 2 deletions

View File

@ -1,7 +1,9 @@
package battleship.model;
import java.util.ArrayList;
public class Battleship {
public ArrayList<Ship> ships;
}

View File

@ -0,0 +1,15 @@
package battleship.model;
import battleship.utils.Pair;
public class Ship {
public Pair<Integer, Integer> coords;
public int size;
public Ship(Pair<Integer, Integer> coords, int size) {
this.coords = coords;
this.size = size;
}
}

View File

@ -1,20 +1,23 @@
package battleship.model.player;
import battleship.model.Ship;
import battleship.utils.Triplet;
import java.util.ArrayList;
public abstract class Player {
protected ArrayList<Ships> ships = new ArrayList<>();
protected ArrayList<Ship> ships = new ArrayList<>();
protected ArrayList<Triplet> moves = new ArrayList<>();
public Player(){
setShips();
}
public void setShips(){
}
public void addMove(Triplet move){
moves.add(move);
}

View File

@ -0,0 +1,53 @@
package battleship.utils;
import java.util.Objects;
/**
* tuple containing 2 unknown type elements
*
* @param <U> left
* @param <K> right
*/
public class Pair<U, K> {
private final U left;
private final K right;
public Pair(U left, K right) {
this.left = left;
this.right = right;
}
public U getLeft() {
return left;
}
public K getRight() {
return right;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Pair<?, ?> other = (Pair<?, ?>) obj;
return this.left.equals(other.getLeft()) && this.left.equals(other.getRight());
}
@Override
public int hashCode() {
return Objects.hash(left.hashCode(), right.hashCode());
}
@Override
public String toString() {
return "(" + left + ", " + right + ")";
}
}