forked from tiyd-rails-2015-01/battleship
-
Notifications
You must be signed in to change notification settings - Fork 0
/
battleship_test.rb
563 lines (490 loc) · 16.2 KB
/
battleship_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
require 'minitest/autorun'
require 'minitest/pride'
require './battleship'
$mock_inputs = []
def get_user_input
$mock_inputs.shift
end
class BattleshipTest < Minitest::Test
#This is not good practice, AND it forces you to do dumb things like test_00_
# in the code. However, it's easier to follow as you're learning if the
# tests always run in the same order.
def self.test_order
:alpha
end
def test_00_ship_class_exists
assert Ship
end
def test_01_ship_knows_its_length
ship = Ship.new(4)
assert_equal 4, ship.length
end
def test_02_ship_can_be_placed_across
ship = Ship.new(4)
assert ship.place(2, 1, true)
refute ship.covers?(1, 1)
assert ship.covers?(2, 1)
assert ship.covers?(3, 1)
assert ship.covers?(4, 1)
assert ship.covers?(5, 1)
refute ship.covers?(6, 1)
refute ship.covers?(4, 2)
end
def test_03_ship_can_be_placed_down
ship = Ship.new(4)
assert ship.place(2, 2, false)
refute ship.covers?(2, 1)
assert ship.covers?(2, 2)
assert ship.covers?(2, 3)
assert ship.covers?(2, 4)
assert ship.covers?(2, 5)
refute ship.covers?(2, 6)
refute ship.covers?(3, 2)
end
def test_04_ship_cant_be_placed_twice
ship = Ship.new(4)
assert ship.place(2, 1, true)
refute ship.place(3, 2, false)
end
def test_05_ships_know_if_they_overlap
ship1 = Ship.new(4)
ship1.place(2, 1, true)
ship2 = Ship.new(4)
ship2.place(3, 1, true)
ship3 = Ship.new(4)
ship3.place(2, 1, false)
assert ship1.overlaps_with?(ship2)
assert ship1.overlaps_with?(ship3)
refute ship2.overlaps_with?(ship3)
end
def test_06_ships_can_be_fired_at
ship = Ship.new(4)
ship.place(2, 1, true)
assert ship.fire_at(2, 1)
refute ship.fire_at(1, 1)
end
def test_07_ships_can_be_sunk
ship = Ship.new(2)
ship.place(2, 1, true)
refute ship.sunk?
ship.fire_at(2, 1)
refute ship.sunk?
ship.fire_at(3, 1)
assert ship.sunk?
end
def test_08_board_class_exists
assert Board
end
def test_09_empty_board
board = Board.new
refute board.has_ship_on?(1, 1)
refute board.has_ship_on?(10, 7)
end
def test_10_place_ship
board = Board.new
assert board.place_ship(Ship.new(4), 3, 3, true)
refute board.has_ship_on?(2, 3)
assert board.has_ship_on?(3, 3)
assert board.has_ship_on?(4, 3)
assert board.has_ship_on?(6, 3)
refute board.has_ship_on?(7, 3)
refute board.has_ship_on?(5, 4)
end
def test_11_cant_place_overlapping_ships
board = Board.new
assert board.place_ship(Ship.new(4), 3, 3, true)
refute board.place_ship(Ship.new(4), 1, 3, true)
refute board.place_ship(Ship.new(4), 4, 3, true)
refute board.place_ship(Ship.new(4), 4, 2, false)
end
def test_12_misses_on_empty_board
board = Board.new
refute board.fire_at(1, 1)
refute board.fire_at(10, 7)
end
def test_13_hits_on_board
board = Board.new
board.place_ship(Ship.new(4), 3, 3, true)
refute board.fire_at(1, 1)
assert board.fire_at(3, 3)
end
def test_14_repeat_miss
board = Board.new
board.place_ship(Ship.new(4), 3, 3, true)
refute board.fire_at(1, 1)
refute board.fire_at(1, 1)
end
def test_15_repeat_hit
board = Board.new
board.place_ship(Ship.new(4), 3, 3, true)
assert board.fire_at(3, 3)
refute board.fire_at(3, 3)
end
def test_16_misses_outside_board
board = Board.new
refute board.fire_at(18, 1)
refute board.fire_at(10, 26)
end
def test_17_empty_board_can_display_itself
board = Board.new
assert_output(empty_board) do
board.display
end
end
def empty_board
%Q{ 1 2 3 4 5 6 7 8 9 10
-----------------------------------------
A | | | | | | | | | | |
B | | | | | | | | | | |
C | | | | | | | | | | |
D | | | | | | | | | | |
E | | | | | | | | | | |
F | | | | | | | | | | |
G | | | | | | | | | | |
H | | | | | | | | | | |
I | | | | | | | | | | |
J | | | | | | | | | | |
-----------------------------------------
}
end
def test_18_full_board_can_display_itself
board = Board.new
board.place_ship(Ship.new(2), 3, 6, true)
board.place_ship(Ship.new(3), 7, 4, true)
board.place_ship(Ship.new(3), 4, 8, true)
board.place_ship(Ship.new(4), 1, 1, true)
board.place_ship(Ship.new(5), 6, 2, false)
assert_output(full_board) do
board.display
end
end
def full_board
%Q{ 1 2 3 4 5 6 7 8 9 10
-----------------------------------------
A | O | O | O | O | | | | | | |
B | | | | | | O | | | | |
C | | | | | | O | | | | |
D | | | | | | O | O | O | O | |
E | | | | | | O | | | | |
F | | | O | O | | O | | | | |
G | | | | | | | | | | |
H | | | | O | O | O | | | | |
I | | | | | | | | | | |
J | | | | | | | | | | |
-----------------------------------------
}
end
def test_19_used_board_can_display_itself
board = Board.new
board.place_ship(Ship.new(4), 6, 4, true)
board.fire_at(7, 4)
board.fire_at(7, 5)
assert_output(used_board) do
board.display
end
end
def used_board
%Q{ 1 2 3 4 5 6 7 8 9 10
-----------------------------------------
A | | | | | | | | | | |
B | | | | | | | | | | |
C | | | | | | | | | | |
D | | | | | | O | X | O | O | |
E | | | | | | | | | | |
F | | | | | | | | | | |
G | | | | | | | | | | |
H | | | | | | | | | | |
I | | | | | | | | | | |
J | | | | | | | | | | |
-----------------------------------------
}
end
def test_20_entire_board_can_be_sunk
board = Board.new
refute board.sunk?
board.place_ship(Ship.new(2), 6, 4, true)
refute board.sunk?
board.fire_at(6, 4)
refute board.sunk?
board.fire_at(7, 4)
assert board.sunk?
end
def test_21_player_classes_exist
assert Player
assert HumanPlayer
assert ComputerPlayer
end
def test_22_players_have_inheritance
assert_equal Player, HumanPlayer.superclass
assert_equal Player, ComputerPlayer.superclass
end
def test_23_humans_can_be_named
assert_equal "Alice", HumanPlayer.new("Alice").name
end
def test_24_computers_cannot_be_named
assert_raises(ArgumentError) do
ComputerPlayer.new("The Red Queen")
end
end
def test_25_players_have_default_names
assert_equal "Dave", HumanPlayer.new.name
assert_equal "HAL 9000", ComputerPlayer.new.name
end
def test_26_players_have_boards
assert_equal Board, HumanPlayer.new.board.class
assert_equal Board, ComputerPlayer.new.board.class
end
def test_27_computer_player_automatically_places_ships
player = ComputerPlayer.new
assert_output("HAL 9000 has placed his ships.\n") do
assert player.place_ships([2, 3, 3, 4, 5])
end
assert_equal 5, player.ships.length
assert_equal 4, player.ships[3].length
end
def test_28_x_of
board = Board.new
assert_equal 1, board.x_of("A1")
assert_equal 1, board.x_of("G1")
assert_equal 6, board.x_of("D6")
assert_equal 10, board.x_of("D10")
end
def test_29_y_of
board = Board.new
assert_equal 1, board.y_of("A1")
assert_equal 7, board.y_of("G1")
assert_equal 4, board.y_of("D6")
assert_equal 4, board.y_of("D10")
end
def test_30_human_player_is_asked_to_place_ships
player = HumanPlayer.new
$mock_inputs.clear
$mock_inputs << "A1"
$mock_inputs << "Down"
$mock_inputs << "A4"
$mock_inputs << "Down"
assert_output("Dave, where would you like to place a ship of length 2?\nAcross or Down?\n"+
"Dave, where would you like to place a ship of length 5?\nAcross or Down?\n") do
assert player.place_ships([2, 5])
end
assert_equal 2, player.ships.length
assert_equal 5, player.ships[1].length
assert player.board.has_ship_on?(1, 1)
assert player.board.has_ship_on?(4, 1)
assert player.board.has_ship_on?(1, 2)
refute player.board.has_ship_on?(1, 3)
end
def test_31_game_class_exists
assert Game
end
def test_32_games_require_players
assert_raises(ArgumentError) do
Game.new
end
human = HumanPlayer.new("Frank")
computer = ComputerPlayer.new
assert Game.new(human, computer)
end
def test_33_game_welcomes_player
human = HumanPlayer.new("Frank")
computer = ComputerPlayer.new
game = Game.new(human, computer)
assert_output("Welcome, Frank and HAL 9000!\nIt's time to play Battleship.\n") do
game.welcome
end
end
def test_34_game_can_ask_to_set_up_ships
set_up_new_game
assert_equal 5, @human.ships.length
assert @human.board.has_ship_on?(1, 2)
assert @human.board.has_ship_on?(3, 3)
assert @human.board.has_ship_on?(9, 5)
refute @human.board.has_ship_on?(7, 7)
assert_equal 5, @computer.ships.length
assert_equal 4, @computer.ships[3].length
end
def set_up_new_game
@human = HumanPlayer.new("Frank")
@computer = ComputerPlayer.new
@game = Game.new(@human, @computer)
$mock_inputs.clear
$mock_inputs << "A1"
$mock_inputs << "Down"
$mock_inputs << "A3"
$mock_inputs << "Down"
$mock_inputs << "A5"
$mock_inputs << "Down"
$mock_inputs << "A7"
$mock_inputs << "Down"
$mock_inputs << "A9"
$mock_inputs << "Down"
assert_output("Frank, where would you like to place a ship of length 2?\nAcross or Down?\n"+
"Frank, where would you like to place a ship of length 3?\nAcross or Down?\n"+
"Frank, where would you like to place a ship of length 3?\nAcross or Down?\n"+
"Frank, where would you like to place a ship of length 4?\nAcross or Down?\n"+
"Frank, where would you like to place a ship of length 5?\nAcross or Down?\n"+
"HAL 9000 has placed his ships.\n") do
@game.place_ships
end
end
def test_35_game_can_have_nonstandard_set_of_ships
human = HumanPlayer.new("Alice")
computer = ComputerPlayer.new
game = Game.new(human, computer, [2, 3])
$mock_inputs.clear
$mock_inputs << "A1"
$mock_inputs << "Down"
$mock_inputs << "A3"
$mock_inputs << "Down"
assert_output("Alice, where would you like to place a ship of length 2?\nAcross or Down?\n"+
"Alice, where would you like to place a ship of length 3?\nAcross or Down?\n"+
"HAL 9000 has placed his ships.\n") do
game.place_ships
end
end
def test_36_game_tells_you_if_your_ships_overlap
human = HumanPlayer.new("Alice")
computer = ComputerPlayer.new
game = Game.new(human, computer, [2, 3])
$mock_inputs.clear
$mock_inputs << "A2"
$mock_inputs << "Down"
$mock_inputs << "A1"
$mock_inputs << "Across"
$mock_inputs << "F1"
$mock_inputs << "Across"
assert_output("Alice, where would you like to place a ship of length 2?\nAcross or Down?\n"+
"Alice, where would you like to place a ship of length 3?\nAcross or Down?\n"+
"Unfortunately, that ship overlaps with one of your other ships. Please try again.\n"+
"Alice, where would you like to place a ship of length 3?\nAcross or Down?\n"+
"HAL 9000 has placed his ships.\n") do
game.place_ships
end
end
def test_37_human_can_take_first_turn
set_up_new_game
$mock_inputs.clear
$mock_inputs << "A1"
# This /(Miss!|Hit!)/ thing just checks to see if Miss! or Hit! was included anywhere in the message.
assert_output(/(Miss!|Hit!)/) do
@game.take_turn
end
end
def test_38_computer_can_take_second_turn
set_up_new_game
$mock_inputs.clear
$mock_inputs << "A1"
assert_output(/(Miss!|Hit!)/) do
@game.take_turn
end
assert_output(/(Miss!|Hit!)/) do
@game.take_turn
end
end
def test_39_display_game_status
set_up_new_game
assert_output(starting_game_status) do
@human.display_game_status
end
end
def starting_game_status
%Q{ 1 2 3 4 5 6 7 8 9 10
-----------------------------------------
A | | | | | | | | | | |
B | | | | | | | | | | |
C | | | | | | | | | | |
D | | | | | | | | | | |
E | | | | | | | | | | |
F | | | | | | | | | | |
G | | | | | | | | | | |
H | | | | | | | | | | |
I | | | | | | | | | | |
J | | | | | | | | | | |
-----------------------------------------
1 2 3 4 5 6 7 8 9 10
-----------------------------------------
A | O | | O | | O | | O | | O | |
B | O | | O | | O | | O | | O | |
C | | | O | | O | | O | | O | |
D | | | | | | | O | | O | |
E | | | | | | | | | O | |
F | | | | | | | | | | |
G | | | | | | | | | | |
H | | | | | | | | | | |
I | | | | | | | | | | |
J | | | | | | | | | | |
-----------------------------------------
}
end
def test_40_game_status_shows_hits_and_misses
human1 = HumanPlayer.new("Amy")
human2 = HumanPlayer.new("Beth")
game = Game.new(human1, human2, [2])
$mock_inputs.clear
# It doesn't matter what messages come up during the turns
assert_output(/./) do
$mock_inputs << "A2" #Amy's ship's location
$mock_inputs << "Down" #Amy's ship's direction
$mock_inputs << "F3" #Beth's ship's location
$mock_inputs << "Across" #Beth's ship's direction
game.place_ships
$mock_inputs << "F3" #Amy's hit
game.take_turn
$mock_inputs << "A2" #Beth's hit
game.take_turn
$mock_inputs << "H4" #Amy's miss
game.take_turn
$mock_inputs << "A2" #Beth's miss (she shot in the same spot as last time)
game.take_turn
end
# Now the visuals matter. Should show Amy's shots up top and Amy's own ship below.
assert_output(mid_game_status) do
human1.display_game_status
end
end
def mid_game_status
%Q{ 1 2 3 4 5 6 7 8 9 10
-----------------------------------------
A | | | | | | | | | | |
B | | | | | | | | | | |
C | | | | | | | | | | |
D | | | | | | | | | | |
E | | | | | | | | | | |
F | | | + | | | | | | | |
G | | | | | | | | | | |
H | | | | - | | | | | | |
I | | | | | | | | | | |
J | | | | | | | | | | |
-----------------------------------------
1 2 3 4 5 6 7 8 9 10
-----------------------------------------
A | | X | | | | | | | | |
B | | O | | | | | | | | |
C | | | | | | | | | | |
D | | | | | | | | | | |
E | | | | | | | | | | |
F | | | | | | | | | | |
G | | | | | | | | | | |
H | | | | | | | | | | |
I | | | | | | | | | | |
J | | | | | | | | | | |
-----------------------------------------
}
end
def test_41_game_can_be_won
human1 = HumanPlayer.new("Amy")
human2 = HumanPlayer.new("Beth")
game = Game.new(human1, human2, [2])
$mock_inputs.clear
$mock_inputs << "A2" #Amy's ship's location
$mock_inputs << "Down" #Amy's ship's direction
$mock_inputs << "F3" #Beth's ship's location
$mock_inputs << "Across" #Beth's ship's direction
$mock_inputs << "F3" #Amy's first shot
$mock_inputs << "A1" #Beth's first shot
$mock_inputs << "F4" #Amy's winning shot
assert_output(/Congratulations, Amy!/) do
game.play #When Amy wins, it has to say 'Congratulations, Amy' somewhere in the victory message.
end
end
end