rubytris 1.0.1

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: 3a9ad03df8c268b8e0055bf03c02cd90ff2b1154
4
+ data.tar.gz: '09dfd091bf76f565abceb5de69c16d4a54e98fee'
5
+ SHA512:
6
+ metadata.gz: 80157df4dee72643b5a381ecadc1c9654a64ac22ef6365466b25593bbc6ba143d4527b4333d0b6ad4ac0b857a4e3a432bb524241e17d8a1fa518a574f6e128de
7
+ data.tar.gz: 6a3d8d1be0296533ade68bd3897c5e236ee4479578a8567fcdace11e4e9ae2d614eae3b69d35cf1591041b144d9426e4458157bb2b94a8a2b3a65fb4b76dcd72
data/.gitignore ADDED
@@ -0,0 +1,12 @@
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
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.3
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at d0o0b_lx3xl@yahoo.co.jp. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in rubytris.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 LeafChage
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,43 @@
1
+ # Rubytris
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/rubytris`. 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 'rubytris'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install rubytris
22
+
23
+ ## Usage
24
+
25
+ $ Rubytris.tetris()
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]/rubytris. 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
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
40
+
41
+ ## Code of Conduct
42
+
43
+ Everyone interacting in the Rubytris project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/rubytris/blob/master/CODE_OF_CONDUCT.md).
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
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "rubytris"
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
data/exe/rubytris ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubytris'
4
+ Rubytris.tetris
5
+
6
+
7
+
@@ -0,0 +1,98 @@
1
+ module Rubytris
2
+ class Current
3
+ BLOCKS = [
4
+ [ [0, 1, 0, 0],
5
+ [1, 1, 1, 0],
6
+ [0, 0, 0, 0],
7
+ [0, 0, 0, 0], ],
8
+
9
+ [ [0, 1, 1, 0],
10
+ [1, 1, 0, 0],
11
+ [0, 0, 0, 0],
12
+ [0, 0, 0, 0], ],
13
+
14
+ [ [1, 1, 0, 0],
15
+ [0, 1, 1, 0],
16
+ [0, 0, 0, 0],
17
+ [0, 0, 0, 0], ],
18
+
19
+ [ [1, 1, 1, 1],
20
+ [0, 0, 0, 0],
21
+ [0, 0, 0, 0],
22
+ [0, 0, 0, 0], ],
23
+
24
+ [ [1, 1, 0, 0],
25
+ [1, 1, 0, 0],
26
+ [0, 0, 0, 0],
27
+ [0, 0, 0, 0], ],
28
+
29
+ [ [1, 0, 0, 0],
30
+ [1, 1, 1, 0],
31
+ [0, 0, 0, 0],
32
+ [0, 0, 0, 0], ],
33
+
34
+ [ [0, 0, 1, 0],
35
+ [1, 1, 1, 0],
36
+ [0, 0, 0, 0],
37
+ [0, 0, 0, 0], ],
38
+ ]
39
+
40
+ def initialize(x, y, current = new_block)
41
+ @x = x
42
+ @y = y
43
+ @current = current
44
+ end
45
+
46
+ def move_position()
47
+ count = 0
48
+ block_position = Array.new(4)
49
+ @current.each_with_index do |block, y|
50
+ block.each_with_index do |b, x|
51
+ if b == 1
52
+ block_position[count] = [ @x + x, @y + y ]
53
+ count += 1
54
+ end
55
+ end
56
+ end
57
+ block_position
58
+ end
59
+
60
+ # move right
61
+ def right()
62
+ Current.new(@x + 1, @y, @current)
63
+ end
64
+
65
+ #move left
66
+ def left()
67
+ Current.new(@x - 1, @y, @current)
68
+ end
69
+
70
+ #move fall
71
+ def fall()
72
+ Current.new(@x, @y + 1, @current)
73
+ end
74
+
75
+ #move rotation
76
+ def rotation()
77
+ tmp = Array.new(4){ Array.new(4, 0)}
78
+ @current.each_with_index do |line, y|
79
+ line.each_with_index do |l, x|
80
+ tmp[x][y] = l
81
+ end
82
+ end
83
+ tmp.each_with_index do |line, i|
84
+ tmp[i] = line.reverse
85
+ end
86
+ Current.new(@x, @y, tmp)
87
+ end
88
+
89
+ #create new block
90
+ def new_block()
91
+ rnd = rand(BLOCKS.length)
92
+ BLOCKS[rnd]
93
+ end
94
+ private
95
+ end
96
+
97
+ end
98
+
@@ -0,0 +1,103 @@
1
+ module Rubytris
2
+ module FieldStatus
3
+ NONE = 0
4
+ WALL = 1
5
+ ACTIVE = 2
6
+ FIX = 3
7
+ end
8
+
9
+ class Field
10
+ WIDTH = 11
11
+ HEIGHT = 20
12
+ FINISH_COUNT = 40
13
+ WALL_CHAR = "□ "
14
+ BLOCK_CHAR = "■ "
15
+ NONE_CHAR = " "
16
+ attr_reader :point
17
+
18
+ def initialize()
19
+ @field = init_field
20
+ @point = 0
21
+ end
22
+
23
+ def clear()
24
+ @field.each_with_index do |line, y|
25
+ line.each_with_index do |l, x|
26
+ @field[y][x] = 0 if l == FieldStatus::ACTIVE
27
+ end
28
+ end
29
+ end
30
+
31
+ def line_clear()
32
+ @field.each_with_index do |line, y|
33
+ if line == [FieldStatus::WALL, FieldStatus::FIX, FieldStatus::FIX, FieldStatus::FIX, FieldStatus::FIX, FieldStatus::FIX, FieldStatus::FIX, FieldStatus::FIX, FieldStatus::FIX, FieldStatus::FIX, FieldStatus::WALL]
34
+ @point += 1
35
+ @field.delete_at(y)
36
+ @field.insert(0, [FieldStatus::WALL, FieldStatus::NONE, FieldStatus::NONE, FieldStatus::NONE, FieldStatus::NONE, FieldStatus::NONE, FieldStatus::NONE, FieldStatus::NONE, FieldStatus::NONE, FieldStatus::NONE, FieldStatus::WALL])
37
+ end
38
+ end
39
+ end
40
+
41
+ def game_finish?()
42
+ @point >= 40
43
+ end
44
+
45
+ def are_block?(next_pos)
46
+ result = false
47
+ next_pos.each do |pos|
48
+ if is_block?(pos[0], pos[1])
49
+ result = true
50
+ end
51
+ end
52
+ result
53
+ end
54
+
55
+ def fix(now_pos)
56
+ now_pos.each do |pos|
57
+ @field[pos[1]][pos[0]] = FieldStatus::FIX
58
+ end
59
+ end
60
+
61
+ def pre_fix(now_pos)
62
+ now_pos.each do |pos|
63
+ @field[pos[1]][pos[0]] = FieldStatus::ACTIVE
64
+ end
65
+ end
66
+
67
+ def write()
68
+ text = "\n\e[25D"
69
+ @field.each do |line|
70
+ line.each do |l|
71
+ if l == FieldStatus::NONE
72
+ text += NONE_CHAR
73
+ elsif l == FieldStatus::ACTIVE || l == FieldStatus::FIX
74
+ text += BLOCK_CHAR
75
+ else
76
+ text += WALL_CHAR
77
+ end
78
+ end
79
+ text += "\n\e[25D"
80
+ end
81
+ puts text
82
+ end
83
+
84
+ private
85
+ def is_block?(x, y)
86
+ @field[y][x] == FieldStatus::WALL || @field[y][x] == FieldStatus::FIX
87
+ end
88
+
89
+ def init_field
90
+ f = []
91
+ HEIGHT.times do |i|
92
+ line = []
93
+ WIDTH.times do |j|
94
+ line[j] = (j == 0 || j == WIDTH - 1 || i == HEIGHT - 1) ? FieldStatus::WALL : FieldStatus::NONE
95
+ end
96
+ f[i] = line
97
+ end
98
+ return f
99
+ end
100
+ end
101
+
102
+ end
103
+
@@ -0,0 +1,3 @@
1
+ module Rubytris
2
+ VERSION = "1.0.1"
3
+ end
data/lib/rubytris.rb ADDED
@@ -0,0 +1,86 @@
1
+ require "rubytris/version"
2
+ require 'io/console'
3
+ require 'rubytris/field'
4
+ require 'rubytris/current'
5
+
6
+ module Rubytris
7
+ extend self
8
+ def display_clear()
9
+ print "\x1b[2J\x1b[0;0H"
10
+ end
11
+
12
+ def fall_to_block(current, field)
13
+ result = current
14
+ loop do
15
+ next_pos = result.fall.move_position
16
+ if field.are_block?(next_pos)
17
+ return result
18
+ else
19
+ result = result.fall
20
+ end
21
+ end
22
+ end
23
+
24
+ def tetris()
25
+ display_clear()
26
+
27
+ field = Field.new()
28
+ field.write()
29
+
30
+ current = Current.new(5, 0)
31
+
32
+ cmd = 'n'
33
+ thread = Thread::start do
34
+ while (cmd = STDIN.getch)
35
+ if cmd == "\C-c"
36
+ break
37
+ end
38
+ end
39
+ end
40
+ loop do
41
+ sleep(0.2)
42
+ self.display_clear()
43
+ puts "left: f, right: j, rotate: space, q: exit \e[25D"
44
+ puts "\e[25D#{field.point}\e[25D"
45
+ case cmd
46
+ when 'q' then
47
+ exit()
48
+ when 'f' then
49
+ tmp = current.left
50
+ when 'j' then
51
+ tmp = current.right
52
+ when 'b' then
53
+ tmp = self.fall_to_block(current, field)
54
+ when ' ' then
55
+ tmp = current.rotation
56
+ else
57
+ tmp = current.fall
58
+ end
59
+ next_pos = tmp.move_position
60
+
61
+ if !field.are_block?(next_pos)
62
+ field.pre_fix(next_pos)
63
+ current = tmp
64
+ elsif cmd != 'n'
65
+ current = current
66
+ else
67
+ field.fix(current.move_position)
68
+ current = Current.new(5, 0)
69
+ next_pos = current.move_position
70
+ if field.are_block?(next_pos)
71
+ puts "game over"
72
+ break
73
+ end
74
+ end
75
+ field.write
76
+ field.clear
77
+ field.line_clear
78
+ cmd = 'n'
79
+ if field.game_finish?()
80
+ puts "success!!!!!!"
81
+ break
82
+ end
83
+ end
84
+ Thread.kill(thread)
85
+ end
86
+ end
data/rubytris.gemspec ADDED
@@ -0,0 +1,36 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "rubytris/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rubytris"
8
+ spec.version = Rubytris::VERSION
9
+ spec.authors = ["LeafChage"]
10
+ spec.email = [""]
11
+
12
+ spec.summary = %q{ruby for tetris}
13
+ spec.description = %q{ruby for tetris}
14
+ spec.homepage = ""
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"] = "TODO: Set to 'http://mygemserver.com'"
21
+ # else
22
+ # raise "RubyGems 2.0 or newer is required to protect against " \
23
+ # "public gem pushes."
24
+ # end
25
+ #
26
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
27
+ f.match(%r{^(test|spec|features)/})
28
+ end
29
+ spec.bindir = "bin"
30
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib"]
32
+
33
+ spec.add_development_dependency "bundler", "~> 1.15"
34
+ spec.add_development_dependency "rake", "~> 10.0"
35
+ spec.add_development_dependency "rspec", "~> 3.0"
36
+ end
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rubytris
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - LeafChage
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-10-18 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
+ description: ruby for tetris
56
+ email:
57
+ - ''
58
+ executables:
59
+ - console
60
+ - setup
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - ".gitignore"
65
+ - ".rspec"
66
+ - ".travis.yml"
67
+ - CODE_OF_CONDUCT.md
68
+ - Gemfile
69
+ - LICENSE.txt
70
+ - README.md
71
+ - Rakefile
72
+ - bin/console
73
+ - bin/setup
74
+ - exe/rubytris
75
+ - lib/rubytris.rb
76
+ - lib/rubytris/current.rb
77
+ - lib/rubytris/field.rb
78
+ - lib/rubytris/version.rb
79
+ - rubytris.gemspec
80
+ homepage: ''
81
+ licenses:
82
+ - MIT
83
+ metadata: {}
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ requirements: []
99
+ rubyforge_project:
100
+ rubygems_version: 2.6.12
101
+ signing_key:
102
+ specification_version: 4
103
+ summary: ruby for tetris
104
+ test_files: []