Class: Codebreaker::Game
- Inherits:
-
Object
- Object
- Codebreaker::Game
- Defined in:
- lib/codebreaker.rb
Instance Attribute Summary collapse
-
#count_of_try ⇒ Object
readonly
Returns the value of attribute count_of_try.
-
#game_status ⇒ Object
readonly
Returns the value of attribute game_status.
-
#is_hint_used ⇒ Object
readonly
Returns the value of attribute is_hint_used.
-
#num_of_try ⇒ Object
readonly
Returns the value of attribute num_of_try.
Class Method Summary collapse
Instance Method Summary collapse
- #hint ⇒ Object
-
#initialize ⇒ Game
constructor
A new instance of Game.
- #match_secret_code(user_code) ⇒ Object
- #save_result(name) ⇒ Object
- #start ⇒ Object
Constructor Details
#initialize ⇒ Game
Returns a new instance of Game.
8 9 10 11 12 13 14 |
# File 'lib/codebreaker.rb', line 8 def initialize @secret_code = nil @count_of_try = nil @game_status = nil @num_of_try = nil @game_status = nil end |
Instance Attribute Details
#count_of_try ⇒ Object (readonly)
Returns the value of attribute count_of_try.
6 7 8 |
# File 'lib/codebreaker.rb', line 6 def count_of_try @count_of_try end |
#game_status ⇒ Object (readonly)
Returns the value of attribute game_status.
6 7 8 |
# File 'lib/codebreaker.rb', line 6 def game_status @game_status end |
#is_hint_used ⇒ Object (readonly)
Returns the value of attribute is_hint_used.
6 7 8 |
# File 'lib/codebreaker.rb', line 6 def is_hint_used @is_hint_used end |
#num_of_try ⇒ Object (readonly)
Returns the value of attribute num_of_try.
6 7 8 |
# File 'lib/codebreaker.rb', line 6 def num_of_try @num_of_try end |
Class Method Details
.load_results ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/codebreaker.rb', line 60 def self.load_results res = [] Psych.load_stream(File.read('../../data/results.yml')) do |item| res << item end return res.map do |item| "#{item[:user_name]}:\ \n\tgame status: #{item[:game_status]}\ \n\tcount of try: #{item[:count_of_try]}\ \n\tis hint used: #{item[:is_hint_used]}\n\n" end.join if res res end |
Instance Method Details
#hint ⇒ Object
42 43 44 45 46 47 48 |
# File 'lib/codebreaker.rb', line 42 def hint unless (@is_hint_used) @is_hint_used = true return @secret_code.sample end 'hint already used' end |
#match_secret_code(user_code) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/codebreaker.rb', line 24 def match_secret_code user_code return 'Game Over' if @game_status == 'lose' return 'You win!' if @game_status == 'win' return 'Wrong format (input 4 digit without spaces). Try again' unless is_right_code? user_code user_code = user_code.chars.map(&:to_i) @num_of_try += 1 if user_code == @secret_code @game_status = "win" return "++++ You win!!!" end res = match_codes user_code if @num_of_try == @count_of_try @game_status = "lose" res += ' Game Over' end res end |
#save_result(name) ⇒ Object
50 51 52 53 54 55 56 57 58 |
# File 'lib/codebreaker.rb', line 50 def save_result(name) res = {} res[:user_name] = name res[:game_status] = @game_status res[:count_of_try] = @num_of_try res[:is_hint_used] = @is_hint_used File.open('../../data/results.yml', 'a') {|f| f.write(res.to_yaml) } end |
#start ⇒ Object
16 17 18 19 20 21 22 |
# File 'lib/codebreaker.rb', line 16 def start @secret_code = (1..4).map { rand(1..6) } @num_of_try = 0 @count_of_try = rand(10..20) @is_hint_used = false @game_status = nil end |