just_shogi 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +8 -0
  3. data/CODE_OF_CONDUCT.md +74 -0
  4. data/Gemfile +5 -0
  5. data/Gemfile.lock +24 -0
  6. data/LICENSE.txt +21 -0
  7. data/README.md +80 -0
  8. data/Rakefile +10 -0
  9. data/bin/console +14 -0
  10. data/bin/setup +8 -0
  11. data/just_shogi.gemspec +29 -0
  12. data/lib/just_shogi.rb +8 -0
  13. data/lib/just_shogi/errors/causes_check_error.rb +22 -0
  14. data/lib/just_shogi/errors/dropped_into_check_error.rb +22 -0
  15. data/lib/just_shogi/errors/error.rb +20 -0
  16. data/lib/just_shogi/errors/invalid_move_error.rb +22 -0
  17. data/lib/just_shogi/errors/invalid_promotion_error.rb +22 -0
  18. data/lib/just_shogi/errors/moved_into_check_error.rb +22 -0
  19. data/lib/just_shogi/errors/no_piece_error.rb +22 -0
  20. data/lib/just_shogi/errors/not_players_turn_error.rb +22 -0
  21. data/lib/just_shogi/errors/off_board_error.rb +22 -0
  22. data/lib/just_shogi/errors/piece_not_found_error.rb +22 -0
  23. data/lib/just_shogi/errors/square_occupied_error.rb +22 -0
  24. data/lib/just_shogi/game_state.rb +355 -0
  25. data/lib/just_shogi/hand.rb +48 -0
  26. data/lib/just_shogi/piece_factory.rb +87 -0
  27. data/lib/just_shogi/pieces/fuhyou.rb +23 -0
  28. data/lib/just_shogi/pieces/ginshou.rb +23 -0
  29. data/lib/just_shogi/pieces/gyokushou.rb +9 -0
  30. data/lib/just_shogi/pieces/hisha.rb +23 -0
  31. data/lib/just_shogi/pieces/kakugyou.rb +23 -0
  32. data/lib/just_shogi/pieces/keima.rb +23 -0
  33. data/lib/just_shogi/pieces/kin_base.rb +23 -0
  34. data/lib/just_shogi/pieces/kinshou.rb +9 -0
  35. data/lib/just_shogi/pieces/kyousha.rb +23 -0
  36. data/lib/just_shogi/pieces/narigin.rb +9 -0
  37. data/lib/just_shogi/pieces/narikei.rb +9 -0
  38. data/lib/just_shogi/pieces/narikyou.rb +9 -0
  39. data/lib/just_shogi/pieces/ou_base.rb +70 -0
  40. data/lib/just_shogi/pieces/oushou.rb +9 -0
  41. data/lib/just_shogi/pieces/piece.rb +18 -0
  42. data/lib/just_shogi/pieces/ryuuma.rb +23 -0
  43. data/lib/just_shogi/pieces/ryuuou.rb +23 -0
  44. data/lib/just_shogi/pieces/tokin.rb +9 -0
  45. data/lib/just_shogi/promotion_factory.rb +71 -0
  46. data/lib/just_shogi/square.rb +51 -0
  47. data/lib/just_shogi/square_set.rb +64 -0
  48. data/lib/just_shogi/version.rb +3 -0
  49. metadata +147 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: baa8a0c184b06f45745883501e31f89fb32382be0872a211f807cd92380c1910
4
+ data.tar.gz: 00d2bc98222f9a525535e51e25e17266bd65e0caee4546ed7a4171f471129113
5
+ SHA512:
6
+ metadata.gz: 1bf5d7b03cb04995be193b191bf68e0e6302301e49c7fb5a4848b274c467b5e4384bd43c1ab74d107dc22168a03714beecca67f16cff0b413c2f8aa5bd107aa1
7
+ data.tar.gz: 4d2e75eeea5e9b124754cb0048a8fcff35920430ed1c84588340a1abcf462982321ea6d71cac4ec8015cd3c533bb523e48c47c19250b5a7bdb54e8e699ef1265
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
@@ -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 mark@mrlhumphreys.com. 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 [https://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in just_shogi.gemspec
4
+ gemspec
5
+
data/Gemfile.lock ADDED
@@ -0,0 +1,24 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ just_shogi (0.1.0)
5
+ board_game_grid (~> 0.1.6)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ board_game_grid (0.1.6)
11
+ minitest (5.14.0)
12
+ rake (13.0.1)
13
+
14
+ PLATFORMS
15
+ ruby
16
+
17
+ DEPENDENCIES
18
+ bundler (~> 2.1.4)
19
+ just_shogi!
20
+ minitest (~> 5.14.0)
21
+ rake (~> 13.0.1)
22
+
23
+ BUNDLED WITH
24
+ 2.1.4
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Mark Humphreys
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,80 @@
1
+ # JustShogi
2
+
3
+ A shogi engine written in ruby. It provides a representation of a shogi game complete with rules enforcement and serialisation.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'just_shogi'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install just_shogi
20
+
21
+ ## Usage
22
+
23
+ To start, a new game state can be instantiated with the default state:
24
+
25
+ ```ruby
26
+ game_state = JustShogi::GameState.default
27
+ ```
28
+
29
+ Moves can be made by passing in the player number and the from and to ids. It will return true if the move is valid, otherwise it will return false. The last argument is optional and if specified to true will attempt to promote the piece.
30
+
31
+ ```ruby
32
+ game_state.move(1, '77', '78', true)
33
+ ```
34
+
35
+ Drops can be made by passing in the player number, the id of the piece in hand and the id of the square that the piece will be placed on. It will return true if the move is valid. Otherwise it will return false.
36
+
37
+ ```ruby
38
+ game_state.drop(1, 23, '78')
39
+ ```
40
+
41
+ The last change with all its details are found in the `last_change` attribute.
42
+
43
+ ```ruby
44
+ game_state.last_change
45
+ ```
46
+
47
+ If something unexpected happens, errors may be found in the errors attribute.
48
+
49
+ ```ruby
50
+ game_state.errors
51
+ ```
52
+
53
+ The winner can be gound by calling winner on the object.
54
+
55
+ ```ruby
56
+ game_state.winner
57
+ ```
58
+
59
+ Also, the game can be serialized into a hash.
60
+
61
+ ```ruby
62
+ game_state.as_json
63
+ ```
64
+
65
+ ## Development
66
+
67
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
68
+
69
+ ## Contributing
70
+
71
+ Bug reports and pull requests are welcome on GitHub at https://github.com/mrlhumphreys/just_shogi. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/mrlhumphreys/just_shogi/blob/master/CODE_OF_CONDUCT.md).
72
+
73
+
74
+ ## License
75
+
76
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
77
+
78
+ ## Code of Conduct
79
+
80
+ Everyone interacting in the JustShogi project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/mrlhumphreys/just_shogi/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 => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "just_shogi"
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,29 @@
1
+ require_relative 'lib/just_shogi/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "just_shogi"
5
+ spec.version = JustShogi::VERSION
6
+ spec.authors = ["Mark Humphreys"]
7
+ spec.email = ["mark@mrlhumphreys.com"]
8
+
9
+ spec.summary = %q{A Shogi engine written in ruby}
10
+ spec.description = %q{Provides a representation of a shogi game complete with rules enforcement and serialisation.}
11
+ spec.homepage = "https://github.com/mrlhumphreys/just_shogi"
12
+ spec.license = "MIT"
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.7.0")
14
+
15
+ # Specify which files should be added to the gem when it is released.
16
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
17
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
18
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 2.1.4"
25
+ spec.add_development_dependency "rake", "~> 13.0.1"
26
+ spec.add_development_dependency "minitest", "~> 5.14.0"
27
+
28
+ spec.add_runtime_dependency "board_game_grid", "~> 0.1.6"
29
+ end
data/lib/just_shogi.rb ADDED
@@ -0,0 +1,8 @@
1
+ require "just_shogi/version"
2
+
3
+ # :nodoc:
4
+ module JustShogi
5
+
6
+ end
7
+
8
+ require 'just_shogi/game_state'
@@ -0,0 +1,22 @@
1
+ require 'just_shogi/errors/error'
2
+
3
+ module JustShogi
4
+
5
+ # = CausesCheckError
6
+ #
7
+ # A causes check error with a message
8
+ class CausesCheckError < Error
9
+
10
+ # New causes check errors can be instantiated with
11
+ #
12
+ # @option [String] message
13
+ # the message to display.
14
+ #
15
+ # ==== Example:
16
+ # # Instantiates a new CausesCheckError
17
+ # JustShogi::CausesCheckError.new("Custom Message")
18
+ def initialize(message="That move would put the ou in check.")
19
+ super
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,22 @@
1
+ require 'just_shogi/errors/error'
2
+
3
+ module JustShogi
4
+
5
+ # = DroppedIntoCheckError
6
+ #
7
+ # A dropped into check error with a message
8
+ class DroppedIntoCheckError < Error
9
+
10
+ # New moved into check errors can be instantiated with
11
+ #
12
+ # @option [String] message
13
+ # the message to display.
14
+ #
15
+ # ==== Example:
16
+ # # Instantiates a new DroppedIntoCheckError
17
+ # JustShogi::DroppedIntoCheckError.new("Custom Message")
18
+ def initialize(message="This move would leave the ou in check.")
19
+ super
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,20 @@
1
+ module JustShogi
2
+
3
+ # = Error
4
+ #
5
+ # An error with a message
6
+ class Error
7
+
8
+ # New errors can be instantiated with
9
+ #
10
+ # @option [String] message
11
+ # the message to display.
12
+ #
13
+ # ==== Example:
14
+ # # Instantiates a new Error
15
+ # JustShogi::Error.new('Custom Message')
16
+ def initialize(message='Genertic Error')
17
+ @message = message
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,22 @@
1
+ require 'just_shogi/errors/error'
2
+
3
+ module JustShogi
4
+
5
+ # = InvalidMoveError
6
+ #
7
+ # An invalid move error with a message
8
+ class InvalidMoveError < Error
9
+
10
+ # New invalid move errors can be instantiated with
11
+ #
12
+ # @option [String] message
13
+ # the message to display.
14
+ #
15
+ # ==== Example:
16
+ # # Instantiates a new InvalidMoveError
17
+ # JustShogi::InvalidMoveError.new("Custom Message")
18
+ def initialize(message="Move is invalid.")
19
+ super
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,22 @@
1
+ require 'just_shogi/errors/error'
2
+
3
+ module JustShogi
4
+
5
+ # = InvalidPromotionError
6
+ #
7
+ # An invalid promotion error with a message
8
+ class InvalidPromotionError < Error
9
+
10
+ # New invalid promotion errors can be instantiated with
11
+ #
12
+ # @option [String] message
13
+ # the message to display.
14
+ #
15
+ # ==== Example:
16
+ # # Instantiates a new InvalidPromotionError
17
+ # JustShogi::InvalidPromotionError.new("Custom Message")
18
+ def initialize(message="This piece cannot promote.")
19
+ super
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,22 @@
1
+ require 'just_shogi/errors/error'
2
+
3
+ module JustShogi
4
+
5
+ # = MovedIntoCheckError
6
+ #
7
+ # A moved into check error with a message
8
+ class MovedIntoCheckError < Error
9
+
10
+ # New moved into check errors can be instantiated with
11
+ #
12
+ # @option [String] message
13
+ # the message to display.
14
+ #
15
+ # ==== Example:
16
+ # # Instantiates a new MovedIntoCheckError
17
+ # JustShogi::MovedIntoCheckError.new("Custom Message")
18
+ def initialize(message="This move would leave the ou in check.")
19
+ super
20
+ end
21
+ end
22
+ end