Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Grid
- {
- // Write your Grid class here
- private Location[][] grid;
- public static final int NUM_ROWS = 10;
- public static final int NUM_COLS = 10;
- private String[][] board;
- private String[][] ships;
- private String[][] shipBoard;
- private int [][] statusBoard;
- public Grid() {
- board = new String[NUM_ROWS][NUM_COLS];
- for(int i = 0; i < board.length; i ++) {
- for(int j = 0; j < board[0].length; j ++) {
- board[i][j] = "- ";
- }
- }
- ships = new String[NUM_ROWS][NUM_COLS];
- for(int i = 0; i < ships.length; i ++) {
- for(int j = 0; j < board[0].length; j ++) {
- ships[i][j] = "O";
- }
- }
- statusBoard = new int[NUM_ROWS][NUM_COLS];
- for(int i = 0; i < ships.length; i ++) {
- for(int j = 0; j < board[0].length; j ++) {
- ships[i][j] = "0";
- }
- }
- shipBoard = new String[NUM_ROWS][NUM_COLS];
- for(int i = 0; i < shipBoard.length; i ++) {
- for(int j = 0; j < shipBoard[0].length; j ++) {
- shipBoard[i][j] = "- ";
- }
- }
- }
- public void markHit(int row, int col) {
- board[row][col] = "X ";
- statusBoard[row][col] = 1;
- }
- public void markMiss(int row, int col) {
- board[row][col] = "O ";
- statusBoard[row][col] = 2;
- }
- public void setStatus(int row, int col, int status) {
- statusBoard[row][col] = status;
- }
- public int getStatus(int row, int col) {
- return statusBoard[row][col];
- }
- public boolean alreadyGuessed(int row, int col) {
- return true;
- }
- public void setShip(int row, int col, boolean val) {
- ships[row][col] = " X ";
- }
- public boolean hasShip(int row, int col) {
- return true;
- }
- public Location get(int row, int col) {
- Location l = new Location();
- return l;
- }
- public int numRows() {
- return NUM_ROWS;
- }
- public int numCols() {
- return NUM_COLS;
- }
- public void printStatus() {
- for(int j = 0; j < board[0].length - 1; j ++) {
- System.out.print(j + 1 + " ");
- }
- System.out.print("10");
- System.out.print("\nA ");
- for(int j = 0; j < board[0].length; j ++) {
- System.out.print(board[0][j]);
- }
- System.out.print("\nB ");
- for(int j = 0; j < board[0].length; j ++) {
- System.out.print(board[1][j]);
- }
- System.out.print("\nC ");
- for(int j = 0; j < board[0].length; j ++) {
- System.out.print(board[2][j]);
- }
- System.out.print("\nD ");
- for(int j = 0; j < board[0].length; j ++) {
- System.out.print(board[3][j]);
- }
- System.out.print("\nE ");
- for(int j = 0; j < board[0].length; j ++) {
- System.out.print(board[4][j]);
- }
- System.out.print("\nF ");
- for(int j = 0; j < board[0].length; j ++) {
- System.out.print(board[5][j]);
- }
- System.out.print("\nG ");
- for(int j = 0; j < board[0].length; j ++) {
- System.out.print(board[6][j]);
- }
- System.out.print("\nH ");
- for(int j = 0; j < board[0].length; j ++) {
- System.out.print(board[7][j]);
- }
- System.out.print("\nI ");
- for(int j = 0; j < board[0].length; j ++) {
- System.out.print(board[8][j]);
- }
- System.out.print("\nJ ");
- for(int j = 0; j < board[0].length; j ++) {
- System.out.print(board[9][j]);
- }
- }
- public void printShips() {
- for(int i = 0; i < ships.length; i ++) {
- for(int j = 0; j < shipBoard[0].length; j ++) {
- if(ships[i][j].equals(" X ")) {
- shipBoard[i][j] = "X ";
- }
- }
- }
- //////////////////////////////////////////////////////////////////////
- for(int j = 0; j < board[0].length - 1; j ++) {
- System.out.print(j + 1 + " ");
- }
- System.out.print("10");
- System.out.print("\nA ");
- for(int j = 0; j < board[0].length; j ++) {
- System.out.print(shipBoard[0][j]);
- }
- System.out.print("\nB ");
- for(int j = 0; j < board[0].length; j ++) {
- System.out.print(shipBoard[1][j]);
- }
- System.out.print("\nC ");
- for(int j = 0; j < board[0].length; j ++) {
- System.out.print(shipBoard[2][j]);
- }
- System.out.print("\nD ");
- for(int j = 0; j < board[0].length; j ++) {
- System.out.print(shipBoard[3][j]);
- }
- System.out.print("\nE ");
- for(int j = 0; j < board[0].length; j ++) {
- System.out.print(shipBoard[4][j]);
- }
- System.out.print("\nF ");
- for(int j = 0; j < board[0].length; j ++) {
- System.out.print(shipBoard[5][j]);
- }
- System.out.print("\nG ");
- for(int j = 0; j < board[0].length; j ++) {
- System.out.print(shipBoard[6][j]);
- }
- System.out.print("\nH ");
- for(int j = 0; j < board[0].length; j ++) {
- System.out.print(shipBoard[7][j]);
- }
- System.out.print("\nI ");
- for(int j = 0; j < board[0].length; j ++) {
- System.out.print(shipBoard[8][j]);
- }
- System.out.print("\nJ ");
- for(int j = 0; j < board[0].length; j ++) {
- System.out.print(shipBoard[9][j]);
- }
- /////////////////////////////////////////////////////////////
- }
- public void addShip(Ship s) {
- ships[s.getRow()][s.getCol()] = " X ";
- int counter = 0;
- int length = s.getLength();
- int direction = s.getDirection();
- if(direction == 0) {
- while(counter < length) {
- ships[s.getRow()][s.getCol() + counter] = " X ";
- counter ++;
- }
- } else if(direction == 1) {
- while(counter < length) {
- ships[s.getRow() + counter][s.getCol()] = " X ";
- counter ++;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement