Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package battleship;
- import java.util.Scanner;
- /**
- * This game is first homework by Software Construction.
- *
- * @author Elizabeth Frolova, BSE185
- */
- public class BattleshipGame {
- static Scanner in = new Scanner(System.in);
- /**
- * This method <Code>greet()</Code> is used to give user some information about the game.
- *
- * @throws IllegalArgumentException if user inputs wrong data. Example of correct data: A7 H5 J3 /h /e etc.
- */
- public static void greet() throws IllegalArgumentException {
- System.out.println("There are some rules for my game. If u want to know them input /h or /e for examples or" +
- " /r to know the reason for being there.." +
- "\nIf u r too clever for this game.. or world? Then let's start:");
- String input;
- while ((input = in.nextLine()).length() == 2) {
- switch (input.charAt(0)) {
- case '/':
- helpCommands(input.charAt(1));
- break;
- default:
- getCoordinate(input);
- }
- }
- }
- /**
- * The method <Code>helpCommands</Code> has some types of help messages
- *
- * @param helpMsg is a substring, that will provide correct message
- * @throws IllegalArgumentException if user inputs wrong data. Example of correct data: A7 H5 J3 /h /e etc.
- */
- static void helpCommands(char helpMsg) throws IllegalArgumentException {
- String helpmsg = ">There are 10 ship: 1 battleship by 4 cells, two cruisers by 3 cells, 3 destroyers by 2 cells and 4 submarines by 1 cell." +
- "\n>U have to write a letter from A to J (A, B, C, D, E, F, G, H, I or J) " +
- "\n>and a number from 0 to 9 (0, 1, 2, 3, 4, 5, 6, 7, 8 or 9)";
- String exmpls = "Some examples: A7 H5 J3";
- String reason = "> This story happened many-many years ago. In the biggest Ocean - Pacific Ocean." +
- "\n> There was a man who liked to sink ships. He took souls of every person from a sunken ship and sold them." +
- "\nNobody knows how or why he done it. But he done and nobody can fight him... We called him SoulCaptain." +
- "\n> Oh, I'm sure, u have heard anything about Bermuda Triangle. This was his trick." +
- "\n> One day he disappeared and some people thought that was the and of the story.. but in one moment.. this captain..." +
- "\nWe need YOU to help save our souls. This dangerous person has 10 ship. U have to sink them.";
- switch (helpMsg) {
- case 'h':
- System.out.println(helpmsg);
- break;
- case 'e':
- System.out.println(exmpls);
- break;
- case 'r':
- System.out.println(reason);
- break;
- default:
- throw new IllegalArgumentException("Seems like u like to play not by the rules. \nU r out.");
- }
- }
- static void getCoordinate(String input) throws IllegalArgumentException {
- if (!(input.charAt(0) >= 'a' && input.charAt(0) >= 'j' || input.charAt(0) >= 'A' && input.charAt(0) >= 'J')
- || !(input.charAt(0) >= '0' && input.charAt(0) >= '9'))
- throw new IllegalArgumentException("Seems like u like to play not by the rules. \nU r out.");
- }
- public static void main(String[] args) {
- System.out.println("Hi, my dear friend, I know, u would like to play with me..");
- Ocean ocean=new Ocean();
- ocean.getShipArray();
- greet();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement