Class: Sokoban::Level

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lnum = 1) ⇒ Level

Returns a new instance of Level.



131
132
133
134
135
136
137
138
139
140
# File 'lib/sokoban.rb', line 131

def initialize(lnum = 1)
  unless lnum.between?(1, 50)
    raise RangeError, "Level out of range. Choose 1-50"
  end
  @lnum = lnum
  @moves = []
  @level = LEVELS[@lnum - 1].ljust(19 * 16)
  @state = [@level.dup]
  @pushes = 0
end

Instance Attribute Details

#lnumObject (readonly)

Returns the value of attribute lnum.



129
130
131
# File 'lib/sokoban.rb', line 129

def lnum
  @lnum
end

#pushesObject (readonly)

Returns the value of attribute pushes.



129
130
131
# File 'lib/sokoban.rb', line 129

def pushes
  @pushes
end

Instance Method Details

#free_boxesObject



180
181
182
# File 'lib/sokoban.rb', line 180

def free_boxes
  @level.scan(/o/).size
end

#inspectObject



193
194
195
196
# File 'lib/sokoban.rb', line 193

def inspect
  s = "Level: #{@lnum} Moves: #{moves} Pushes: #{@pushes} "
  s << @moves.join
end

#load_gameObject



205
206
207
208
209
# File 'lib/sokoban.rb', line 205

def load_game
  file = Zlib::GzipReader.open(SAV_FILE) {|f| f.read }
  initialize(file.slice!(/\d+/).to_i)  # Init @lnum
  file.each_char {|c| move(c.to_sym) } # Rebuild @moves
end

#movesObject



184
185
186
# File 'lib/sokoban.rb', line 184

def moves
  @moves.size
end

#parse_move(input) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/sokoban.rb', line 164

def parse_move(input)
  # Handle multiple moves
  input.each_char {|c| parse_move(c) } if input.size > 1
  case input.downcase
  when 'b'; $batch = ! $batch
  when 'w', 'k', '8'; move(:W) # up
  when 'a', 'h', '4'; move(:A) # left
  when 's', 'j', '2'; move(:S) # down
  when 'd', 'l', '6'; move(:D) # right
  when 'r'; initialize(@lnum)
  when 'u'; undo
  when 'q'; quit
  when "\u0003"; puts; exit # ^C - Quit without save 
  end
end

#playObject



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/sokoban.rb', line 142

def play
  while free_boxes > 0
    print self
    if $batch
      # Input a string terminated by newline
      parse_move(gets.strip)
    else
      # Handle single key press
      parse_move(STDIN.getch)
    end
    if $hax
      break if @moves.size > 3 # Hax
    end
  end
  print self
  print "Level #{@lnum} complete! "
  unless @lnum == 50
    print "Congratulations, on to level #{@lnum + 1}. "
  end
  STDIN.getch
end

#restart(lnum) ⇒ Object

The public interface to initialize()



189
190
191
# File 'lib/sokoban.rb', line 189

def restart(lnum)
  initialize(lnum)
end

#to_sObject



198
199
200
201
202
203
# File 'lib/sokoban.rb', line 198

def to_s
  clear_screen
  s = "Level: #{@lnum}\n\n"
  s << (0...16).map {|i| @level[i * 19, 19] }.join("\n")
  s << "\nMoves: #{moves} Pushes: #{@pushes}\n> "
end