xo 0.0.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -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
data/lib/xo/ai/novice.rb DELETED
@@ -1,11 +0,0 @@
1
- require 'xo/ai/expert'
2
-
3
- module XO::AI
4
-
5
- class Novice < Expert
6
-
7
- def self.get_moves(grid, player)
8
- all_moves
9
- end
10
- end
11
- end