Add Ship.java, Pair.java, fix Player.java, add ArrayList of ships in Battleship.java
This commit is contained in:
parent
2eb488f14a
commit
48ba657ef1
@ -1,7 +1,9 @@
|
||||
package battleship.model;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Battleship {
|
||||
|
||||
|
||||
public ArrayList<Ship> ships;
|
||||
|
||||
}
|
||||
|
15
src/battleship/model/Ship.java
Normal file
15
src/battleship/model/Ship.java
Normal 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;
|
||||
}
|
||||
|
||||
}
|
@ -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);
|
||||
}
|
||||
|
53
src/battleship/utils/Pair.java
Normal file
53
src/battleship/utils/Pair.java
Normal 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 + ")";
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user