Class: Mastermind::PlayGame
- Inherits:
-
Object
- Object
- Mastermind::PlayGame
- Defined in:
- lib/mastermind/play_game.rb
Instance Attribute Summary collapse
-
#command ⇒ Object
Returns the value of attribute command.
-
#instream ⇒ Object
Returns the value of attribute instream.
-
#interact ⇒ Object
Returns the value of attribute interact.
-
#outstream ⇒ Object
Returns the value of attribute outstream.
Instance Method Summary collapse
- #get_command ⇒ Object
- #get_name(player_no) ⇒ Object
-
#initialize(instream, outstream, interact) ⇒ PlayGame
constructor
A new instance of PlayGame.
- #player_gen(num_players) ⇒ Object
- #process_command ⇒ Object
- #quit? ⇒ Boolean
- #run ⇒ Object
- #valid_name?(name) ⇒ Boolean
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
#command ⇒ Object
Returns the value of attribute command.
7 8 9 |
# File 'lib/mastermind/play_game.rb', line 7 def command @command end |
#instream ⇒ Object
Returns the value of attribute instream.
7 8 9 |
# File 'lib/mastermind/play_game.rb', line 7 def instream @instream end |
#interact ⇒ Object
Returns the value of attribute interact.
7 8 9 |
# File 'lib/mastermind/play_game.rb', line 7 def interact @interact end |
#outstream ⇒ Object
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_command ⇒ Object
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_command ⇒ Object
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
70 71 72 |
# File 'lib/mastermind/play_game.rb', line 70 def quit? command == "Q" || command == "QUIT" end |
#run ⇒ Object
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. until quit? get_command process_command end outstream.puts interact.print_intro end |
#valid_name?(name) ⇒ Boolean
66 67 68 |
# File 'lib/mastermind/play_game.rb', line 66 def valid_name?(name) name.length > 0 && name.length < 12 end |