class GameInterface

Attributes

command[R]
display[R]
in_stream[R]
out_stream[R]

Public Class Methods

new(in_stream, out_stream) click to toggle source
# File lib/game_interface.rb, line 10
def initialize(in_stream, out_stream)
  @command = ''
  @display = Display.new
  @in_stream = in_stream
  @out_stream = out_stream
end

Public Instance Methods

display_instructions() click to toggle source
# File lib/game_interface.rb, line 45
def display_instructions
  out_stream.puts display.instructions
  out_stream.puts display.win_question
end
display_invalid_input_message() click to toggle source
# File lib/game_interface.rb, line 50
def display_invalid_input_message
  out_stream.puts display.invalid_input
end
finished?() click to toggle source
# File lib/game_interface.rb, line 62
def finished?
  command == 'q' || command == 'quit'
end
instructions?() click to toggle source
# File lib/game_interface.rb, line 58
def instructions?
  command == 'i'
end
play?() click to toggle source
# File lib/game_interface.rb, line 54
def play?
  command == 'p'
end
play_game() click to toggle source
# File lib/game_interface.rb, line 38
def play_game
  out_stream.puts display.level
  @command = in_stream.gets.strip
  Game.new(in_stream, out_stream, display, selected_level).play
  out_stream.puts display.win_question
end
process_initial_command() click to toggle source
# File lib/game_interface.rb, line 26
def process_initial_command
  case
  when play?
    play_game
  when instructions?
    display_instructions
  when finished?
  else
    display_invalid_input_message
  end
end
start() click to toggle source
# File lib/game_interface.rb, line 17
def start
  out_stream.puts display.welcome
  out_stream.puts display.initial_message
  until finished?
    @command = in_stream.gets.strip
    process_initial_command
  end
end

Private Instance Methods

default_level() click to toggle source
# File lib/game_interface.rb, line 80
def default_level
  'rgby'
end
levels() click to toggle source
# File lib/game_interface.rb, line 72
def levels
  @levels ||= {
    'e' => 'rgby',
    'i' => 'rgbyw',
    'h' => 'rgbywcm'
  }
end
selected_level() click to toggle source
# File lib/game_interface.rb, line 68
def selected_level
  levels[@command] || default_level
end