battleship 1.0.1 → 1.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +15 -0
- data/{LICENSE → LICENSE.md} +0 -0
- data/{README → README.md} +5 -3
- data/bin/battleship +1 -0
- data/lib/battleship/battleship.rb +10 -0
- data/lib/battleship/board.rb +35 -31
- data/lib/battleship/carrier.rb +10 -0
- data/lib/battleship/destroyer.rb +10 -0
- data/lib/battleship/game.rb +62 -138
- data/lib/battleship/grid_cell.rb +17 -15
- data/lib/battleship/patrol_boat.rb +10 -0
- data/lib/battleship/player.rb +17 -13
- data/lib/battleship/ship.rb +6 -29
- data/lib/battleship/submarine.rb +10 -0
- metadata +30 -23
checksums.yaml
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
---
|
|
2
|
+
!binary "U0hBMQ==":
|
|
3
|
+
metadata.gz: !binary |-
|
|
4
|
+
ODk4NmI4MWUzNTZkNzBiYTcyNWI4N2JkZjk4MGJkNDIyOTAyNGMwZA==
|
|
5
|
+
data.tar.gz: !binary |-
|
|
6
|
+
Yjg1YjYyNjQyNzc1YWVjNjY1NTc2OTk0MDJhZWRlNTMxMjFmOTVlOA==
|
|
7
|
+
!binary "U0hBNTEy":
|
|
8
|
+
metadata.gz: !binary |-
|
|
9
|
+
MTUzMzZmYjc1ODAxMGQ2M2M2ZjcyOTE2YWMwYTUzYTI4ZGM2ODBlZjQ1NGU5
|
|
10
|
+
MjY1YTNiY2JkNWI1OGYwZjA2MjZkOGI3NzMxMjBmNTM4Zjk1NGY0MWZkYjRh
|
|
11
|
+
NmM3ZDYyMTI4MzNiODk1NTAyZjNjZmE5ODdlZjQ5ZWY1ZjY5MmY=
|
|
12
|
+
data.tar.gz: !binary |-
|
|
13
|
+
M2Y2YWJjM2NjYzcyOTgzMDM2MjhjODI3ODBlODFiNTMzZmM5ZTg2MDY4YzRi
|
|
14
|
+
ZWMzYzBiZTNhNmI3Y2EyYmYwNDJiNjEyYTMzYWJiYjRjYjQ3Y2EwMjU0NDZj
|
|
15
|
+
NTYzODFkYzQ4NTNiMjBlZjViOGZkYWFlMzgyZWE3NTRiYjQwNjE=
|
data/{LICENSE → LICENSE.md}
RENAMED
|
File without changes
|
data/{README → README.md}
RENAMED
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
battleship
|
|
2
|
-
|
|
1
|
+
battleship v 1.1.2
|
|
2
|
+
=
|
|
3
3
|
|
|
4
4
|
Version of the classic board game Battleship written in Ruby.
|
|
5
5
|
|
|
6
6
|
When placing ships, "starting position" refers to the top end of the ship when
|
|
7
|
-
placing vertically or left end of the ship when placing horizontally.
|
|
7
|
+
placing vertically or left end of the ship when placing horizontally.
|
|
8
|
+
|
|
9
|
+
Version 1.1.2 adds color via the 'colorize' gem.
|
data/bin/battleship
CHANGED
data/lib/battleship/board.rb
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
require_relative 'grid_cell'
|
|
2
|
-
require_relative '
|
|
2
|
+
require_relative 'carrier'
|
|
3
|
+
require_relative 'battleship'
|
|
4
|
+
require_relative 'destroyer'
|
|
5
|
+
require_relative 'submarine'
|
|
6
|
+
require_relative 'patrol_boat'
|
|
3
7
|
|
|
4
8
|
class Board
|
|
5
9
|
|
|
@@ -11,38 +15,52 @@ class Board
|
|
|
11
15
|
COLUMN = ['1','2','3','4','5','6','7','8','9','10']
|
|
12
16
|
|
|
13
17
|
def initialize
|
|
14
|
-
@grid = Array.new(BOARD_DIM).map! {Array.new(BOARD_DIM).map! {
|
|
18
|
+
@grid = Array.new(BOARD_DIM).map! {Array.new(BOARD_DIM).map! {GridCell.new}}
|
|
15
19
|
end
|
|
16
20
|
|
|
17
|
-
#print board grid
|
|
18
21
|
def to_s
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
22
|
+
row_letter = ('A'..'Z').to_a
|
|
23
|
+
i = 0
|
|
24
|
+
puts " 1 2 3 4 5 6 7 8 9 10".colorize(:green)
|
|
25
|
+
@grid.each do |row|
|
|
26
|
+
print row_letter[i].colorize(:green) + ' '
|
|
27
|
+
row.each {|cell| print cell.to_s + ' '}
|
|
28
|
+
print "\n"
|
|
29
|
+
i += 1
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def place_ship(ship, start_position, orientation)
|
|
34
|
+
row = start_position[:row]
|
|
35
|
+
column = start_position[:column]
|
|
36
|
+
ship.length.times do
|
|
37
|
+
if orientation == :horizontal
|
|
38
|
+
self.grid[row][column].ship = ship
|
|
39
|
+
self.grid[row][column].status = :occupied
|
|
40
|
+
column += 1
|
|
41
|
+
else
|
|
42
|
+
self.grid[row][column].ship = ship
|
|
43
|
+
self.grid[row][column].status = :occupied
|
|
44
|
+
row += 1
|
|
45
|
+
end
|
|
27
46
|
end
|
|
28
47
|
end
|
|
29
48
|
|
|
30
49
|
#validate ship placement remains within board
|
|
31
50
|
def valid_coordinates?(ship, start_position, orientation)
|
|
32
51
|
if orientation == :horizontal
|
|
33
|
-
(start_position[:column] +
|
|
52
|
+
(start_position[:column] + ship.length) <= BOARD_DIM
|
|
34
53
|
else
|
|
35
|
-
(start_position[:row] +
|
|
54
|
+
(start_position[:row] + ship.length) <= BOARD_DIM
|
|
36
55
|
end
|
|
37
56
|
end
|
|
38
57
|
|
|
39
58
|
#validate ship placement conflicts with another placed ship
|
|
40
59
|
def check_clearance?(ship, start_position, orientation)
|
|
41
|
-
length = Ship::LENGTH[ship]
|
|
42
60
|
row = start_position[:row]
|
|
43
61
|
column = start_position[:column]
|
|
44
|
-
length.times do
|
|
45
|
-
if self.grid[row][column].
|
|
62
|
+
ship.length.times do
|
|
63
|
+
if self.grid[row][column].ship
|
|
46
64
|
return false
|
|
47
65
|
elsif orientation == :horizontal
|
|
48
66
|
column += 1
|
|
@@ -52,18 +70,4 @@ class Board
|
|
|
52
70
|
end
|
|
53
71
|
return true
|
|
54
72
|
end
|
|
55
|
-
|
|
56
|
-
# method currently not used
|
|
57
|
-
# def hit_or_miss(cell)
|
|
58
|
-
# if @grid[cell[:row], cell[:column]].status == :open
|
|
59
|
-
# return :miss
|
|
60
|
-
# elsif @grid[cell[:row], cell[:column]].status == :hit ||
|
|
61
|
-
# @grid[cell[:row], cell[:column]].status == :miss
|
|
62
|
-
# return :called
|
|
63
|
-
# else
|
|
64
|
-
# @grid[cell[:row], cell[:column]].status = :hit
|
|
65
|
-
# return :hit
|
|
66
|
-
# end
|
|
67
|
-
# end
|
|
68
|
-
|
|
69
|
-
end
|
|
73
|
+
end
|
data/lib/battleship/game.rb
CHANGED
|
@@ -1,21 +1,20 @@
|
|
|
1
1
|
require_relative 'player'
|
|
2
2
|
require_relative 'board'
|
|
3
|
+
require 'colorize'
|
|
3
4
|
|
|
4
5
|
class Game
|
|
5
6
|
|
|
6
7
|
attr_reader :opp_targeting_queue
|
|
7
8
|
|
|
9
|
+
FLEET = [:carrier, :battleship, :destroyer, :submarine, :patrol_boat]
|
|
10
|
+
|
|
8
11
|
def play
|
|
9
|
-
puts "\nLet's play some Battleship!\n\n"
|
|
12
|
+
puts "\nLet's play some Battleship!\n\n".colorize(:light_red)
|
|
10
13
|
set_player
|
|
11
14
|
set_opponent
|
|
12
15
|
puts "\nNow place your ships, #{@player.name}."
|
|
13
|
-
deploy_ship(@player,
|
|
14
|
-
|
|
15
|
-
deploy_ship(@player, @player.destroyer)
|
|
16
|
-
deploy_ship(@player, @player.submarine)
|
|
17
|
-
deploy_ship(@player, @player.patrol)
|
|
18
|
-
deploy_opp_ships
|
|
16
|
+
FLEET.each {|ship| deploy_ship(@player, ship)}
|
|
17
|
+
FLEET.each {|ship| deploy_opp_ship(@opponent, ship)}
|
|
19
18
|
play_rounds
|
|
20
19
|
end
|
|
21
20
|
|
|
@@ -41,14 +40,13 @@ class Game
|
|
|
41
40
|
|
|
42
41
|
def deploy_ship(player, ship)
|
|
43
42
|
|
|
44
|
-
#prompt for placement orientation and validate input
|
|
45
43
|
valid = false
|
|
46
44
|
while valid == false do
|
|
47
45
|
print "\n"
|
|
48
46
|
player.board.to_s #print board for reference
|
|
49
47
|
position = {}
|
|
50
48
|
while valid == false do
|
|
51
|
-
print "\n#{ship.
|
|
49
|
+
print "\n#{ship.capitalize} orientation: horizontal(H) or vertical(V)? "
|
|
52
50
|
input = gets.chomp.rstrip.upcase
|
|
53
51
|
if input == 'H' || input == 'HORIZONTAL'
|
|
54
52
|
orientation = :horizontal
|
|
@@ -57,81 +55,65 @@ class Game
|
|
|
57
55
|
orientation = :vertical
|
|
58
56
|
valid = true
|
|
59
57
|
else
|
|
60
|
-
puts "Invalid orientation entry."
|
|
58
|
+
puts "Invalid orientation entry.".colorize(:light_yellow)
|
|
61
59
|
end
|
|
62
60
|
end
|
|
63
61
|
|
|
64
|
-
#prompt for placement starting position and validate input
|
|
65
62
|
valid = false
|
|
66
63
|
while valid == false do
|
|
67
|
-
print "\n#{ship.
|
|
64
|
+
print "\n#{ship.capitalize} starting position (Ex. A10): "
|
|
68
65
|
input = gets.chomp.rstrip.upcase
|
|
69
66
|
position[:row] = Board::ROW.rindex(input.split(//, 2)[0])
|
|
70
67
|
position[:column] = Board::COLUMN.rindex(input.split(//, 2)[1])
|
|
71
68
|
if !position[:row].nil? && !position[:column].nil?
|
|
72
69
|
valid = true
|
|
73
70
|
else
|
|
74
|
-
puts "Invalid coordinates."
|
|
71
|
+
puts "Invalid coordinates.".colorize(:yellow)
|
|
75
72
|
end
|
|
76
73
|
end
|
|
77
74
|
|
|
78
|
-
#validate board clearances for given orientation and position
|
|
79
75
|
valid = false
|
|
80
|
-
if player.board.valid_coordinates?(ship
|
|
81
|
-
player.board.check_clearance?(ship
|
|
82
|
-
|
|
83
|
-
|
|
76
|
+
if player.board.valid_coordinates?(player.send(ship), position, orientation) &&
|
|
77
|
+
player.board.check_clearance?(player.send(ship), position, orientation)
|
|
78
|
+
player.board.place_ship(player.send(ship), position, orientation)
|
|
79
|
+
valid = true
|
|
84
80
|
else
|
|
85
|
-
puts "Invalid position for ship."
|
|
81
|
+
puts "Invalid position for ship.".colorize(:yellow)
|
|
86
82
|
end
|
|
87
83
|
end
|
|
88
84
|
end
|
|
89
85
|
|
|
90
86
|
#randomly place opponent's ships
|
|
91
|
-
def
|
|
87
|
+
def deploy_opp_ship(opponent, ship)
|
|
92
88
|
position = {}
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
end
|
|
89
|
+
valid = false
|
|
90
|
+
while valid == false
|
|
91
|
+
orientation = [:horizontal, :vertical].sample
|
|
92
|
+
if orientation == :horizontal
|
|
93
|
+
rows = Board::ROW
|
|
94
|
+
columns = Board::COLUMN[0..9 - opponent.send(ship).length]
|
|
95
|
+
else
|
|
96
|
+
rows = Board::ROW[0..9 - opponent.send(ship).length]
|
|
97
|
+
columns = Board::COLUMN
|
|
98
|
+
end
|
|
104
99
|
|
|
105
|
-
|
|
106
|
-
|
|
100
|
+
position[:row] = Board::ROW.rindex(rows.sample)
|
|
101
|
+
position[:column] = Board::COLUMN.rindex(columns.sample)
|
|
107
102
|
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
when :carrier
|
|
113
|
-
@opponent.carrier.place_ship(@opponent.board, position, orientation)
|
|
114
|
-
when :battleship
|
|
115
|
-
@opponent.battleship.place_ship(@opponent.board, position, orientation)
|
|
116
|
-
when :destroyer
|
|
117
|
-
@opponent.destroyer.place_ship(@opponent.board, position, orientation)
|
|
118
|
-
when :submarine
|
|
119
|
-
@opponent.submarine.place_ship(@opponent.board, position, orientation)
|
|
120
|
-
when :patrol
|
|
121
|
-
@opponent.patrol.place_ship(@opponent.board, position, orientation)
|
|
122
|
-
end
|
|
123
|
-
end
|
|
103
|
+
if opponent.board.valid_coordinates?(opponent.send(ship), position, orientation) &&
|
|
104
|
+
opponent.board.check_clearance?(opponent.send(ship), position, orientation)
|
|
105
|
+
valid = true
|
|
106
|
+
opponent.board.place_ship(opponent.send(ship), position, orientation)
|
|
124
107
|
end
|
|
125
108
|
end
|
|
126
|
-
# @opponent.board.to_s #print for testing only
|
|
127
109
|
end
|
|
128
110
|
|
|
129
|
-
#alternate rounds between player and opponent, determine if game won
|
|
130
111
|
def play_rounds
|
|
131
112
|
puts "\n\nTime to sink some ships! Good luck, #{@player.name}\n\n"
|
|
132
113
|
game_over = false
|
|
133
114
|
while game_over == false
|
|
134
115
|
@player.print_boards
|
|
116
|
+
|
|
135
117
|
player_round
|
|
136
118
|
if @opponent.ships_left == 0
|
|
137
119
|
winner = @player.name
|
|
@@ -145,10 +127,9 @@ class Game
|
|
|
145
127
|
game_over = true
|
|
146
128
|
end
|
|
147
129
|
end
|
|
148
|
-
puts "\n\n#{winner} WINS!\n\n"
|
|
130
|
+
puts "\n\n#{winner} WINS!\n\n".colorize(:light_blue)
|
|
149
131
|
end
|
|
150
132
|
|
|
151
|
-
#prompt player for shot coordinates, validate, assess hit or miss
|
|
152
133
|
def player_round
|
|
153
134
|
target = {}
|
|
154
135
|
valid = false
|
|
@@ -157,55 +138,33 @@ class Game
|
|
|
157
138
|
input = gets.chomp.rstrip.upcase
|
|
158
139
|
target[:row] = Board::ROW.rindex(input.split(//, 2)[0])
|
|
159
140
|
target[:column] = Board::COLUMN.rindex(input.split(//, 2)[1])
|
|
160
|
-
if
|
|
161
|
-
|
|
141
|
+
if target[:row].nil? || target[:column].nil?
|
|
142
|
+
puts "Invalid coordinates.".colorize(:yellow)
|
|
143
|
+
elsif @player.target_board.grid[target[:row]][target[:column]].status != :open
|
|
144
|
+
puts "Coordinates already called. Try again.".colorize(:yellow)
|
|
162
145
|
else
|
|
163
|
-
|
|
146
|
+
valid = true
|
|
164
147
|
end
|
|
165
148
|
end
|
|
149
|
+
player_cell = @player.target_board.grid[target[:row]][target[:column]]
|
|
150
|
+
opponent_cell = @opponent.board.grid[target[:row]][target[:column]]
|
|
166
151
|
|
|
167
|
-
if
|
|
168
|
-
puts "\n\"MISS!\""
|
|
169
|
-
|
|
152
|
+
if opponent_cell.status == :open
|
|
153
|
+
puts "\n\"MISS!\"".colorize(:yellow)
|
|
154
|
+
player_cell.miss
|
|
170
155
|
else
|
|
171
|
-
puts "\n\"HIT!\""
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
end
|
|
180
|
-
when :battleship
|
|
181
|
-
@opponent.battleship.hit
|
|
182
|
-
if @opponent.battleship.sunk?
|
|
183
|
-
@opponent.ships_left -= 1
|
|
184
|
-
puts "Opponent's battleship sunk! #{@opponent.ships_left} more ships to go."
|
|
185
|
-
end
|
|
186
|
-
when :destroyer
|
|
187
|
-
@opponent.destroyer.hit
|
|
188
|
-
if @opponent.destroyer.sunk?
|
|
189
|
-
@opponent.ships_left -= 1
|
|
190
|
-
puts "Opponent's destroyer sunk! #{@opponent.ships_left} more ships to go."
|
|
191
|
-
end
|
|
192
|
-
when :submarine
|
|
193
|
-
@opponent.submarine.hit
|
|
194
|
-
if @opponent.submarine.sunk?
|
|
195
|
-
@opponent.ships_left -= 1
|
|
196
|
-
puts "Opponent's submarine sunk! #{@opponent.ships_left} more ships to go."
|
|
197
|
-
end
|
|
198
|
-
when :patrol
|
|
199
|
-
@opponent.patrol.hit
|
|
200
|
-
if @opponent.patrol.sunk?
|
|
201
|
-
@opponent.ships_left -= 1
|
|
202
|
-
puts "Opponent's patrol boat sunk! #{@opponent.ships_left} more ships to go."
|
|
203
|
-
end
|
|
156
|
+
puts "\n\"HIT!\"".colorize(:light_red)
|
|
157
|
+
player_cell.hit
|
|
158
|
+
opponent_cell.ship.hit
|
|
159
|
+
opponent_cell.hit
|
|
160
|
+
|
|
161
|
+
if opponent_cell.ship.sunk?
|
|
162
|
+
@opponent.ships_left -= 1
|
|
163
|
+
puts "Opponent's #{opponent_cell.ship.class.to_s.downcase} sunk! #{@opponent.ships_left} more ships to go.".colorize(:light_red)
|
|
204
164
|
end
|
|
205
165
|
end
|
|
206
166
|
end
|
|
207
167
|
|
|
208
|
-
#opponent takes shot from queue, assess hit or miss
|
|
209
168
|
def opponent_round
|
|
210
169
|
puts "\n---------- Opponent's turn ----------"
|
|
211
170
|
target_coords = opp_targeting_queue.pop
|
|
@@ -214,56 +173,21 @@ class Game
|
|
|
214
173
|
target[:row] = Board::ROW.rindex(target_coords[0])
|
|
215
174
|
target[:column] = Board::COLUMN.rindex(target_coords[1])
|
|
216
175
|
|
|
176
|
+
player_cell = @player.board.grid[target[:row]][target[:column]]
|
|
177
|
+
|
|
217
178
|
print "\nOpponent called \"#{target_coords[0]}#{target_coords[1]}\" "
|
|
218
179
|
if @player.board.grid[target[:row]][target[:column]].status == :open
|
|
219
|
-
puts "- MISS!\n\n"
|
|
180
|
+
puts "- MISS!\n\n".colorize(:yellow)
|
|
220
181
|
else
|
|
221
|
-
puts "- HIT!"
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
else
|
|
229
|
-
puts "\nYour carrier has been hit!"
|
|
230
|
-
end
|
|
231
|
-
when :battleship
|
|
232
|
-
@player.battleship.hit
|
|
233
|
-
if @player.battleship.sunk?
|
|
234
|
-
@player.ships_left -= 1
|
|
235
|
-
puts "\nYour battleship has been sunk!"
|
|
236
|
-
else
|
|
237
|
-
puts "\nYour battleship has been hit!"
|
|
238
|
-
end
|
|
239
|
-
when :destroyer
|
|
240
|
-
@player.destroyer.hit
|
|
241
|
-
if @player.destroyer.sunk?
|
|
242
|
-
@player.ships_left -= 1
|
|
243
|
-
puts "\nYour destroyer has been sunk!"
|
|
244
|
-
else
|
|
245
|
-
puts "\nYour destroyer has been hit!"
|
|
246
|
-
end
|
|
247
|
-
when :submarine
|
|
248
|
-
@player.submarine.hit
|
|
249
|
-
if @player.submarine.sunk?
|
|
250
|
-
@player.ships_left -= 1
|
|
251
|
-
puts "\nYour submarine has been sunk!"
|
|
252
|
-
else
|
|
253
|
-
puts "\nYour submarine has been hit!"
|
|
254
|
-
end
|
|
255
|
-
when :patrol
|
|
256
|
-
@player.patrol.hit
|
|
257
|
-
if @player.patrol.sunk?
|
|
258
|
-
@player.ships_left -= 1
|
|
259
|
-
puts "\nYour patrol boat has been sunk!"
|
|
260
|
-
else
|
|
261
|
-
puts "\nYour patrol boat has been hit!"
|
|
262
|
-
end
|
|
182
|
+
puts "- HIT!".colorize(:light_red)
|
|
183
|
+
player_cell.hit
|
|
184
|
+
player_cell.ship.hit
|
|
185
|
+
if player_cell.ship.sunk?
|
|
186
|
+
puts "\nYour #{player_cell.ship.class.to_s.downcase} has been sunk!".colorize(:light_red)
|
|
187
|
+
else
|
|
188
|
+
puts "\nYour #{player_cell.ship.class.to_s.downcase} has been hit!".colorize(:light_red)
|
|
263
189
|
end
|
|
264
|
-
@player.board.grid[target[:row]][target[:column]].hit
|
|
265
190
|
end
|
|
266
191
|
puts "-------------------------------------\n\n"
|
|
267
192
|
end
|
|
268
|
-
|
|
269
193
|
end
|
data/lib/battleship/grid_cell.rb
CHANGED
|
@@ -1,23 +1,26 @@
|
|
|
1
|
-
class
|
|
1
|
+
class GridCell
|
|
2
2
|
|
|
3
|
-
attr_accessor :status
|
|
3
|
+
attr_accessor :status, :ship
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
:miss => '0',
|
|
9
|
-
:carrier => 'C',
|
|
10
|
-
:battleship => 'B',
|
|
11
|
-
:destroyer => 'D',
|
|
12
|
-
:submarine => 'S',
|
|
13
|
-
:patrol => 'P'}
|
|
5
|
+
FILL_CHAR = {:open => '+',
|
|
6
|
+
:hit => 'X',
|
|
7
|
+
:miss => '0'}
|
|
14
8
|
|
|
15
|
-
def initialize(status = :open)
|
|
9
|
+
def initialize(status = :open, ship = nil)
|
|
16
10
|
@status = status
|
|
11
|
+
@ship = ship
|
|
17
12
|
end
|
|
18
13
|
|
|
19
14
|
def to_s
|
|
20
|
-
|
|
15
|
+
if @ship && @status != :hit
|
|
16
|
+
@ship.to_s
|
|
17
|
+
elsif @status == :hit
|
|
18
|
+
FILL_CHAR[@status].colorize(:light_red)
|
|
19
|
+
elsif @status == :miss
|
|
20
|
+
FILL_CHAR[@status].colorize(:yellow)
|
|
21
|
+
else
|
|
22
|
+
FILL_CHAR[@status]
|
|
23
|
+
end
|
|
21
24
|
end
|
|
22
25
|
|
|
23
26
|
def hit
|
|
@@ -27,5 +30,4 @@ FILL_CHAR = {:open => '+',
|
|
|
27
30
|
def miss
|
|
28
31
|
@status = :miss
|
|
29
32
|
end
|
|
30
|
-
|
|
31
|
-
end
|
|
33
|
+
end
|
data/lib/battleship/player.rb
CHANGED
|
@@ -1,20 +1,25 @@
|
|
|
1
1
|
require_relative 'board'
|
|
2
|
-
require_relative '
|
|
2
|
+
require_relative 'carrier'
|
|
3
|
+
require_relative 'battleship'
|
|
4
|
+
require_relative 'destroyer'
|
|
5
|
+
require_relative 'submarine'
|
|
6
|
+
require_relative 'patrol_boat'
|
|
3
7
|
|
|
4
8
|
class Player
|
|
5
9
|
|
|
6
10
|
attr_accessor :name, :ships_left
|
|
7
|
-
attr_reader :board, :target_board, :carrier, :battleship, :destroyer,
|
|
11
|
+
attr_reader :board, :target_board, :carrier, :battleship, :destroyer,
|
|
12
|
+
:submarine, :patrol_boat
|
|
8
13
|
|
|
9
14
|
def initialize(name)
|
|
10
15
|
@name = name
|
|
11
16
|
@board = Board.new
|
|
12
17
|
@target_board = Board.new
|
|
13
|
-
@carrier =
|
|
14
|
-
@battleship =
|
|
15
|
-
@destroyer =
|
|
16
|
-
@submarine =
|
|
17
|
-
@
|
|
18
|
+
@carrier = Carrier.new
|
|
19
|
+
@battleship = Battleship.new
|
|
20
|
+
@destroyer = Destroyer.new
|
|
21
|
+
@submarine = Submarine.new
|
|
22
|
+
@patrol_boat = PatrolBoat.new
|
|
18
23
|
@ships_left = Board::NUM_SHIPS
|
|
19
24
|
end
|
|
20
25
|
|
|
@@ -24,19 +29,18 @@ class Player
|
|
|
24
29
|
|
|
25
30
|
#print side-by-side ship status and shpts taken boards
|
|
26
31
|
def print_boards
|
|
27
|
-
puts " SHIP STATUS SHOTS TAKEN"
|
|
28
|
-
puts " 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10"
|
|
32
|
+
puts " SHIP STATUS SHOTS TAKEN".colorize(:light_red)
|
|
33
|
+
puts " 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10".colorize(:green)
|
|
29
34
|
row_letter = ('A'..'Z').to_a
|
|
30
35
|
row_number = 0
|
|
31
36
|
@board.grid.each do |row1|
|
|
32
|
-
print row_letter[row_number] + ' '
|
|
37
|
+
print row_letter[row_number].colorize(:green) + ' '
|
|
33
38
|
row1.each {|cell| print cell.to_s + ' '}
|
|
34
39
|
print " "
|
|
35
|
-
print row_letter[row_number] + ' '
|
|
40
|
+
print row_letter[row_number].colorize(:green) + ' '
|
|
36
41
|
@target_board.grid[row_number].each {|cell| print cell.to_s + ' '}
|
|
37
42
|
print "\n"
|
|
38
43
|
row_number += 1
|
|
39
44
|
end
|
|
40
45
|
end
|
|
41
|
-
|
|
42
|
-
end
|
|
46
|
+
end
|
data/lib/battleship/ship.rb
CHANGED
|
@@ -1,43 +1,20 @@
|
|
|
1
1
|
class Ship
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
:battleship => 4,
|
|
5
|
-
:destroyer => 3,
|
|
6
|
-
:submarine => 3,
|
|
7
|
-
:patrol => 2}
|
|
3
|
+
attr_reader :length, :hits, :fill_char
|
|
8
4
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
def initialize(type)
|
|
12
|
-
@type = type
|
|
13
|
-
@length = LENGTH[type]
|
|
5
|
+
def initialize
|
|
14
6
|
@hits = 0
|
|
15
7
|
end
|
|
16
8
|
|
|
17
|
-
def
|
|
18
|
-
|
|
19
|
-
column = start_position[:column]
|
|
20
|
-
@length.times do
|
|
21
|
-
if orientation == :horizontal
|
|
22
|
-
board.grid[row][column].status = @type
|
|
23
|
-
column += 1
|
|
24
|
-
else
|
|
25
|
-
board.grid[row][column].status = @type
|
|
26
|
-
row += 1
|
|
27
|
-
end
|
|
28
|
-
end
|
|
9
|
+
def hit
|
|
10
|
+
@hits += 1
|
|
29
11
|
end
|
|
30
12
|
|
|
31
13
|
def sunk?
|
|
32
14
|
@length == @hits
|
|
33
15
|
end
|
|
34
16
|
|
|
35
|
-
def hit
|
|
36
|
-
@hits += 1
|
|
37
|
-
end
|
|
38
|
-
|
|
39
17
|
def to_s
|
|
40
|
-
|
|
18
|
+
@fill_char.colorize(:light_blue)
|
|
41
19
|
end
|
|
42
|
-
|
|
43
|
-
end
|
|
20
|
+
end
|
metadata
CHANGED
|
@@ -1,27 +1,30 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: battleship
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.
|
|
5
|
-
prerelease:
|
|
4
|
+
version: 1.1.2
|
|
6
5
|
platform: ruby
|
|
7
6
|
authors:
|
|
8
7
|
- David Rodriguez
|
|
9
8
|
autorequire:
|
|
10
9
|
bindir: bin
|
|
11
10
|
cert_chain: []
|
|
12
|
-
date: 2012-
|
|
13
|
-
dependencies:
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
11
|
+
date: 2012-03-20 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: colorize
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - ! '>='
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: 0.5.8
|
|
20
|
+
type: :runtime
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - ! '>='
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: 0.5.8
|
|
27
|
+
description: Ruby text-based version of the classic board game
|
|
25
28
|
email: davidrodriguez212@gmail.com
|
|
26
29
|
executables:
|
|
27
30
|
- battleship
|
|
@@ -29,35 +32,39 @@ extensions: []
|
|
|
29
32
|
extra_rdoc_files: []
|
|
30
33
|
files:
|
|
31
34
|
- bin/battleship
|
|
35
|
+
- lib/battleship/battleship.rb
|
|
32
36
|
- lib/battleship/board.rb
|
|
37
|
+
- lib/battleship/carrier.rb
|
|
38
|
+
- lib/battleship/destroyer.rb
|
|
33
39
|
- lib/battleship/game.rb
|
|
34
40
|
- lib/battleship/grid_cell.rb
|
|
41
|
+
- lib/battleship/patrol_boat.rb
|
|
35
42
|
- lib/battleship/player.rb
|
|
36
43
|
- lib/battleship/ship.rb
|
|
37
|
-
-
|
|
38
|
-
-
|
|
39
|
-
|
|
44
|
+
- lib/battleship/submarine.rb
|
|
45
|
+
- LICENSE.md
|
|
46
|
+
- README.md
|
|
47
|
+
homepage: https://github.com/rodriguezd/ruby_battleship
|
|
40
48
|
licenses: []
|
|
49
|
+
metadata: {}
|
|
41
50
|
post_install_message:
|
|
42
51
|
rdoc_options: []
|
|
43
52
|
require_paths:
|
|
44
53
|
- lib
|
|
45
54
|
required_ruby_version: !ruby/object:Gem::Requirement
|
|
46
|
-
none: false
|
|
47
55
|
requirements:
|
|
48
56
|
- - ! '>='
|
|
49
57
|
- !ruby/object:Gem::Version
|
|
50
58
|
version: '1.9'
|
|
51
59
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
52
|
-
none: false
|
|
53
60
|
requirements:
|
|
54
61
|
- - ! '>='
|
|
55
62
|
- !ruby/object:Gem::Version
|
|
56
63
|
version: '0'
|
|
57
64
|
requirements: []
|
|
58
65
|
rubyforge_project:
|
|
59
|
-
rubygems_version:
|
|
66
|
+
rubygems_version: 2.0.3
|
|
60
67
|
signing_key:
|
|
61
|
-
specification_version:
|
|
62
|
-
summary: Ruby text
|
|
68
|
+
specification_version: 4
|
|
69
|
+
summary: Ruby text-based version of the classic board game
|
|
63
70
|
test_files: []
|