Class: Mastermind::PlayGame

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(instream, outstream, interact) ⇒ PlayGame

Returns a new instance of PlayGame.



9
10
11
12
13
14
# File 'lib/mastermind/play_game.rb', line 9

def initialize(instream, outstream, interact)
  @instream  = instream
  @outstream = outstream
  @interact  = interact
  @command   = ""
end

Instance Attribute Details

#commandObject

Returns the value of attribute command.



7
8
9
# File 'lib/mastermind/play_game.rb', line 7

def command
  @command
end

#instreamObject

Returns the value of attribute instream.



7
8
9
# File 'lib/mastermind/play_game.rb', line 7

def instream
  @instream
end

#interactObject

Returns the value of attribute interact.



7
8
9
# File 'lib/mastermind/play_game.rb', line 7

def interact
  @interact
end

#outstreamObject

Returns the value of attribute outstream.



7
8
9
# File 'lib/mastermind/play_game.rb', line 7

def outstream
  @outstream
end

Instance Method Details

#get_commandObject



26
27
28
29
# File 'lib/mastermind/play_game.rb', line 26

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

#get_name(player_no) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/mastermind/play_game.rb', line 57

def get_name(player_no)
  name = ""
  until valid_name?(name)
    outstream.print interact.name_prompt(player_no)
    name = instream.gets.strip
  end
  name
end

#player_gen(num_players) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/mastermind/play_game.rb', line 47

def player_gen(num_players)
  players = []
  outstream.puts interact.print_player_intro
  num_players.times do |i|
    name = get_name(i+1)
    players << Player.new(name)
  end
  players
end

#process_commandObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/mastermind/play_game.rb', line 31

def process_command
  case
  when command == "S"
    players = player_gen(1)
    Mastermind::GameRound.new(instream, outstream, interact, players).play
    self.command = "Q"
  when command == "M"
    players = player_gen(2)
    Mastermind::GameRound.new(instream, outstream, interact, players).play
    self.command = "Q"
  when command == "I"
    puts "play-game"
  else                    outstream.puts interact.print_invalid(command)
  end
end

#quit?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/mastermind/play_game.rb', line 70

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

#runObject



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

def run
  outstream.puts interact.print_game_info
  outstream.puts interact.print_game_options
  until quit?
    get_command
    process_command
  end
  outstream.puts interact.print_intro
end

#valid_name?(name) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/mastermind/play_game.rb', line 66

def valid_name?(name)
  name.length > 0 && name.length < 12
end