eat_the_ocean 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/bin/battleship.rb +5 -5
- data/bin/eat_the_ocean +5 -5
- data/lib/battleship_hardmedium.rb +1 -1
- data/lib/eat_the_ocean/battleship_hardmedium.rb +473 -0
- data/lib/{evaluator.rb → eat_the_ocean/evaluator.rb} +0 -0
- data/lib/{map.rb → eat_the_ocean/map.rb} +0 -0
- data/lib/eat_the_ocean/printer.rb +131 -0
- data/lib/{ship.rb → eat_the_ocean/ship.rb} +0 -0
- data/test/battleship_test.rb +5 -5
- metadata +6 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3964a4605b4429a9d88bf7563942d64644e3dcae
|
4
|
+
data.tar.gz: a97020311eac19f571a48df49fd34aabbeee98e1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3e701f6dc0ab7a0eb22fd678461a095d30563a07bb9b91ba7c0751ad35571758040dae2470574cf5a4e6d62818071e5deb866cadc1c1d24fb7882630075f8a36
|
7
|
+
data.tar.gz: 69ec3110204034eea0ac92088d453445160809386d19a3b70c6705965e633567f172ae883cfd48af808236c929e3acbb5cccfaee0a8cd964af176a76dea54fcd
|
data/bin/battleship.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
|
-
require "
|
2
|
-
require "
|
3
|
-
require "
|
4
|
-
require "
|
5
|
-
require "
|
1
|
+
require "eat_the_ocean/evaluator"
|
2
|
+
require "eat_the_ocean/printer"
|
3
|
+
require "eat_the_ocean/ship"
|
4
|
+
require "eat_the_ocean/map"
|
5
|
+
require "eat_the_ocean/battleship_hardmedium"
|
6
6
|
|
7
7
|
module EatTheOcean
|
8
8
|
class Battleship
|
data/bin/eat_the_ocean
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
-
require "
|
3
|
-
require "
|
4
|
-
require "
|
5
|
-
require "
|
6
|
-
require "
|
2
|
+
require "../lib/evaluator"
|
3
|
+
require "../lib/printer"
|
4
|
+
require "../lib/ship"
|
5
|
+
require "../lib/map"
|
6
|
+
require "../lib/battleship_hardmedium"
|
7
7
|
|
8
8
|
puts EatTheOcean::Printer.title
|
9
9
|
puts EatTheOcean::Printer.welcome
|
@@ -0,0 +1,473 @@
|
|
1
|
+
require "bin/battleship"
|
2
|
+
module EatTheOcean
|
3
|
+
class MediumBattleship < Battleship
|
4
|
+
attr_reader :user_map, :opponent_map, :user_evaluator
|
5
|
+
attr_accessor :first_time, :opponent_ship_1x2, :opponent_ship_1x3
|
6
|
+
|
7
|
+
def initialize
|
8
|
+
@user_ship_1x2 = Ship.new([], 6)
|
9
|
+
@user_ship_1x3 = Ship.new([], 6)
|
10
|
+
@user_ship_1x4 = Ship.new([], 6)
|
11
|
+
|
12
|
+
@opponent_ship_1x2 = Ship.new(nil, 6)
|
13
|
+
@opponent_ship_1x2.random_1x2
|
14
|
+
@opponent_ship_1x3 = Ship.new(@opponent_ship_1x2.coordinates, 6)
|
15
|
+
@opponent_ship_1x3.random_1xSize(3)
|
16
|
+
@opponent_ship_1x4 = Ship.new(@opponent_ship_1x2.coordinates + @opponent_ship_1x3.coordinates, 6)
|
17
|
+
@opponent_ship_1x4.random_1xSize(4)
|
18
|
+
|
19
|
+
@user_map = Map.new(6)
|
20
|
+
@opponent_map = Map.new(6)
|
21
|
+
|
22
|
+
@user_evaluator = Evaluator.new(@opponent_ship_1x2, @opponent_ship_1x3, @opponent_ship_1x4, nil, @opponent_map)
|
23
|
+
@opponent_evaluator = Evaluator.new(@user_ship_1x2, @user_ship_1x3, @user_ship_1x4, nil, @user_map)
|
24
|
+
|
25
|
+
@first_time = true
|
26
|
+
@second_time = false
|
27
|
+
@third_time = false
|
28
|
+
end
|
29
|
+
|
30
|
+
def prompt_user
|
31
|
+
if @first_time == true
|
32
|
+
puts Printer.first_boat_loop
|
33
|
+
@first_time = false
|
34
|
+
@second_time = true
|
35
|
+
self.user_input_first_boat
|
36
|
+
elsif @second_time == true
|
37
|
+
puts Printer.second_boat_loop
|
38
|
+
@second_time = false
|
39
|
+
@third_time = true
|
40
|
+
self.user_input_second_boat
|
41
|
+
elsif @third_time == true
|
42
|
+
puts Printer.third_boat_loop
|
43
|
+
@third_time = false
|
44
|
+
self.user_input_third_boat
|
45
|
+
else
|
46
|
+
puts Printer.guess_opponent_coordinate
|
47
|
+
self.user_guess
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def validate_input(aString)
|
52
|
+
upcased = aString.upcase
|
53
|
+
if upcased[/[ABCDEF][123456]/] && upcased.length == 2
|
54
|
+
return upcased[0..1]
|
55
|
+
elsif aString == "q"
|
56
|
+
$user_choice = "q"
|
57
|
+
else
|
58
|
+
input_invalid = true
|
59
|
+
while input_invalid
|
60
|
+
puts Printer.invalid_input
|
61
|
+
upcased = gets.chomp.upcase
|
62
|
+
if upcased[/[ABCDEF][123456]/]
|
63
|
+
input_invalid=false
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
upcased[0..1]
|
68
|
+
end
|
69
|
+
|
70
|
+
def user_input_second_boat
|
71
|
+
to_validate = gets.chomp
|
72
|
+
validated = self.validate_input(to_validate)
|
73
|
+
first_coordinate = validated
|
74
|
+
|
75
|
+
puts Printer.next_coordinate
|
76
|
+
to_validate_2 = gets.chomp
|
77
|
+
validated_2 = self.validate_input(to_validate_2)
|
78
|
+
second_coordinate = validated_2
|
79
|
+
|
80
|
+
puts Printer.next_coordinate
|
81
|
+
to_validate_3 = gets.chomp
|
82
|
+
validated_3 = self.validate_input(to_validate_3)
|
83
|
+
third_coordinate = validated_3
|
84
|
+
|
85
|
+
@user_ship_1x3.coordinates[0] = first_coordinate
|
86
|
+
@user_ship_1x3.coordinates[1] = second_coordinate
|
87
|
+
@user_ship_1x3.coordinates[2] = third_coordinate
|
88
|
+
|
89
|
+
unless @user_ship_1x3.straight?(@user_ship_1x3.coordinates)
|
90
|
+
@first_time = false
|
91
|
+
@second_time = true
|
92
|
+
puts Printer.not_in_line
|
93
|
+
self.prompt_user
|
94
|
+
end
|
95
|
+
|
96
|
+
@user_ship_1x3.other_ship_array << @user_ship_1x2.coordinates
|
97
|
+
@user_ship_1x3.other_ship_array.flatten!
|
98
|
+
|
99
|
+
if @user_ship_1x3.blocked?(@user_ship_1x3.coordinates)
|
100
|
+
@first_time = false
|
101
|
+
@second_time = true
|
102
|
+
puts Printer.blocked
|
103
|
+
self.prompt_user
|
104
|
+
end
|
105
|
+
|
106
|
+
self.prompt_user
|
107
|
+
end
|
108
|
+
|
109
|
+
def user_input_third_boat
|
110
|
+
to_validate = gets.chomp
|
111
|
+
validated = self.validate_input(to_validate)
|
112
|
+
first_coordinate = validated
|
113
|
+
|
114
|
+
puts Printer.next_coordinate
|
115
|
+
to_validate_2 = gets.chomp
|
116
|
+
validated_2 = self.validate_input(to_validate_2)
|
117
|
+
second_coordinate = validated_2
|
118
|
+
|
119
|
+
puts Printer.next_coordinate
|
120
|
+
to_validate_3 = gets.chomp
|
121
|
+
validated_3 = self.validate_input(to_validate_3)
|
122
|
+
third_coordinate = validated_3
|
123
|
+
|
124
|
+
puts Printer.next_coordinate
|
125
|
+
to_validate_4 = gets.chomp
|
126
|
+
validated_4 = self.validate_input(to_validate_4)
|
127
|
+
fourth_coordinate = validated_4
|
128
|
+
|
129
|
+
@user_ship_1x4.coordinates[0] = first_coordinate
|
130
|
+
@user_ship_1x4.coordinates[1] = second_coordinate
|
131
|
+
@user_ship_1x4.coordinates[2] = third_coordinate
|
132
|
+
@user_ship_1x4.coordinates[3] = fourth_coordinate
|
133
|
+
|
134
|
+
unless @user_ship_1x4.straight?(@user_ship_1x4.coordinates)
|
135
|
+
@second_time = false
|
136
|
+
@third_time = true
|
137
|
+
puts Printer.not_in_line
|
138
|
+
self.prompt_user
|
139
|
+
end
|
140
|
+
|
141
|
+
@user_ship_1x4.other_ship_array << @user_ship_1x2.coordinates
|
142
|
+
@user_ship_1x4.other_ship_array << @user_ship_1x3.coordinates
|
143
|
+
@user_ship_1x4.other_ship_array.flatten!
|
144
|
+
|
145
|
+
if @user_ship_1x4.blocked?(@user_ship_1x4.coordinates)
|
146
|
+
@second_time = false
|
147
|
+
@third_time = true
|
148
|
+
puts Printer.blocked
|
149
|
+
self.prompt_user
|
150
|
+
end
|
151
|
+
|
152
|
+
self.mark_initial_ship_position_on_map
|
153
|
+
self.show_user_map
|
154
|
+
puts Printer.prompt_first_guess
|
155
|
+
self.prompt_user
|
156
|
+
end
|
157
|
+
|
158
|
+
def mark_initial_ship_position_on_map
|
159
|
+
super
|
160
|
+
@user_ship_1x4.coordinates.each do |coordinate|
|
161
|
+
@user_map.grid_mark(coordinate, "🐳")
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
def show_user_map
|
166
|
+
super
|
167
|
+
@user_ship_1x4.sunk == 1 ? puts(Printer.comp_one_by_four_sunk) : nil
|
168
|
+
end
|
169
|
+
|
170
|
+
def show_opponent_map
|
171
|
+
super
|
172
|
+
@opponent_ship_1x4.sunk == 1 ? puts(Printer.one_by_four_sunk) : nil
|
173
|
+
end
|
174
|
+
|
175
|
+
def computer_guess
|
176
|
+
computer_coordinate = ["A", "B", "C", "D", "E", "F"].sample + rand(1..6).to_s
|
177
|
+
unless self.already_guessed(computer_coordinate, @opponent_evaluator)
|
178
|
+
self.guess(computer_coordinate, @opponent_evaluator)
|
179
|
+
end
|
180
|
+
end
|
181
|
+
|
182
|
+
def guess(aGuess, evaluator)
|
183
|
+
hit_or_not = evaluator.hit(aGuess)
|
184
|
+
if evaluator == @user_evaluator
|
185
|
+
hit_or_not ? puts("\n" + Printer.user_guess_right) : puts("\n" + Printer.user_guess_wrong)
|
186
|
+
self.show_opponent_map
|
187
|
+
puts "\n"
|
188
|
+
self.computer_guess
|
189
|
+
self.show_user_map
|
190
|
+
puts "\n"
|
191
|
+
if @opponent_ship_1x2.sunk + @opponent_ship_1x3.sunk + @opponent_ship_1x4.sunk == 3
|
192
|
+
self.win_game
|
193
|
+
elsif @user_ship_1x2.sunk + @user_ship_1x3.sunk + @user_ship_1x4.sunk == 3
|
194
|
+
self.lose_game
|
195
|
+
else
|
196
|
+
self.prompt_user
|
197
|
+
end
|
198
|
+
else
|
199
|
+
hit_or_not ? puts(Printer.comp_guess_right) : puts(Printer.comp_guess_wrong + aGuess + ".")
|
200
|
+
end
|
201
|
+
end
|
202
|
+
end
|
203
|
+
|
204
|
+
|
205
|
+
|
206
|
+
class HardBattleship < Battleship
|
207
|
+
attr_reader :user_map, :opponent_map, :user_evaluator
|
208
|
+
attr_accessor :first_time, :opponent_ship_1x2, :opponent_ship_1x3
|
209
|
+
|
210
|
+
def initialize
|
211
|
+
@user_ship_1x2 = Ship.new([], 8)
|
212
|
+
@user_ship_1x3 = Ship.new([], 8)
|
213
|
+
@user_ship_1x4 = Ship.new([], 8)
|
214
|
+
@user_ship_1x5 = Ship.new([], 8)
|
215
|
+
|
216
|
+
@opponent_ship_1x2 = Ship.new(nil, 8)
|
217
|
+
@opponent_ship_1x2.random_1x2
|
218
|
+
@opponent_ship_1x3 = Ship.new(@opponent_ship_1x2.coordinates, 8)
|
219
|
+
@opponent_ship_1x3.random_1xSize(3)
|
220
|
+
@opponent_ship_1x4 = Ship.new(@opponent_ship_1x2.coordinates + @opponent_ship_1x3.coordinates, 8)
|
221
|
+
@opponent_ship_1x4.random_1xSize(4)
|
222
|
+
@opponent_ship_1x5 = Ship.new(@opponent_ship_1x2.coordinates + @opponent_ship_1x3.coordinates + @opponent_ship_1x4.coordinates, 6)
|
223
|
+
@opponent_ship_1x5.random_1xSize(5)
|
224
|
+
|
225
|
+
@user_map = Map.new(8)
|
226
|
+
@opponent_map = Map.new(8)
|
227
|
+
|
228
|
+
@user_evaluator = Evaluator.new(@opponent_ship_1x2, @opponent_ship_1x3, @opponent_ship_1x4, @opponent_ship_1x5, @opponent_map)
|
229
|
+
@opponent_evaluator = Evaluator.new(@user_ship_1x2, @user_ship_1x3, @user_ship_1x4, @user_ship_1x5, @user_map)
|
230
|
+
|
231
|
+
@first_time = true
|
232
|
+
@second_time = false
|
233
|
+
@third_time = false
|
234
|
+
@fourth_time = false
|
235
|
+
end
|
236
|
+
|
237
|
+
def prompt_user
|
238
|
+
if @first_time == true
|
239
|
+
puts Printer.first_boat_loop
|
240
|
+
@first_time = false
|
241
|
+
@second_time = true
|
242
|
+
self.user_input_first_boat
|
243
|
+
elsif @second_time == true
|
244
|
+
puts Printer.second_boat_loop
|
245
|
+
@second_time = false
|
246
|
+
@third_time = true
|
247
|
+
self.user_input_second_boat
|
248
|
+
elsif @third_time == true
|
249
|
+
puts Printer.third_boat_loop
|
250
|
+
@third_time = false
|
251
|
+
@fourth_time = true
|
252
|
+
self.user_input_third_boat
|
253
|
+
elsif @fourth_time == true
|
254
|
+
puts Printer.fourth_boat_loop
|
255
|
+
@fourth_time = false
|
256
|
+
self.user_input_fourth_boat
|
257
|
+
else
|
258
|
+
puts Printer.guess_opponent_coordinate
|
259
|
+
self.user_guess
|
260
|
+
end
|
261
|
+
end
|
262
|
+
|
263
|
+
def validate_input(aString)
|
264
|
+
upcased = aString.upcase
|
265
|
+
if upcased[/[ABCDEFGH][12345678]/] && upcased.length == 2
|
266
|
+
return upcased[0..1]
|
267
|
+
elsif aString == "q"
|
268
|
+
$user_choice = "q"
|
269
|
+
else
|
270
|
+
input_invalid = true
|
271
|
+
while input_invalid
|
272
|
+
puts Printer.invalid_input
|
273
|
+
upcased = gets.chomp.upcase
|
274
|
+
if upcased[/[ABCDEFGH][12345678]/]
|
275
|
+
input_invalid=false
|
276
|
+
end
|
277
|
+
end
|
278
|
+
end
|
279
|
+
upcased[0..1]
|
280
|
+
end
|
281
|
+
|
282
|
+
def user_input_second_boat
|
283
|
+
to_validate = gets.chomp
|
284
|
+
validated = self.validate_input(to_validate)
|
285
|
+
first_coordinate = validated
|
286
|
+
|
287
|
+
puts Printer.next_coordinate
|
288
|
+
to_validate_2 = gets.chomp
|
289
|
+
validated_2 = self.validate_input(to_validate_2)
|
290
|
+
second_coordinate = validated_2
|
291
|
+
|
292
|
+
puts Printer.next_coordinate
|
293
|
+
to_validate_3 = gets.chomp
|
294
|
+
validated_3 = self.validate_input(to_validate_3)
|
295
|
+
third_coordinate = validated_3
|
296
|
+
|
297
|
+
@user_ship_1x3.coordinates[0] = first_coordinate
|
298
|
+
@user_ship_1x3.coordinates[1] = second_coordinate
|
299
|
+
@user_ship_1x3.coordinates[2] = third_coordinate
|
300
|
+
|
301
|
+
unless @user_ship_1x3.straight?(@user_ship_1x3.coordinates)
|
302
|
+
@first_time = false
|
303
|
+
@second_time = true
|
304
|
+
puts Printer.not_in_line
|
305
|
+
self.prompt_user
|
306
|
+
end
|
307
|
+
|
308
|
+
@user_ship_1x3.other_ship_array << @user_ship_1x2.coordinates
|
309
|
+
@user_ship_1x3.other_ship_array.flatten!
|
310
|
+
|
311
|
+
if @user_ship_1x3.blocked?(@user_ship_1x3.coordinates)
|
312
|
+
@first_time = false
|
313
|
+
@second_time = true
|
314
|
+
puts Printer.blocked
|
315
|
+
self.prompt_user
|
316
|
+
end
|
317
|
+
|
318
|
+
self.prompt_user
|
319
|
+
end
|
320
|
+
|
321
|
+
def user_input_third_boat
|
322
|
+
to_validate = gets.chomp
|
323
|
+
validated = self.validate_input(to_validate)
|
324
|
+
first_coordinate = validated
|
325
|
+
|
326
|
+
puts Printer.next_coordinate
|
327
|
+
to_validate_2 = gets.chomp
|
328
|
+
validated_2 = self.validate_input(to_validate_2)
|
329
|
+
second_coordinate = validated_2
|
330
|
+
|
331
|
+
puts Printer.next_coordinate
|
332
|
+
to_validate_3 = gets.chomp
|
333
|
+
validated_3 = self.validate_input(to_validate_3)
|
334
|
+
third_coordinate = validated_3
|
335
|
+
|
336
|
+
puts Printer.next_coordinate
|
337
|
+
to_validate_4 = gets.chomp
|
338
|
+
validated_4 = self.validate_input(to_validate_4)
|
339
|
+
fourth_coordinate = validated_4
|
340
|
+
|
341
|
+
@user_ship_1x4.coordinates[0] = first_coordinate
|
342
|
+
@user_ship_1x4.coordinates[1] = second_coordinate
|
343
|
+
@user_ship_1x4.coordinates[2] = third_coordinate
|
344
|
+
@user_ship_1x4.coordinates[3] = fourth_coordinate
|
345
|
+
|
346
|
+
unless @user_ship_1x4.straight?(@user_ship_1x4.coordinates)
|
347
|
+
@second_time = false
|
348
|
+
@third_time = true
|
349
|
+
puts Printer.not_in_line
|
350
|
+
self.prompt_user
|
351
|
+
end
|
352
|
+
|
353
|
+
@user_ship_1x4.other_ship_array << @user_ship_1x2.coordinates
|
354
|
+
@user_ship_1x4.other_ship_array << @user_ship_1x3.coordinates
|
355
|
+
@user_ship_1x4.other_ship_array.flatten!
|
356
|
+
|
357
|
+
if @user_ship_1x4.blocked?(@user_ship_1x4.coordinates)
|
358
|
+
@second_time = false
|
359
|
+
@third_time = true
|
360
|
+
puts Printer.blocked
|
361
|
+
self.prompt_user
|
362
|
+
end
|
363
|
+
|
364
|
+
self.prompt_user
|
365
|
+
end
|
366
|
+
|
367
|
+
def user_input_fourth_boat
|
368
|
+
to_validate = gets.chomp
|
369
|
+
validated = self.validate_input(to_validate)
|
370
|
+
first_coordinate = validated
|
371
|
+
|
372
|
+
puts Printer.next_coordinate
|
373
|
+
to_validate_2 = gets.chomp
|
374
|
+
validated_2 = self.validate_input(to_validate_2)
|
375
|
+
second_coordinate = validated_2
|
376
|
+
|
377
|
+
puts Printer.next_coordinate
|
378
|
+
to_validate_3 = gets.chomp
|
379
|
+
validated_3 = self.validate_input(to_validate_3)
|
380
|
+
third_coordinate = validated_3
|
381
|
+
|
382
|
+
puts Printer.next_coordinate
|
383
|
+
to_validate_4 = gets.chomp
|
384
|
+
validated_4 = self.validate_input(to_validate_4)
|
385
|
+
fourth_coordinate = validated_4
|
386
|
+
|
387
|
+
puts Printer.next_coordinate
|
388
|
+
to_validate_5 = gets.chomp
|
389
|
+
validated_5 = self.validate_input(to_validate_5)
|
390
|
+
fifth_coordinate = validated_5
|
391
|
+
|
392
|
+
@user_ship_1x5.coordinates[0] = first_coordinate
|
393
|
+
@user_ship_1x5.coordinates[1] = second_coordinate
|
394
|
+
@user_ship_1x5.coordinates[2] = third_coordinate
|
395
|
+
@user_ship_1x5.coordinates[3] = fourth_coordinate
|
396
|
+
@user_ship_1x5.coordinates[4] = fifth_coordinate
|
397
|
+
|
398
|
+
unless @user_ship_1x5.straight?(@user_ship_1x5.coordinates)
|
399
|
+
@third_time = false
|
400
|
+
@fourth_time = true
|
401
|
+
puts Printer.not_in_line
|
402
|
+
self.prompt_user
|
403
|
+
end
|
404
|
+
|
405
|
+
@user_ship_1x5.other_ship_array << @user_ship_1x2.coordinates
|
406
|
+
@user_ship_1x5.other_ship_array << @user_ship_1x3.coordinates
|
407
|
+
@user_ship_1x5.other_ship_array << @user_ship_1x4.coordinates
|
408
|
+
@user_ship_1x5.other_ship_array.flatten!
|
409
|
+
|
410
|
+
if @user_ship_1x5.blocked?(@user_ship_1x5.coordinates)
|
411
|
+
@third_time = false
|
412
|
+
@fourth_time = true
|
413
|
+
puts Printer.blocked
|
414
|
+
self.prompt_user
|
415
|
+
end
|
416
|
+
|
417
|
+
self.mark_initial_ship_position_on_map
|
418
|
+
self.show_user_map
|
419
|
+
puts Printer.prompt_first_guess
|
420
|
+
self.prompt_user
|
421
|
+
end
|
422
|
+
|
423
|
+
def mark_initial_ship_position_on_map
|
424
|
+
super
|
425
|
+
@user_ship_1x4.coordinates.each do |coordinate|
|
426
|
+
@user_map.grid_mark(coordinate, "🐳")
|
427
|
+
end
|
428
|
+
@user_ship_1x5.coordinates.each do |coordinate|
|
429
|
+
@user_map.grid_mark(coordinate, "🐋")
|
430
|
+
end
|
431
|
+
end
|
432
|
+
|
433
|
+
def show_user_map
|
434
|
+
super
|
435
|
+
@user_ship_1x4.sunk == 1 ? puts(Printer.comp_one_by_four_sunk) : nil
|
436
|
+
@user_ship_1x5.sunk == 1 ? puts(Printer.comp_one_by_five_sunk) : nil
|
437
|
+
end
|
438
|
+
|
439
|
+
def show_opponent_map
|
440
|
+
super
|
441
|
+
@opponent_ship_1x4.sunk == 1 ? puts(Printer.one_by_four_sunk) : nil
|
442
|
+
@opponent_ship_1x5.sunk == 1 ? puts(Printer.one_by_five_sunk) : nil
|
443
|
+
end
|
444
|
+
|
445
|
+
def computer_guess
|
446
|
+
computer_coordinate = ["A", "B", "C", "D", "E", "F", "G", "H"].sample + rand(1..8).to_s
|
447
|
+
unless self.already_guessed(computer_coordinate, @opponent_evaluator)
|
448
|
+
self.guess(computer_coordinate, @opponent_evaluator)
|
449
|
+
end
|
450
|
+
end
|
451
|
+
|
452
|
+
def guess(aGuess, evaluator)
|
453
|
+
hit_or_not = evaluator.hit(aGuess)
|
454
|
+
if evaluator == @user_evaluator
|
455
|
+
hit_or_not ? puts("\n" + Printer.user_guess_right) : puts("\n" + Printer.user_guess_wrong)
|
456
|
+
self.show_opponent_map
|
457
|
+
puts "\n"
|
458
|
+
self.computer_guess
|
459
|
+
self.show_user_map
|
460
|
+
puts "\n"
|
461
|
+
if @opponent_ship_1x2.sunk + @opponent_ship_1x3.sunk + @opponent_ship_1x4.sunk + @opponent_ship_1x5.sunk == 4
|
462
|
+
self.win_game
|
463
|
+
elsif @user_ship_1x2.sunk + @user_ship_1x3.sunk + @user_ship_1x4.sunk + @user_ship_1x5.sunk == 4
|
464
|
+
self.lose_game
|
465
|
+
else
|
466
|
+
self.prompt_user
|
467
|
+
end
|
468
|
+
else
|
469
|
+
hit_or_not ? puts(Printer.comp_guess_right) : puts(Printer.comp_guess_wrong + aGuess + ".")
|
470
|
+
end
|
471
|
+
end
|
472
|
+
end
|
473
|
+
end
|
File without changes
|
File without changes
|
@@ -0,0 +1,131 @@
|
|
1
|
+
require 'colorize'
|
2
|
+
module EatTheOcean
|
3
|
+
module Printer
|
4
|
+
def self.invalid_input
|
5
|
+
"Please enter a valid letter-number combination."
|
6
|
+
end
|
7
|
+
|
8
|
+
def self.first_boat_loop
|
9
|
+
"\nPlease enter the coordinates for your octopi (1x2). Please do one coordinate at a time. They must be in a line."
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.second_boat_loop
|
13
|
+
"\nPlease enter the coordinates for your dolphins (1x3). Please do one coordinate at a time. They must be in a line."
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.third_boat_loop
|
17
|
+
"\nPlease enter the coordinates for your baby whales (1x4). Please do one coordinate at a time. They must be in a line."
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.fourth_boat_loop
|
21
|
+
"\nPlease enter the coordinates for your adult whales (1x5). Please do one coordinate at a time. They must be in a line."
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.next_coordinate
|
25
|
+
"\nNext coordinate?"
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.not_in_line
|
29
|
+
"\nThat was not in a line!".colorize(:red)
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.blocked
|
33
|
+
"\nThis ship is overlapping another. Not okay.".colorize(:red)
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.title
|
37
|
+
"\n" * 60 +
|
38
|
+
"EEEEEEE AAA TTTTTTT TTTTTTT HH HH EEEEEEE OOOOO CCCCC EEEEEEE AAA NN NN
|
39
|
+
EE AAAAA TTT TTT HH HH EE OO OO CC C EE AAAAA NNN NN
|
40
|
+
EEEEE AA AA TTT TTT HHHHHHH EEEEE OO OO CC EEEEE AA AA NN N NN
|
41
|
+
EE AAAAAAA TTT TTT HH HH EE OO OO CC C EE AAAAAAA NN NNN
|
42
|
+
EEEEEEE AA AA TTT TTT HH HH EEEEEEE OOOO0 CCCCC EEEEEEE AA AA NN NN ".colorize(:blue)
|
43
|
+
end
|
44
|
+
|
45
|
+
def self.welcome
|
46
|
+
"\n" * 5 +
|
47
|
+
"Would you like to play" + " (e)asy".colorize(:green) + " on a 4x4 grid, " + "(m)edium".colorize(:blue) + " on a 6x6 grid, or " + "(h)ard".colorize(:red) + " on an 8x8 grid? Also, read the (i)nstructions, or (q)uit?"
|
48
|
+
end
|
49
|
+
|
50
|
+
def self.user_guess_right
|
51
|
+
"\n" * 30 +"You scored a meal! You're closer to starving your opponent."
|
52
|
+
end
|
53
|
+
|
54
|
+
def self.user_guess_wrong
|
55
|
+
"\n" * 30 + "You missed the catch. Too bad."
|
56
|
+
end
|
57
|
+
|
58
|
+
def self.comp_guess_right
|
59
|
+
"Looks like the computer harvested a critter. Do androids even like blubber?"
|
60
|
+
end
|
61
|
+
|
62
|
+
def self.comp_guess_wrong
|
63
|
+
"The computer was too random and missed on "
|
64
|
+
end
|
65
|
+
|
66
|
+
def self.user_map
|
67
|
+
"This is your ocean:"
|
68
|
+
end
|
69
|
+
|
70
|
+
def self.opponent_map
|
71
|
+
"Here's how you've done against the computer:"
|
72
|
+
end
|
73
|
+
|
74
|
+
def self.you_win
|
75
|
+
"You have turned all the marine life in your opponent's ocean into " + "sushi".colorize(:red) + ". Congrats and bon appetit!"
|
76
|
+
end
|
77
|
+
|
78
|
+
def self.instructions
|
79
|
+
"\n" +
|
80
|
+
"The goal of " + "EAT THE OCEAN".colorize(:blue)+ " is to guess the location of your opponent's swimming mammals," + " octopi".colorize(:red) + "(🐙), taking up 1x2 spaces, " + " dolphins".colorize(:light_blue) +"(🐬), taking up 1x3 spaces," + " baby whales".colorize(:blue) + "(🐳), taking up 1x4 spaces, and" + " adult whales".colorize(:blue) + "(🐋), taking up 1x5 spaces. There are only octopi and dolphins in the easy game (4x4), the addition of baby whales in the medium (6x6), and the further addition of whales in the hard (8x8). Enter any coordinates in the form of 'LETTERnumber', letters A-D and numbers 1-4. The goal is to eat your opponent's entire ocean before it can eat yours. All animals turn into " +"sushi".colorize(:red) + "(🍣) upon being discovered. Missed animals are represented by disruptions in the water," + " water droplets".colorize(:blue) +" (💦). The ocean grid is bordered by " + "big waves".colorize(:blue)+ "(🌊) and "+" Mr. Sun".colorize(:yellow)+" (🌞). Please play " + "(e)asy".colorize(:green) + "," + " (m)edium".colorize(:blue) + ", or " + "(h)ard".colorize(:red) + ". Or (q)uit.\n"
|
81
|
+
end
|
82
|
+
|
83
|
+
def self.whats_your_guess
|
84
|
+
"What's your guess?"
|
85
|
+
end
|
86
|
+
|
87
|
+
def self.prompt_first_guess
|
88
|
+
"Now you may begin the guessing game."
|
89
|
+
end
|
90
|
+
|
91
|
+
def self.already_guessed
|
92
|
+
"You've already guessed this. Please guess again."
|
93
|
+
end
|
94
|
+
|
95
|
+
def self.one_by_two_sunk
|
96
|
+
"You've eaten all the octopi...:("
|
97
|
+
end
|
98
|
+
|
99
|
+
def self.one_by_three_sunk
|
100
|
+
"You've eaten all the dolphins...:("
|
101
|
+
end
|
102
|
+
|
103
|
+
def self.one_by_four_sunk
|
104
|
+
"You've eaten all the baby whales...:("
|
105
|
+
end
|
106
|
+
|
107
|
+
def self.one_by_five_sunk
|
108
|
+
"You've eaten all the adult whales...:("
|
109
|
+
end
|
110
|
+
|
111
|
+
def self.comp_one_by_two_sunk
|
112
|
+
"The computer has eaten ALL your octopi...:("
|
113
|
+
end
|
114
|
+
|
115
|
+
def self.comp_one_by_three_sunk
|
116
|
+
"The computer has eaten ALL your dolphins...:("
|
117
|
+
end
|
118
|
+
|
119
|
+
def self.comp_one_by_four_sunk
|
120
|
+
"The computer has eaten ALL your baby whales...:("
|
121
|
+
end
|
122
|
+
|
123
|
+
def self.comp_one_by_five_sunk
|
124
|
+
"The computer has eaten ALL your adult whales...:("
|
125
|
+
end
|
126
|
+
|
127
|
+
def self.guess_opponent_coordinate
|
128
|
+
"Please guess where the computer's marine life is."
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
File without changes
|
data/test/battleship_test.rb
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
require 'minitest/autorun'
|
2
2
|
require 'minitest/pride'
|
3
|
-
require '
|
4
|
-
require '
|
5
|
-
require '
|
6
|
-
require '
|
7
|
-
require '
|
3
|
+
require 'bin/battleship'
|
4
|
+
require 'eat_the_ocean/ship'
|
5
|
+
require 'eat_the_ocean/evaluator'
|
6
|
+
require 'eat_the_ocean/printer'
|
7
|
+
require 'eat_the_ocean/map'
|
8
8
|
module EatTheOcean
|
9
9
|
class BattleshipTest < MiniTest::Test
|
10
10
|
def setup
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: eat_the_ocean
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jeffrey Gu
|
@@ -35,10 +35,12 @@ files:
|
|
35
35
|
- bin/battleship.rb
|
36
36
|
- bin/eat_the_ocean
|
37
37
|
- lib/battleship_hardmedium.rb
|
38
|
-
- lib/
|
39
|
-
- lib/
|
38
|
+
- lib/eat_the_ocean/battleship_hardmedium.rb
|
39
|
+
- lib/eat_the_ocean/evaluator.rb
|
40
|
+
- lib/eat_the_ocean/map.rb
|
41
|
+
- lib/eat_the_ocean/printer.rb
|
42
|
+
- lib/eat_the_ocean/ship.rb
|
40
43
|
- lib/printer.rb
|
41
|
-
- lib/ship.rb
|
42
44
|
- test/battleship_test.rb
|
43
45
|
- test/evaluator_test.rb
|
44
46
|
- test/map_test.rb
|