From 48ba657ef141c9f3389e494c5942dcbb2a6327b6 Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Tue, 23 Mar 2021 10:37:51 +0100 Subject: [PATCH] Add Ship.java, Pair.java, fix Player.java, add ArrayList of ships in Battleship.java --- src/battleship/model/Battleship.java | 4 +- src/battleship/model/Ship.java | 15 +++++++ src/battleship/model/player/Player.java | 5 ++- src/battleship/utils/Pair.java | 53 +++++++++++++++++++++++++ 4 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 src/battleship/model/Ship.java create mode 100644 src/battleship/utils/Pair.java diff --git a/src/battleship/model/Battleship.java b/src/battleship/model/Battleship.java index a98968b..19cf433 100644 --- a/src/battleship/model/Battleship.java +++ b/src/battleship/model/Battleship.java @@ -1,7 +1,9 @@ package battleship.model; +import java.util.ArrayList; + public class Battleship { - + public ArrayList ships; } diff --git a/src/battleship/model/Ship.java b/src/battleship/model/Ship.java new file mode 100644 index 0000000..675c79f --- /dev/null +++ b/src/battleship/model/Ship.java @@ -0,0 +1,15 @@ +package battleship.model; + +import battleship.utils.Pair; + +public class Ship { + + public Pair coords; + public int size; + + public Ship(Pair coords, int size) { + this.coords = coords; + this.size = size; + } + +} diff --git a/src/battleship/model/player/Player.java b/src/battleship/model/player/Player.java index 900a03d..fad7cc2 100644 --- a/src/battleship/model/player/Player.java +++ b/src/battleship/model/player/Player.java @@ -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 = new ArrayList<>(); + protected ArrayList ships = new ArrayList<>(); protected ArrayList moves = new ArrayList<>(); public Player(){ setShips(); } + public void setShips(){ } + public void addMove(Triplet move){ moves.add(move); } diff --git a/src/battleship/utils/Pair.java b/src/battleship/utils/Pair.java new file mode 100644 index 0000000..73c963c --- /dev/null +++ b/src/battleship/utils/Pair.java @@ -0,0 +1,53 @@ +package battleship.utils; + +import java.util.Objects; + +/** + * tuple containing 2 unknown type elements + * + * @param left + * @param right + */ +public class Pair { + + 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 + ")"; + } +}