Class: Codebreaker::Game
- Inherits:
-
Object
- Object
- Codebreaker::Game
- Defined in:
- lib/codebreaker/game.rb
Constant Summary collapse
- MAX_SCORE =
500
- MAX_ROUNDS =
500
- CODE_LENGTH =
4
- ROUND_PENALTY =
10
- HINT_PENALTY =
50
Instance Attribute Summary collapse
-
#game_status ⇒ Object
readonly
Returns the value of attribute game_status.
-
#gues_results ⇒ Object
readonly
Returns the value of attribute gues_results.
-
#hint_val ⇒ Object
readonly
Returns the value of attribute hint_val.
-
#round_number ⇒ Object
readonly
Returns the value of attribute round_number.
Instance Method Summary collapse
- #check(gues) ⇒ Object
- #hint ⇒ Object
-
#initialize ⇒ Game
constructor
A new instance of Game.
- #score ⇒ Object
- #start ⇒ Object
Constructor Details
#initialize ⇒ Game
Returns a new instance of Game.
11 12 13 14 15 16 17 |
# File 'lib/codebreaker/game.rb', line 11 def initialize @secret_code = '' @round_number = 0 @gues_results = {} @game_status = 'play' @hint_val = '' end |
Instance Attribute Details
#game_status ⇒ Object (readonly)
Returns the value of attribute game_status.
9 10 11 |
# File 'lib/codebreaker/game.rb', line 9 def game_status @game_status end |
#gues_results ⇒ Object (readonly)
Returns the value of attribute gues_results.
9 10 11 |
# File 'lib/codebreaker/game.rb', line 9 def gues_results @gues_results end |
#hint_val ⇒ Object (readonly)
Returns the value of attribute hint_val.
9 10 11 |
# File 'lib/codebreaker/game.rb', line 9 def hint_val @hint_val end |
#round_number ⇒ Object (readonly)
Returns the value of attribute round_number.
9 10 11 |
# File 'lib/codebreaker/game.rb', line 9 def round_number @round_number end |
Instance Method Details
#check(gues) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/codebreaker/game.rb', line 25 def check (gues) raise ArgumentError, 'argument length must be equal 4' unless gues.to_s.length == CODE_LENGTH raise ArgumentError, 'argument must contain only numbers from 1 to 6' unless gues.to_s[/[1-6]+/].length == CODE_LENGTH pluses = minuses = '' sc_array = @secret_code.split('') gues_array = gues.to_s.split('') gues_array.each_with_index do |val, index| if (val == sc_array[index]) sc_array[index] = '0' gues_array[index] = '+' pluses += '+' end end gues_array.each do |val| if (sc_array.include?(val)) sc_array[sc_array.find_index(val)] = '-' minuses += '-' end end result = pluses + minuses @gues_results[gues.to_s] = result @round_number += 1 @game_status = 'win' if result == '++++' @game_status = 'loose' if @round_number >= MAX_ROUNDS result end |
#hint ⇒ Object
55 56 57 58 59 60 61 62 63 64 |
# File 'lib/codebreaker/game.rb', line 55 def hint if @hint_val.to_s.empty? @hint_val = '' hint_pos = Random.rand(CODE_LENGTH); hint_pos.times {@hint_val += '*'} @hint_val += @secret_code[hint_pos].to_s (CODE_LENGTH - hint_pos - 1).times {@hint_val += '*'} end @hint_val end |
#score ⇒ Object
66 67 68 69 70 |
# File 'lib/codebreaker/game.rb', line 66 def score hints_used = 0 hints_used = 1 unless @hint_val.empty? MAX_SCORE - hints_used*HINT_PENALTY - @round_number*ROUND_PENALTY end |
#start ⇒ Object
19 20 21 22 23 |
# File 'lib/codebreaker/game.rb', line 19 def start CODE_LENGTH.times { @secret_code += (Random.rand(6) + 1).to_s } if @secret_code.empty? end |