Class: SeaBattle
- Inherits:
-
Object
show all
- Defined in:
- lib/sea_battle.rb,
lib/sea_battle/gui.rb,
lib/sea_battle/cell.rb,
lib/sea_battle/board.rb,
lib/sea_battle/support.rb,
lib/sea_battle/version.rb,
lib/sea_battle/random_ship.rb
Defined Under Namespace
Modules: Support
Classes: Board, Cell, GUI, RandomShip
Constant Summary
collapse
- VERSION =
"0.1.3"
Instance Method Summary
collapse
Constructor Details
#initialize(first_board, second_board, last_attack_move = nil) ⇒ SeaBattle
Returns a new instance of SeaBattle.
7
8
9
10
11
|
# File 'lib/sea_battle.rb', line 7
def initialize(first_board, second_board, last_attack_move = nil)
@last_attack_move = last_attack_move.split(";") unless last_attack_move.nil?
@first_board = first_board
@second_board = second_board
end
|
Instance Method Details
#active_user ⇒ Object
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
# File 'lib/sea_battle.rb', line 13
def active_user
return :first_player if @last_attack_move.nil?
player, row, column = @last_attack_move
if player.to_sym == :first_player
if @second_board.is_in_ship?(row.to_i, column.to_i)
return :first_player
else
return :second_player
end
else
if @first_board.is_in_ship?(row.to_i, column.to_i)
return :second_player
else
return :first_player
end
end
end
|
#is_activated? ⇒ Boolean
31
32
33
|
# File 'lib/sea_battle.rb', line 31
def is_activated?
not first_status.eql?(:initialized) and not second_status.eql?(:initialized)
end
|
#is_sunken_ship?(row, column, player) ⇒ Boolean
35
36
37
38
39
40
41
|
# File 'lib/sea_battle.rb', line 35
def is_sunken_ship?(row, column, player)
if player == :first_player
@second_board.is_sunken_ship?(row, column)
else
@first_board.is_sunken_ship?(row, column)
end
end
|
#last_attack_move ⇒ Object
43
44
45
46
|
# File 'lib/sea_battle.rb', line 43
def last_attack_move
return if @last_attack_move.nil?
@last_attack_move.join(";")
end
|
#move(player, type, row, column) ⇒ Object
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
# File 'lib/sea_battle.rb', line 48
def move(player, type, row, column)
return false unless winner_is.nil?
return false unless [:first_player, :second_player].include?(player)
return false unless [:choose, :attack, :mark].include?(type)
case type
when :attack
return false unless active_user == player
return false unless first_status.eql?(:activated) and second_status.eql?(:activated)
if player == :first_player
@second_board.attack(row, column)
else
@first_board.attack(row, column)
end
@last_attack_move = [player, row, column]
else
end
true
end
|
#winner_is ⇒ Object
69
70
71
72
73
|
# File 'lib/sea_battle.rb', line 69
def winner_is
return :second_player if first_status.eql? :finished
return :first_player if second_status.eql? :finished
nil
end
|