Class: GameCodebreaker::Game

Inherits:
Object
  • Object
show all
Defined in:
lib/game_codebreaker/game.rb

Constant Summary collapse

JUST_A_MAGIC_NUMBER =
10

Instance Attribute Summary collapse

Instance Method Summary collapse

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.

Raises:

  • (ArgumentError)


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

#codeObject (readonly)

Returns the value of attribute code.



8
9
10
# File 'lib/game_codebreaker/game.rb', line 8

def code
  @code
end

#game_overObject (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

#hintObject (readonly)

Returns the value of attribute hint.



8
9
10
# File 'lib/game_codebreaker/game.rb', line 8

def hint
  @hint
end

#hintsObject (readonly)

Returns the value of attribute hints.



8
9
10
# File 'lib/game_codebreaker/game.rb', line 8

def hints
  @hints
end

#historyObject (readonly)

Returns the value of attribute history.



8
9
10
# File 'lib/game_codebreaker/game.rb', line 8

def history
  @history
end

#lengthObject (readonly)

Returns the value of attribute length.



8
9
10
# File 'lib/game_codebreaker/game.rb', line 8

def length
  @length
end

#levelObject (readonly)

Returns the value of attribute level.



8
9
10
# File 'lib/game_codebreaker/game.rb', line 8

def level
  @level
end

#level_nameObject (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

#turnsObject (readonly)

Returns the value of attribute turns.



8
9
10
# File 'lib/game_codebreaker/game.rb', line 8

def turns
  @turns
end

#winObject (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

Returns:

  • (Boolean)


50
51
52
# File 'lib/game_codebreaker/game.rb', line 50

def game_over?
  @game_over
end

#get_hintObject



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

Returns:

  • (Boolean)


54
55
56
# File 'lib/game_codebreaker/game.rb', line 54

def win?
  @win
end