This repository was archived by the owner on Mar 1, 2019. It is now read-only.
forked from tiyd-rails-2016-01/battleship
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.rb
More file actions
72 lines (62 loc) · 1.34 KB
/
game.rb
File metadata and controls
72 lines (62 loc) · 1.34 KB
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
class Game
def initialize(player_one, player_two)
@player_one = player_one
@player_two = player_two
@players = [@player_one, @player_two]
@turns = 0
end
def welcome
puts "Welcome, #{@player_one.name} and #{@player_two.name}!\nIt's time to play Battleship.\n"
end
def place_ships
@player_one.place_ships
@player_two.place_ships
end
def display_status
if @turns == 0
puts "SHOTS TAKEN:"
puts Grid::EMPTYGRID
puts "\nYOUR BOARD:"
@players[0].grid.display
else
puts "SHOTS TAKEN:"
@players[1].grid.display_shots
puts "\nYOUR BOARD:"
@players[0].grid.display
end
end
def x_of(string)
string[1..-1].to_i
end
def y_of(string)
("A".."J").each_with_index do |l, i|
if string[0] == l
return i + 1
end
end
end
def take_turn
player_one = @players[0]
player_two = @players[1]
coordinates = player_one.call_shot
x = x_of("#{coordinates}")
y = y_of("#{coordinates}")
if player_two.grid.fire_at(x, y)
puts "Hit!"
else
puts "Miss!"
player_two.grid.missed << [x,y]
end
@turns =+ 1
@players.reverse!
end
def play
welcome
place_ships
take_turn
until @players[0].grid.sunk?
take_turn
end
puts "Congratulations, #{@players[1].name}!"
end
end