feat: add battleship

This commit is contained in:
2025-05-12 22:31:47 +03:30
parent f80247b8b5
commit 0610b9d686
2 changed files with 93 additions and 0 deletions
+47
View File
@@ -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;
}
}
+46
View File
@@ -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;
}
}