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