checkers-game 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.rubocop.yml +42 -0
- data/.tool-versions +1 -0
- data/.travis.yml +6 -0
- data/CHANGELOG.md +3 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/LICENSE.txt +21 -0
- data/README.md +50 -0
- data/Rakefile +8 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/checkers-game.gemspec +30 -0
- data/exe/checkers +37 -0
- data/gems.locked +59 -0
- data/gems.rb +12 -0
- data/lib/checkers.rb +16 -0
- data/lib/checkers/ai/engine/alphabeta.rb +35 -0
- data/lib/checkers/ai/engine/base.rb +37 -0
- data/lib/checkers/ai/engine/minmax.rb +37 -0
- data/lib/checkers/ai/node.rb +31 -0
- data/lib/checkers/ai/tree.rb +29 -0
- data/lib/checkers/board.rb +146 -0
- data/lib/checkers/board/moves.rb +33 -0
- data/lib/checkers/board/score.rb +72 -0
- data/lib/checkers/game/engine.rb +28 -0
- data/lib/checkers/game/state.rb +25 -0
- data/lib/checkers/gui.rb +9 -0
- data/lib/checkers/gui/scene.rb +58 -0
- data/lib/checkers/gui/scene/board.rb +108 -0
- data/lib/checkers/gui/scene/piece_animation.rb +61 -0
- data/lib/checkers/jump_move.rb +18 -0
- data/lib/checkers/move.rb +16 -0
- data/lib/checkers/ruby2d/piece.rb +29 -0
- data/lib/checkers/ruby2d/square_with_piece.rb +28 -0
- data/lib/checkers/version.rb +5 -0
- metadata +124 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 2dacf43dad5d11379eeca4315e0d48c49b96b838f815418a926aed9ca9116466
|
4
|
+
data.tar.gz: 55458abe12a585ee25fd0c03d918549aa428d11fbf105b56d89d90e96b5fc047
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9d2e1dd30599da4eaf09d0bfb706670578d0153b3ccb15cce35328afe44da8aacab43c5bb478fabca9b6e2d9d58ae6c51a763a603793ca58fdf11e8ed99be2c0
|
7
|
+
data.tar.gz: 3a8e7bac2a86f41fde3c30b739492a243f7402044a97e8518bad9471319c6c141868eddbb836fba89c80dcfefc80fb91b972080806edb583c6223e57dfd68164
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
AllCops:
|
2
|
+
TargetRubyVersion: 2.7
|
3
|
+
Exclude:
|
4
|
+
- Gemfile
|
5
|
+
- '*.gemspec'
|
6
|
+
- 'vendor/**/*'
|
7
|
+
- 'bin/*'
|
8
|
+
NewCops: enable
|
9
|
+
|
10
|
+
Style/Documentation:
|
11
|
+
Enabled: false
|
12
|
+
|
13
|
+
Metrics/BlockLength:
|
14
|
+
Enabled: true
|
15
|
+
Exclude:
|
16
|
+
- spec/**/*
|
17
|
+
|
18
|
+
Naming/AccessorMethodName:
|
19
|
+
Enabled: false
|
20
|
+
|
21
|
+
Naming/MethodParameterName:
|
22
|
+
Enabled: false
|
23
|
+
|
24
|
+
Metrics/AbcSize:
|
25
|
+
Enabled: true
|
26
|
+
Exclude:
|
27
|
+
- lib/checkers/ai/engine/**/*
|
28
|
+
|
29
|
+
Metrics/CyclomaticComplexity:
|
30
|
+
Enabled: true
|
31
|
+
Exclude:
|
32
|
+
- lib/checkers/ai/engine/**/*
|
33
|
+
|
34
|
+
Metrics/PerceivedComplexity:
|
35
|
+
Enabled: true
|
36
|
+
Exclude:
|
37
|
+
- lib/checkers/ai/engine/**/*
|
38
|
+
|
39
|
+
Metrics/MethodLength:
|
40
|
+
Enabled: true
|
41
|
+
Exclude:
|
42
|
+
- lib/checkers/ai/engine/**/*
|
data/.tool-versions
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ruby 2.7.2
|
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -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 grzegorz.jakubiak@outlook.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/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 Grzegorz Jakubiak
|
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,50 @@
|
|
1
|
+
# Checkers Game
|
2
|
+
|
3
|
+
Checkers game with AI built with Ruby2D.
|
4
|
+
|
5
|
+
## Launching
|
6
|
+
|
7
|
+
You may need to install some Ruby2D dependencies on your machine follow [this link](https://www.ruby2d.com/learn/get-started/)
|
8
|
+
|
9
|
+
```
|
10
|
+
checkers
|
11
|
+
```
|
12
|
+
|
13
|
+
## Installation
|
14
|
+
|
15
|
+
Add this line to your application's Gemfile:
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
gem 'checkers-game'
|
19
|
+
```
|
20
|
+
|
21
|
+
And then execute:
|
22
|
+
|
23
|
+
$ bundle install
|
24
|
+
|
25
|
+
Or install it yourself as:
|
26
|
+
|
27
|
+
$ gem install checkers-game
|
28
|
+
|
29
|
+
## Usage
|
30
|
+
|
31
|
+
TODO: Write usage instructions here
|
32
|
+
|
33
|
+
## Development
|
34
|
+
|
35
|
+
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.
|
36
|
+
|
37
|
+
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).
|
38
|
+
|
39
|
+
## Contributing
|
40
|
+
|
41
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/checkers-game. 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/[USERNAME]/checkers-game/blob/master/CODE_OF_CONDUCT.md).
|
42
|
+
|
43
|
+
|
44
|
+
## License
|
45
|
+
|
46
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
47
|
+
|
48
|
+
## Code of Conduct
|
49
|
+
|
50
|
+
Everyone interacting in the Checkers::Game project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/checkers-game/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "checkers"
|
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,30 @@
|
|
1
|
+
require_relative 'lib/checkers/version'
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "checkers-game"
|
5
|
+
spec.version = Checkers::VERSION
|
6
|
+
spec.authors = ["Grzegorz Jakubiak"]
|
7
|
+
spec.email = ["grzegorz.jakubiak@outlook.com"]
|
8
|
+
|
9
|
+
spec.summary = %q{Game of checkers}
|
10
|
+
spec.description = %q{Game of checkers}
|
11
|
+
spec.homepage = "https://github.com/grzegorz-jakubiak/checkers-game"
|
12
|
+
spec.license = "MIT"
|
13
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.7.0")
|
14
|
+
|
15
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
16
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
17
|
+
|
18
|
+
# Specify which files should be added to the gem when it is released.
|
19
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
20
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
21
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
22
|
+
end
|
23
|
+
spec.bindir = "exe"
|
24
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
25
|
+
spec.require_paths = ["lib"]
|
26
|
+
|
27
|
+
spec.add_development_dependency 'rspec', '~> 3.2'
|
28
|
+
spec.add_runtime_dependency 'zeitwerk', '~> 2.4'
|
29
|
+
spec.add_runtime_dependency 'ruby2d', '~> 0.9.4'
|
30
|
+
end
|
data/exe/checkers
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require 'bundler/setup'
|
5
|
+
require 'checkers'
|
6
|
+
|
7
|
+
window = Window.new
|
8
|
+
window.set(title: 'Checkers')
|
9
|
+
|
10
|
+
state = Checkers::Game::State.new(:human)
|
11
|
+
ai = Checkers::AI::Engine::Minmax.new
|
12
|
+
game_engine = Checkers::Game::Engine.new(state, ai)
|
13
|
+
scene = Checkers::GUI::Scene.new(state, game_engine)
|
14
|
+
|
15
|
+
window.on :mouse_up do |event|
|
16
|
+
x = event.x
|
17
|
+
y = event.y
|
18
|
+
|
19
|
+
scene.handle_click(x, y) if event.button == :left
|
20
|
+
end
|
21
|
+
|
22
|
+
animating = nil
|
23
|
+
|
24
|
+
window.update do
|
25
|
+
if animating.nil?
|
26
|
+
unless scene.animation_queue.empty?
|
27
|
+
animating = scene.animation_queue.pop
|
28
|
+
animating.call
|
29
|
+
end
|
30
|
+
elsif animating.finished == false
|
31
|
+
animating.call
|
32
|
+
else
|
33
|
+
animating = nil
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
window.show
|
data/gems.locked
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
checkers-game (0.1.0)
|
5
|
+
ruby2d (~> 0.9.4)
|
6
|
+
zeitwerk (~> 2.4)
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: https://rubygems.org/
|
10
|
+
specs:
|
11
|
+
ast (2.4.1)
|
12
|
+
diff-lcs (1.4.4)
|
13
|
+
parallel (1.19.2)
|
14
|
+
parser (2.7.2.0)
|
15
|
+
ast (~> 2.4.1)
|
16
|
+
rainbow (3.0.0)
|
17
|
+
rake (12.3.3)
|
18
|
+
regexp_parser (1.8.2)
|
19
|
+
rexml (3.2.4)
|
20
|
+
rspec (3.9.0)
|
21
|
+
rspec-core (~> 3.9.0)
|
22
|
+
rspec-expectations (~> 3.9.0)
|
23
|
+
rspec-mocks (~> 3.9.0)
|
24
|
+
rspec-core (3.9.3)
|
25
|
+
rspec-support (~> 3.9.3)
|
26
|
+
rspec-expectations (3.9.2)
|
27
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
28
|
+
rspec-support (~> 3.9.0)
|
29
|
+
rspec-mocks (3.9.1)
|
30
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
31
|
+
rspec-support (~> 3.9.0)
|
32
|
+
rspec-support (3.9.3)
|
33
|
+
rubocop (0.93.1)
|
34
|
+
parallel (~> 1.10)
|
35
|
+
parser (>= 2.7.1.5)
|
36
|
+
rainbow (>= 2.2.2, < 4.0)
|
37
|
+
regexp_parser (>= 1.8)
|
38
|
+
rexml
|
39
|
+
rubocop-ast (>= 0.6.0)
|
40
|
+
ruby-progressbar (~> 1.7)
|
41
|
+
unicode-display_width (>= 1.4.0, < 2.0)
|
42
|
+
rubocop-ast (0.8.0)
|
43
|
+
parser (>= 2.7.1.5)
|
44
|
+
ruby-progressbar (1.10.1)
|
45
|
+
ruby2d (0.9.4)
|
46
|
+
unicode-display_width (1.7.0)
|
47
|
+
zeitwerk (2.4.0)
|
48
|
+
|
49
|
+
PLATFORMS
|
50
|
+
ruby
|
51
|
+
|
52
|
+
DEPENDENCIES
|
53
|
+
checkers-game!
|
54
|
+
rake (~> 12.0)
|
55
|
+
rspec (~> 3.2)
|
56
|
+
rubocop (~> 0.93.1)
|
57
|
+
|
58
|
+
BUNDLED WITH
|
59
|
+
2.1.4
|
data/gems.rb
ADDED
data/lib/checkers.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'matrix'
|
4
|
+
require 'observer'
|
5
|
+
require 'forwardable'
|
6
|
+
|
7
|
+
require 'ruby2d'
|
8
|
+
require 'zeitwerk'
|
9
|
+
|
10
|
+
loader = Zeitwerk::Loader.for_gem
|
11
|
+
loader.inflector.inflect(
|
12
|
+
'ai' => 'AI',
|
13
|
+
'gui' => 'GUI',
|
14
|
+
'ruby2d' => 'Ruby2D'
|
15
|
+
)
|
16
|
+
loader.setup
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Checkers
|
4
|
+
module AI
|
5
|
+
module Engine
|
6
|
+
class Alphabeta < Base
|
7
|
+
def next_board(board)
|
8
|
+
super(board) { |root, tree_depth| alphabeta(root, tree_depth, Float::MIN, Float::MAX, true) }
|
9
|
+
end
|
10
|
+
|
11
|
+
private
|
12
|
+
|
13
|
+
def alphabeta(node, tree_depth, a, b, maxplayer)
|
14
|
+
return node.score if tree_depth.zero? || node.children_size.zero?
|
15
|
+
|
16
|
+
if maxplayer
|
17
|
+
node.children.each do |child|
|
18
|
+
a = max(a, alphabeta(child, tree_depth - 1, a, b, !maxplayer))
|
19
|
+
break if a >= b
|
20
|
+
end
|
21
|
+
|
22
|
+
node.score = a
|
23
|
+
else
|
24
|
+
node.children.each do |child|
|
25
|
+
b = min(b, alphabeta(child, tree_depth - 1, a, b, !maxplayer))
|
26
|
+
break if a >= b
|
27
|
+
end
|
28
|
+
|
29
|
+
node.score = b
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|