Class: Play
Instance Attribute Summary collapse
-
#code ⇒ Object
Returns the value of attribute code.
-
#history ⇒ Object
Returns the value of attribute history.
-
#level ⇒ Object
Returns the value of attribute level.
-
#turns ⇒ Object
Returns the value of attribute turns.
-
#win ⇒ Object
Returns the value of attribute win.
Attributes inherited from Message
Instance Method Summary collapse
- #dice ⇒ Object
- #game_over?(string) ⇒ Boolean
-
#initialize ⇒ Play
constructor
A new instance of Play.
- #process ⇒ Object
- #start ⇒ Object
- #to_think(skynet, human) ⇒ Object
Methods inherited from Message
#mess, #print_info, #question, #user_data
Constructor Details
#initialize ⇒ Play
Returns a new instance of Play.
8 9 10 11 12 13 |
# File 'lib/game_codebreaker/play.rb', line 8 def initialize @code = "" @turns = 0 @history = [] @level = 15 end |
Instance Attribute Details
#code ⇒ Object
Returns the value of attribute code.
6 7 8 |
# File 'lib/game_codebreaker/play.rb', line 6 def code @code end |
#history ⇒ Object
Returns the value of attribute history.
6 7 8 |
# File 'lib/game_codebreaker/play.rb', line 6 def history @history end |
#level ⇒ Object
Returns the value of attribute level.
6 7 8 |
# File 'lib/game_codebreaker/play.rb', line 6 def level @level end |
#turns ⇒ Object
Returns the value of attribute turns.
6 7 8 |
# File 'lib/game_codebreaker/play.rb', line 6 def turns @turns end |
#win ⇒ Object
Returns the value of attribute win.
6 7 8 |
# File 'lib/game_codebreaker/play.rb', line 6 def win @win end |
Instance Method Details
#dice ⇒ Object
15 16 17 18 |
# File 'lib/game_codebreaker/play.rb', line 15 def dice 4.times{ @code << rand(1..6).to_s } @code.to_i end |
#game_over?(string) ⇒ Boolean
53 54 55 |
# File 'lib/game_codebreaker/play.rb', line 53 def game_over?( string ) return true if string.count("+") == 4 end |
#process ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/game_codebreaker/play.rb', line 26 def process @level.times do skynet = Array.new(@code.to_s.split(//)) human = question( mess 1 ).split(//) list = [@code, human.join("")] p result = to_think( skynet, human ) @turns += 1; @history << ( list << result ) p @code return @win = true if game_over?( result ) end @win = false end |
#start ⇒ Object
20 21 22 23 24 |
# File 'lib/game_codebreaker/play.rb', line 20 def start dice process Game.new( @code, @turns, @history, @win, @level ) end |
#to_think(skynet, human) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/game_codebreaker/play.rb', line 39 def to_think( skynet, human ) string = "" (0..3).each do |i| if skynet[i] == human[i] string += "+" skynet[i] = nil human[i] = nil end end skynet.compact!; human.compact!; human.each { |val| string += "-" if skynet.include?( val ) } string end |