feat: add battleship
This commit is contained in:
@@ -0,0 +1,47 @@
|
|||||||
|
package Client;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class BattleShip {
|
||||||
|
static final Map<String, Integer> ships = Map.of(
|
||||||
|
"Carrier", 5,
|
||||||
|
"Battleship", 4,
|
||||||
|
"Cruiser", 3,
|
||||||
|
"Submarine", 3,
|
||||||
|
"Destroyer", 2
|
||||||
|
);
|
||||||
|
|
||||||
|
private final int length;
|
||||||
|
private final String type;
|
||||||
|
private final List<BattleShipCell> cellsInBoard = new ArrayList<>();
|
||||||
|
|
||||||
|
public BattleShip(String type) {
|
||||||
|
this.type = type;
|
||||||
|
if(ships.get(type) == null)
|
||||||
|
{
|
||||||
|
System.out.println("SHIP NOT FOUND");
|
||||||
|
}
|
||||||
|
length = ships.get(type);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addShipCell(BattleShipCell cell){
|
||||||
|
cellsInBoard.add(cell);
|
||||||
|
}
|
||||||
|
public boolean isAllHit() {
|
||||||
|
for (BattleShipCell cell : cellsInBoard) {
|
||||||
|
if (!cell.isMarked())
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getLength() {
|
||||||
|
return length;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getType() {
|
||||||
|
return type;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,46 @@
|
|||||||
|
package Client;
|
||||||
|
|
||||||
|
import Client.utils.AnsiColor;
|
||||||
|
|
||||||
|
public class BattleShipCell {
|
||||||
|
private BattleShip battleShip;
|
||||||
|
private boolean isMarked;
|
||||||
|
private boolean isEnemyShip;
|
||||||
|
public BattleShipCell()
|
||||||
|
{
|
||||||
|
this.isEnemyShip = false;
|
||||||
|
this.isMarked = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isMarked() {
|
||||||
|
return isMarked;
|
||||||
|
}
|
||||||
|
public void setMarked(boolean value) {
|
||||||
|
isMarked = value;
|
||||||
|
}
|
||||||
|
public boolean isShip() {
|
||||||
|
return battleShip != null || isEnemyShip;
|
||||||
|
}
|
||||||
|
public BattleShip getBattleShip() {
|
||||||
|
return battleShip;
|
||||||
|
}
|
||||||
|
public void setBattleShip(BattleShip battleShip) {
|
||||||
|
this.battleShip = battleShip;
|
||||||
|
}
|
||||||
|
public void setEnemyShip(boolean value) {
|
||||||
|
this.isEnemyShip = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
if(isMarked){
|
||||||
|
return (isShip())? AnsiColor.RED + "X" + AnsiColor.RESET: AnsiColor.BLUE + "O" + AnsiColor.RESET;
|
||||||
|
}
|
||||||
|
if(isShip())
|
||||||
|
{
|
||||||
|
return AnsiColor.WHITE + "S" + AnsiColor.RESET;
|
||||||
|
}
|
||||||
|
return AnsiColor.GRAY + "." + AnsiColor.RESET;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user