rbstar 1.0.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 (5) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +2 -2
  3. data/bin/rbstar +13 -10
  4. data/lib/rbstar.rb +70 -54
  5. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: aef457553174b349c2807ef8d8b5dd9ccb276e5fdea50b6905b7cf44acb6a53c
4
- data.tar.gz: abd66c50515ca381e5e8338be9efada823cd001f314ebeaa0427f17bca93a1a5
3
+ metadata.gz: bb2ed0e7e0da2b7a9f2f673daf96784eb0bf70f9f5a9c14d2efa8a6a0d2924af
4
+ data.tar.gz: d4f8c7522ba1d4f56c0ec8bac3270764ae14f60ca11cb1a4de01fb8086147dcc
5
5
  SHA512:
6
- metadata.gz: b1c0ba42fa4292b5bf5e8bd3c5c62856701ec02ecf899f36e78efae0836e8d7050e7e47a8b3452635c289c3d601bbde8dedea648ff75a96de02e075afdddcab8
7
- data.tar.gz: 14fcbc76b4009fb16603aba7c45adc4adab57324aaa4f2651998fea17bf38cc0367bb424a79a13c23a7eb03166b34267fdb3f87517db59a610861d99f80c9952
6
+ metadata.gz: 105f4d01ae804d28a5b72907c97919bc79172b81c891b950a0727adcdbf1beaad4b9dabbe49d7c12b459729c3203e569a807f99de6d759f818ac12c67273e0b1
7
+ data.tar.gz: e69c9a866bec03c60d1f17bc7a715b0695bd79eb6262b75c463403c7c68e1c1a81eef6ce98be4ba488356baa07479a1da70205eb0bf7824308a148a4169f88f3
data/README.md CHANGED
@@ -1,3 +1,3 @@
1
- Puzzle game inspired by an old HP 48 called dstar
1
+ Puzzle game inspired by an old HP 48 called dstar [ ]https://www.hpcalc.org/details/912 ]
2
2
 
3
- https://www.hpcalc.org/details/912
3
+ `gem install rbstar` then run `rbstar`
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
@@ -84,4 +84,7 @@ def ncurses_main( level )
84
84
  exit!
85
85
  end
86
86
 
87
- ncurses_main( 0 )
87
+ level = 0
88
+ level = ARGV.first.to_i - 1 if ARGV.length == 1
89
+
90
+ ncurses_main( level )
data/lib/rbstar.rb CHANGED
@@ -1,47 +1,60 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RbStar
4
- VERSION = '1.0.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
- :whats_moving
13
+ :whats_moving,
14
+ :distance
13
15
 
14
16
  def initialize( level = 0 )
15
- @levels = [
16
- '#################@## x#H## x #### ##x ## ## x #### x x x ## x x## x ## ##x x#################',
17
- ' # # # # # ### x @# #x #x x # # x x # # # x # # #H# x # # # # #xx## # # # # ',
18
- '################# x#@## ## ##H## #x x ## x x## x## #x x x# x### ##x #x x x####x ##x #################',
19
- '################# #H## # ###x#x x#x#x#x#x## # #x x# # # ####x#x#x x#x#x#x## # ## # #@ #################',
20
- ' ############## #@ # # # ## #x # x x # ### # # ##x #x# #### # x # ##x# # # # #H## # x# #x# ############## ',
21
- ' ############ # x #x x# # x # ## # x ##@ x ### x # ### x # ##H # x ##x #################',
22
- '################# # ## ### #x ##x# #x #x # # # # # # ### ## ## # #x# #x# # ## @#x H #x#################',
23
- '############### # x## ### #x ## x ## x## # #x ### ## #x# ### # x#x ##xHx# x #@# ### # ###############',
24
- ' # ########### #x#x # @##x x# x # # # x## x# ## #x #xHx x## x## # #x#x # # # ############ ',
25
- ' ########### #### x ## H ###x x# x## x #x #x # # # x # x##x#x # x# #@# #x ### ### # # # # ######### # #',
26
- '################# # @## #xx xx #### x ## x##x #x#xx ##### ## ## ##x x# x H x###x### # ## ## ########### ',
27
- '## ## #### #@#####x ### x### xx x ## ## ##x #x# ## # x ###x ## ## ## ## #H# ## x ## x #################',
28
- ' ############## # @# x ### # #x x## ## x # ## x #x## # x ### x x #x##H # x # # # ############## ',
29
- '#################x#x x#x## x#@ ## ## H x ## x# ## x ## x# # ##x#x x#x#################',
30
- ' ###### ####### # x# x ## # x # # x ## @# #xx #x # # # # x H# ##x # #x # # x # #x x# ############## ',
31
- '################## H#x x x##x @x#x #### ### x #### x#x# ##xx x#x ### x ####x ###x# # #################',
32
- '################# x# #@ ## # x#xx#x # ## #x##x# x ## x# x# ## x#x x# ## # # ##x# # ## x #x H #################',
33
- '################# x x H# ## #x#x #x ## #x# #x ## x # x#x ## #x# # x# ## x#x # x # ##x#@ # # #################',
34
- '#################x ## ##x## # # #x ## x# x## x ## # #x ## # x# ## ## x# ##x #H## x# #x ##@#################',
35
- '################# x#x ###x x# ##x ### # # x # # ## H # ## # @x## # # x # # ### x## #x x### x#x #################',
36
- '################# ### x ### # # ### ##x x ## x x x ### # ###x ## x x @ H x xx################# ',
37
- '#################x# #x# #x # ## # ##x # #x x ### #x x #### x # ###x ## #@#H x ################# ',
38
- ' ############## # # #x# #x # ## x # ### # x #x ## #x # xx x ###x # ## x ## #@#H x # ############## ',
39
- '################# # ### ##x x ##x### #x x# #### xx x# ## ## #x x # ## ## ## @#H###xx################# ',
40
- '################# # ## x ##x x ## #x x ## ## x ## #x ## #x x# x ## ##x #@ H ################# '
41
- ]
42
- @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]
43
50
  @distance = 0
44
- @level = level
51
+ @level = if level.negative?
52
+ 0
53
+ elsif level > levels.size
54
+ levels.size - 1
55
+ else
56
+ level
57
+ end
45
58
  @board = ''
46
59
 
47
60
  load_level( @level )
@@ -50,41 +63,41 @@ module RbStar
50
63
  def get_pos( actor )
51
64
  pos = {}
52
65
  p = @board.index( actor )
53
- pos[ 'y' ] = ( p / LEVEL_WIDTH ).floor
54
- pos[ 'x' ] = p - ( pos[ 'y' ] * LEVEL_WIDTH )
66
+ pos[ :y ] = ( p / width ).floor
67
+ pos[ :x ] = p - ( pos[ :y ] * width )
55
68
 
56
69
  pos
57
70
  end
58
71
 
59
72
  def get_cell( x, y )
60
- @board[ y * LEVEL_WIDTH + x ]
73
+ @board[ y * width + x ]
61
74
  end
62
75
 
63
76
  def set_cell( x, y, value )
64
- @board[ y * LEVEL_WIDTH + x ] = value
77
+ @board[ y * width + x ] = value
65
78
  end
66
79
 
67
80
  def move( direction )
68
- d = { 'x' => 0, 'y' => 0 }
69
- d[ 'x' ] = -1 if direction == :left
70
- d[ 'x' ] = 1 if direction == :right
71
- d[ 'y' ] = -1 if direction == :up
72
- 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
73
86
 
74
87
  coord = get_pos( @whats_moving )
75
88
 
76
- while ( coord[ 'y' ] + d[ 'y' ] >= 0 && coord[ 'y' ] + d[ 'y' ] < LEVEL_HEIGHT ) &&
77
- ( coord[ 'x' ] + d[ 'x' ] >= 0 && coord[ 'x' ] + d[ 'x' ] < LEVEL_WIDTH ) &&
78
- ( get_cell( coord[ 'x' ] + d[ 'x' ], coord[ 'y' ] + d[ 'y' ] ) == ' ' ) ||
79
- ( @whats_moving == '@' &&
80
- 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] )
81
94
 
82
- set_cell( coord[ 'x' ], coord[ 'y' ], ' ' )
95
+ set_cell( coord[ :x ], coord[ :y ], ' ' )
83
96
 
84
- coord[ 'x' ] = coord[ 'x' ] + d[ 'x' ]
85
- coord[ 'y' ] = coord[ 'y' ] + d[ 'y' ]
97
+ coord[ :x ] = coord[ :x ] + delta[ :x ]
98
+ coord[ :y ] = coord[ :y ] + delta[ :y ]
86
99
 
87
- set_cell( coord[ 'x' ], coord[ 'y' ], @whats_moving )
100
+ set_cell( coord[ :x ], coord[ :y ], @whats_moving )
88
101
 
89
102
  @distance += 1
90
103
  end
@@ -92,6 +105,9 @@ module RbStar
92
105
 
93
106
  def load_level( level )
94
107
  @level = level
108
+ @level = 0 if @level.negative?
109
+ @level = (@levels.length - 1) if @level >= @levels.length
110
+
95
111
  @board = @levels[ @level ].dup
96
112
  end
97
113
 
@@ -104,11 +120,11 @@ module RbStar
104
120
  end
105
121
 
106
122
  def switch_moving
107
- @whats_moving = @whats_moving == '@' ? 'H' : '@'
123
+ @whats_moving = @whats_moving == cells[:actor] ? cells[:blocker] : cells[:actor]
108
124
  end
109
125
 
110
126
  def count_gifts
111
- @board.count( 'x' )
127
+ @board.count( cells[:gem] )
112
128
  end
113
129
 
114
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.0.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