Skip to content

Instantly share code, notes, and snippets.

@zufardhiyaulhaq
Created February 23, 2020 13:35
Show Gist options
  • Save zufardhiyaulhaq/9b0abdbc2e802e4fb901267870c7137a to your computer and use it in GitHub Desktop.
Save zufardhiyaulhaq/9b0abdbc2e802e4fb901267870c7137a to your computer and use it in GitHub Desktop.
package com.bootcamp.battleship;
import java.util.ArrayList;
public class Player {
public static final String SHIP_VALUE = "B";
public Map map;
private String name;
private ArrayList<Coordinate> missile = new ArrayList<Coordinate>();
public Player(String name, Map map){
if (name.isEmpty()) {
throw new IllegalArgumentException("Name should not empty");
}
this.name = name;
this.map = map;
}
public String getName() {
return this.name;
}
public void addShip(Coordinate shipCoordinate) {
checkCoordinate(shipCoordinate);
this.map.change(shipCoordinate, SHIP_VALUE);
}
public void addMissile(Coordinate missileCoordinate) {
checkCoordinate(missileCoordinate);
this.missile.add(missileCoordinate);
}
public int countShip() {
return this.map.count(SHIP_VALUE);
}
public void attack(Player enemy) {
for (Coordinate eachMissile : this.missile) {
System.out.flush();
System.out.println(enemy.map.get(eachMissile));
if (enemy.map.get(eachMissile).equals(SHIP_VALUE)) {
enemy.map.change(eachMissile, "X");
} else {
enemy.map.change(eachMissile, "O");
}
}
}
private void checkCoordinate(Coordinate coordinate) {
int xAxis = coordinate.getXAxis();
int yAxis = coordinate.getYAxis();
if (xAxis < 0 || yAxis < 0) {
throw new IllegalArgumentException("Coordinate should not negative");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment