Class: Sokoban1::Sokoban
- Inherits:
-
Object
- Object
- Sokoban1::Sokoban
- 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.(File.dirname(__FILE__))
Instance Attribute Summary collapse
-
#level ⇒ Object
readonly
Returns the value of attribute level.
-
#moves ⇒ Object
readonly
Returns the value of attribute moves.
Class Method Summary collapse
Instance Method Summary collapse
- #can_move_down? ⇒ Boolean
- #can_move_left? ⇒ Boolean
- #can_move_right? ⇒ Boolean
- #can_move_up? ⇒ Boolean
- #display ⇒ Object
-
#initialize(file = File.join(PATH, "sokoban_levels.txt")) ⇒ Sokoban
constructor
A new instance of Sokoban.
- #level_solved? ⇒ Boolean
- #load_level(level = @level += 1, file = @level_file) ⇒ Object
- #move_down ⇒ Object
- #move_left ⇒ Object
- #move_right ⇒ Object
- #move_up ⇒ Object
- #over? ⇒ Boolean
- #restart_level ⇒ Object
- #save(file = File.join(PATH, "sokoban_saved_game.yaml")) ⇒ Object
- #undo ⇒ Object
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
#level ⇒ Object (readonly)
Returns the value of attribute level.
21 22 23 |
# File 'lib/sokoban/game_engine.rb', line 21 def level @level end |
#moves ⇒ Object (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
Instance Method Details
#can_move_down? ⇒ Boolean
47 |
# File 'lib/sokoban/game_engine.rb', line 47 def can_move_down?( ) can_move? :down end |
#can_move_left? ⇒ Boolean
48 |
# File 'lib/sokoban/game_engine.rb', line 48 def can_move_left?( ) can_move? :left end |
#can_move_right? ⇒ Boolean
49 |
# File 'lib/sokoban/game_engine.rb', line 49 def can_move_right?( ) can_move? :right end |
#can_move_up? ⇒ Boolean
50 |
# File 'lib/sokoban/game_engine.rb', line 50 def can_move_up?( ) can_move? :up end |
#display ⇒ Object
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
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_down ⇒ Object
92 |
# File 'lib/sokoban/game_engine.rb', line 92 def move_down( ) move :down end |
#move_left ⇒ Object
93 |
# File 'lib/sokoban/game_engine.rb', line 93 def move_left( ) move :left end |
#move_right ⇒ Object
94 |
# File 'lib/sokoban/game_engine.rb', line 94 def move_right( ) move :right end |
#move_up ⇒ Object
95 |
# File 'lib/sokoban/game_engine.rb', line 95 def move_up( ) move :up end |
#over? ⇒ Boolean
97 98 99 |
# File 'lib/sokoban/game_engine.rb', line 97 def over? @over end |
#restart_level ⇒ Object
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 |
#undo ⇒ Object
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 |