Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.61 KB | None | 0 0
  1. package battleship;
  2.  
  3. import java.util.Scanner;
  4.  
  5. /**
  6. * This game is first homework by Software Construction.
  7. *
  8. * @author Elizabeth Frolova, BSE185
  9. */
  10. public class BattleshipGame {
  11. static Scanner in = new Scanner(System.in);
  12.  
  13. /**
  14. * This method <Code>greet()</Code> is used to give user some information about the game.
  15. *
  16. * @throws IllegalArgumentException if user inputs wrong data. Example of correct data: A7 H5 J3 /h /e etc.
  17. */
  18. public static void greet() throws IllegalArgumentException {
  19. System.out.println("There are some rules for my game. If u want to know them input /h or /e for examples or" +
  20. " /r to know the reason for being there.." +
  21. "\nIf u r too clever for this game.. or world? Then let's start:");
  22.  
  23. String input;
  24. while ((input = in.nextLine()).length() == 2) {
  25. switch (input.charAt(0)) {
  26. case '/':
  27. helpCommands(input.charAt(1));
  28. break;
  29. default:
  30. getCoordinate(input);
  31. }
  32. }
  33.  
  34. }
  35.  
  36. /**
  37. * The method <Code>helpCommands</Code> has some types of help messages
  38. *
  39. * @param helpMsg is a substring, that will provide correct message
  40. * @throws IllegalArgumentException if user inputs wrong data. Example of correct data: A7 H5 J3 /h /e etc.
  41. */
  42. static void helpCommands(char helpMsg) throws IllegalArgumentException {
  43. 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." +
  44. "\n>U have to write a letter from A to J (A, B, C, D, E, F, G, H, I or J) " +
  45. "\n>and a number from 0 to 9 (0, 1, 2, 3, 4, 5, 6, 7, 8 or 9)";
  46. String exmpls = "Some examples: A7 H5 J3";
  47. String reason = "> This story happened many-many years ago. In the biggest Ocean - Pacific Ocean." +
  48. "\n> There was a man who liked to sink ships. He took souls of every person from a sunken ship and sold them." +
  49. "\nNobody knows how or why he done it. But he done and nobody can fight him... We called him SoulCaptain." +
  50. "\n> Oh, I'm sure, u have heard anything about Bermuda Triangle. This was his trick." +
  51. "\n> One day he disappeared and some people thought that was the and of the story.. but in one moment.. this captain..." +
  52. "\nWe need YOU to help save our souls. This dangerous person has 10 ship. U have to sink them.";
  53.  
  54. switch (helpMsg) {
  55. case 'h':
  56. System.out.println(helpmsg);
  57. break;
  58. case 'e':
  59. System.out.println(exmpls);
  60. break;
  61. case 'r':
  62. System.out.println(reason);
  63. break;
  64. default:
  65. throw new IllegalArgumentException("Seems like u like to play not by the rules. \nU r out.");
  66. }
  67. }
  68.  
  69. static void getCoordinate(String input) throws IllegalArgumentException {
  70. if (!(input.charAt(0) >= 'a' && input.charAt(0) >= 'j' || input.charAt(0) >= 'A' && input.charAt(0) >= 'J')
  71. || !(input.charAt(0) >= '0' && input.charAt(0) >= '9'))
  72. throw new IllegalArgumentException("Seems like u like to play not by the rules. \nU r out.");
  73.  
  74. }
  75.  
  76.  
  77. public static void main(String[] args) {
  78. System.out.println("Hi, my dear friend, I know, u would like to play with me..");
  79. Ocean ocean=new Ocean();
  80. ocean.getShipArray();
  81. greet();
  82. }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement