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.
-
#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: []) ⇒ 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: []) ⇒ Game
Returns a new instance of Game.
10 11 12 13 14 15 16 17 18 19 20 |
# 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: [] ) @code, @length = code, length @level, @turns = level, turns @history, @hint = history, hint @hinted, @win = hinted, win @hints, @game_over = hints, game_over 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 |
#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
44 45 46 47 |
# File 'lib/game_codebreaker/game.rb', line 44 def check_game( string ) @game_over = true if @turns == @level (@win = true; @game_over = true) if string.count("+") == @length end |
#game_over? ⇒ Boolean
49 50 51 |
# File 'lib/game_codebreaker/game.rb', line 49 def game_over? @game_over end |
#get_hint ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/game_codebreaker/game.rb', line 32 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
22 23 24 25 26 27 28 29 30 |
# File 'lib/game_codebreaker/game.rb', line 22 def respond( string ) return if game_over? skynet = Array.new(@code.split(//)) human = string.split(//) list = [@code, human.join("")] result = process( skynet, human ) check_game( result ) @turns += 1; @history << ( list << result ) end |
#win? ⇒ Boolean
53 54 55 |
# File 'lib/game_codebreaker/game.rb', line 53 def win? @win end |