Advertisement
Guest User

dumb

a guest
Aug 3rd, 2015
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.65 KB | None | 0 0
  1. //Program 4 -- Sunk!
  2. //Entered by Lexie Brown
  3.  
  4.  
  5. import java.util.Scanner;
  6. import java.io.*;
  7.  
  8. public class Battleship
  9. {
  10.  
  11.     public static void main(String[] args) throws IOException
  12.     {
  13.         Scanner kb = new Scanner(System.in);
  14.        
  15.         int attackCounter = 1;  //counter for number of moves
  16.         boolean gameOver = false;   //variable to determine whether the game is over or not
  17.                                     //game over = Q, >50 shots, or a win state
  18.         String line;
  19.        
  20.         //declare and open the input file:
  21.         FileReader reader = new FileReader("input.data");
  22.         BufferedReader inFile = new BufferedReader(reader);
  23.        
  24.         //2D array of char to hold the playing board.
  25.         char[][] board = new char[10][10];
  26.        
  27.         //Second board to print out.
  28.         char [][] showBoard = new char[10][10];
  29.    
  30.         //For loops to fill showBoard with dots.
  31.         for (int row = 0; row < 10; row++)
  32.             for (int col = 0; col < 10; col++)
  33.                 showBoard[row][col] = '.';
  34.        
  35.        
  36.         //Read the data into the array.
  37.         for (int row= 0; row < 10; row++)
  38.         {
  39.            
  40.             //Read line of data.
  41.             line = inFile.readLine();
  42.            
  43.             for (int col = 0; col < 10; col++)
  44.             {
  45.                 //Assign data for each x,y.
  46.                 char pointR = line.charAt(col);
  47.                 board[row][col] = pointR;
  48.                
  49.                
  50.                 //Print array if needed:
  51.                 System.out.print(board[row][col]); 
  52.                
  53.             }   //end col for loop
  54.            
  55.             //Extra formatting for printing.
  56.             System.out.print("\n");
  57.         }   //end row for loop
  58.        
  59.        
  60.        
  61.         inFile.close();
  62.        
  63.         //Opening message.
  64.         System.out.println("\t\t\tBATTLESHIP."
  65.                             + "\nFOR EACH MOVE, ENTER ONE LETTER, A-J; ENTER ONE NUMBER, 0-9.");
  66.        
  67.        
  68.         //Prompt for and get user input.
  69.         String cmd = getCommand(kb, attackCounter);
  70.        
  71.        
  72.         while (!cmd.equals("Q") && (!gameOver))
  73.         {
  74.             if (cmd.equals("S"))
  75.                 showBoard(showBoard);
  76.             else
  77.             {
  78.                 System.out.println("\n" + processShot(cmd, board));
  79.                 attackCounter++;
  80.             }
  81.  
  82.             if (attackCounter >= 50)
  83.                 gameOver = true;
  84.            
  85.            
  86.             //Next user input.
  87.             cmd = getCommand(kb, attackCounter);
  88.         }
  89.        
  90.         if (gameOver)
  91.             System.out.print("\n GAME OVER. You have taken the max amount of attacks.");
  92.        
  93.         //Print board when requested.
  94.        
  95.        
  96.     }   //end method main
  97.    
  98.    
  99.     //method to prompt the player for a command or a cell location
  100.     public static String getCommand(Scanner kb, int counter)
  101.     {
  102.  
  103.         //Prompts the user for either a command or cell location.
  104.         //Prompt includes the number of attacks and which commands can be called.
  105.         System.out.print("Move #" + counter + ". Enter either a letter-number pair for attack, Q to quit, or S to show the current board: ");
  106.         String cmd = kb.nextLine();
  107.         String cmdUp = cmd.toUpperCase();
  108.        
  109.         //System.out.println(cmdUp);
  110.        
  111.        
  112.         return cmdUp;
  113.     }   //end method getCommand
  114.    
  115.    
  116.     //Method to process the coordinates that the user entered.
  117.     public static String processShot(String cmd, char[][] board)
  118.     {
  119.         String returnMsg = ""; //'Hit!' or 'Miss.'
  120.         int rowValue = cmd.charAt(0);
  121.         int colValue = cmd.charAt(1);
  122.        
  123.         if (!cmd.equals("S"))
  124.         {
  125.        
  126.         switch (rowValue)
  127.         {
  128.         case 'A': rowValue = 0;
  129.         break;
  130.         case 'B': rowValue = 1;
  131.         break;
  132.         case 'C': rowValue = 2;
  133.         break;
  134.         case 'D': rowValue = 3;
  135.         break;
  136.         case 'E': rowValue = 4;
  137.         break;
  138.         case 'F': rowValue = 5;
  139.         break;
  140.         case 'G': rowValue = 6;
  141.         break;
  142.         case 'H': rowValue = 7;
  143.         break;
  144.         case 'I': rowValue = 8;
  145.         break;
  146.         case 'J': rowValue = 9;
  147.         break;
  148.         }
  149.        
  150.        
  151.         switch (colValue)
  152.         {
  153.         case '0': colValue = 0;
  154.         break;
  155.         case '1': colValue = 1;
  156.         break;
  157.         case '2': colValue = 2;
  158.         break;
  159.         case '3': colValue = 3;
  160.         break;
  161.         case '4': colValue = 4;
  162.         break;
  163.         case '5': colValue = 5;
  164.         break;
  165.         case '6': colValue = 6;
  166.         break;
  167.         case '7': colValue = 7;
  168.         break;
  169.         case '8': colValue = 8;
  170.         break;
  171.         case '9': colValue = 9;
  172.         break;
  173.         }
  174.        
  175.        
  176.         //System.out.println("Row: " + rowValue + " Col: " + colValue);
  177.        
  178.         /*for (int i = 0; i < board.length; i++)
  179.         {
  180.             for (int j = 0; j < 10; j++)
  181.                 System.out.print(board[i][j]);
  182.             System.out.println();
  183.         }*/
  184.    
  185.        
  186.         if (board[rowValue][colValue] != '.')
  187.         {
  188.             returnMsg = "Hit!\n";
  189.             board[rowValue][colValue] = 'X';
  190.         }
  191.         else
  192.             returnMsg = "Miss!\n";
  193.             board[rowValue][colValue] = 'O';
  194.         }
  195.        
  196.         else
  197.             if (cmd.equals("S"))
  198.             returnMsg = "S";
  199.  
  200.         return returnMsg;
  201.     }   //end method processShot
  202.    
  203.    
  204.     //Method to display the contents of the board, indicating which are hits and misses.
  205.     public static void showBoard(char[][] board)
  206.     {
  207.         for (int row = 0; row < 10; row++)
  208.         {
  209.             for (int col = 0; col < 10; col++)
  210.                 System.out.print(board[row][col]);
  211.            
  212.             System.out.println();
  213.         }
  214.        
  215.    
  216.     }
  217.  
  218. }   //end class Battleship
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement