Module: RSokoban::Game

Included in:
GameGUI, GameUI
Defined in:
lib/rsokoban/game/game.rb

Overview

I provide the basic API to run a game. I am user interface agnostic.

Since:

  • 0.74.1

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#level_numberFixnum (readonly)

Returns:

  • (Fixnum)

Since:

  • 0.74.1



8
9
10
# File 'lib/rsokoban/game/game.rb', line 8

def level_number
  @level_number
end

#set_nameFixnum (readonly)

Returns:

  • (Fixnum)

Since:

  • 0.74.1



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.

Parameters:

  • the (UI)

    user interface associated to this game

  • setname (String) (defaults to: 'microban.xsb')

    The set of levels to begin with

Since:

  • 0.74.1



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_heightFixnum

Returns:

  • (Fixnum)

Since:

  • 0.74.1



33
34
35
# File 'lib/rsokoban/game/game.rb', line 33

def level_height
	@level.height
end

#level_titleString

Returns:

Since:

  • 0.74.1



38
39
40
# File 'lib/rsokoban/game/game.rb', line 38

def level_title
	@level.title
end

#level_widthFixnum

Returns:

  • (Fixnum)

Since:

  • 0.74.1



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.

Parameters:

  • setname (String)

    the name of the set (with .xsb extension)

Since:

  • 0.74.1



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.

Parameters:

  • num (Fixnum)

    the number of the set (base 1)

Returns:

  • See the implementation of start_level in a descendant module (GameUI or GameGUI).

Since:

  • 0.74.1



112
113
114
115
# File 'lib/rsokoban/game/game.rb', line 112

def load_level num
	@level_number = num
	start_level
end

#man_xFixnum

Get x coordinate of the man

Returns:

  • (Fixnum)

Since:

  • 0.74.1



50
51
52
# File 'lib/rsokoban/game/game.rb', line 50

def man_x
	@level.man.x
end

#man_yFixnum

Get y coordinate of the man

Returns:

  • (Fixnum)

Since:

  • 0.74.1



56
57
58
# File 'lib/rsokoban/game/game.rb', line 56

def man_y
	@level.man.y
end

#map_as_arrayArray<String>

Get current map of the game as an array of strings

Returns:

Since:

  • 0.74.1



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

Parameters:

  • direction (:up|:down|:right|:left)

Returns:

  • (Object)

Since:

  • 0.74.1



63
64
65
# File 'lib/rsokoban/game/game.rb', line 63

def move direction
	@level.move direction
end

#move_numberFixnum

Get current move number

Returns:

  • (Fixnum)

Since:

  • 0.74.1



69
70
71
# File 'lib/rsokoban/game/game.rb', line 69

def move_number
	@level.move_number
end

#next_levelObject

Load and start the next level of the set

Returns:

  • See the implementation of start_level in a descendant module (GameUI or GameGUI).

Since:

  • 0.74.1



104
105
106
107
# File 'lib/rsokoban/game/game.rb', line 104

def next_level
	@level_number += 1
	start_level
end

#recordObject

Get record for current level (in # of moves)

Since:

  • 0.76



135
136
137
# File 'lib/rsokoban/game/game.rb', line 135

def record
	@level.record
end

#redoMoveResult

Get result of redo last undone move

Returns:

Since:

  • 0.74.1



81
82
83
# File 'lib/rsokoban/game/game.rb', line 81

def redo
	@level.redo
end

#restart_setObject

Restart current set from level 1.

Returns:

  • See the implementation of start_level in a descendant module (GameUI or GameGUI).

Since:

  • 0.74.1



119
120
121
122
# File 'lib/rsokoban/game/game.rb', line 119

def restart_set
	@level_number = 1
	start_level
end

#runObject

Note:

You must override me in a concrete class.

Start the game loop.

Raises:

  • (NotImplementedError)

Since:

  • 0.74.1



23
24
25
# File 'lib/rsokoban/game/game.rb', line 23

def run
	raise NotImplementedError
end

#set_sizeFixnum

Get numbers of level from the current set

Returns:

  • (Fixnum)

Since:

  • 0.74.1



93
94
95
# File 'lib/rsokoban/game/game.rb', line 93

def set_size
	@level_loader.size
end

#set_titleString

Get title of the current set

Returns:

Since:

  • 0.74.1



87
88
89
# File 'lib/rsokoban/game/game.rb', line 87

def set_title
	@level_loader.title
end

#start_levelObject

Note:

You must override me.

Raises:

  • (NotImplementedError)

Since:

  • 0.74.1



98
99
100
# File 'lib/rsokoban/game/game.rb', line 98

def start_level
	raise NotImplementedError
end

#undoMoveResult

Get result of undo last move

Returns:

Since:

  • 0.74.1



75
76
77
# File 'lib/rsokoban/game/game.rb', line 75

def undo
	@level.undo
end

#update_recordObject

Update record if needed.

Returns:

  • true if old record is beaten.

Since:

  • 0.76



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