Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Program 4 -- Sunk!
- //Entered by Lexie Brown
- import java.util.Scanner;
- import java.io.*;
- public class Battleship
- {
- public static void main(String[] args) throws IOException
- {
- Scanner kb = new Scanner(System.in);
- int attackCounter = 1; //counter for number of moves
- boolean gameOver = false; //variable to determine whether the game is over or not
- //game over = Q, >50 shots, or a win state
- String line;
- //declare and open the input file:
- FileReader reader = new FileReader("input.data");
- BufferedReader inFile = new BufferedReader(reader);
- //2D array of char to hold the playing board.
- char[][] board = new char[10][10];
- //Second board to print out.
- char [][] showBoard = new char[10][10];
- //For loops to fill showBoard with dots.
- for (int row = 0; row < 10; row++)
- for (int col = 0; col < 10; col++)
- showBoard[row][col] = '.';
- //Read the data into the array.
- for (int row= 0; row < 10; row++)
- {
- //Read line of data.
- line = inFile.readLine();
- for (int col = 0; col < 10; col++)
- {
- //Assign data for each x,y.
- char pointR = line.charAt(col);
- board[row][col] = pointR;
- //Print array if needed:
- System.out.print(board[row][col]);
- } //end col for loop
- //Extra formatting for printing.
- System.out.print("\n");
- } //end row for loop
- inFile.close();
- //Opening message.
- System.out.println("\t\t\tBATTLESHIP."
- + "\nFOR EACH MOVE, ENTER ONE LETTER, A-J; ENTER ONE NUMBER, 0-9.");
- //Prompt for and get user input.
- String cmd = getCommand(kb, attackCounter);
- while (!cmd.equals("Q") && (!gameOver))
- {
- if (cmd.equals("S"))
- showBoard(showBoard);
- else
- {
- System.out.println("\n" + processShot(cmd, board));
- attackCounter++;
- }
- if (attackCounter >= 50)
- gameOver = true;
- //Next user input.
- cmd = getCommand(kb, attackCounter);
- }
- if (gameOver)
- System.out.print("\n GAME OVER. You have taken the max amount of attacks.");
- //Print board when requested.
- } //end method main
- //method to prompt the player for a command or a cell location
- public static String getCommand(Scanner kb, int counter)
- {
- //Prompts the user for either a command or cell location.
- //Prompt includes the number of attacks and which commands can be called.
- System.out.print("Move #" + counter + ". Enter either a letter-number pair for attack, Q to quit, or S to show the current board: ");
- String cmd = kb.nextLine();
- String cmdUp = cmd.toUpperCase();
- //System.out.println(cmdUp);
- return cmdUp;
- } //end method getCommand
- //Method to process the coordinates that the user entered.
- public static String processShot(String cmd, char[][] board)
- {
- String returnMsg = ""; //'Hit!' or 'Miss.'
- int rowValue = cmd.charAt(0);
- int colValue = cmd.charAt(1);
- if (!cmd.equals("S"))
- {
- switch (rowValue)
- {
- case 'A': rowValue = 0;
- break;
- case 'B': rowValue = 1;
- break;
- case 'C': rowValue = 2;
- break;
- case 'D': rowValue = 3;
- break;
- case 'E': rowValue = 4;
- break;
- case 'F': rowValue = 5;
- break;
- case 'G': rowValue = 6;
- break;
- case 'H': rowValue = 7;
- break;
- case 'I': rowValue = 8;
- break;
- case 'J': rowValue = 9;
- break;
- }
- switch (colValue)
- {
- case '0': colValue = 0;
- break;
- case '1': colValue = 1;
- break;
- case '2': colValue = 2;
- break;
- case '3': colValue = 3;
- break;
- case '4': colValue = 4;
- break;
- case '5': colValue = 5;
- break;
- case '6': colValue = 6;
- break;
- case '7': colValue = 7;
- break;
- case '8': colValue = 8;
- break;
- case '9': colValue = 9;
- break;
- }
- //System.out.println("Row: " + rowValue + " Col: " + colValue);
- /*for (int i = 0; i < board.length; i++)
- {
- for (int j = 0; j < 10; j++)
- System.out.print(board[i][j]);
- System.out.println();
- }*/
- if (board[rowValue][colValue] != '.')
- {
- returnMsg = "Hit!\n";
- board[rowValue][colValue] = 'X';
- }
- else
- returnMsg = "Miss!\n";
- board[rowValue][colValue] = 'O';
- }
- else
- if (cmd.equals("S"))
- returnMsg = "S";
- return returnMsg;
- } //end method processShot
- //Method to display the contents of the board, indicating which are hits and misses.
- public static void showBoard(char[][] board)
- {
- for (int row = 0; row < 10; row++)
- {
- for (int col = 0; col < 10; col++)
- System.out.print(board[row][col]);
- System.out.println();
- }
- }
- } //end class Battleship
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement