Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.21 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Scanner;
  3.  
  4. public class SeaBattle2 {
  5. private static final int FIELD_SIZE = 15;
  6. private static final int SHIP_SIZE = 3;
  7. private static final int SHIPS_QUANTITY = 3;
  8. private static char[] gameField = new char[FIELD_SIZE];
  9. private static int[] shipsOnField = new int[FIELD_SIZE];
  10. private static int shipFieldsCount = SHIP_SIZE * SHIPS_QUANTITY;
  11. private static Scanner scanner = new Scanner(System.in);
  12.  
  13. public static void main(String[] args) {
  14. startGame();
  15. }
  16.  
  17. private static void startGame() {
  18. initialize();
  19. System.out.println("Игра Морской бой");
  20. do {
  21. showGameField();
  22. int index;
  23. while (true) {
  24. System.out.printf("Задайте ячейку от 0 до %d:%n", FIELD_SIZE - 1);
  25. if (scanner.hasNextInt()) {
  26. index = scanner.nextInt();
  27. scanner.nextLine();
  28. break;
  29. } else {
  30. scanner.nextLine();
  31. }
  32. }
  33. if (index < 0 || index > FIELD_SIZE - 1) {
  34. continue;
  35. }
  36. switch (gameField[index]) {
  37. case 'X':
  38. gameField[index] = '#';
  39. shipFieldsCount--;
  40. if (isKilled(index)) {
  41. System.out.println("Убит!");
  42. } else {
  43. System.out.println("Ранен!");
  44. }
  45. break;
  46. case '*':
  47. case '#':
  48. System.out.println("Уже стреляли.");
  49. break;
  50. default:
  51. System.out.println("Промах.");
  52. gameField[index] = '*';
  53. }
  54. } while (shipFieldsCount > 0);
  55. System.out.println("Победа! Все корабли потоплены.");
  56. System.out.println();
  57. showGameField();
  58. }
  59.  
  60. private static void initialize() {
  61. Arrays.fill(gameField, '_');
  62.  
  63. int leftBound = 0;
  64. int rightBound;
  65. int startIndex;
  66. int remainingQuantity = SHIPS_QUANTITY;
  67. while (remainingQuantity > 0) {
  68. rightBound = FIELD_SIZE - (SHIP_SIZE + 1) * (remainingQuantity - 1) - SHIP_SIZE;
  69. startIndex = getRandom(leftBound, rightBound + 1);
  70. for (int i = 0; i < SHIP_SIZE; i++) {
  71. gameField[startIndex + i] = 'X';
  72. shipsOnField[startIndex + i] = remainingQuantity;
  73. }
  74. leftBound = startIndex + SHIP_SIZE + 1;
  75. remainingQuantity--;
  76. }
  77. }
  78.  
  79. private static void showGameField() {
  80. System.out.println(Arrays.toString(gameField));
  81. System.out.println();
  82. }
  83.  
  84. private static int getRandom(int from, int to) {
  85. return (int) (from + Math.random() * (to - from));
  86. }
  87.  
  88. private static boolean isKilled(int index) {
  89. int shipIndex = shipsOnField[index];
  90. shipsOnField[index] = -1;
  91. for (int value : shipsOnField) {
  92. if (value == shipIndex) {
  93. return false;
  94. }
  95. }
  96. return true;
  97. }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement