Advertisement
Guest User

Untitled

a guest
Jan 16th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.66 KB | None | 0 0
  1. import java.util.*;
  2.  
  3.  
  4. public class Battleships {
  5.  
  6. public static void main(String[] args) {
  7.  
  8. introduction();
  9. twoDimensionalArray();
  10. coordinates();
  11. attack();
  12. }
  13.  
  14. public static void introduction() {
  15. System.out.println();
  16. System.out.println(" |__\n" +
  17. " |\\/\n" +
  18. " ---\n" +
  19. " / | [\n" +
  20. " ! | |||\n" +
  21. " _/| _/|-++'\n" +
  22. " + +--| |--|--|_ |-\n" +
  23. " { /|__| |/\\__| |--- |||__/\n" +
  24. " +---------------___[}-_===_.'____ /\\\n" +
  25. " ____`-' ||___-{]_| _[}- | |_[___\\==-- \\/ _\n" +
  26. " __..._____--==/___]_|__|_____________________________[___\\==--____,------' .7\n" +
  27. "| ---- BATTLESHIPS ----- /\n" +
  28. " \\_________________________________________________________________________|");
  29.  
  30. System.out.println();
  31. }
  32.  
  33. public static void twoDimensionalArray() {
  34.  
  35. //creates Array
  36. String[][] Array = new String[10][10];
  37. System.out.println(Array[0][0]);
  38.  
  39. //fills Array with spaces
  40. for (int row = 0; row < Array.length; row++) {
  41. for (int column = 0; column < Array[0].length; column++) {
  42. Array[row][column] = "+";
  43. }
  44. }
  45.  
  46. //prints grid
  47. System.out.println(" 0123456789");
  48.  
  49. for (int row = 0; row < Array.length; row++) {
  50. System.out.print(row + "|");
  51. for (int column = 0; column < Array[0].length; column++) {
  52. System.out.print(Array[row][column]);
  53. }
  54. System.out.println("|" + row);
  55. }
  56.  
  57. System.out.println(" 0123456789");
  58. System.out.println();
  59.  
  60. }
  61.  
  62. public static void coordinates() {
  63. Scanner Scanner = new Scanner(System.in);
  64.  
  65. //creates Array
  66. String[][] Array = new String[10][10];
  67. System.out.println(Array[0][0]);
  68.  
  69. //fills Array with spaces
  70. for (int row = 0; row < Array.length; row++) {
  71. for (int column = 0; column < Array[0].length; column++) {
  72. Array[row][column] = "+";
  73. }
  74. }
  75.  
  76. System.out.println("***DEPLOY YOUR SHIPS!***");
  77. System.out.println("They will appear as '@' symbols.");
  78.  
  79.  
  80. //Coordinate declaration
  81. int x;
  82. int y;
  83.  
  84. for (int j = 0; j < 5; j++) {
  85.  
  86. //Gets coordinates
  87. do {
  88. System.out.print("Please enter X coordinate: ");
  89. x = Scanner.nextInt();
  90. } while (x < 0 || x > 9);
  91.  
  92. do {
  93. System.out.print("Please enter Y coordinate: ");
  94. y = Scanner.nextInt();
  95. } while (y < 0 || y > 9);
  96.  
  97. if (Array[x][y] == "@") {
  98.  
  99. System.out.println("You have entered the same coordinates twice. Please try again.");
  100. j--;
  101. }
  102.  
  103. //Saves coordinates
  104. Array[x][y] = "@";
  105. }
  106.  
  107. System.out.println();
  108. System.out.println("HERE ARE YOUR BATTLESHIPS!");
  109. System.out.println();
  110. System.out.println(" 0123456789");
  111.  
  112. for (int row = 0; row < Array.length; row++) {
  113. System.out.print(row + "|");
  114. for (int column = 0; column < Array[0].length; column++) {
  115. System.out.print(Array[row][column]);
  116. }
  117. System.out.println("|" + row);
  118. }
  119.  
  120. System.out.println(" 0123456789");
  121. System.out.println();
  122.  
  123. System.out.println("DEPLOYING ENEMY SHIPS...");
  124.  
  125. //Adding enemy ships to Array
  126.  
  127.  
  128. for (int k = 0; k < 5; k++) {
  129.  
  130. //Generating random numbers for Array coordinates
  131. Random random = new Random();
  132. int randomNumber1 = random.nextInt(10);
  133. int randomNumber2 = random.nextInt(10);
  134.  
  135. //Checking to make sure they don't overlap players coordinates
  136. if (Array[randomNumber1][randomNumber2] == "@") {
  137. k--;
  138. } else {
  139. Array[randomNumber1][randomNumber2] = "X";
  140. }
  141.  
  142. }
  143.  
  144. //print of complete Array (commented out)
  145. /*System.out.println();
  146. System.out.println(" 0123456789");
  147.  
  148. for (int row = 0; row < Array.length; row++){
  149. System.out.print(row + "|");
  150. for (int column = 0; column < Array[0].length; column++){
  151. System.out.print(Array[row][column]);
  152. }
  153. System.out.println("|" + row);
  154. }
  155.  
  156. System.out.println(" 0123456789");
  157. System.out.println(); */
  158.  
  159. }
  160.  
  161. private static void attack() {
  162.  
  163. Scanner scanner = new Scanner(System.in);
  164.  
  165. System.out.println("ENTER ATTACK COORDINATES!");
  166.  
  167. int x;
  168. int y;
  169.  
  170. //Gets coordinates
  171. do {
  172. System.out.print("Please enter X coordinate: ");
  173. x = Scanner.nextInt();
  174. } while (x < 0 || x > 9);
  175.  
  176. do {
  177. System.out.print("Please enter Y coordinate: ");
  178. y = Scanner.nextInt();
  179. } while (y < 0 || y > 9);
  180.  
  181. if (Array[x][y] == "@") {
  182.  
  183.  
  184. }
  185.  
  186. }
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement