Class: RbStar::Board
- Inherits:
-
Object
- Object
- RbStar::Board
- Defined in:
- lib/rbstar.rb
Instance Attribute Summary collapse
-
#board ⇒ Object
readonly
Returns the value of attribute board.
-
#cells ⇒ Object
readonly
Returns the value of attribute cells.
-
#distance ⇒ Object
readonly
Returns the value of attribute distance.
-
#height ⇒ Object
readonly
Returns the value of attribute height.
-
#level ⇒ Object
readonly
Returns the value of attribute level.
-
#levels ⇒ Object
readonly
Returns the value of attribute levels.
-
#whats_moving ⇒ Object
readonly
Returns the value of attribute whats_moving.
-
#width ⇒ Object
readonly
Returns the value of attribute width.
Instance Method Summary collapse
- #count_gifts ⇒ Object
- #get_cell(x, y) ⇒ Object
- #get_pos(actor) ⇒ Object
-
#initialize(level = 0) ⇒ Board
constructor
A new instance of Board.
- #load_level(level) ⇒ Object
- #move(direction) ⇒ Object
- #next_level ⇒ Object
- #prev_level ⇒ Object
- #reload_level ⇒ Object
- #set_cell(x, y, value) ⇒ Object
- #success? ⇒ Boolean
- #switch_moving ⇒ Object
Constructor Details
#initialize(level = 0) ⇒ Board
Returns a new instance of Board.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/rbstar.rb', line 16 def initialize( level = 0 ) @height = 9 @width = 16 @levels = ['#################@## x#H## x #### ##x ## ## x #### x x x ## x x## x ## ##x x#################', ' # # # # # ### x @# #x #x x # # x x # # # x # # #H# x # # # # #xx## # # # # ', '################# x#@## ## ##H## #x x ## x x## x## #x x x# x### ##x #x x x####x ##x #################', '################# #H## # ###x#x x#x#x#x#x## # #x x# # # ####x#x#x x#x#x#x## # ## # #@ #################', ' ############## #@ # # # ## #x # x x # ### # # ##x #x# #### # x # ##x# # # # #H## # x# #x# ############## ', ' ############ # x #x x# # x # ## # x ##@ x ### x # ### x # ##H # x ##x #################', '################# # ## ### #x ##x# #x #x # # # # # # ### ## ## # #x# #x# # ## @#x H #x#################', '############### # x## ### #x ## x ## x## # #x ### ## #x# ### # x#x ##xHx# x #@# ### # ###############', ' # ########### #x#x # @##x x# x # # # x## x# ## #x #xHx x## x## # #x#x # # # ############ ', ' ########### #### x ## H ###x x# x## x #x #x # # # x # x##x#x # x# #@# #x ### ### # # # # ######### # #', '################# # @## #xx xx #### x ## x##x #x#xx ##### ## ## ##x x# x H x###x### # ## ## ########### ', '## ## #### #@#####x ### x### xx x ## ## ##x #x# ## # x ###x ## ## ## ## #H# ## x ## x #################', ' ############## # @# x ### # #x x## ## x # ## x #x## # x ### x x #x##H # x # # # ############## ', '#################x#x x#x## x#@ ## ## H x ## x# ## x ## x# # ##x#x x#x#################', ' ###### ####### # x# x ## # x # # x ## @# #xx #x # # # # x H# ##x # #x # # x # #x x# ############## ', '################## H#x x x##x @x#x #### ### x #### x#x# ##xx x#x ### x ####x ###x# # #################', '################# x# #@ ## # x#xx#x # ## #x##x# x ## x# x# ## x#x x# ## # # ##x# # ## x #x H #################', '################# x x H# ## #x#x #x ## #x# #x ## x # x#x ## #x# # x# ## x#x # x # ##x#@ # # #################', '#################x ## ##x## # # #x ## x# x## x ## # #x ## # x# ## ## x# ##x #H## x# #x ##@#################', '################# x#x ###x x# ##x ### # # x # # ## H # ## # @x## # # x # # ### x## #x x### x#x #################', '################# ### x ### # # ### ##x x ## x x x ### # ###x ## x x @ H x xx################# ', '#################x# #x# #x # ## # ##x # #x x ### #x x #### x # ###x ## #@#H x ################# ', ' ############## # # #x# #x # ## x # ### # x #x ## #x # xx x ###x # ## x ## #@#H x # ############## ', '################# # ### ##x x ##x### #x x# #### xx x# ## ## #x x # ## ## ## @#H###xx################# ', '################# # ## x ##x x ## #x x ## ## x ## #x ## #x x# x ## ##x #@ H ################# '].freeze @cells = { actor: '@', blocker: 'H', wall: '#', gem: 'x', emptiness: ' ' }.freeze @whats_moving = cells[:actor] @distance = 0 @level = if level.negative? 0 elsif level > levels.size levels.size - 1 else level end @board = '' load_level( @level ) end |
Instance Attribute Details
#board ⇒ Object (readonly)
Returns the value of attribute board.
7 8 9 |
# File 'lib/rbstar.rb', line 7 def board @board end |
#cells ⇒ Object (readonly)
Returns the value of attribute cells.
7 8 9 |
# File 'lib/rbstar.rb', line 7 def cells @cells end |
#distance ⇒ Object (readonly)
Returns the value of attribute distance.
7 8 9 |
# File 'lib/rbstar.rb', line 7 def distance @distance end |
#height ⇒ Object (readonly)
Returns the value of attribute height.
7 8 9 |
# File 'lib/rbstar.rb', line 7 def height @height end |
#level ⇒ Object (readonly)
Returns the value of attribute level.
7 8 9 |
# File 'lib/rbstar.rb', line 7 def level @level end |
#levels ⇒ Object (readonly)
Returns the value of attribute levels.
7 8 9 |
# File 'lib/rbstar.rb', line 7 def levels @levels end |
#whats_moving ⇒ Object (readonly)
Returns the value of attribute whats_moving.
7 8 9 |
# File 'lib/rbstar.rb', line 7 def whats_moving @whats_moving end |
#width ⇒ Object (readonly)
Returns the value of attribute width.
7 8 9 |
# File 'lib/rbstar.rb', line 7 def width @width end |
Instance Method Details
#count_gifts ⇒ Object
131 132 133 |
# File 'lib/rbstar.rb', line 131 def count_gifts @board.count( cells[:gem] ) end |
#get_cell(x, y) ⇒ Object
71 72 73 |
# File 'lib/rbstar.rb', line 71 def get_cell( x, y ) @board[ y * width + x ] end |
#get_pos(actor) ⇒ Object
63 64 65 66 67 68 69 |
# File 'lib/rbstar.rb', line 63 def get_pos( actor ) rawpos = @board.index( actor ) pos = { y: ( rawpos / width ).floor } pos[ :x ] = rawpos - ( pos[ :y ] * width ) pos end |
#load_level(level) ⇒ Object
105 106 107 108 109 110 111 112 113 |
# File 'lib/rbstar.rb', line 105 def load_level( level ) @level = level @level = 0 if @level.negative? @level = (@levels.length - 1) if @level >= @levels.length @board = @levels[ @level ].dup @distance = 0 @whats_moving = cells[:actor] end |
#move(direction) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/rbstar.rb', line 79 def move( direction ) delta = { x: 0, y: 0 } delta[ :x ] = -1 if direction == :left delta[ :x ] = 1 if direction == :right delta[ :y ] = -1 if direction == :up delta[ :y ] = 1 if direction == :down coord = get_pos( @whats_moving ) while ( coord[ :y ] + delta[ :y ] >= 0 && coord[ :y ] + delta[ :y ] < height ) && ( coord[ :x ] + delta[ :x ] >= 0 && coord[ :x ] + delta[ :x ] < width ) && ( get_cell( coord[ :x ] + delta[ :x ], coord[ :y ] + delta[ :y ] ) == cells[:emptiness] ) || ( @whats_moving == cells[:actor] && get_cell( coord[ :x ] + delta[ :x ], coord[ :y ] + delta[ :y ] ) == cells[:gem] ) set_cell( coord[ :x ], coord[ :y ], ' ' ) coord[ :x ] = coord[ :x ] + delta[ :x ] coord[ :y ] = coord[ :y ] + delta[ :y ] set_cell( coord[ :x ], coord[ :y ], @whats_moving ) @distance += 1 end end |
#next_level ⇒ Object
123 124 125 |
# File 'lib/rbstar.rb', line 123 def next_level load_level( @level + 1 ) if @level < @levels.length end |
#prev_level ⇒ Object
119 120 121 |
# File 'lib/rbstar.rb', line 119 def prev_level load_level( @level - 1 ) if @level.positive? end |
#reload_level ⇒ Object
115 116 117 |
# File 'lib/rbstar.rb', line 115 def reload_level load_level( @level ) end |
#set_cell(x, y, value) ⇒ Object
75 76 77 |
# File 'lib/rbstar.rb', line 75 def set_cell( x, y, value ) @board[ y * width + x ] = value end |
#success? ⇒ Boolean
135 136 137 |
# File 'lib/rbstar.rb', line 135 def success? count_gifts.zero? end |
#switch_moving ⇒ Object
127 128 129 |
# File 'lib/rbstar.rb', line 127 def switch_moving @whats_moving = @whats_moving == cells[:actor] ? cells[:blocker] : cells[:actor] end |