Module: RSokoban::Game
Overview
I provide the basic API to run a game. I am user interface agnostic.
Instance Attribute Summary collapse
- #level_number ⇒ Fixnum readonly
- #set_name ⇒ Fixnum readonly
Instance Method Summary collapse
-
#initialize(ui, setname = 'microban.xsb') ⇒ Object
Construct a new game that you can later run.
- #level_height ⇒ Fixnum
- #level_title ⇒ String
- #level_width ⇒ Fixnum
-
#load_a_new_set(setname) ⇒ Object
Load a new set of levels and start its first level.
-
#load_level(num) ⇒ Object
Load a level from the current set.
-
#man_x ⇒ Fixnum
Get x coordinate of the man.
-
#man_y ⇒ Fixnum
Get y coordinate of the man.
-
#map_as_array ⇒ Array<String>
Get current map of the game as an array of strings.
-
#move(direction) ⇒ Object
Get result of the move.
-
#move_number ⇒ Fixnum
Get current move number.
-
#next_level ⇒ Object
Load and start the next level of the set.
-
#record ⇒ Object
Get record for current level (in # of moves).
-
#redo ⇒ MoveResult
Get result of redo last undone move.
-
#restart_set ⇒ Object
Restart current set from level 1.
-
#run ⇒ Object
Start the game loop.
-
#set_size ⇒ Fixnum
Get numbers of level from the current set.
-
#set_title ⇒ String
Get title of the current set.
- #start_level ⇒ Object
-
#undo ⇒ MoveResult
Get result of undo last move.
-
#update_record ⇒ Object
Update record if needed.
Instance Attribute Details
#level_number ⇒ Fixnum (readonly)
8 9 10 |
# File 'lib/rsokoban/game/game.rb', line 8 def level_number @level_number end |
#set_name ⇒ Fixnum (readonly)
8 9 10 |
# File 'lib/rsokoban/game/game.rb', line 8 def set_name @set_name end |
Instance Method Details
#initialize(ui, setname = 'microban.xsb') ⇒ Object
Construct a new game that you can later run.
13 14 15 16 17 18 19 |
# File 'lib/rsokoban/game/game.rb', line 13 def initialize ui, setname = 'microban.xsb' @set_name = setname @level_loader = SetLoader.new setname conf = Config.new @level_number = conf['level'] @ui = ui end |
#level_height ⇒ Fixnum
33 34 35 |
# File 'lib/rsokoban/game/game.rb', line 33 def level_height @level.height end |
#level_title ⇒ String
38 39 40 |
# File 'lib/rsokoban/game/game.rb', line 38 def level_title @level.title end |
#level_width ⇒ Fixnum
28 29 30 |
# File 'lib/rsokoban/game/game.rb', line 28 def level_width @level.width end |
#load_a_new_set(setname) ⇒ Object
Load a new set of levels and start its first level.
126 127 128 129 130 131 |
# File 'lib/rsokoban/game/game.rb', line 126 def load_a_new_set setname @set_name = setname @level_loader = SetLoader.new setname @level_number = 1 @level = @level_loader.level(@level_number) end |
#load_level(num) ⇒ Object
Load a level from the current set.
112 113 114 115 |
# File 'lib/rsokoban/game/game.rb', line 112 def load_level num @level_number = num start_level end |
#man_x ⇒ Fixnum
Get x coordinate of the man
50 51 52 |
# File 'lib/rsokoban/game/game.rb', line 50 def man_x @level.man.x end |
#man_y ⇒ Fixnum
Get y coordinate of the man
56 57 58 |
# File 'lib/rsokoban/game/game.rb', line 56 def man_y @level.man.y end |
#map_as_array ⇒ Array<String>
Get current map of the game as an array of strings
44 45 46 |
# File 'lib/rsokoban/game/game.rb', line 44 def map_as_array @level.map_as_array end |
#move(direction) ⇒ Object
Get result of the move
63 64 65 |
# File 'lib/rsokoban/game/game.rb', line 63 def move direction @level.move direction end |
#move_number ⇒ Fixnum
Get current move number
69 70 71 |
# File 'lib/rsokoban/game/game.rb', line 69 def move_number @level.move_number end |
#next_level ⇒ Object
Load and start the next level of the set
104 105 106 107 |
# File 'lib/rsokoban/game/game.rb', line 104 def next_level @level_number += 1 start_level end |
#record ⇒ Object
Get record for current level (in # of moves)
135 136 137 |
# File 'lib/rsokoban/game/game.rb', line 135 def record @level.record end |
#redo ⇒ MoveResult
Get result of redo last undone move
81 82 83 |
# File 'lib/rsokoban/game/game.rb', line 81 def redo @level.redo end |
#restart_set ⇒ Object
Restart current set from level 1.
119 120 121 122 |
# File 'lib/rsokoban/game/game.rb', line 119 def restart_set @level_number = 1 start_level end |
#run ⇒ Object
You must override me in a concrete class.
Start the game loop.
23 24 25 |
# File 'lib/rsokoban/game/game.rb', line 23 def run raise NotImplementedError end |
#set_size ⇒ Fixnum
Get numbers of level from the current set
93 94 95 |
# File 'lib/rsokoban/game/game.rb', line 93 def set_size @level_loader.size end |
#set_title ⇒ String
Get title of the current set
87 88 89 |
# File 'lib/rsokoban/game/game.rb', line 87 def set_title @level_loader.title end |
#start_level ⇒ Object
You must override me.
98 99 100 |
# File 'lib/rsokoban/game/game.rb', line 98 def start_level raise NotImplementedError end |
#undo ⇒ MoveResult
Get result of undo last move
75 76 77 |
# File 'lib/rsokoban/game/game.rb', line 75 def undo @level.undo end |
#update_record ⇒ Object
Update record if needed.
142 143 144 145 146 147 148 149 |
# File 'lib/rsokoban/game/game.rb', line 142 def update_record if record.nil? or move_number < record @level.update_record true else false end end |