othello_ruby 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 3c69a3db4047bf393c8b9b3935cf94291a44db262110832b729fc98d4f6cb5b3
4
+ data.tar.gz: d0c8b5e8e9faa6e9a36f0601e2ef8e98550e79f59bb77d3f466ff7b78180ea78
5
+ SHA512:
6
+ metadata.gz: '08e129dddad08609a8810711c8dddaa0a7bbd7af0236fc2b34275998daf31e2d3e3554f16c067f8733de1907c6a574ee99ef5af908a32660d9dcc8eee070b063'
7
+ data.tar.gz: 97d00e35e81d356d851fb3822d03cd91925eab0592f811487e227beafb14e99ef49e34fb0dc2591b1d8a7fad076242e496c704eb0ebef50b19714d45180419bf
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.4.4
7
+ before_install: gem install bundler -v 2.0.1
@@ -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 31506@toyota.kosen-ac.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,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in othello_ruby.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Riki Ueno
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,85 @@
1
+ # OthelloRuby
2
+
3
+ ## Installation
4
+
5
+ Add this line to your application's Gemfile:
6
+
7
+ ```ruby
8
+ gem 'othello_ruby'
9
+ ```
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install othello_ruby
18
+
19
+ ## Usage
20
+
21
+ othello format
22
+ ```ruby
23
+ a b c d e f g h
24
+ +---+---+---+---+---+---+---+---+
25
+ 1 | | | | | | | | |
26
+ +---+---+---+---+---+---+---+---+
27
+ 2 | | | | | | | | |
28
+ +---+---+---+---+---+---+---+---+
29
+ 3 | | | | | | | | |
30
+ +---+---+---+---+---+---+---+---+
31
+ 4 | | | | 0 | 1 | | | |
32
+ +---+---+---+---+---+---+---+---+
33
+ 5 | | | | 1 | 0 | | | |
34
+ +---+---+---+---+---+---+---+---+
35
+ 6 | | | | | | | | |
36
+ +---+---+---+---+---+---+---+---+
37
+ 7 | | | | | | | | |
38
+ +---+---+---+---+---+---+---+---+
39
+ 8 | | | | | | | | |
40
+ +---+---+---+---+---+---+---+---+
41
+ ```
42
+
43
+ initialize
44
+ ```ruby
45
+ @bord = OthelloRuby::Game::Bord.new(first_move_color: "#{color1}", passive_move_color: "#{color2}" )
46
+ @user1 = OthelloRuby::Game::User.new("#{color1}")
47
+ @user2 = OthelloRuby::Game::User.new("#{color2}")
48
+ ```
49
+
50
+ game
51
+ ```ruby
52
+ @user1.set(@bord, "3d")
53
+ puts @bord.show #=>
54
+ a b c d e f g h
55
+ +---+---+---+---+---+---+---+---+
56
+ 1 | | | | | | | | |
57
+ +---+---+---+---+---+---+---+---+
58
+ 2 | | | | | | | | |
59
+ +---+---+---+---+---+---+---+---+
60
+ 3 | | | | 1 | | | | |
61
+ +---+---+---+---+---+---+---+---+
62
+ 4 | | | | 1 | 1 | | | |
63
+ +---+---+---+---+---+---+---+---+
64
+ 5 | | | | 1 | 0 | | | |
65
+ +---+---+---+---+---+---+---+---+
66
+ 6 | | | | | | | | |
67
+ +---+---+---+---+---+---+---+---+
68
+ 7 | | | | | | | | |
69
+ +---+---+---+---+---+---+---+---+
70
+ 8 | | | | | | | | |
71
+ +---+---+---+---+---+---+---+---+
72
+ @user1.count(@bord) #=> 4
73
+ ```
74
+
75
+ ## Contributing
76
+
77
+ Bug reports and pull requests are welcome on GitHub at https://github.com/g-ken/othello_ruby. 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.
78
+
79
+ ## License
80
+
81
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
82
+
83
+ ## Code of Conduct
84
+
85
+ Everyone interacting in the OthelloRuby project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/othello_ruby/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "othello_ruby"
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,231 @@
1
+ require "othello_ruby/version"
2
+
3
+ module OthelloRuby
4
+ EIGHT_DIRECTION = %w(left right upward under left_upward left_under right_upward right_under)
5
+ REPLACE = {"a" => 0, "b" => 1, "c" => 2, "d" => 3, "e" => 4, "f" => 5, "g" => 6, "h" => 7}
6
+ class Error < StandardError; end
7
+ # Your code goes here...
8
+ module Game
9
+ module BordWork
10
+ def get_reverse_pattern(active, passive, bit_string)
11
+ patterns = []
12
+ EIGHT_DIRECTION.each do |name|
13
+ patterns << send("get_#{name}_pat", active, passive, bit_string)
14
+ end
15
+ patterns.delete(0)
16
+ patterns.compact
17
+ end
18
+
19
+ EIGHT_DIRECTION.each do |name|
20
+ define_method("get_#{name}_pat") do |active, passive, bit_string|
21
+ return nil if ((active | passive) & bit_string) != 0
22
+ pattern = 0
23
+ mask = send("#{name}_direction", bit_string)
24
+ while mask != 0 && (mask & passive) != 0
25
+ pattern |= mask
26
+ mask = send("#{name}_direction", mask)
27
+ end
28
+ if (mask & active) == 0
29
+ return 0
30
+ else
31
+ return pattern
32
+ end
33
+ end
34
+ end
35
+
36
+ def left_direction(bit_string)
37
+ (bit_string >> 1) & 0x7F7F7F7F7F7F7F7F
38
+ end
39
+
40
+ def right_direction(bit_string)
41
+ (bit_string << 1) & 0xFEFEFEFEFEFEFEFE
42
+ end
43
+
44
+ def upward_direction(bit_string)
45
+ bit_string >> 8
46
+ end
47
+
48
+ def under_direction(bit_string)
49
+ (bit_string << 8) & 0xFFFFFFFFFFFFFFFF
50
+ end
51
+
52
+ def left_upward_direction(bit_string)
53
+ bit_string >> 9
54
+ end
55
+
56
+ def left_under_direction(bit_string)
57
+ (bit_string << 7) & 0xFFFFFFFFFFFFFFFF
58
+ end
59
+
60
+ def right_upward_direction(bit_string)
61
+ bit_string >> 7
62
+ end
63
+
64
+ def right_under_direction(bit_string)
65
+ (bit_string << 9) & 0xFFFFFFFFFFFFFFFF
66
+ end
67
+ end
68
+
69
+ class Bord
70
+ include BordWork
71
+ attr_accessor :white, :black
72
+ attr_reader :first_move_color, :passive_move_color
73
+ def initialize(**options)
74
+ @white_square = options[:white_square] || "0"
75
+ @black_square = options[:black_square] || "1"
76
+ @first_move_color = options[:first_move_color] || "black"
77
+ @passive_move_color = options[:passive_move_color] || "white"
78
+ @white = 0x0000001008000000
79
+ @black = 0x0000000810000000
80
+ end
81
+
82
+ def show
83
+ message = " a b c d e f g h\n +---+---+---+---+---+---+---+---+\n"
84
+ 8.times do |row|
85
+ message << "#{row+1} |"
86
+ 8.times do |col|
87
+ if @white[row * 8 + col] == 1
88
+ message << " #{@white_square} "
89
+ elsif @black[row * 8 + col] == 1
90
+ message << " #{@black_square} "
91
+ else
92
+ message << " "
93
+ end
94
+ message << "|"
95
+ end
96
+ message << "\n +---+---+---+---+---+---+---+---+\n"
97
+ end
98
+ message
99
+ end
100
+
101
+ def white_count
102
+ count = 0
103
+ 64.times do |i|
104
+ count += 1 if @white[i] == 1
105
+ end
106
+ count
107
+ end
108
+
109
+ def black_count
110
+ count = 0
111
+ 64.times do |i|
112
+ count += 1 if @black[i] == 1
113
+ end
114
+ count
115
+ end
116
+
117
+ def white_set(coordinate)
118
+ bit_string = convert_to_number(coordinate)
119
+ patterns = get_reverse_pattern(@white, @black, bit_string)
120
+ if patterns.empty?
121
+ nil
122
+ else
123
+ pattern = patterns.inject(&:+)
124
+ @white ^= (bit_string | pattern)
125
+ @black ^= pattern
126
+ end
127
+ end
128
+
129
+ def black_set(coordinate)
130
+ bit_string = convert_to_number(coordinate)
131
+ patterns = get_reverse_pattern(@black, @white, bit_string)
132
+ if patterns.empty?
133
+ nil
134
+ else
135
+ pattern = patterns.inject(&:|)
136
+ @black ^= (bit_string | pattern)
137
+ @white ^= pattern
138
+ end
139
+ end
140
+
141
+ def search_white_position
142
+ positions = []
143
+ 64.times do |num|
144
+ positions << num unless get_reverse_pattern(@white, @black, 1 << num).empty?
145
+ end
146
+ positions.map!{|position| 1 << position }
147
+ positions.inject(&:|)
148
+ end
149
+
150
+ def search_black_position
151
+ positions = []
152
+ 64.times do |num|
153
+ positions << num unless get_reverse_pattern(@black, @white, 1 << num).empty?
154
+ end
155
+ positions.map{|position| 1 << position }
156
+ positions.inject(&:|)
157
+ end
158
+ end
159
+
160
+ class User
161
+ include BordWork
162
+ def initialize(color)
163
+ @color = color
164
+ end
165
+
166
+ def set(bord, coordinate)
167
+ bit_string = convert_to_number(coordinate)
168
+ case @color
169
+ when bord.first_move_color
170
+ patterns = get_reverse_pattern(bord.black, bord.white, bit_string)
171
+ when bord.passive_move_color
172
+ patterns = get_reverse_pattern(bord.white, bord.black, bit_string)
173
+ else
174
+ patterns = []
175
+ end
176
+ if patterns.empty?
177
+ nil
178
+ else
179
+ pattern = patterns.inject(&:|)
180
+ case @color
181
+ when bord.first_move_color
182
+ bord.black ^= (bit_string | pattern)
183
+ bord.white ^= pattern
184
+ when bord.passive_move_color
185
+ bord.white ^= (bit_string | pattern)
186
+ bord.black ^= pattern
187
+ else
188
+ nil
189
+ end
190
+ end
191
+ end
192
+
193
+ def count(bord)
194
+ count = 0
195
+ case @color
196
+ when bord.first_move_color
197
+ bord = bord.black
198
+ when bord.passive_move_color
199
+ bord = bord.white
200
+ else
201
+ nil
202
+ end
203
+ 64.times do |i|
204
+ count += 1 if bord[i] == 1
205
+ end
206
+ count
207
+ end
208
+
209
+ private
210
+ def convert_to_number(coordinate)
211
+ bit_string = 0
212
+ if coordinate =~ /[1-8][A-H]|[A-H][1-8]/i
213
+ row, col = coordinate.slice(/[1-8][A-H]|[A-H][1-8]/i).split("").partition{|item| item.match(/[1-8]/)}
214
+ bit_string = 1 << (REPLACE[col.first.downcase] + (8 * (row.first.to_i - 1)))
215
+ else
216
+ end
217
+ bit_string
218
+ end
219
+ end
220
+ end
221
+ class Log
222
+ def initialize(**options)
223
+ end
224
+
225
+ def info
226
+ end
227
+
228
+ def load
229
+ end
230
+ end
231
+ end
@@ -0,0 +1,3 @@
1
+ module OthelloRuby
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,29 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "othello_ruby/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "othello_ruby"
8
+ spec.version = OthelloRuby::VERSION
9
+ spec.authors = ["Riki Ueno"]
10
+ spec.email = ["31506@toyota.kosen-ac.jp"]
11
+
12
+ spec.summary = %q{Othello/reversi}
13
+ spec.description = %q{}
14
+ spec.homepage = "https://github.com/g-ken/othello_ruby"
15
+ spec.license = "MIT"
16
+
17
+ # Specify which files should be added to the gem when it is released.
18
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
19
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
20
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ end
22
+ spec.bindir = "exe"
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ["lib"]
25
+
26
+ spec.add_development_dependency "bundler", "~> 2.0"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "minitest", "~> 5.0"
29
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: othello_ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Riki Ueno
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-03-15 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: '2.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
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: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ description: ''
56
+ email:
57
+ - 31506@toyota.kosen-ac.jp
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".travis.yml"
64
+ - CODE_OF_CONDUCT.md
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - bin/console
70
+ - bin/setup
71
+ - lib/othello_ruby.rb
72
+ - lib/othello_ruby/version.rb
73
+ - othello_ruby.gemspec
74
+ homepage: https://github.com/g-ken/othello_ruby
75
+ licenses:
76
+ - MIT
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubygems_version: 3.0.1
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: Othello/reversi
97
+ test_files: []