rbstar 1.1.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/bin/rbstar +10 -10
  3. data/lib/rbstar.rb +63 -54
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bb6514b47650e632547437b4a4777122ccdcefbf59ae585f6cce37120ba57e11
4
- data.tar.gz: a796028bdf4223de6492a7cc879ab9fc926e73f575f1623d938bcce1c6f02143
3
+ metadata.gz: bb2ed0e7e0da2b7a9f2f673daf96784eb0bf70f9f5a9c14d2efa8a6a0d2924af
4
+ data.tar.gz: d4f8c7522ba1d4f56c0ec8bac3270764ae14f60ca11cb1a4de01fb8086147dcc
5
5
  SHA512:
6
- metadata.gz: 10f47d478d81328d2b46746bdf026c8a68bb5fe9ca121dc0a2ceaea0b3be2456ccb882b1d089681e6cf26b34f6ba2c92a8d34d5ff510a1e8656338ba281aed6d
7
- data.tar.gz: 34989ffd923503f149823b9fc555a30a4f39a02b14780f8fd6a5e78b5f48403b3f3c4d82951eda46bc45718eca0637ce0f81b9a9fb9b91bcec441e5243336383
6
+ metadata.gz: 105f4d01ae804d28a5b72907c97919bc79172b81c891b950a0727adcdbf1beaad4b9dabbe49d7c12b459729c3203e569a807f99de6d759f818ac12c67273e0b1
7
+ data.tar.gz: e69c9a866bec03c60d1f17bc7a715b0695bd79eb6262b75c463403c7c68e1c1a81eef6ce98be4ba488356baa07479a1da70205eb0bf7824308a148a4169f88f3
data/bin/rbstar CHANGED
@@ -6,23 +6,23 @@ require 'curses'
6
6
  require 'rbstar'
7
7
 
8
8
  def display_curses( field,
9
- theme = { '@' => { text: '()' },
10
- 'H' => { text: '[]' },
11
- '#' => { text: '##' },
12
- 'x' => { text: '<>' },
13
- ' ' => { text: ' ' } } )
9
+ theme = { field.cells[:actor] => '()',
10
+ field.cells[:blocker] => '[]',
11
+ field.cells[:wall] => '██',
12
+ field.cells[:gem] => '<>',
13
+ field.cells[:emptiness] => ' ' } )
14
14
  Curses.setpos 0, 0
15
- Curses.addstr "Level #{field.level}, #{field.count_gifts} gems left, #{theme[ field.whats_moving ][:text]} moving"
15
+ Curses.addstr "Level #{field.level + 1}/#{field.levels.length}, #{theme[ field.whats_moving ]} moving, #{field.count_gifts} gems left, score: #{field.distance} "
16
16
 
17
- RbStar::LEVEL_HEIGHT.times do |y|
17
+ field.height.times do |y|
18
18
  Curses.setpos y + 2, 0
19
19
 
20
- RbStar::LEVEL_WIDTH.times do |x|
20
+ field.width.times do |x|
21
21
  cell = field.get_cell( x, y )
22
22
 
23
23
  Curses.attrset( Curses::A_BOLD ) if cell == field.whats_moving
24
24
 
25
- Curses.addstr theme[ cell ][:text]
25
+ Curses.addstr theme[ cell ]
26
26
 
27
27
  Curses.attrset( Curses::A_NORMAL )
28
28
  end
@@ -85,6 +85,6 @@ def ncurses_main( level )
85
85
  end
86
86
 
87
87
  level = 0
88
- level = ARGV.first.to_i if ARGV.length == 1
88
+ level = ARGV.first.to_i - 1 if ARGV.length == 1
89
89
 
90
90
  ncurses_main( level )
data/lib/rbstar.rb CHANGED
@@ -1,51 +1,57 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RbStar
4
- VERSION = '1.1.0'
5
-
6
- LEVEL_HEIGHT = 9
7
- LEVEL_WIDTH = 16
4
+ VERSION = '1.2.0'
8
5
 
9
6
  class Board
10
7
  attr_reader :board,
8
+ :levels,
9
+ :cells,
10
+ :height,
11
+ :width,
11
12
  :level,
12
13
  :whats_moving,
13
14
  :distance
14
15
 
15
16
  def initialize( level = 0 )
16
- @levels = [
17
- '#################@## x#H## x #### ##x ## ## x #### x x x ## x x## x ## ##x x#################',
18
- ' # # # # # ### x @# #x #x x # # x x # # # x # # #H# x # # # # #xx## # # # # ',
19
- '################# x#@## ## ##H## #x x ## x x## x## #x x x# x### ##x #x x x####x ##x #################',
20
- '################# #H## # ###x#x x#x#x#x#x## # #x x# # # ####x#x#x x#x#x#x## # ## # #@ #################',
21
- ' ############## #@ # # # ## #x # x x # ### # # ##x #x# #### # x # ##x# # # # #H## # x# #x# ############## ',
22
- ' ############ # x #x x# # x # ## # x ##@ x ### x # ### x # ##H # x ##x #################',
23
- '################# # ## ### #x ##x# #x #x # # # # # # ### ## ## # #x# #x# # ## @#x H #x#################',
24
- '############### # x## ### #x ## x ## x## # #x ### ## #x# ### # x#x ##xHx# x #@# ### # ###############',
25
- ' # ########### #x#x # @##x x# x # # # x## x# ## #x #xHx x## x## # #x#x # # # ############ ',
26
- ' ########### #### x ## H ###x x# x## x #x #x # # # x # x##x#x # x# #@# #x ### ### # # # # ######### # #',
27
- '################# # @## #xx xx #### x ## x##x #x#xx ##### ## ## ##x x# x H x###x### # ## ## ########### ',
28
- '## ## #### #@#####x ### x### xx x ## ## ##x #x# ## # x ###x ## ## ## ## #H# ## x ## x #################',
29
- ' ############## # @# x ### # #x x## ## x # ## x #x## # x ### x x #x##H # x # # # ############## ',
30
- '#################x#x x#x## x#@ ## ## H x ## x# ## x ## x# # ##x#x x#x#################',
31
- ' ###### ####### # x# x ## # x # # x ## @# #xx #x # # # # x H# ##x # #x # # x # #x x# ############## ',
32
- '################## H#x x x##x @x#x #### ### x #### x#x# ##xx x#x ### x ####x ###x# # #################',
33
- '################# x# #@ ## # x#xx#x # ## #x##x# x ## x# x# ## x#x x# ## # # ##x# # ## x #x H #################',
34
- '################# x x H# ## #x#x #x ## #x# #x ## x # x#x ## #x# # x# ## x#x # x # ##x#@ # # #################',
35
- '#################x ## ##x## # # #x ## x# x## x ## # #x ## # x# ## ## x# ##x #H## x# #x ##@#################',
36
- '################# x#x ###x x# ##x ### # # x # # ## H # ## # @x## # # x # # ### x## #x x### x#x #################',
37
- '################# ### x ### # # ### ##x x ## x x x ### # ###x ## x x @ H x xx################# ',
38
- '#################x# #x# #x # ## # ##x # #x x ### #x x #### x # ###x ## #@#H x ################# ',
39
- ' ############## # # #x# #x # ## x # ### # x #x ## #x # xx x ###x # ## x ## #@#H x # ############## ',
40
- '################# # ### ##x x ##x### #x x# #### xx x# ## ## #x x # ## ## ## @#H###xx################# ',
41
- '################# # ## x ##x x ## #x x ## ## x ## #x ## #x x# x ## ##x #@ H ################# '
42
- ]
43
- @whats_moving = '@'
17
+ @height = 9
18
+ @width = 16
19
+ @levels = ['#################@## x#H## x #### ##x ## ## x #### x x x ## x x## x ## ##x x#################',
20
+ ' # # # # # ### x @# #x #x x # # x x # # # x # # #H# x # # # # #xx## # # # # ',
21
+ '################# x#@## ## ##H## #x x ## x x## x## #x x x# x### ##x #x x x####x ##x #################',
22
+ '################# #H## # ###x#x x#x#x#x#x## # #x x# # # ####x#x#x x#x#x#x## # ## # #@ #################',
23
+ ' ############## #@ # # # ## #x # x x # ### # # ##x #x# #### # x # ##x# # # # #H## # x# #x# ############## ',
24
+ ' ############ # x #x x# # x # ## # x ##@ x ### x # ### x # ##H # x ##x #################',
25
+ '################# # ## ### #x ##x# #x #x # # # # # # ### ## ## # #x# #x# # ## @#x H #x#################',
26
+ '############### # x## ### #x ## x ## x## # #x ### ## #x# ### # x#x ##xHx# x #@# ### # ###############',
27
+ ' # ########### #x#x # @##x x# x # # # x## x# ## #x #xHx x## x## # #x#x # # # ############ ',
28
+ ' ########### #### x ## H ###x x# x## x #x #x # # # x # x##x#x # x# #@# #x ### ### # # # # ######### # #',
29
+ '################# # @## #xx xx #### x ## x##x #x#xx ##### ## ## ##x x# x H x###x### # ## ## ########### ',
30
+ '## ## #### #@#####x ### x### xx x ## ## ##x #x# ## # x ###x ## ## ## ## #H# ## x ## x #################',
31
+ ' ############## # @# x ### # #x x## ## x # ## x #x## # x ### x x #x##H # x # # # ############## ',
32
+ '#################x#x x#x## x#@ ## ## H x ## x# ## x ## x# # ##x#x x#x#################',
33
+ ' ###### ####### # x# x ## # x # # x ## @# #xx #x # # # # x H# ##x # #x # # x # #x x# ############## ',
34
+ '################## H#x x x##x @x#x #### ### x #### x#x# ##xx x#x ### x ####x ###x# # #################',
35
+ '################# x# #@ ## # x#xx#x # ## #x##x# x ## x# x# ## x#x x# ## # # ##x# # ## x #x H #################',
36
+ '################# x x H# ## #x#x #x ## #x# #x ## x # x#x ## #x# # x# ## x#x # x # ##x#@ # # #################',
37
+ '#################x ## ##x## # # #x ## x# x## x ## # #x ## # x# ## ## x# ##x #H## x# #x ##@#################',
38
+ '################# x#x ###x x# ##x ### # # x # # ## H # ## # @x## # # x # # ### x## #x x### x#x #################',
39
+ '################# ### x ### # # ### ##x x ## x x x ### # ###x ## x x @ H x xx################# ',
40
+ '#################x# #x# #x # ## # ##x # #x x ### #x x #### x # ###x ## #@#H x ################# ',
41
+ ' ############## # # #x# #x # ## x # ### # x #x ## #x # xx x ###x # ## x ## #@#H x # ############## ',
42
+ '################# # ### ##x x ##x### #x x# #### xx x# ## ## #x x # ## ## ## @#H###xx################# ',
43
+ '################# # ## x ##x x ## #x x ## ## x ## #x ## #x x# x ## ##x #@ H ################# '].freeze
44
+ @cells = { actor: '@',
45
+ blocker: 'H',
46
+ wall: '#',
47
+ gem: 'x',
48
+ emptiness: ' ' }.freeze
49
+ @whats_moving = cells[:actor]
44
50
  @distance = 0
45
51
  @level = if level.negative?
46
52
  0
47
- elsif level > @levels.size
48
- @levels.size - 1
53
+ elsif level > levels.size
54
+ levels.size - 1
49
55
  else
50
56
  level
51
57
  end
@@ -57,41 +63,41 @@ module RbStar
57
63
  def get_pos( actor )
58
64
  pos = {}
59
65
  p = @board.index( actor )
60
- pos[ 'y' ] = ( p / LEVEL_WIDTH ).floor
61
- pos[ 'x' ] = p - ( pos[ 'y' ] * LEVEL_WIDTH )
66
+ pos[ :y ] = ( p / width ).floor
67
+ pos[ :x ] = p - ( pos[ :y ] * width )
62
68
 
63
69
  pos
64
70
  end
65
71
 
66
72
  def get_cell( x, y )
67
- @board[ y * LEVEL_WIDTH + x ]
73
+ @board[ y * width + x ]
68
74
  end
69
75
 
70
76
  def set_cell( x, y, value )
71
- @board[ y * LEVEL_WIDTH + x ] = value
77
+ @board[ y * width + x ] = value
72
78
  end
73
79
 
74
80
  def move( direction )
75
- d = { 'x' => 0, 'y' => 0 }
76
- d[ 'x' ] = -1 if direction == :left
77
- d[ 'x' ] = 1 if direction == :right
78
- d[ 'y' ] = -1 if direction == :up
79
- d[ 'y' ] = 1 if direction == :down
81
+ delta = { x: 0, y: 0 }
82
+ delta[ :x ] = -1 if direction == :left
83
+ delta[ :x ] = 1 if direction == :right
84
+ delta[ :y ] = -1 if direction == :up
85
+ delta[ :y ] = 1 if direction == :down
80
86
 
81
87
  coord = get_pos( @whats_moving )
82
88
 
83
- while ( coord[ 'y' ] + d[ 'y' ] >= 0 && coord[ 'y' ] + d[ 'y' ] < LEVEL_HEIGHT ) &&
84
- ( coord[ 'x' ] + d[ 'x' ] >= 0 && coord[ 'x' ] + d[ 'x' ] < LEVEL_WIDTH ) &&
85
- ( get_cell( coord[ 'x' ] + d[ 'x' ], coord[ 'y' ] + d[ 'y' ] ) == ' ' ) ||
86
- ( @whats_moving == '@' &&
87
- get_cell( coord[ 'x' ] + d[ 'x' ], coord[ 'y' ] + d[ 'y' ] ) == 'x' )
89
+ while ( coord[ :y ] + delta[ :y ] >= 0 && coord[ :y ] + delta[ :y ] < height ) &&
90
+ ( coord[ :x ] + delta[ :x ] >= 0 && coord[ :x ] + delta[ :x ] < width ) &&
91
+ ( get_cell( coord[ :x ] + delta[ :x ], coord[ :y ] + delta[ :y ] ) == cells[:emptiness] ) ||
92
+ ( @whats_moving == cells[:actor] &&
93
+ get_cell( coord[ :x ] + delta[ :x ], coord[ :y ] + delta[ :y ] ) == cells[:gem] )
88
94
 
89
- set_cell( coord[ 'x' ], coord[ 'y' ], ' ' )
95
+ set_cell( coord[ :x ], coord[ :y ], ' ' )
90
96
 
91
- coord[ 'x' ] = coord[ 'x' ] + d[ 'x' ]
92
- coord[ 'y' ] = coord[ 'y' ] + d[ 'y' ]
97
+ coord[ :x ] = coord[ :x ] + delta[ :x ]
98
+ coord[ :y ] = coord[ :y ] + delta[ :y ]
93
99
 
94
- set_cell( coord[ 'x' ], coord[ 'y' ], @whats_moving )
100
+ set_cell( coord[ :x ], coord[ :y ], @whats_moving )
95
101
 
96
102
  @distance += 1
97
103
  end
@@ -99,6 +105,9 @@ module RbStar
99
105
 
100
106
  def load_level( level )
101
107
  @level = level
108
+ @level = 0 if @level.negative?
109
+ @level = (@levels.length - 1) if @level >= @levels.length
110
+
102
111
  @board = @levels[ @level ].dup
103
112
  end
104
113
 
@@ -111,11 +120,11 @@ module RbStar
111
120
  end
112
121
 
113
122
  def switch_moving
114
- @whats_moving = @whats_moving == '@' ? 'H' : '@'
123
+ @whats_moving = @whats_moving == cells[:actor] ? cells[:blocker] : cells[:actor]
115
124
  end
116
125
 
117
126
  def count_gifts
118
- @board.count( 'x' )
127
+ @board.count( cells[:gem] )
119
128
  end
120
129
 
121
130
  def success?
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rbstar
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gwenhael Le Moine
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-11-22 00:00:00.000000000 Z
11
+ date: 2022-11-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: curses