Class: RubyAI
- Inherits:
-
Gosu::Window
- Object
- Gosu::Window
- RubyAI
- Defined in:
- lib/ruby-ai.rb
Constant Summary collapse
- COLORS =
{ :player => [1.0, 0.5764706134796143, 0.0, 1.0], #orange, paintbrush 'tangerine' :goal => [0.0, 0.9764706492424011, 0.0, 1.0], #green, paintbrush 'spring' :wall => [0.5803921818733215, 0.32156863808631897, 0.0, 1.0], #brown, paintbrush 'mocha' :empty => [0.0, 0.0, 0.0, 1.0], #black, paintbrush 'licorice' }
- DIMS =
{:x => 64, :y => 64}
Instance Attribute Summary collapse
-
#tiles ⇒ Object
readonly
Returns the value of attribute tiles.
Instance Method Summary collapse
- #add_tile(pixel, x, y) ⇒ Object
- #button_down(id) ⇒ Object
- #centering_modifiers(image_dims) ⇒ Object
- #draw ⇒ Object
-
#initialize(level) ⇒ RubyAI
constructor
A new instance of RubyAI.
- #load_level ⇒ Object
- #pixel_equal?(pixel, values) ⇒ Boolean
- #rotate90!(dims, coords) ⇒ Object
- #rough_equal?(float1, float2) ⇒ Boolean
- #update ⇒ Object
- #won? ⇒ Boolean
Constructor Details
#initialize(level) ⇒ RubyAI
Returns a new instance of RubyAI.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/ruby-ai.rb', line 24 def initialize(level) super(DIMS[:x] * 10, DIMS[:y] * 10, false) self.caption = 'Ruby AI' @drawn = [] @ui = UI.new(self) @drawn << @ui @tiles = [] @level = level load_level @ai = AI.new(@player) unless @ai.respond_to?('level', "#{@level}") puts "Your AI class must define a method 'level(num)'" @player.stop = true end @ai_tick = 0.5 @last_ai_update = Time.now end |
Instance Attribute Details
#tiles ⇒ Object (readonly)
Returns the value of attribute tiles.
13 14 15 |
# File 'lib/ruby-ai.rb', line 13 def tiles @tiles end |
Instance Method Details
#add_tile(pixel, x, y) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/ruby-ai.rb', line 103 def add_tile(pixel, x, y) if pixel_equal?(pixel, COLORS[:wall]) wall = Wall.new(self, {:x => x, :y => y}) @tiles << wall @drawn << wall elsif pixel_equal?(pixel, COLORS[:player]) @player = Player.new(self, {:x => x, :y => y}) @drawn << @player empty = Empty.new(self, {:x => x, :y => y}) @tiles << empty elsif pixel_equal?(pixel, COLORS[:goal]) @goal = Goal.new(self, {:x => x, :y => y}) @tiles << @goal @drawn << @goal elsif pixel_equal?(pixel, COLORS[:empty]) empty = Empty.new(self, {:x => x, :y => y}) @tiles << empty end end |
#button_down(id) ⇒ Object
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/ruby-ai.rb', line 174 def (id) if id == Gosu::KbEscape close elsif id == Gosu::KbReturn if @player.stop || @won exit end if @player.pause @player.pause = false else @player.pause = true end elsif id == Gosu::KbDown @ai_tick = @ai_tick * 2 unless @ai_tick > 1 elsif id == Gosu::KbUp @ai_tick = @ai_tick / 2 unless @ai_tick < 0.01 end end |
#centering_modifiers(image_dims) ⇒ Object
86 87 88 89 90 91 92 93 94 95 |
# File 'lib/ruby-ai.rb', line 86 def centering_modifiers(image_dims) mods = {:x => 0, :y => 0} [:x, :y].each do |dim| if image_dims[dim] < DIMS[dim] diff = DIMS[dim]- image_dims[dim] mods[dim] = diff / 2 end end mods end |
#draw ⇒ Object
170 171 172 |
# File 'lib/ruby-ai.rb', line 170 def draw @drawn.each {|o| o.send(:draw)} end |
#load_level ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/ruby-ai.rb', line 46 def load_level level_image = Gosu::Image.new(self, "#{Dir.pwd}/levels/level#{@level}.png", false) puts "Loading #{level_image.width * level_image.height} tiles." image_dims = {:x => level_image.width, :y => level_image.height} reverse = {:x => [true, false].sample, :y => [true, false].sample} # reverse = {:x => false, :y => false} rotate_by = [0, 90, 180, 270].sample # rotate_by = 270 count = 0 coords = {} mods = centering_modifiers(image_dims) image_dims[:x].times do |x| image_dims[:y].times do |y| count += 1 if count > 99 print '.' count = 0 end coords[:x] = x coords[:y] = y [:x, :y].each do |dim| coords[dim] = image_dims[dim] - 1 - coords[dim] if reverse[dim] end (rotate_by / 90).times do rotate90!(image_dims, coords) end pixel = level_image.get_pixel(x, y) add_tile(pixel, (coords[:x] + mods[:x]) * 10, (coords[:y] + mods[:y]) * 10) end end puts 'Done.' end |
#pixel_equal?(pixel, values) ⇒ Boolean
124 125 126 127 128 129 |
# File 'lib/ruby-ai.rb', line 124 def pixel_equal?(pixel, values) 4.times do |i| return false unless rough_equal?(pixel[i], values[i]) end true end |
#rotate90!(dims, coords) ⇒ Object
97 98 99 100 101 |
# File 'lib/ruby-ai.rb', line 97 def rotate90!(dims, coords) old = coords[:y] coords[:y] = coords[:x] coords[:x] = dims[:y] - 1 - old end |
#rough_equal?(float1, float2) ⇒ Boolean
131 132 133 134 135 |
# File 'lib/ruby-ai.rb', line 131 def rough_equal?(float1, float2) one = (float1 * 100).to_i two = (float2 * 100).to_i one == two end |
#update ⇒ Object
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/ruby-ai.rb', line 137 def update if won? @ui.text = 'Success! Press <enter> to exit.' return end if @player.stop @ui.text = 'Game over. Press <enter> to exit.' return end if @player.pause @ui.text = 'Paused. Press <enter> to resume.' return end @ui.text = '' if Time.now - @last_ai_update > @ai_tick @last_ai_update = Time.now @player.send(:moved=, false) @ai.level(@level.to_i) end end |
#won? ⇒ Boolean
162 163 164 165 166 167 168 |
# File 'lib/ruby-ai.rb', line 162 def won? return true if @won if @player.x == @goal.x && @player.y == @goal.y puts 'Success!' @won = true end end |