Class: Mastermind::GameRound
- Inherits:
-
Object
- Object
- Mastermind::GameRound
- Defined in:
- lib/mastermind/game_round.rb
Instance Attribute Summary collapse
-
#command ⇒ Object
Returns the value of attribute command.
-
#guesses ⇒ Object
Returns the value of attribute guesses.
-
#instream ⇒ Object
Returns the value of attribute instream.
-
#interact ⇒ Object
Returns the value of attribute interact.
-
#max_guesses ⇒ Object
Returns the value of attribute max_guesses.
-
#outstream ⇒ Object
Returns the value of attribute outstream.
-
#players ⇒ Object
Returns the value of attribute players.
-
#round_over ⇒ Object
Returns the value of attribute round_over.
-
#secret ⇒ Object
Returns the value of attribute secret.
-
#start_time ⇒ Object
Returns the value of attribute start_time.
-
#valid_colors ⇒ Object
Returns the value of attribute valid_colors.
Instance Method Summary collapse
- #color_string ⇒ Object
- #correct_color(player) ⇒ Object
- #correct_guess?(player) ⇒ Boolean
- #correct_pos(player) ⇒ Object
- #game_round_over? ⇒ Boolean
- #get_secret(player) ⇒ Object
- #guess(player) ⇒ Object
- #guesses_remaining?(player) ⇒ Boolean
-
#initialize(instream, outstream, interact, players) ⇒ GameRound
constructor
A new instance of GameRound.
- #num_guesses(player) ⇒ Object
- #out_of_guesses(player) ⇒ Object
- #play ⇒ Object
- #player_reset ⇒ Object
- #player_round_over!(player) ⇒ Object
- #player_turn ⇒ Object
- #process_command(player) ⇒ Object
- #quit?(player) ⇒ Boolean
- #quit_confirm(player) ⇒ Object
- #round_over? ⇒ Boolean
- #secret_gen ⇒ Object
- #set_turn_pos ⇒ Object
- #single_player? ⇒ Boolean
- #valid_guess?(command, secret, valid_colors) ⇒ Boolean
- #win!(player) ⇒ Object
Constructor Details
#initialize(instream, outstream, interact, players) ⇒ GameRound
Returns a new instance of GameRound.
19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/mastermind/game_round.rb', line 19 def initialize(instream, outstream, interact, players) @instream = instream @outstream = outstream @interact = interact @valid_colors = Mastermind::Processor.colors(6) @max_guesses = 12 @command = "" @guesses = [] @round_over = false @start_time = Time.now @players = players end |
Instance Attribute Details
#command ⇒ Object
Returns the value of attribute command.
6 7 8 |
# File 'lib/mastermind/game_round.rb', line 6 def command @command end |
#guesses ⇒ Object
Returns the value of attribute guesses.
6 7 8 |
# File 'lib/mastermind/game_round.rb', line 6 def guesses @guesses end |
#instream ⇒ Object
Returns the value of attribute instream.
6 7 8 |
# File 'lib/mastermind/game_round.rb', line 6 def instream @instream end |
#interact ⇒ Object
Returns the value of attribute interact.
6 7 8 |
# File 'lib/mastermind/game_round.rb', line 6 def interact @interact end |
#max_guesses ⇒ Object
Returns the value of attribute max_guesses.
6 7 8 |
# File 'lib/mastermind/game_round.rb', line 6 def max_guesses @max_guesses end |
#outstream ⇒ Object
Returns the value of attribute outstream.
6 7 8 |
# File 'lib/mastermind/game_round.rb', line 6 def outstream @outstream end |
#players ⇒ Object
Returns the value of attribute players.
6 7 8 |
# File 'lib/mastermind/game_round.rb', line 6 def players @players end |
#round_over ⇒ Object
Returns the value of attribute round_over.
6 7 8 |
# File 'lib/mastermind/game_round.rb', line 6 def round_over @round_over end |
#secret ⇒ Object
Returns the value of attribute secret.
6 7 8 |
# File 'lib/mastermind/game_round.rb', line 6 def secret @secret end |
#start_time ⇒ Object
Returns the value of attribute start_time.
6 7 8 |
# File 'lib/mastermind/game_round.rb', line 6 def start_time @start_time end |
#valid_colors ⇒ Object
Returns the value of attribute valid_colors.
6 7 8 |
# File 'lib/mastermind/game_round.rb', line 6 def valid_colors @valid_colors end |
Instance Method Details
#color_string ⇒ Object
103 104 105 |
# File 'lib/mastermind/game_round.rb', line 103 def color_string Mastermind.color_option_string(6) end |
#correct_color(player) ⇒ Object
160 161 162 |
# File 'lib/mastermind/game_round.rb', line 160 def correct_color(player) Mastermind::Processor.num_correct_colors(player.command, player.secret) end |
#correct_guess?(player) ⇒ Boolean
127 128 129 |
# File 'lib/mastermind/game_round.rb', line 127 def correct_guess?(player) player.command.chars == player.secret end |
#correct_pos(player) ⇒ Object
156 157 158 |
# File 'lib/mastermind/game_round.rb', line 156 def correct_pos(player) Mastermind::Processor.num_correct_pos(player.command, player.secret) end |
#game_round_over? ⇒ Boolean
116 117 118 |
# File 'lib/mastermind/game_round.rb', line 116 def game_round_over? round_over end |
#get_secret(player) ⇒ Object
75 76 77 78 79 80 81 82 83 84 |
# File 'lib/mastermind/game_round.rb', line 75 def get_secret(player) outstream.puts interact.print_player_secret_intro(player.name) code = "yyyy" until valid_guess?(code, player.secret, valid_colors) outstream.print interact.secret_guess_prompt code = instream.noecho(&:gets).strip.upcase outstream.puts end code.chars end |
#guess(player) ⇒ Object
144 145 146 147 148 149 150 |
# File 'lib/mastermind/game_round.rb', line 144 def guess(player) player.guesses << player.command if correct_guess?(player) then win!(player) elsif !guesses_remaining?(player) then out_of_guesses(player) else outstream.puts interact.print_guess_stats(num_guesses(player), correct_pos(player), correct_color(player), player.command, max_guesses, player) end end |
#guesses_remaining?(player) ⇒ Boolean
131 132 133 |
# File 'lib/mastermind/game_round.rb', line 131 def guesses_remaining?(player) num_guesses(player) < max_guesses end |
#num_guesses(player) ⇒ Object
152 153 154 |
# File 'lib/mastermind/game_round.rb', line 152 def num_guesses(player) player.guesses.length end |
#out_of_guesses(player) ⇒ Object
139 140 141 142 |
# File 'lib/mastermind/game_round.rb', line 139 def out_of_guesses(player) outstream.puts interact.print_out_of_guesses(num_guesses(player), correct_pos(player), correct_color(player), player.command, max_guesses, player) player_round_over!(player) end |
#play ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/mastermind/game_round.rb', line 32 def play player_reset outstream.puts interact.print_round_intro(color_string) secret_gen set_turn_pos outstream.puts interact.multi_player_div if !single_player? until round_over? || game_round_over? player_turn end @round_over = true end |
#player_reset ⇒ Object
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/mastermind/game_round.rb', line 44 def player_reset @players.each do |player| player.secret = ["X", "X", "X", "X"] player.round_over = false player.guesses = [] player.start_time = Time.now player.completion_time = nil player.turn_pos = nil end end |
#player_round_over!(player) ⇒ Object
107 108 109 110 |
# File 'lib/mastermind/game_round.rb', line 107 def player_round_over!(player) player.round_over = true outstream.puts interact.print_round_over if round_over? end |
#player_turn ⇒ Object
55 56 57 58 59 60 61 62 63 |
# File 'lib/mastermind/game_round.rb', line 55 def player_turn @players.each do |player| if player.round_over == false && !game_round_over? outstream.print interact.guess_prompt(player) player.command = instream.gets.strip.upcase process_command(player) end end end |
#process_command(player) ⇒ Object
91 92 93 94 95 96 97 |
# File 'lib/mastermind/game_round.rb', line 91 def process_command(player) case when quit?(player) then quit_confirm(player) when !valid_guess?(player.command, player.secret, valid_colors) then outstream.puts interact.print_invalid_guess(player) when valid_guess?(player.command, player.secret, valid_colors) then guess(player) end end |
#quit?(player) ⇒ Boolean
135 136 137 |
# File 'lib/mastermind/game_round.rb', line 135 def quit?(player) player.command == "Q" || player.command == "QUIT" end |
#quit_confirm(player) ⇒ Object
168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/mastermind/game_round.rb', line 168 def quit_confirm(player) outstream.puts interact.print_are_you_sure outstream.print interact.command_prompt confirmation = instream.gets.strip.upcase case confirmation when "Y", "YES" self.round_over = true player.command = "Q" else player.command = "" end end |
#round_over? ⇒ Boolean
112 113 114 |
# File 'lib/mastermind/game_round.rb', line 112 def round_over? !@players.any? {|player| player.round_over == false} end |
#secret_gen ⇒ Object
65 66 67 68 69 70 71 72 73 |
# File 'lib/mastermind/game_round.rb', line 65 def secret_gen if single_player? then @players.first.secret = Mastermind::Processor.secret(4, 6) else @players.shuffle outstream.puts interact.print_get_secret @players.last.secret = get_secret(@players.first) @players.first.secret = get_secret(@players.last) end end |
#set_turn_pos ⇒ Object
86 87 88 89 |
# File 'lib/mastermind/game_round.rb', line 86 def set_turn_pos @players[0].turn_pos = 0 @players[1].turn_pos = 1 if @players[1] end |
#single_player? ⇒ Boolean
99 100 101 |
# File 'lib/mastermind/game_round.rb', line 99 def single_player? @players.length == 1 end |
#valid_guess?(command, secret, valid_colors) ⇒ Boolean
164 165 166 |
# File 'lib/mastermind/game_round.rb', line 164 def valid_guess?(command, secret, valid_colors) Mastermind::Processor.validate(command, secret, valid_colors) end |
#win!(player) ⇒ Object
120 121 122 123 124 125 |
# File 'lib/mastermind/game_round.rb', line 120 def win!(player) time = Time.now - player.start_time player.completion_time = time outstream.puts interact.print_win(num_guesses(player), time.round, player.secret, player) player_round_over!(player) end |