a_maze_ing 0.1.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 967c7e20d3d8dc4f3f9071b12a11576d824e488b
4
+ data.tar.gz: 887fd304827234b2b2a6bd74361e31747270bdf5
5
+ SHA512:
6
+ metadata.gz: 960780d275a4ad4c9fa2f7472d91d894c1fd801200cae33816d08df429754df8bce792e327edf49d53dc1374b256473b0e34945a51f1aa287004cebbf35a636b
7
+ data.tar.gz: b3cf5c3fb1ad45b2adf04b0858f6a1081bf1b1d0f84f5077f9bde75512c20555e59874be0fd6cfffe752aa5b4bff87e2df5d4148c656b6836ae9bf1753ca9345
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
13
+ .gem
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.1
5
+ before_install: gem install bundler -v 1.15.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in a_maze_ing.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # AMazeIng
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/a_maze_ing`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'a_maze_ing'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install a_maze_ing
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/a_maze_ing.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,38 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ require "a_maze_ing/version"
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = "a_maze_ing"
9
+ spec.version = AMazeIng::VERSION
10
+ spec.authors = ["gt-cuongtran"]
11
+ spec.email = ["cuong.tran.asiantech@gmail.com"]
12
+
13
+ spec.summary = 'maze solving game'
14
+ # spec.description = %q{TODO: Write a longer description or delete this line.}
15
+ # spec.homepage = "public repo URL."
16
+ spec.license = "MIT"
17
+
18
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
19
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
20
+ # if spec.respond_to?(:metadata)
21
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
22
+ # else
23
+ # raise "RubyGems 2.0 or newer is required to protect against " \
24
+ # "public gem pushes."
25
+ # end
26
+
27
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
28
+ f.match(%r{^(test|spec|features)/})
29
+ end
30
+ spec.executables = "a_maze_ing"
31
+ # spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
32
+ spec.require_paths = ["lib"]
33
+
34
+ spec.add_development_dependency "bundler", "~> 1.15"
35
+ spec.add_development_dependency "rake", "~> 10.0"
36
+ spec.add_development_dependency "rspec", "~> 3.0"
37
+ spec.add_runtime_dependency "gosu","~> 0.12.0"
38
+ end
data/bin/a_maze_ing ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ require "a_maze_ing"
3
+
4
+ AMazeIng::GameWindow.new.show
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "a_maze_ing"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,88 @@
1
+ module AMazeIng
2
+ class Cell
3
+ attr_accessor :cell_index_x, :cell_index_y, :walls, :visited, :neigh_bors, :is_current
4
+ def initialize(cell_index_x, cell_index_y)
5
+ @cell_index_x = cell_index_x
6
+ @cell_index_y = cell_index_y
7
+
8
+ @walls = [true, true, true, true]
9
+ @visited = false
10
+ @color = Color::GREEN
11
+ @is_current = false
12
+ end
13
+
14
+ def cell_index(i, j, cells_length)
15
+ if i < 0 || j < 0 || i > $cols-1 || j > $rows-1
16
+ return cells_length
17
+ else
18
+ return i + j * $cols
19
+ end
20
+ end
21
+
22
+ def check_neigh_bors(cells)
23
+ @neigh_bors = Array.new
24
+
25
+ top = cells[cell_index(cell_index_x, cell_index_y - 1, cells.length)]
26
+ right = cells[cell_index(cell_index_x + 1, cell_index_y, cells.length)]
27
+ bottom = cells[cell_index(cell_index_x, cell_index_y + 1, cells.length)]
28
+ left = cells[cell_index(cell_index_x - 1, cell_index_y, cells.length)]
29
+
30
+ if top
31
+ if !top.visited
32
+ @neigh_bors.push(top);
33
+ end
34
+ end
35
+
36
+ if right
37
+ if !right.visited
38
+ @neigh_bors.push(right);
39
+ end
40
+ end
41
+
42
+ if bottom
43
+ if !bottom.visited
44
+ @neigh_bors.push(bottom);
45
+ end
46
+ end
47
+
48
+ if left
49
+ if !left.visited
50
+ @neigh_bors.push(left);
51
+ end
52
+ end
53
+
54
+ if @neigh_bors.length > 0
55
+ max = @neigh_bors.length - 1
56
+ random_index = rand(0..max)
57
+ return @neigh_bors[random_index]
58
+ else
59
+ return nil
60
+ end
61
+ end
62
+
63
+ def draw(cell_size, color)
64
+ x = @cell_index_x * cell_size
65
+ y = @cell_index_y * cell_size
66
+
67
+ if @walls[0]
68
+ draw_line x, y, color,
69
+ x + cell_size, y, color
70
+ end
71
+
72
+ if @walls[1]
73
+ draw_line x + cell_size, y, color,
74
+ x + cell_size, y + cell_size, color
75
+ end
76
+
77
+ if @walls[2]
78
+ draw_line x + cell_size, y + cell_size, color,
79
+ x, y + cell_size, color
80
+ end
81
+
82
+ if @walls[3]
83
+ draw_line x, y + cell_size, color,
84
+ x, y, color
85
+ end
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,195 @@
1
+ #!/usr/bin/env ruby
2
+ # credits to: http://en.wikipedia.org/wiki/Maze_generation_algorithm
3
+ require 'gosu'
4
+ include Gosu
5
+
6
+ module AMazeIng
7
+ class GameWindow < Window
8
+ $dimension = 550
9
+ $rows = $cols = 11
10
+
11
+ until $dimension/$cols % 3 == 0
12
+ $dimension -= 1
13
+ end
14
+
15
+ $cell_size = $dimension/$cols
16
+ $speed_per_tick = $cell_size/3
17
+ $player_size = 0.5 * $cell_size
18
+
19
+ $cells = Array.new
20
+
21
+ def initialize
22
+ super $dimension + 100, $dimension, false
23
+ self.caption = "Maze"
24
+ generate_maze
25
+ end
26
+
27
+ def generate_maze
28
+ $cells = Array.new
29
+ @stack = Array.new
30
+ @player = Player.new
31
+ # @player.set_position(0,0)
32
+
33
+ $cell_size = $dimension/$cols
34
+ $player_size = 0.5 * $cell_size
35
+
36
+ $rows.times do |row_index|
37
+ $cols.times do |col_index|
38
+ cell = Cell.new(col_index, row_index)
39
+ $cells.push(cell)
40
+ end
41
+ end
42
+
43
+ $cells[0].visited = true
44
+ @current_cell = $cells[0]
45
+ # @current_cell.is_current = true
46
+ @stack.push(@current_cell)
47
+
48
+ while @stack.length > 0 do
49
+ next_cell = @current_cell.check_neigh_bors($cells)
50
+ if next_cell
51
+ remove_walls(@current_cell, next_cell)
52
+ next_cell.visited = true
53
+ @stack.push(@current_cell)
54
+ # @current_cell.is_current = true
55
+ @current_cell = next_cell
56
+ elsif @stack.length > 0
57
+ # @stack[-1].is_current = false
58
+ @current_cell = @stack.pop
59
+ end
60
+ end
61
+ end
62
+
63
+ def remove_walls(current_cell, next_cell)
64
+ magic_number = next_cell.cell_index_x - current_cell.cell_index_x
65
+ if magic_number == 1
66
+ # next cell is on the right
67
+ current_cell.walls[1] = next_cell.walls[3] = false
68
+ elsif magic_number == -1
69
+ # next cell is on the left
70
+ current_cell.walls[3] = next_cell.walls[1] = false
71
+ elsif magic_number == 0
72
+ # next cell is either on top or bottom
73
+ magic_number = next_cell.cell_index_y - current_cell.cell_index_y
74
+ if magic_number == 1
75
+ #next cell is bottom
76
+ current_cell.walls[2] = next_cell.walls[0] = false
77
+ elsif magic_number ==-1
78
+ #next cell is on top
79
+ current_cell.walls[0] = next_cell.walls[2] = false
80
+ end
81
+ end
82
+ end
83
+
84
+ def draw_target(cell)
85
+ cell_index_x = cell.cell_index_x
86
+ cell_index_y = cell.cell_index_y
87
+ x = (cell_index_x * $cell_size) + $cell_size/2 - $player_size/2
88
+ y = (cell_index_y * $cell_size) + $cell_size/2 - $player_size/2
89
+ draw_quad x, y, Color::GREEN,
90
+ x+$player_size, y, Color::GREEN,
91
+ x+$player_size, y+$player_size, Color::GREEN,
92
+ x, y+$player_size, Color::GREEN
93
+ end
94
+
95
+ def check_for_finish
96
+ if @player.cell_index_x == $cells[-1].cell_index_x && @player.cell_index_y == $cells[-1].cell_index_y
97
+ puts 'finished'
98
+ $rows += 3
99
+ $cols += 3
100
+ generate_maze
101
+ end
102
+ end
103
+
104
+
105
+
106
+ def update
107
+
108
+ end
109
+
110
+ def draw
111
+
112
+ $cells.each do |cell|
113
+ if cell.visited
114
+ cell.draw($cell_size, Color::BLUE)
115
+ else
116
+ cell.draw($cell_size, Color::GREEN)
117
+ end
118
+ end
119
+ @player.draw
120
+ @player.move
121
+ draw_target($cells[-1])
122
+ end
123
+
124
+ # def check_for_path(ignore_path)
125
+ # path = nil
126
+ # $cells[@player.cell_index_x + @player.cell_index_y * $cols].walls.each_with_index do |wall, i|
127
+ # if !wall and i != ignore_path
128
+ # if path == nil
129
+ # path = i
130
+ # else
131
+ # path = nil
132
+ # break
133
+ # end
134
+ # end
135
+ # end
136
+ # return path
137
+ # end
138
+
139
+ def button_down(id)
140
+ if id == Gosu::KB_LEFT
141
+ if !$cells[@player.cell_index_x + @player.cell_index_y * $cols].walls[3]
142
+
143
+ @player.path = 3
144
+ @player.cell_index_x -= 1
145
+ @player.set_target
146
+ @player.is_moving = true
147
+ # @player.move_left
148
+ # check_for_finish
149
+ end
150
+ end
151
+
152
+ if id == Gosu::KB_RIGHT
153
+ if !$cells[@player.cell_index_x + @player.cell_index_y * $cols].walls[1]
154
+
155
+ @player.path = 1
156
+ @player.cell_index_x += 1
157
+ @player.set_target
158
+ @player.is_moving = true
159
+ # @player.move_right
160
+ # check_for_finish
161
+ end
162
+ end
163
+
164
+ if id == Gosu::KB_UP
165
+ if !$cells[@player.cell_index_x + @player.cell_index_y * $cols].walls[0]
166
+
167
+ @player.path = 0
168
+ @player.cell_index_y -= 1
169
+ @player.set_target
170
+ @player.is_moving = true
171
+ # @player.move_up
172
+ # check_for_finish
173
+ end
174
+ end
175
+
176
+ if id == Gosu::KB_DOWN
177
+ if !$cells[@player.cell_index_x + @player.cell_index_y * $cols].walls[2]
178
+
179
+ @player.path = 2
180
+ @player.cell_index_y += 1
181
+ @player.set_target
182
+ @player.is_moving = true
183
+ # @player.move_down
184
+ # check_for_finish
185
+ end
186
+ end
187
+
188
+ if id == Gosu::KB_ESCAPE
189
+ close
190
+ else
191
+ super
192
+ end
193
+ end
194
+ end
195
+ end
@@ -0,0 +1,87 @@
1
+ module AMazeIng
2
+ class Player
3
+ attr_accessor :cell_index_x, :cell_index_y, :color, :target_x, :target_y, :is_moving, :path
4
+ def initialize
5
+ @color = Color::RED
6
+ @cell_index_x = @cell_index_y = 0
7
+ @is_moving = false
8
+ @path = nil
9
+ set_target
10
+ @x = @target_x
11
+ @y = @target_y
12
+ end
13
+
14
+ def set_target
15
+ @target_x = (@cell_index_x * $cell_size) + $cell_size/2 - $player_size/2
16
+ @target_y = (@cell_index_y * $cell_size) + $cell_size/2 - $player_size/2
17
+ end
18
+
19
+ def check_for_path(ignored_path)
20
+ path = nil
21
+ $cells[@cell_index_x + @cell_index_y * $cols].walls.each_with_index do |wall, i|
22
+ if !wall and i != ignored_path
23
+ if path == nil
24
+ path = i
25
+ else
26
+ path = nil
27
+ break
28
+ end
29
+ end
30
+ end
31
+ return path
32
+ end
33
+
34
+ def move
35
+ if @is_moving
36
+ if @x == @target_x && @y == @target_y
37
+ # check for available path,
38
+ # and ignore the the opposite of the LAST path
39
+ # cuz you don't wanna go back where you just left
40
+ @path = check_for_path(@path == 0 ? 2:
41
+ @path == 1 ? 3:
42
+ @path == 2 ? 0: 1)
43
+ if @path != nil
44
+ # set new player's cell index depend on "current @path"
45
+ if @path == 0
46
+ @cell_index_y -= 1
47
+ elsif @path == 1
48
+ @cell_index_x += 1
49
+ elsif @path == 2
50
+ @cell_index_y += 1
51
+ else
52
+ @cell_index_x -= 1
53
+ end
54
+ set_target
55
+ else
56
+
57
+ # no "available" @path found player stop moving
58
+ @is_moving = false
59
+ end
60
+ else
61
+
62
+ # target's position is different than curent position,
63
+ # move to the target
64
+ if @x < @target_x
65
+ @x += $speed_per_tick
66
+ elsif @x > @target_x
67
+ @x -= $speed_per_tick
68
+ end
69
+
70
+ if @y < @target_y
71
+ @y += $speed_per_tick
72
+ elsif @y > @target_y
73
+ @y -= $speed_per_tick
74
+ end
75
+
76
+ end
77
+ end
78
+ end
79
+
80
+ def draw
81
+ draw_quad @x, @y, @color,
82
+ @x+$player_size, @y, @color,
83
+ @x+$player_size, @y+$player_size, @color,
84
+ @x, @y+$player_size, @color
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,3 @@
1
+ module AMazeIng
2
+ VERSION = "0.1.0"
3
+ end
data/lib/a_maze_ing.rb ADDED
@@ -0,0 +1,4 @@
1
+ require "a_maze_ing/version"
2
+ require "a_maze_ing/cell"
3
+ require "a_maze_ing/player"
4
+ require "a_maze_ing/maze"
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: a_maze_ing
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - gt-cuongtran
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-06-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.15'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.15'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: gosu
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.12.0
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.12.0
69
+ description:
70
+ email:
71
+ - cuong.tran.asiantech@gmail.com
72
+ executables:
73
+ - a_maze_ing
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".rspec"
79
+ - ".travis.yml"
80
+ - Gemfile
81
+ - README.md
82
+ - Rakefile
83
+ - a_maze_ing.gemspec
84
+ - bin/a_maze_ing
85
+ - bin/console
86
+ - bin/setup
87
+ - lib/a_maze_ing.rb
88
+ - lib/a_maze_ing/cell.rb
89
+ - lib/a_maze_ing/maze.rb
90
+ - lib/a_maze_ing/player.rb
91
+ - lib/a_maze_ing/version.rb
92
+ homepage:
93
+ licenses:
94
+ - MIT
95
+ metadata: {}
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 2.6.11
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: maze solving game
116
+ test_files: []