Guest User

Battleship 8x8

a guest
Jan 27th, 2013
11,967
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.08 KB | None | 0 0
  1.    import java.util.*;
  2.    import java.util.Scanner;
  3.    public class BattleshipLab
  4.    {
  5.       Scanner input = new Scanner(System.in);
  6.       public static final boolean DEBUG = false;
  7.       public static void breakln()
  8.       {
  9.          System.out.println("_____________________________________");
  10.          System.out.println("");
  11.       }
  12.       public static void createBoard(String[][] board)
  13.       {
  14.          for(int r = 0; r < board.length; r++)
  15.          {
  16.             for(int c = 0; c < board[0].length; c++)
  17.             {
  18.                board[r][c] = "~";
  19.             }
  20.          }
  21.       }
  22.       public static void showBoard(String[][] board)
  23.       {
  24.          breakln();
  25.          for(int r = 0; r < board.length; r++)
  26.          {
  27.             if(DEBUG == true)
  28.             {
  29.                for(int c = 0; c < board[0].length; c++)
  30.                {
  31.                   System.out.print(" "+board[r][c]);
  32.                }
  33.                System.out.println("");
  34.             }
  35.             else
  36.             {
  37.                for(int c = 0; c < board[0].length; c++)
  38.                {
  39.                   if(board[r][c].equals("S"))
  40.                   {
  41.                      System.out.print(" "+"~");
  42.                   }
  43.                   else
  44.                   {
  45.                      System.out.print(" "+board[r][c]);
  46.                   }
  47.                }
  48.                System.out.println("");
  49.             }
  50.          }
  51.          breakln();
  52.       }
  53.       public static void createShip(String[][] board,int size)
  54.       {
  55.          if(Math.random() < 0.5)
  56.          {
  57.             int col = (int)(Math.random()*5);
  58.             int row = (int)(Math.random()*7);
  59.             for(int i = 0; i<size; i++)
  60.             {
  61.                board[row][col+i] = "S";
  62.             }
  63.          }
  64.          else
  65.          {
  66.             int col = (int)(Math.random()*7);
  67.             int row = (int)(Math.random()*5);
  68.             for(int i = 0; i<size; i++)
  69.             {
  70.                board[row+i][col] = "S";
  71.             }
  72.          }
  73.       }
  74.       public static int userFire(String[][] board, int hits, int torps)
  75.       {
  76.          Scanner input = new Scanner(System.in);
  77.          int row,col;
  78.          System.out.println("You have: "+ torps +" torpedos left!");
  79.          System.out.println("Select a row to fire in: ");
  80.          row = input.nextInt();
  81.          while(row > 8 || row < 1) // Error checking for row
  82.          {
  83.             System.out.println("Enter a valid row (1 -> 8)");
  84.             row = input.nextInt();
  85.          }
  86.          System.out.println("Select a column to fire in: ");
  87.          col = input.nextInt();
  88.          while(col > 8 || col < 1) // Error checking for column
  89.          {
  90.             System.out.println("Enter a valid col (1 -> 8)");
  91.             col = input.nextInt();
  92.          }
  93.          if(board[row-1][col-1].equals("S"))
  94.          {
  95.             hits ++;
  96.             System.out.println("~~~~~~~ HIT ~~~~~~~");
  97.             board[row-1][col-1] = "!";
  98.          }
  99.          else
  100.          {
  101.             System.out.println("~~~~~~~ MISS ~~~~~~~");
  102.             board[row-1][col-1] = "M";
  103.          }
  104.          return hits;
  105.       }
  106.       public static void finall(int hits, int torps)
  107.       {
  108.          if(hits < 4)
  109.             System.out.println("Sorry, but you lost because you didn't sink the ship.");
  110.          if(torps < 1)
  111.             System.out.println("You have lost all your torpedos");
  112.          else
  113.             if(hits >= 4)
  114.             {
  115.                System.out.println("You have beaten the game battleship, Thanks for playing!");
  116.             }
  117.          System.out.println("Good game, well played!");
  118.       }
  119.       public static void main(String[] arg)
  120.       {
  121.          String[][] board = new String[8][8];
  122.          createBoard(board);
  123.          createShip(board, 4);
  124.          int torps = 15;
  125.          int hits = 0;
  126.          /// Starting real stuff
  127.          while(torps > 0 && hits < 4)
  128.          {
  129.             showBoard(board);
  130.             hits = userFire(board, hits, torps);
  131.             torps --;
  132.          }
  133.          finall(hits, torps);
  134.       }
Advertisement
Add Comment
Please, Sign In to add comment