class Battleship::GamePlay
Constants
- ANSWERS
- GAMEOVER_MSG
- MATCHES
- ROUND_MSG
- STARS
- START_MSG
- TURN_MSG
Attributes
matches[R]
output[R]
Public Class Methods
call()
click to toggle source
# File lib/battleship/gameplay.rb, line 13 def self.call new.call end
new(player_class=Player, output=STDOUT)
click to toggle source
# File lib/battleship/gameplay.rb, line 19 def initialize(player_class=Player, output=STDOUT) @player_class = player_class @players = nil @winner = nil @matches = [] @output = output end
Public Instance Methods
call()
click to toggle source
# File lib/battleship/gameplay.rb, line 35 def call output.puts(GAMEOVER_MSG.call(play)) end
players()
click to toggle source
# File lib/battleship/gameplay.rb, line 27 def players return @players if @players p1, p2 = [1, 2].map { |i| @player_class.factory(name: "Player #{i}", output: @output) } p1.enemy = p2 p2.enemy = p1 @players = [p1, p2] end
Private Instance Methods
fight()
click to toggle source
# File lib/battleship/gameplay.rb, line 61 def fight loop do players.each do |player| output.puts(TURN_MSG.call(player.name)) player.shot return player.name if player.enemy.gameover? end end end
play()
click to toggle source
# File lib/battleship/gameplay.rb, line 39 def play loop do output.puts(START_MSG.call(matches.size+1)) setup round return winner.name if winner end end
reset!()
click to toggle source
# File lib/battleship/gameplay.rb, line 77 def reset! @players = nil end
round()
click to toggle source
# File lib/battleship/gameplay.rb, line 71 def round matches << fight output.puts(ROUND_MSG.call(matches[-1])) reset! end
setup()
click to toggle source
# File lib/battleship/gameplay.rb, line 54 def setup players.shuffle.each do |player| output.puts(TURN_MSG.call(player.name)) player.setup end end
winner()
click to toggle source
# File lib/battleship/gameplay.rb, line 48 def winner @winner ||= players.detect do |player| matches.count(player.name) > 1 end end