Skip to content

Instantly share code, notes, and snippets.

@zufardhiyaulhaq
Created February 23, 2020 13:44
Show Gist options
  • Save zufardhiyaulhaq/8fbe67bbe81c2cc27c8039a86e1f7a8b to your computer and use it in GitHub Desktop.
Save zufardhiyaulhaq/8fbe67bbe81c2cc27c8039a86e1f7a8b to your computer and use it in GitHub Desktop.
package com.bootcamp.battleship;
import java.util.Arrays;
public class Map {
private String[][] map;
private int grid;
public Map(int grid) {
int minArray = 1;
if (grid < minArray) {
throw new IllegalArgumentException("Grid must larger than 0");
}
this.grid = grid;
fill();
}
public String get(Coordinate coordinate) {
checkCoordinate(coordinate);
int xAxis = coordinate.getXAxis();
int yAxis = coordinate.getYAxis();
return this.map[xAxis][yAxis];
}
public void change(Coordinate coordinate, String withValue) {
checkCoordinate(coordinate);
int xAxis = coordinate.getXAxis();
int yAxis = coordinate.getYAxis();
this.map[xAxis][yAxis] = withValue;
}
public int count(String countValue) {
int count = 0;
for(String[] line: this.map) {
for(String value: line) {
if (value.equals(countValue)) {
count += 1;
}
}
}
return count;
}
private void fill() {
this.map = new String[this.grid][this.grid];
for (String[] line: this.map) {
Arrays.fill(line, "_");
}
}
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