xo 0.0.1 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +13 -5
- data/.gitignore +4 -0
- data/.travis.yml +3 -0
- data/Gemfile +3 -0
- data/Gemfile.lock +43 -0
- data/README.md +92 -27
- data/Rakefile +2 -0
- data/bin/xo +314 -0
- data/lib/xo.rb +0 -21
- data/lib/xo/ai.rb +1 -3
- data/lib/xo/ai/geometric_grid.rb +113 -0
- data/lib/xo/ai/minimax.rb +187 -89
- data/lib/xo/engine.rb +187 -68
- data/lib/xo/evaluator.rb +137 -62
- data/lib/xo/grid.rb +153 -24
- data/lib/xo/version.rb +1 -1
- data/spec/spec_helper.rb +3 -0
- data/spec/xo/ai/geometric_grid_spec.rb +137 -0
- data/spec/xo/ai/minimax_spec.rb +56 -36
- data/spec/xo/engine_spec.rb +296 -20
- data/spec/xo/evaluator_spec.rb +210 -39
- data/spec/xo/grid_spec.rb +198 -55
- data/xo.gemspec +9 -2
- metadata +63 -27
- data/lib/xo/ai/advanced_beginner.rb +0 -17
- data/lib/xo/ai/expert.rb +0 -64
- data/lib/xo/ai/novice.rb +0 -11
@@ -1,17 +0,0 @@
|
|
1
|
-
require 'xo/ai/expert'
|
2
|
-
|
3
|
-
module XO::AI
|
4
|
-
|
5
|
-
class AdvancedBeginner < Expert
|
6
|
-
|
7
|
-
def self.get_moves(grid, player)
|
8
|
-
smart_moves = super(grid, player)
|
9
|
-
dumb_moves = all_moves - smart_moves
|
10
|
-
|
11
|
-
# if there are no dumb moves then we have no choice but to make a smart move
|
12
|
-
# otherwise, 75% of the time we'll make a smart move and the other 25% of the
|
13
|
-
# time we'll make a dumb move
|
14
|
-
dumb_moves.empty? ? smart_moves : (rand < 0.75 ? smart_moves : dumb_moves)
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
data/lib/xo/ai/expert.rb
DELETED
@@ -1,64 +0,0 @@
|
|
1
|
-
require 'xo/grid'
|
2
|
-
require 'xo/evaluator'
|
3
|
-
require 'xo/ai'
|
4
|
-
|
5
|
-
module XO::AI
|
6
|
-
|
7
|
-
class Expert
|
8
|
-
|
9
|
-
def self.suggest_moves(grid, player)
|
10
|
-
result = XO::Evaluator.analyze(grid, player)
|
11
|
-
|
12
|
-
case result[:status]
|
13
|
-
when :ok
|
14
|
-
get_moves(grid, player)
|
15
|
-
when :game_over
|
16
|
-
[]
|
17
|
-
else
|
18
|
-
raise IllegalGridStatusError
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
def self.get_moves(grid, player)
|
23
|
-
if moves = MOVES_CACHE[grid] || MOVES_CACHE[invert_grid(grid)]
|
24
|
-
moves.map { |pos| XO::Position.new(*pos) }
|
25
|
-
else
|
26
|
-
XO::AI.minimax(grid, player).moves
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
def self.all_moves(grid)
|
31
|
-
grid.enum_for(:each_free).map { |r, c| XO::Position.new(r, c) }
|
32
|
-
end
|
33
|
-
|
34
|
-
private
|
35
|
-
|
36
|
-
def self.invert_grid(grid)
|
37
|
-
(new_grid = grid.dup).each do |r, c, val|
|
38
|
-
new_grid[r, c] = XO::other_token(val)
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
def self.one_x_grid(r, c)
|
43
|
-
XO::Grid.new.tap do |grid|
|
44
|
-
grid[r, c] = :x
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
MOVES_CACHE = {
|
49
|
-
XO::Grid.new => [[1, 1], [1, 2], [1, 3], [2, 1], [2, 2], [2, 3], [3, 1], [3, 2], [3, 3]],
|
50
|
-
|
51
|
-
one_x_grid(1, 1) => [[2, 2]],
|
52
|
-
one_x_grid(1, 3) => [[2, 2]],
|
53
|
-
one_x_grid(3, 1) => [[2, 2]],
|
54
|
-
one_x_grid(3, 3) => [[2, 2]],
|
55
|
-
|
56
|
-
one_x_grid(1, 2) => [[1, 1], [1, 3], [2, 2], [3, 2]],
|
57
|
-
one_x_grid(2, 1) => [[1, 1], [2, 2], [2, 3], [3, 1]],
|
58
|
-
one_x_grid(2, 3) => [[1, 3], [2, 1], [2, 2], [3, 3]],
|
59
|
-
one_x_grid(3, 2) => [[1, 2], [2, 2], [3, 1], [3, 3]],
|
60
|
-
|
61
|
-
one_x_grid(2, 2) => [[1, 1], [1, 3], [3, 1], [3, 3]]
|
62
|
-
}
|
63
|
-
end
|
64
|
-
end
|