matts_tictactoe_core 0.1.0

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: e9b3823f8f4c11cb7b42db9a31b4d4fccf4c5d2e
4
+ data.tar.gz: '08090c8cca6b1813388e92defa0c0db1f0efe63f'
5
+ SHA512:
6
+ metadata.gz: 35f02be1fe539a7e6ecd160ae2d0cbe1bf4d88b4db07e0eb83be8ad26ff68dcd02c0dd9839b2f5d91e5291aff51b08e277741763418c774756d7d0511417e973
7
+ data.tar.gz: 637ce39177f3c77e7dfdee24e3a8fc50d8d0c4349f3d69db214c0278d02cd84e4985192beeb5c6b31d7243074228d31043ac945b58dada9ec42e9a1f9f414095
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+ .DS_Store
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
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.16.0.pre.2
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 matts_tictactoe_core.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,35 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ matts_tictactoe_core (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.3)
10
+ rake (10.5.0)
11
+ rspec (3.6.0)
12
+ rspec-core (~> 3.6.0)
13
+ rspec-expectations (~> 3.6.0)
14
+ rspec-mocks (~> 3.6.0)
15
+ rspec-core (3.6.0)
16
+ rspec-support (~> 3.6.0)
17
+ rspec-expectations (3.6.0)
18
+ diff-lcs (>= 1.2.0, < 2.0)
19
+ rspec-support (~> 3.6.0)
20
+ rspec-mocks (3.6.0)
21
+ diff-lcs (>= 1.2.0, < 2.0)
22
+ rspec-support (~> 3.6.0)
23
+ rspec-support (3.6.0)
24
+
25
+ PLATFORMS
26
+ ruby
27
+
28
+ DEPENDENCIES
29
+ bundler (~> 1.16.a)
30
+ matts_tictactoe_core!
31
+ rake (~> 10.0)
32
+ rspec (~> 3.0)
33
+
34
+ BUNDLED WITH
35
+ 1.16.0.pre.2
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Matt Glover
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,39 @@
1
+ # MattsTictactoeCore
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/matts_tictactoe_core`. 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 'matts_tictactoe_core'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install matts_tictactoe_core
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]/matts_tictactoe_core.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
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 "matts_tictactoe_core"
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,52 @@
1
+ require "matts_tictactoe_core/mini_max"
2
+
3
+ module MattsTictactoeCore
4
+ class AlphaBeta < MiniMax
5
+
6
+ MAX_VALUE = 10000
7
+ MIN_VALUE = -10000
8
+
9
+ def execute
10
+ possible_moves.each do |move|
11
+ score = move_score(move)
12
+ update_result!(move, score) if better_score?(score)
13
+ break if (@beta <= @alpha)
14
+ end
15
+
16
+ self
17
+ end
18
+
19
+ private
20
+ def setup(options)
21
+ @alpha = options.fetch(:alpha, MIN_VALUE)
22
+ @beta = options.fetch(:beta, MAX_VALUE)
23
+ self.score = initial_score
24
+ end
25
+
26
+ def initial_score
27
+ is_maximising ? @alpha : @beta
28
+ end
29
+
30
+ def better_score?(score)
31
+ is_maximising ? (score > @alpha) : (score < @beta)
32
+ end
33
+
34
+ def update_result!(move, score)
35
+ update_alpha_beta!(score)
36
+ self.move = move
37
+ self.score = score
38
+ end
39
+
40
+ def update_alpha_beta!(score)
41
+ if is_maximising
42
+ @alpha = score
43
+ else
44
+ @beta = score
45
+ end
46
+ end
47
+
48
+ def additional_options
49
+ {alpha: @alpha, beta: @beta}
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,118 @@
1
+ require "matts_tictactoe_core/lines"
2
+
3
+ module MattsTictactoeCore
4
+ class Board
5
+
6
+ attr_reader :moves
7
+
8
+ def initialize(moves = [])
9
+ @moves = moves
10
+ @lines = Lines.new(self)
11
+ end
12
+
13
+ def squares
14
+ (1..total_squares)
15
+ end
16
+
17
+ def total_squares
18
+ size ** 2
19
+ end
20
+
21
+ def size
22
+ 3
23
+ end
24
+
25
+ def move(square)
26
+ valid_move?(square) ? Board.new(moves + [square]) : self
27
+ end
28
+
29
+ def valid_move?(square)
30
+ square_in_bounds?(square) && square_empty?(square)
31
+ end
32
+
33
+ def complete?
34
+ winner? || full?
35
+ end
36
+
37
+ def winner?
38
+ winner != nil
39
+ end
40
+
41
+ def winner
42
+ line_statuses.find { |status| status != :none }
43
+ end
44
+
45
+ def rows_as_square_numbers_and_statuses
46
+ @lines.rows.map do |line|
47
+ line.map { |square_number| [square_number, square_status(square_number)] }
48
+ end
49
+ end
50
+
51
+ def next_player
52
+ moves.length.even? ? :x : :o
53
+ end
54
+
55
+ def ==(other)
56
+ other.class == self.class && other.moves == moves
57
+ end
58
+
59
+ def empty_squares
60
+ squares.to_a - moves
61
+ end
62
+
63
+ private
64
+ def square_in_bounds?(square)
65
+ squares.include?(square)
66
+ end
67
+
68
+ def square_empty?(square)
69
+ !moves.include?(square)
70
+ end
71
+
72
+ def line_statuses
73
+ lines_of_square_statuses.map do |line|
74
+ next :x if line_of?(line, :x)
75
+ next :o if line_of?(line, :o)
76
+ :none
77
+ end
78
+ end
79
+
80
+ def lines_of_square_statuses
81
+ @lines.all.map { |line| line.map(&method(:square_status)) }
82
+ end
83
+
84
+ def square_status(square)
85
+ square_mark(square) || :empty
86
+ end
87
+
88
+ def square_mark(square)
89
+ if is_square_marked(square, :x)
90
+ :x
91
+ elsif is_square_marked(square, :o)
92
+ :o
93
+ end
94
+ end
95
+
96
+ def is_square_marked(square, mark)
97
+ player_moves(mark).include?(square)
98
+ end
99
+
100
+ def player_moves(player_mark)
101
+ move_selector = player_mark == :x ? :even? : :odd?
102
+ select_moves { |i| i.send(move_selector) }
103
+ end
104
+
105
+ def select_moves(&index_selector)
106
+ move_indexes = moves.each_index.select(&index_selector)
107
+ moves.values_at(*move_indexes)
108
+ end
109
+
110
+ def line_of?(line, status)
111
+ line.all? { |square_status| square_status == status }
112
+ end
113
+
114
+ def full?
115
+ moves.length == total_squares
116
+ end
117
+ end
118
+ end
@@ -0,0 +1,21 @@
1
+ require "matts_tictactoe_core/alpha_beta"
2
+
3
+ module MattsTictactoeCore
4
+ class ComputerPlayer
5
+
6
+ DefaultSleepTime = 0.5
7
+
8
+ def initialize(options = {})
9
+ @options = options
10
+ end
11
+
12
+ def type
13
+ :computer
14
+ end
15
+
16
+ def get_move(board)
17
+ sleep(@options.fetch(:sleep_time, DefaultSleepTime))
18
+ AlphaBeta.new(board, @options).execute.move
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,53 @@
1
+ require "matts_tictactoe_core/board"
2
+ require "matts_tictactoe_core/human_player"
3
+ require "matts_tictactoe_core/computer_player"
4
+
5
+ module MattsTictactoeCore
6
+ class Game
7
+
8
+ attr_reader :board, :player_x_type, :player_o_type
9
+
10
+ def initialize(options = {})
11
+ @max_depth = options.fetch(:max_depth, 6)
12
+ @board = options.fetch(:board, Board.new)
13
+ @player_x_type = options[:player_x_type]
14
+ @player_o_type = options[:player_o_type]
15
+ @computer_player = options.fetch(:computer_player, build_computer_player)
16
+ @human_player = options.fetch(:human_player, build_human_player)
17
+ end
18
+
19
+ def next_player
20
+ next_player_type == :computer ? computer_player : human_player
21
+ end
22
+
23
+ def next_player_type
24
+ instance_variable_get("@player_#{board.next_player}_type")
25
+ end
26
+
27
+ def next_move(square)
28
+ @board = board.move(square)
29
+ end
30
+
31
+ def complete?
32
+ @board.complete?
33
+ end
34
+
35
+ private
36
+
37
+ def computer_player
38
+ @computer_player
39
+ end
40
+
41
+ def human_player
42
+ @human_player
43
+ end
44
+
45
+ def build_computer_player
46
+ ComputerPlayer.new
47
+ end
48
+
49
+ def build_human_player
50
+ HumanPlayer.new
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,7 @@
1
+ module MattsTictactoeCore
2
+ class HumanPlayer
3
+ def type
4
+ :human
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,54 @@
1
+ module MattsTictactoeCore
2
+ class Lines
3
+ def initialize(board)
4
+ @board = board
5
+ end
6
+
7
+ def all
8
+ rows + columns + diagonals
9
+ end
10
+
11
+ def rows
12
+ (1..total_squares)
13
+ .step(size)
14
+ .map(&method(:row))
15
+ end
16
+
17
+ private
18
+ def row(start)
19
+ (start...start + size).to_a
20
+ end
21
+
22
+ def columns
23
+ (1..size).map(&method(:column))
24
+ end
25
+
26
+ def column(start)
27
+ ((start..total_squares).step(size)).to_a
28
+ end
29
+
30
+ def diagonals
31
+ [diagonal_from_top_left, diagonal_from_top_right]
32
+ end
33
+
34
+ def diagonal_from_top_left
35
+ (squares.step(size + 1)).to_a
36
+ end
37
+
38
+ def diagonal_from_top_right
39
+ ((size...total_squares).step(size - 1)).to_a
40
+ end
41
+
42
+ def size
43
+ @board.size
44
+ end
45
+
46
+ def total_squares
47
+ @board.total_squares
48
+ end
49
+
50
+ def squares
51
+ @board.squares
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,103 @@
1
+ module MattsTictactoeCore
2
+
3
+ class MiniMax
4
+
5
+ DRAWING_SCORE = OUT_OF_DEPTH_SCORE = 0
6
+ INITIAL_DEPTH = 0
7
+
8
+ attr_accessor :move, :score
9
+ attr_reader :is_maximising
10
+
11
+ def initialize(initial_board, options = {})
12
+ @initial_board = initial_board
13
+ @depth = options.fetch(:depth, INITIAL_DEPTH)
14
+ @is_maximising = options.fetch(:is_maximising, true)
15
+ @max_depth = options.fetch(:max_depth, max_move_depth)
16
+ setup(options)
17
+ end
18
+
19
+ def execute
20
+ @move, @score = move_scores.send(max_or_min_by, &:last)
21
+ self
22
+ end
23
+
24
+ private
25
+ def setup(options) end
26
+
27
+ def move_scores
28
+ possible_moves.map { |move| [move, move_score(move)] }
29
+ end
30
+
31
+ def max_or_min_by
32
+ @is_maximising ? :max_by : :min_by
33
+ end
34
+
35
+ def max_move_depth
36
+ @initial_board.total_squares
37
+ end
38
+
39
+ def possible_moves
40
+ @initial_board.empty_squares
41
+ end
42
+
43
+ def move_score(move)
44
+ board_score(@initial_board.move(move))
45
+ end
46
+
47
+ def board_score(board)
48
+ return final_score(board) if board.complete?
49
+ return out_of_depth_score if out_of_depth?
50
+ return recursive_score(board)
51
+ end
52
+
53
+ def final_score(board)
54
+ board.winner? ? winning_score : drawing_score
55
+ end
56
+
57
+ def winning_score
58
+ (base_winning_score * mini_max_multiplier) - depth_offset
59
+ end
60
+
61
+ def base_winning_score
62
+ max_move_depth + 1
63
+ end
64
+
65
+ def mini_max_multiplier
66
+ @is_maximising ? 1 : -1
67
+ end
68
+
69
+ def depth_offset
70
+ @is_maximising ? @depth : -@depth
71
+ end
72
+
73
+ def drawing_score
74
+ DRAWING_SCORE
75
+ end
76
+
77
+ def out_of_depth?
78
+ next_depth == @max_depth
79
+ end
80
+
81
+ def next_depth
82
+ @depth + 1
83
+ end
84
+
85
+ def out_of_depth_score
86
+ OUT_OF_DEPTH_SCORE
87
+ end
88
+
89
+ def recursive_score(board)
90
+ self.class.new(board, recursive_options).execute.score
91
+ end
92
+
93
+ def recursive_options
94
+ { depth: next_depth,
95
+ is_maximising: !@is_maximising,
96
+ max_depth: @max_depth }.merge(additional_options)
97
+ end
98
+
99
+ def additional_options
100
+ {}
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,3 @@
1
+ module MattsTictactoeCore
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,8 @@
1
+ require "matts_tictactoe_core/version"
2
+ require "matts_tictactoe_core/alpha_beta"
3
+ require "matts_tictactoe_core/board"
4
+ require "matts_tictactoe_core/computer_player"
5
+ require "matts_tictactoe_core/game"
6
+ require "matts_tictactoe_core/human_player"
7
+ require "matts_tictactoe_core/lines"
8
+ require "matts_tictactoe_core/mini_max"
@@ -0,0 +1,35 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "matts_tictactoe_core/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "matts_tictactoe_core"
8
+ spec.version = MattsTictactoeCore::VERSION
9
+ spec.authors = ["Matt Glover"]
10
+ spec.email = ["matthewglover@me.com"]
11
+
12
+ spec.summary = "Core Tic Toe Package"
13
+ spec.homepage = "https://github.com/matthewglover/matts_tictactoe_core"
14
+ spec.license = "MIT"
15
+
16
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
18
+ # if spec.respond_to?(:metadata)
19
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
20
+ # else
21
+ # raise "RubyGems 2.0 or newer is required to protect against " \
22
+ # "public gem pushes."
23
+ # end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
26
+ f.match(%r{^(test|spec|features)/})
27
+ end
28
+ spec.bindir = "exe"
29
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+
32
+ spec.add_development_dependency "bundler", "~> 1.16.a"
33
+ spec.add_development_dependency "rake", "~> 10.0"
34
+ spec.add_development_dependency "rspec", "~> 3.0"
35
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: matts_tictactoe_core
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Matt Glover
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-09-28 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.16.a
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.16.a
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:
56
+ email:
57
+ - matthewglover@me.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - Gemfile.lock
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - bin/console
71
+ - bin/setup
72
+ - lib/matts_tictactoe_core.rb
73
+ - lib/matts_tictactoe_core/alpha_beta.rb
74
+ - lib/matts_tictactoe_core/board.rb
75
+ - lib/matts_tictactoe_core/computer_player.rb
76
+ - lib/matts_tictactoe_core/game.rb
77
+ - lib/matts_tictactoe_core/human_player.rb
78
+ - lib/matts_tictactoe_core/lines.rb
79
+ - lib/matts_tictactoe_core/mini_max.rb
80
+ - lib/matts_tictactoe_core/version.rb
81
+ - matts_tictactoe_core.gemspec
82
+ homepage: https://github.com/matthewglover/matts_tictactoe_core
83
+ licenses:
84
+ - MIT
85
+ metadata: {}
86
+ post_install_message:
87
+ rdoc_options: []
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ requirements: []
101
+ rubyforge_project:
102
+ rubygems_version: 2.6.13
103
+ signing_key:
104
+ specification_version: 4
105
+ summary: Core Tic Toe Package
106
+ test_files: []