Class: Mastermind::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/mastermind/cli.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(instream, outstream) ⇒ CLI

Returns a new instance of CLI.



8
9
10
11
12
13
# File 'lib/mastermind/cli.rb', line 8

def initialize(instream, outstream)
  @instream  = instream
  @outstream = outstream
  @command   = ""
  @interact  = Mastermind::Interact.new
end

Instance Attribute Details

#commandObject

Returns the value of attribute command.



5
6
7
# File 'lib/mastermind/cli.rb', line 5

def command
  @command
end

#gameObject (readonly)

Returns the value of attribute game.



6
7
8
# File 'lib/mastermind/cli.rb', line 6

def game
  @game
end

#instreamObject (readonly)

Returns the value of attribute instream.



6
7
8
# File 'lib/mastermind/cli.rb', line 6

def instream
  @instream
end

#interactObject (readonly)

Returns the value of attribute interact.



6
7
8
# File 'lib/mastermind/cli.rb', line 6

def interact
  @interact
end

#outstreamObject (readonly)

Returns the value of attribute outstream.



6
7
8
# File 'lib/mastermind/cli.rb', line 6

def outstream
  @outstream
end

Instance Method Details

#get_commandObject



25
26
27
28
# File 'lib/mastermind/cli.rb', line 25

def get_command
  outstream.print interact.command_prompt
  self.command = instream.gets.strip.upcase
end

#instructions?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/mastermind/cli.rb', line 47

def instructions?
  command == 'I' || command == 'INSTRUCTIONS'
end

#play?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/mastermind/cli.rb', line 43

def play?
  command == 'P' || command == 'PLAY'
end

#process_commandObject



30
31
32
33
34
35
36
37
# File 'lib/mastermind/cli.rb', line 30

def process_command
  case
  when quit?         then outstream.puts interact.print_farewell
  when instructions? then outstream.puts interact.print_instructions(Mastermind.color_option_string(6))
  when play?         then Mastermind::PlayGame.new(instream, outstream, interact).run
  else                    outstream.puts interact.print_invalid(command)
  end
end

#quit?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/mastermind/cli.rb', line 39

def quit?
  command == 'Q' || command == 'QUIT'
end

#runObject



15
16
17
18
19
20
21
22
23
# File 'lib/mastermind/cli.rb', line 15

def run
  outstream.puts interact.screen_clear
  outstream.puts interact.print_title
  outstream.puts interact.print_intro
  until quit?
    get_command
    process_command
  end
end