Class: Sokoban1::Sokoban

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

Constant Summary collapse

WALL =
"#"
OPEN_FLOOR =
" "
MAN =
"@"
CRATE =
"o"
STORAGE =
"."
MAN_ON_STORAGE =
"+"
CRATE_ON_STORAGE =
"*"
MAX_UNDO =
10
PATH =
File.expand_path(File.dirname(__FILE__))

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file = File.join(PATH, "sokoban_levels.txt")) ⇒ Sokoban

Returns a new instance of Sokoban.



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/sokoban/game_engine.rb', line 34

def initialize( file = File.join(PATH, "sokoban_levels.txt") )
  @level_file = file

  @board = [ ]
  @level = 0
  @over = false

  @undos = [ ]
  @moves = 0

  load_level
end

Instance Attribute Details

#levelObject (readonly)

Returns the value of attribute level.



21
22
23
# File 'lib/sokoban/game_engine.rb', line 21

def level
  @level
end

#movesObject (readonly)

Returns the value of attribute moves.



21
22
23
# File 'lib/sokoban/game_engine.rb', line 21

def moves
  @moves
end

Class Method Details

.load(file = File.join(PATH, "sokoban_saved_game.yaml")) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/sokoban/game_engine.rb', line 23

def self.load( file = File.join(PATH, "sokoban_saved_game.yaml") )
  game = nil

  File.open file do |f|
    game = YAML.load(f)
  end

  game ||= Sokoban.new
  game
end

Instance Method Details

#can_move_down?Boolean

Returns:

  • (Boolean)


47
# File 'lib/sokoban/game_engine.rb', line 47

def can_move_down?( )	can_move? :down		end

#can_move_left?Boolean

Returns:

  • (Boolean)


48
# File 'lib/sokoban/game_engine.rb', line 48

def can_move_left?( )	can_move? :left		end

#can_move_right?Boolean

Returns:

  • (Boolean)


49
# File 'lib/sokoban/game_engine.rb', line 49

def can_move_right?( )	can_move? :right	end

#can_move_up?Boolean

Returns:

  • (Boolean)


50
# File 'lib/sokoban/game_engine.rb', line 50

def can_move_up?( )		can_move? :up		end

#displayObject



52
53
54
# File 'lib/sokoban/game_engine.rb', line 52

def display
  @board.inject("") { |dis, row| dis + row.join + "\n" }
end

#level_solved?Boolean

Returns:

  • (Boolean)


56
57
58
59
60
61
62
63
# File 'lib/sokoban/game_engine.rb', line 56

def level_solved?
  @board.each_with_index do |row, y|
    row.each_with_index do |cell, x|
      return false if cell == CRATE
    end
  end
  true
end

#load_level(level = @level += 1, file = @level_file) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/sokoban/game_engine.rb', line 65

def load_level( level = @level += 1, file = @level_file )
  loaded = false

  File.open file do |f|
    count = 0
    while lvl = f.gets("")
      count += 1
      if count == level
        @board = [ ]
        lvl.chomp!
        lvl.each_line { |e| @board << e.chomp.split("") }
        loaded =  true
        break
      end
    end
  end

  if loaded
    @undos = [ ]
    @moves = 0
  else
    @over = true
  end

  loaded
end

#move_downObject



92
# File 'lib/sokoban/game_engine.rb', line 92

def move_down( )	move :down		end

#move_leftObject



93
# File 'lib/sokoban/game_engine.rb', line 93

def move_left( )	move :left		end

#move_rightObject



94
# File 'lib/sokoban/game_engine.rb', line 94

def move_right( )	move :right	end

#move_upObject



95
# File 'lib/sokoban/game_engine.rb', line 95

def move_up( )		move :up		end

#over?Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/sokoban/game_engine.rb', line 97

def over?
  @over
end

#restart_levelObject



101
102
103
# File 'lib/sokoban/game_engine.rb', line 101

def restart_level
  load_level @level
end

#save(file = File.join(PATH, "sokoban_saved_game.yaml")) ⇒ Object



105
106
107
108
109
# File 'lib/sokoban/game_engine.rb', line 105

def save( file = File.join(PATH, "sokoban_saved_game.yaml") )
  File.open(file, "w") do |f|
    f << YAML.dump(self)
  end
end

#undoObject



111
112
113
114
115
116
# File 'lib/sokoban/game_engine.rb', line 111

def undo
  if @undos.size > 0
    @board = @undos.pop
    @moves -= 1
  end
end