Class: GameCodebreaker::Game
- Inherits:
-
Object
- Object
- GameCodebreaker::Game
- Defined in:
- lib/game_codebreaker/game.rb
Constant Summary collapse
- JUST_A_MAGIC_NUMBER =
10
Instance Attribute Summary collapse
-
#code ⇒ Object
readonly
Returns the value of attribute code.
-
#game_over ⇒ Object
readonly
Returns the value of attribute game_over.
-
#hint ⇒ Object
readonly
Returns the value of attribute hint.
-
#hints ⇒ Object
readonly
Returns the value of attribute hints.
-
#history ⇒ Object
readonly
Returns the value of attribute history.
-
#length ⇒ Object
readonly
Returns the value of attribute length.
-
#level ⇒ Object
readonly
Returns the value of attribute level.
-
#level_name ⇒ Object
readonly
Returns the value of attribute level_name.
-
#turns ⇒ Object
readonly
Returns the value of attribute turns.
-
#win ⇒ Object
readonly
Returns the value of attribute win.
Instance Method Summary collapse
- #check_game(string) ⇒ Object
- #game_over? ⇒ Boolean
- #get_hint ⇒ Object
-
#initialize(code: "", length: 4, level: (Options.level)[:level], turns: 0, history: [], hint: , hinted: 0, win: false, game_over: false, hints: [], level_name: 'low') ⇒ Game
constructor
A new instance of Game.
- #respond(string) ⇒ Object
- #win? ⇒ Boolean
Constructor Details
#initialize(code: "", length: 4, level: (Options.level)[:level], turns: 0, history: [], hint: , hinted: 0, win: false, game_over: false, hints: [], level_name: 'low') ⇒ Game
Returns a new instance of Game.
10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/game_codebreaker/game.rb', line 10 def initialize( code: "", length: 4, level: (Options.level)[:level], turns: 0, history: [], hint: (Options.level)[:hint], hinted: 0, win: false, game_over: false, hints: [], level_name: 'low' ) @code, @length = code, length @level, @turns = level, turns @history, @hint = history, hint @hinted, @win, = hinted, win @hints, @game_over = hints, game_over @level_name = level_name generate_code if @code == "" raise ArgumentError, 'Length code must be equal to variable with name \'length\'' if @code.size != @length end |
Instance Attribute Details
#code ⇒ Object (readonly)
Returns the value of attribute code.
8 9 10 |
# File 'lib/game_codebreaker/game.rb', line 8 def code @code end |
#game_over ⇒ Object (readonly)
Returns the value of attribute game_over.
8 9 10 |
# File 'lib/game_codebreaker/game.rb', line 8 def game_over @game_over end |
#hint ⇒ Object (readonly)
Returns the value of attribute hint.
8 9 10 |
# File 'lib/game_codebreaker/game.rb', line 8 def hint @hint end |
#hints ⇒ Object (readonly)
Returns the value of attribute hints.
8 9 10 |
# File 'lib/game_codebreaker/game.rb', line 8 def hints @hints end |
#history ⇒ Object (readonly)
Returns the value of attribute history.
8 9 10 |
# File 'lib/game_codebreaker/game.rb', line 8 def history @history end |
#length ⇒ Object (readonly)
Returns the value of attribute length.
8 9 10 |
# File 'lib/game_codebreaker/game.rb', line 8 def length @length end |
#level ⇒ Object (readonly)
Returns the value of attribute level.
8 9 10 |
# File 'lib/game_codebreaker/game.rb', line 8 def level @level end |
#level_name ⇒ Object (readonly)
Returns the value of attribute level_name.
8 9 10 |
# File 'lib/game_codebreaker/game.rb', line 8 def level_name @level_name end |
#turns ⇒ Object (readonly)
Returns the value of attribute turns.
8 9 10 |
# File 'lib/game_codebreaker/game.rb', line 8 def turns @turns end |
#win ⇒ Object (readonly)
Returns the value of attribute win.
8 9 10 |
# File 'lib/game_codebreaker/game.rb', line 8 def win @win end |
Instance Method Details
#check_game(string) ⇒ Object
45 46 47 48 |
# File 'lib/game_codebreaker/game.rb', line 45 def check_game( string ) @game_over = true if @turns == @level (@win = true; @game_over = true) if string.count("+") == @length end |
#game_over? ⇒ Boolean
50 51 52 |
# File 'lib/game_codebreaker/game.rb', line 50 def game_over? @game_over end |
#get_hint ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/game_codebreaker/game.rb', line 33 def get_hint ( @hints << "there is no hints anymore"; @hints.uniq!; return ) if @hint == 0 pozition = nil; old_pozitions = [] @hints.each do |str| str.split(//).each_with_index { |word, index| old_pozitions << index unless word.to_i == 0 } end ( @length*JUST_A_MAGIC_NUMBER ).times { pozition = rand( @length ); break unless old_pozitions.include?( pozition ) } result = "" @length.times { |i| pozition == i ? result << @code.to_s[i] : result << "-" } @hint -= 1; @hints << result end |
#respond(string) ⇒ Object
23 24 25 26 27 28 29 30 31 |
# File 'lib/game_codebreaker/game.rb', line 23 def respond( string ) return if game_over? skynet = Array.new(@code.split(//)) human = string.split(//) list = [@code, human.join("")] result = process( skynet, human ) @turns += 1; @history << ( list << result ) check_game( result ) end |
#win? ⇒ Boolean
54 55 56 |
# File 'lib/game_codebreaker/game.rb', line 54 def win? @win end |