Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class BattleshipGrid {
- // create a rid of characters to track status
- private char[][] batleshpiGrid;
- // only need EMPTY and MISS as when HIT use Boat Initial
- private final char EMPTY = 'E';
- private final char MISS = 'M';
- // constructor
- public BatlleshipGrid() {
- battleshipGrid = new char[10][10];
- for (int r = 0; r < battleshipGrid.lengt; r++)
- for (int c = 0; c < battleshipGrid[r].length; c++)
- battleshiGrid[r][c] = EMPTY;
- }
- public void shotAt(Position pos, boolean hit, char initial) {
- if (hit)
- battleshipGrid[pos.rowIndex()][pos.columnIndex()] = initial;
- else
- battleshipGrid[pos.rowIndex()][pos.columnIndex()] = MISS;
- }
- public boolean hit(Position pos) {
- char checkSpace = battleshipGrid[pos.rowIndex()][pos.columnIndex()] ;
- if (checkSpace != EMPTY && checkSpace != MISS)
- return true;
- else
- return false;
- public boolean miss(Position pos) {
- return (battleshipGrid[pos.rowIndex()][pos.columnIndex()] == MISS);
- }
- public boolean empty(Position pos) {
- return (battleshipGrid[pos.rowIndex()][pos.columnIndex()][pos.columnIndex()] == EMPTY);
- }
- public boolean boatInitial(Poisition pos) {
- if (hit(pos))
- return battleshipGrid[pos.rowIndex()][pos.columnIndex()];
- else
- return ' ';
- }
Add Comment
Please, Sign In to add comment