mineswiper 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +49 -0
- data/Gemfile +8 -0
- data/Guardfile +70 -0
- data/LICENSE.txt +21 -0
- data/README.md +41 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/mineswiper/board.rb +145 -0
- data/lib/mineswiper/display.rb +124 -0
- data/lib/mineswiper/player.rb +23 -0
- data/lib/mineswiper/tile.rb +47 -0
- data/lib/mineswiper/version.rb +3 -0
- data/lib/mineswiper.rb +46 -0
- data/mineswiper.gemspec +35 -0
- metadata +118 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: f6b38ed1b4be75ea36f861f0424264be0cc23019
|
4
|
+
data.tar.gz: f527a31c8b794270e5f9ed7feb0fb063b1d206dc
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 36a551d2e7248efefb91495517214f26a9b2b74c0aad6c92eef42e166d58fc76d4cabfb6c38b0ba292b91fce1067d63d00e204b4bbf023291c5351f87164a572
|
7
|
+
data.tar.gz: b1bc60017ea4dcef23ddad27a42ccda593d5e747d97de100bd311c4f302f53a2e0a4c8ef4c5304d7a82d9dec8b27eb99fd18d6ab7a3aec62d6906967651b916e
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, and in the interest of
|
4
|
+
fostering an open and welcoming community, we pledge to respect all people who
|
5
|
+
contribute through reporting issues, posting feature requests, updating
|
6
|
+
documentation, submitting pull requests or patches, and other activities.
|
7
|
+
|
8
|
+
We are committed to making participation in this project a harassment-free
|
9
|
+
experience for everyone, regardless of level of experience, gender, gender
|
10
|
+
identity and expression, sexual orientation, disability, personal appearance,
|
11
|
+
body size, race, ethnicity, age, religion, or nationality.
|
12
|
+
|
13
|
+
Examples of unacceptable behavior by participants include:
|
14
|
+
|
15
|
+
* The use of sexualized language or imagery
|
16
|
+
* Personal attacks
|
17
|
+
* Trolling or insulting/derogatory comments
|
18
|
+
* Public or private harassment
|
19
|
+
* Publishing other's private information, such as physical or electronic
|
20
|
+
addresses, without explicit permission
|
21
|
+
* Other unethical or unprofessional conduct
|
22
|
+
|
23
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
24
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
25
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
26
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
27
|
+
threatening, offensive, or harmful.
|
28
|
+
|
29
|
+
By adopting this Code of Conduct, project maintainers commit themselves to
|
30
|
+
fairly and consistently applying these principles to every aspect of managing
|
31
|
+
this project. Project maintainers who do not follow or enforce the Code of
|
32
|
+
Conduct may be permanently removed from the project team.
|
33
|
+
|
34
|
+
This code of conduct applies both within project spaces and in public spaces
|
35
|
+
when an individual is representing the project or its community.
|
36
|
+
|
37
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
38
|
+
reported by contacting a project maintainer at lby89757@hotmail.com. All
|
39
|
+
complaints will be reviewed and investigated and will result in a response that
|
40
|
+
is deemed necessary and appropriate to the circumstances. Maintainers are
|
41
|
+
obligated to maintain confidentiality with regard to the reporter of an
|
42
|
+
incident.
|
43
|
+
|
44
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
45
|
+
version 1.3.0, available at
|
46
|
+
[http://contributor-covenant.org/version/1/3/0/][version]
|
47
|
+
|
48
|
+
[homepage]: http://contributor-covenant.org
|
49
|
+
[version]: http://contributor-covenant.org/version/1/3/0/
|
data/Gemfile
ADDED
data/Guardfile
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
# A sample Guardfile
|
2
|
+
# More info at https://github.com/guard/guard#readme
|
3
|
+
|
4
|
+
## Uncomment and set this to only include directories you want to watch
|
5
|
+
# directories %w(app lib config test spec features) \
|
6
|
+
# .select{|d| Dir.exists?(d) ? d : UI.warning("Directory #{d} does not exist")}
|
7
|
+
|
8
|
+
## Note: if you are using the `directories` clause above and you are not
|
9
|
+
## watching the project directory ('.'), then you will want to move
|
10
|
+
## the Guardfile to a watched dir and symlink it back, e.g.
|
11
|
+
#
|
12
|
+
# $ mkdir config
|
13
|
+
# $ mv Guardfile config/
|
14
|
+
# $ ln -s config/Guardfile .
|
15
|
+
#
|
16
|
+
# and, you'll have to watch "config/Guardfile" instead of "Guardfile"
|
17
|
+
|
18
|
+
# Note: The cmd option is now required due to the increasing number of ways
|
19
|
+
# rspec may be run, below are examples of the most common uses.
|
20
|
+
# * bundler: 'bundle exec rspec'
|
21
|
+
# * bundler binstubs: 'bin/rspec'
|
22
|
+
# * spring: 'bin/rspec' (This will use spring if running and you have
|
23
|
+
# installed the spring binstubs per the docs)
|
24
|
+
# * zeus: 'zeus rspec' (requires the server to be started separately)
|
25
|
+
# * 'just' rspec: 'rspec'
|
26
|
+
|
27
|
+
guard :rspec, cmd: "bundle exec rspec" do
|
28
|
+
require "guard/rspec/dsl"
|
29
|
+
dsl = Guard::RSpec::Dsl.new(self)
|
30
|
+
|
31
|
+
# Feel free to open issues for suggestions and improvements
|
32
|
+
|
33
|
+
# RSpec files
|
34
|
+
rspec = dsl.rspec
|
35
|
+
watch(rspec.spec_helper) { rspec.spec_dir }
|
36
|
+
watch(rspec.spec_support) { rspec.spec_dir }
|
37
|
+
watch(rspec.spec_files)
|
38
|
+
|
39
|
+
# Ruby files
|
40
|
+
ruby = dsl.ruby
|
41
|
+
dsl.watch_spec_files_for(ruby.lib_files)
|
42
|
+
|
43
|
+
# Rails files
|
44
|
+
rails = dsl.rails(view_extensions: %w(erb haml slim))
|
45
|
+
dsl.watch_spec_files_for(rails.app_files)
|
46
|
+
dsl.watch_spec_files_for(rails.views)
|
47
|
+
|
48
|
+
watch(rails.controllers) do |m|
|
49
|
+
[
|
50
|
+
rspec.spec.call("routing/#{m[1]}_routing"),
|
51
|
+
rspec.spec.call("controllers/#{m[1]}_controller"),
|
52
|
+
rspec.spec.call("acceptance/#{m[1]}")
|
53
|
+
]
|
54
|
+
end
|
55
|
+
|
56
|
+
# Rails config changes
|
57
|
+
watch(rails.spec_helper) { rspec.spec_dir }
|
58
|
+
watch(rails.routes) { "#{rspec.spec_dir}/routing" }
|
59
|
+
watch(rails.app_controller) { "#{rspec.spec_dir}/controllers" }
|
60
|
+
|
61
|
+
# Capybara features specs
|
62
|
+
watch(rails.view_dirs) { |m| rspec.spec.call("features/#{m[1]}") }
|
63
|
+
watch(rails.layouts) { |m| rspec.spec.call("features/#{m[1]}") }
|
64
|
+
|
65
|
+
# Turnip features and steps
|
66
|
+
watch(%r{^spec/acceptance/(.+)\.feature$})
|
67
|
+
watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) do |m|
|
68
|
+
Dir[File.join("**/#{m[1]}.feature")][0] || "spec/acceptance"
|
69
|
+
end
|
70
|
+
end
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 BranLiang
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# Mineswiper
|
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/mineswiper`. 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 'mineswiper'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install mineswiper
|
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]/mineswiper. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
36
|
+
|
37
|
+
|
38
|
+
## License
|
39
|
+
|
40
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
41
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "mineswiper"
|
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
|
data/bin/setup
ADDED
@@ -0,0 +1,145 @@
|
|
1
|
+
module Mineswiper
|
2
|
+
|
3
|
+
class Board
|
4
|
+
|
5
|
+
SIZE = 24
|
6
|
+
MINE_PERCENTAGE = 10
|
7
|
+
OFFSETS = [[-1, -1], [-1, 0], [-1, 1], [0, 1], [1, 1], [1, 0], [1, -1], [0, -1]]
|
8
|
+
|
9
|
+
attr_accessor :grid, :savegame
|
10
|
+
attr_reader :all_indices, :lost, :won
|
11
|
+
|
12
|
+
def initialize(grid=Array.new(SIZE) { Array.new(SIZE) })
|
13
|
+
@grid = grid
|
14
|
+
@lost = false
|
15
|
+
@won = false
|
16
|
+
@all_indices = []
|
17
|
+
end
|
18
|
+
|
19
|
+
def prepared_board
|
20
|
+
populate_mines
|
21
|
+
get_indices
|
22
|
+
assign_bomb_counts
|
23
|
+
end
|
24
|
+
|
25
|
+
def populate_mines
|
26
|
+
# vals = [:bomb]
|
27
|
+
@grid.each_index do |row|
|
28
|
+
@grid[row].each_index do |column|
|
29
|
+
@grid[row][column] = make_random_tiles([row, column])
|
30
|
+
end
|
31
|
+
end
|
32
|
+
return true
|
33
|
+
end
|
34
|
+
|
35
|
+
def make_random_tiles(pos)
|
36
|
+
bomb_status = title_roll
|
37
|
+
Tile.new(pos, bomb_status)
|
38
|
+
end
|
39
|
+
|
40
|
+
def title_roll
|
41
|
+
roll = rand(100)
|
42
|
+
roll <= MINE_PERCENTAGE
|
43
|
+
end
|
44
|
+
|
45
|
+
def assign_bomb_counts
|
46
|
+
@all_indices.each do |pos|
|
47
|
+
count_adjacent_bombs(pos)
|
48
|
+
end
|
49
|
+
return true
|
50
|
+
end
|
51
|
+
|
52
|
+
def get_indices
|
53
|
+
(0...SIZE).each do |row|
|
54
|
+
(0...SIZE).each do |col|
|
55
|
+
@all_indices << [row, col]
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def count_adjacent_bombs(pos)
|
61
|
+
row, col = pos
|
62
|
+
neighbors = find_neighbers(pos)
|
63
|
+
neighbors.each do |neighbor|
|
64
|
+
row2, col2 = neighbor
|
65
|
+
if @grid[row2][col2].bomb?
|
66
|
+
@grid[row][col].adj_bombs += 1
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def find_neighbers(pos)
|
72
|
+
row, col = pos
|
73
|
+
neighbors = []
|
74
|
+
OFFSETS.each do |pos|
|
75
|
+
drow, dcol = [pos[0]+row, pos[1]+col]
|
76
|
+
unless out_of_bounds?([drow, dcol])
|
77
|
+
neighbors << [drow, dcol]
|
78
|
+
end
|
79
|
+
end
|
80
|
+
neighbors
|
81
|
+
end
|
82
|
+
|
83
|
+
def out_of_bounds?(pos)
|
84
|
+
drow, dcol = pos
|
85
|
+
[drow, dcol].any? { |el| el < 0 || (el > (SIZE - 1))}
|
86
|
+
end
|
87
|
+
|
88
|
+
def parse_input(input)
|
89
|
+
return if input == nil
|
90
|
+
if input.include?(:flag)
|
91
|
+
flag_pos(input[1])
|
92
|
+
else
|
93
|
+
eval_move(input)
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
def flag_pos(pos)
|
98
|
+
x, y = pos
|
99
|
+
tile = @grid[x][y]
|
100
|
+
return if tile.hidden == false
|
101
|
+
tile.flagged? ? tile.flag = false : tile.flag_it
|
102
|
+
end
|
103
|
+
|
104
|
+
def lost?
|
105
|
+
@lost
|
106
|
+
end
|
107
|
+
|
108
|
+
def won?
|
109
|
+
@all_indices.each do |tile|
|
110
|
+
x, y = tile
|
111
|
+
if grid[x][y].hidden == true && grid[x][y].bomb == false
|
112
|
+
return false
|
113
|
+
end
|
114
|
+
end
|
115
|
+
return true
|
116
|
+
end
|
117
|
+
|
118
|
+
def eval_move(pos)
|
119
|
+
x, y = pos
|
120
|
+
if grid[x][y].bomb
|
121
|
+
@lost = true
|
122
|
+
else
|
123
|
+
reveal_titles(pos)
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
def reveal_titles(pos)
|
128
|
+
x, y = pos
|
129
|
+
return if grid[x][y].flagged?
|
130
|
+
grid[x][y].hidden = false
|
131
|
+
return if grid[x][y].adj_bombs > 0
|
132
|
+
neighbors = find_neighbers([x, y])
|
133
|
+
neighbors.each do |neighbor_pos|
|
134
|
+
x, y = neighbor_pos
|
135
|
+
reveal_titles(neighbor_pos) unless grid[x][y].hidden == false || grid[x][y].bomb
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
def in_bounds?(pos)
|
140
|
+
pos.all? { |x| x.between?(0, SIZE-1) }
|
141
|
+
end
|
142
|
+
|
143
|
+
end
|
144
|
+
|
145
|
+
end
|
@@ -0,0 +1,124 @@
|
|
1
|
+
module Mineswiper
|
2
|
+
class Display
|
3
|
+
|
4
|
+
COLORS = {
|
5
|
+
0 => :light_black,
|
6
|
+
1 => :green,
|
7
|
+
2 => :cyan,
|
8
|
+
3 => :light_blue,
|
9
|
+
4 => :blue,
|
10
|
+
5 => :light_magenta,
|
11
|
+
6 => :magenta,
|
12
|
+
7 => :light_black,
|
13
|
+
8 => :black
|
14
|
+
}
|
15
|
+
|
16
|
+
GLOBAL_BACKGROUND = :light_white
|
17
|
+
|
18
|
+
|
19
|
+
attr_accessor :board
|
20
|
+
|
21
|
+
def initialize(board)
|
22
|
+
@board = board
|
23
|
+
@cursor_pos = [0, 0]
|
24
|
+
end
|
25
|
+
|
26
|
+
def build_grid
|
27
|
+
@board.grid.each_index do |i|
|
28
|
+
rowdisplay = ""
|
29
|
+
@board.grid[i].each_index do |j|
|
30
|
+
output = board.grid[i][j].tilerender
|
31
|
+
color_options = colors_for(i, j)
|
32
|
+
rowdisplay << output.colorize(color_options) + " ".colorize(background: GLOBAL_BACKGROUND) unless output.nil?
|
33
|
+
end
|
34
|
+
puts rowdisplay
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def colors_for(i, j)
|
39
|
+
if [i, j] == @cursor_pos
|
40
|
+
bg = :light_red
|
41
|
+
else
|
42
|
+
bg = GLOBAL_BACKGROUND
|
43
|
+
end
|
44
|
+
|
45
|
+
if (@board.grid[i][j].hidden == false)
|
46
|
+
color = COLORS[@board.grid[i][j].adj_bombs]
|
47
|
+
if @board.grid[i][j].bomb?
|
48
|
+
color = :light_red
|
49
|
+
end
|
50
|
+
elsif @board.grid[i][j].flagged?
|
51
|
+
color = :light_red
|
52
|
+
else
|
53
|
+
color = :white
|
54
|
+
end
|
55
|
+
{ background: bg, color: color }
|
56
|
+
end
|
57
|
+
|
58
|
+
def render
|
59
|
+
system("clear")
|
60
|
+
puts "W-A-S-D to move the cursor - Spacebar to reveal."
|
61
|
+
build_grid
|
62
|
+
end
|
63
|
+
|
64
|
+
KEYMAP = {
|
65
|
+
" " => :space,
|
66
|
+
"f" => :f,
|
67
|
+
"a" => :left,
|
68
|
+
"s" => :down,
|
69
|
+
"w" => :up,
|
70
|
+
"d" => :right,
|
71
|
+
"\u0003" => :ctrl_c,
|
72
|
+
}
|
73
|
+
|
74
|
+
MOVES = {
|
75
|
+
left: [0, -1],
|
76
|
+
right: [0, 1],
|
77
|
+
up: [-1, 0],
|
78
|
+
down: [1, 0]
|
79
|
+
}
|
80
|
+
|
81
|
+
def get_input
|
82
|
+
key = KEYMAP[read_char]
|
83
|
+
handle_key(key)
|
84
|
+
end
|
85
|
+
|
86
|
+
def handle_key(key)
|
87
|
+
case key
|
88
|
+
when :ctrl_c
|
89
|
+
exit 0
|
90
|
+
when :space
|
91
|
+
@cursor_pos
|
92
|
+
when :left, :right, :up, :down
|
93
|
+
update_pos(MOVES[key])
|
94
|
+
nil
|
95
|
+
when :f
|
96
|
+
[:flag, @cursor_pos]
|
97
|
+
else
|
98
|
+
puts key
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
def read_char
|
103
|
+
STDIN.echo = false
|
104
|
+
STDIN.raw!
|
105
|
+
|
106
|
+
input = STDIN.getc.chr
|
107
|
+
if input == "\e" then
|
108
|
+
input << STDIN.read_nonblock(3) rescue nil
|
109
|
+
input << STDIN.read_nonblock(2) rescue nil
|
110
|
+
end
|
111
|
+
ensure
|
112
|
+
STDIN.echo = true
|
113
|
+
STDIN.cooked!
|
114
|
+
|
115
|
+
return input
|
116
|
+
end
|
117
|
+
|
118
|
+
def update_pos(diff)
|
119
|
+
new_pos = [@cursor_pos[0] + diff[0], @cursor_pos[1] + diff[1]]
|
120
|
+
@cursor_pos = new_pos if @board.in_bounds?(new_pos)
|
121
|
+
end
|
122
|
+
|
123
|
+
end
|
124
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module Mineswiper
|
2
|
+
|
3
|
+
class Player
|
4
|
+
|
5
|
+
attr_reader :display
|
6
|
+
|
7
|
+
def initialize(board)
|
8
|
+
@display = Display.new(board)
|
9
|
+
end
|
10
|
+
|
11
|
+
def move
|
12
|
+
result = nil
|
13
|
+
until result
|
14
|
+
@display.render
|
15
|
+
result = @display.get_input
|
16
|
+
end
|
17
|
+
result
|
18
|
+
end
|
19
|
+
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module Mineswiper
|
2
|
+
class Tile
|
3
|
+
|
4
|
+
attr_accessor :state, :flag, :hidden, :number, :adj_bombs
|
5
|
+
attr_reader :pos, :bomb
|
6
|
+
|
7
|
+
def initialize(pos, bomb=false)
|
8
|
+
@pos, @bomb = pos, bomb
|
9
|
+
@hidden = true
|
10
|
+
@flag = false
|
11
|
+
@adj_bombs = 0
|
12
|
+
end
|
13
|
+
|
14
|
+
def reveal
|
15
|
+
@hidden = false
|
16
|
+
end
|
17
|
+
|
18
|
+
def bomb?
|
19
|
+
@bomb == true
|
20
|
+
end
|
21
|
+
|
22
|
+
def flagged?
|
23
|
+
@flag
|
24
|
+
end
|
25
|
+
|
26
|
+
def flag_it
|
27
|
+
@flag = true
|
28
|
+
end
|
29
|
+
|
30
|
+
def unflag
|
31
|
+
flag = false
|
32
|
+
end
|
33
|
+
|
34
|
+
def tilerender
|
35
|
+
if flagged?
|
36
|
+
return "\u2691".encode('utf-8')
|
37
|
+
elsif @hidden
|
38
|
+
return "\u25A0".encode('utf-8')
|
39
|
+
elsif bomb?
|
40
|
+
return "\u2622".encode('utf-8')
|
41
|
+
else
|
42
|
+
return self.adj_bombs.to_s
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
end
|
data/lib/mineswiper.rb
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
require_relative "mineswiper/version"
|
2
|
+
require_relative "mineswiper/board"
|
3
|
+
require_relative "mineswiper/tile"
|
4
|
+
require_relative 'mineswiper/player'
|
5
|
+
require_relative 'mineswiper/display'
|
6
|
+
require 'colorize'
|
7
|
+
require 'io/console'
|
8
|
+
require 'pry-byebug'
|
9
|
+
|
10
|
+
module Mineswiper
|
11
|
+
class Game
|
12
|
+
|
13
|
+
attr_accessor :board, :name, :player
|
14
|
+
|
15
|
+
def initialize(name: "Player1", board: Board.new)
|
16
|
+
@board = board
|
17
|
+
@board.prepared_board
|
18
|
+
@player = Player.new(@board)
|
19
|
+
end
|
20
|
+
|
21
|
+
def play
|
22
|
+
until game_over?
|
23
|
+
# binding.pry
|
24
|
+
board.parse_input(player.move)
|
25
|
+
end
|
26
|
+
|
27
|
+
if board.lost?
|
28
|
+
board.all_indices.each do |idx|
|
29
|
+
x, y = idx
|
30
|
+
board.grid[x][y].hidden = false
|
31
|
+
end
|
32
|
+
player.display.render
|
33
|
+
puts "You lose! BOOOM"
|
34
|
+
else
|
35
|
+
puts "You win"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def game_over?
|
40
|
+
board.won? || board.lost?
|
41
|
+
end
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
end
|
46
|
+
end
|
data/mineswiper.gemspec
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'mineswiper/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "mineswiper"
|
8
|
+
spec.version = Mineswiper::VERSION
|
9
|
+
spec.authors = ["BranLiang"]
|
10
|
+
spec.email = ["lby89757@hotmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Command line version Minesweeper game. Original author: https://github.com/ndevvy/minesweeper}
|
13
|
+
spec.description = %q{require 'mineswiper'}
|
14
|
+
spec.homepage = "http://www.liangboyuan.pub"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
18
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
19
|
+
# if spec.respond_to?(:metadata)
|
20
|
+
# spec.metadata['allowed_push_host'] = 'https://rubygems.org/'
|
21
|
+
# else
|
22
|
+
# raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
|
23
|
+
# end
|
24
|
+
|
25
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
26
|
+
spec.bindir = "exe"
|
27
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
|
+
spec.require_paths = ["lib"]
|
29
|
+
|
30
|
+
spec.add_development_dependency "bundler", "~> 1.12"
|
31
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
32
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
33
|
+
|
34
|
+
spec.add_dependency "colorize"
|
35
|
+
end
|
metadata
ADDED
@@ -0,0 +1,118 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mineswiper
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- BranLiang
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-08-01 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.12'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.12'
|
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: colorize
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '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'
|
69
|
+
description: require 'mineswiper'
|
70
|
+
email:
|
71
|
+
- lby89757@hotmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rspec"
|
78
|
+
- ".travis.yml"
|
79
|
+
- CODE_OF_CONDUCT.md
|
80
|
+
- Gemfile
|
81
|
+
- Guardfile
|
82
|
+
- LICENSE.txt
|
83
|
+
- README.md
|
84
|
+
- Rakefile
|
85
|
+
- bin/console
|
86
|
+
- bin/setup
|
87
|
+
- lib/mineswiper.rb
|
88
|
+
- lib/mineswiper/board.rb
|
89
|
+
- lib/mineswiper/display.rb
|
90
|
+
- lib/mineswiper/player.rb
|
91
|
+
- lib/mineswiper/tile.rb
|
92
|
+
- lib/mineswiper/version.rb
|
93
|
+
- mineswiper.gemspec
|
94
|
+
homepage: http://www.liangboyuan.pub
|
95
|
+
licenses:
|
96
|
+
- MIT
|
97
|
+
metadata: {}
|
98
|
+
post_install_message:
|
99
|
+
rdoc_options: []
|
100
|
+
require_paths:
|
101
|
+
- lib
|
102
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
103
|
+
requirements:
|
104
|
+
- - ">="
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
version: '0'
|
107
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
108
|
+
requirements:
|
109
|
+
- - ">="
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
version: '0'
|
112
|
+
requirements: []
|
113
|
+
rubyforge_project:
|
114
|
+
rubygems_version: 2.5.1
|
115
|
+
signing_key:
|
116
|
+
specification_version: 4
|
117
|
+
summary: 'Command line version Minesweeper game. Original author: https://github.com/ndevvy/minesweeper'
|
118
|
+
test_files: []
|