Advertisement
Guest User

airware

a guest
Nov 26th, 2014
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.77 KB | None | 0 0
  1. package airware;
  2.  
  3.  
  4. public class Board {
  5.     public int rows;
  6.     public int cols;
  7.     private int[][] board;
  8.     private int SHIP_LENGTH = 3;
  9.    
  10.     public Board(int rows, int cols) {
  11.         this.rows = rows;
  12.         this.cols = cols;
  13.         this.board = new int[rows][cols];
  14.         for (int i = 0; i < rows; i++) {
  15.             for (int j = 0; j < cols; j++) {
  16.                 this.board[i][j] = 0;
  17.             }
  18.         }
  19.     }
  20.    
  21.     public int[][] getBoard() {
  22.         return board;
  23.     }
  24.    
  25.     public void placeShip() {
  26.         while (true) {
  27.             int x = (int) Math.floor(Math.random() * rows);
  28.             int y = (int) Math.floor(Math.random() * cols);
  29.             int orientation = (int) Math.floor(Math.random() * 2); // 0 or 1
  30.             if (isValidPlacement(x, y, orientation)) {
  31.                 // placeShip
  32.                 if (orientation == 0) {
  33.                     for (int i = 0; i < SHIP_LENGTH; i++) {
  34.                         board[x][y+i] = 1;
  35.                     }
  36.                 } else if (orientation == 1) {
  37.                     for (int i = 0; i < SHIP_LENGTH; i++) {
  38.                         board[x+i][y] = 1;
  39.                     }
  40.                 }
  41.                 break;
  42.             }
  43.         }
  44.     }
  45.    
  46.     /*
  47.      * Orientation: 0 = horizontal, 1 = vertical
  48.      */
  49.     private boolean isValidPlacement(int x, int y, int orientation) {
  50.         if (this.board[x][y] != 0) {
  51.             return false;
  52.         }
  53.         if (orientation == 0) {
  54.             if (y+SHIP_LENGTH < 0 || y+SHIP_LENGTH >= cols) {
  55.                 return false;
  56.             }
  57.             for (int i = 0; i < SHIP_LENGTH; i++) {
  58.                 if (board[x][y+i] != 0) {
  59.                     return false;
  60.                 }
  61.             }
  62.         } else if (orientation == 1) {
  63.             if (x+SHIP_LENGTH < 0 || x+SHIP_LENGTH >= rows) {
  64.                 return false;
  65.             }
  66.             for (int i = 0; i < SHIP_LENGTH; i++) {
  67.                 if (board[x+i][y] != 0) {
  68.                     return false;
  69.                 }
  70.             }
  71.         }
  72.         return true;
  73.     }
  74.    
  75.     public boolean guess(int x, int y) {
  76.         // out of bounds
  77.         if (x < 0 || y < 0 || x >= rows || y >= cols) {
  78.             return false;
  79.         // in bounds, check board
  80.         } else {
  81.             if (board[x][y] == 0) {
  82.                 return false;
  83.             } else {
  84.                 board[x][y] = -1;
  85.                 return true;
  86.             }
  87.         }
  88.     }
  89. }
  90.  
  91.  
  92. package airware;
  93.  
  94. import static org.junit.Assert.*;
  95.  
  96. import org.junit.Test;
  97.  
  98. public class BoardTest {
  99.  
  100.     @Test
  101.     public void createBoardTest() {
  102.         Board b = new Board(10, 10);
  103.         int[][] board = b.getBoard();
  104.         int[][] result = new int[10][10];
  105.         for (int i = 0; i < 10; i++) {
  106.             for (int j = 0; j < 10; j++) {
  107.                 result[i][j] = 0;
  108.             }
  109.         }
  110.         assertEquals(board, result);
  111.     }
  112.    
  113.     @Test
  114.     public void placeShipTest() {
  115.         Board b = new Board(10, 10);
  116.         b.placeShip();
  117.         int[][] board = b.getBoard();
  118.         int counter = 0;
  119.         for (int i = 0; i < 10; i++) {
  120.             for (int j = 0; j < 10; j++) {
  121.                 if (board[i][j] == 1) {
  122.                     counter++;
  123.                 }
  124.             }
  125.         }
  126.         assertEquals(counter, 3);
  127.     }
  128.    
  129.     @Test
  130.     public void guessTest() {
  131.         Board b = new Board(10, 10);
  132.         b.placeShip();
  133.         int trueGuesses = 0;
  134.         for (int i = 0; i < 10; i++) {
  135.             for (int j = 0; j < 10; j++) {
  136.                 if (b.guess(i, j)) {
  137.                     trueGuesses++;
  138.                 }
  139.             }
  140.         }
  141.         assertEquals(trueGuesses, 3);
  142.     }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement