brilliant_moves

Battleships.java

Sep 7th, 2012
403
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 2.50 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Battleships {
  4.  
  5.     /*
  6.     *   Program:    Battleships.java
  7.     *   Purpose:    Emulate popular board game
  8.     *   Creator:    Chris Clarke
  9.     *   Created:    26.03.2012
  10.     */
  11.  
  12.     // change this to a create larger or smaller board, e.g. 5 or 7
  13.     static final int BOARDSIZE = 6;
  14.  
  15.     // create global array
  16.     static int [][] grid = new int[BOARDSIZE][BOARDSIZE];
  17.  
  18.     // create global variables
  19.     static int row, column;
  20.  
  21.     public static void main(String args[]) {
  22.         Scanner scanner = new Scanner(System.in);
  23.  
  24.         int hits = 0, misses = 0;
  25.         int numShips = 0;
  26.  
  27.         // set all grid values to zero
  28.         for (int r = 0; r < BOARDSIZE; r++) {
  29.             for (int c = 0; c < BOARDSIZE; c++) {
  30.                 grid[r][c] = 0;
  31.             }
  32.         }
  33.  
  34.         // display board dimensions
  35.         System.out.println("Board size = "+BOARDSIZE+" by "+BOARDSIZE);
  36.         System.out.println("Row and column are 0 to "+(BOARDSIZE-1));
  37.  
  38.         // set the number of ships on the board
  39.         do {
  40.             System.out.print("Enter number of ships(1..5): ");
  41.             numShips = scanner.nextInt();
  42.         } while (numShips<1||numShips>5);
  43.  
  44.         // set targets, 1 for each ship
  45.         for (int i=0; i<numShips; i++)
  46.             setTarget();
  47.  
  48.         // keep getting user input and checking it
  49.         do {
  50.             // get row
  51.             System.out.print("Enter row: ");
  52.             row = scanner.nextInt();
  53.             // get column
  54.             System.out.print("Enter column: ");
  55.             column = scanner.nextInt();
  56.  
  57.             // check valid row and column
  58.             if ( row < 0 || row > (BOARDSIZE-1))
  59.                 System.out.println( "Error in row number");
  60.             else if ( column < 0 || column > (BOARDSIZE-1))
  61.                 System.out.println( "Error in column number");
  62.             else if ( grid[row][column] == 1) {
  63.                 System.out.println( "***** hit! *****");
  64.                 // will need to register as a miss if same square tried again,
  65.                 // so change value
  66.                 grid[row][column] = 2;
  67.                 hits++;
  68.             } else {
  69.                 System.out.println( "miss!");
  70.                 misses++;
  71.             }
  72.         } while (hits<numShips);
  73.  
  74.         System.out.print("Game Over.  You won in ");
  75.         int attempts = hits + misses;
  76.         if (attempts == 1) {
  77.             System.out.println("1 attempt.  Lucky!");
  78.         } else {
  79.             System.out.println(attempts+" attempts.");
  80.         }
  81.     }
  82.  
  83.     public static void setTarget() {
  84.         // select a blank square at random, set its value to 1
  85.  
  86.         row = (int) (Math.random()*BOARDSIZE);      // 0 to BOARDSIZE-1 inclusive
  87.         column = (int) (Math.random()*BOARDSIZE);   // 0 to BOARDSIZE-1 inclusive
  88.         if (grid[row][column]==0) {         // blank square
  89.             grid[row][column] = 1;          // set a battleship
  90.         } else {
  91.             setTarget();                // recursive procedure call
  92.         }
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment