-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic_ai.rb
124 lines (107 loc) · 2.73 KB
/
basic_ai.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
class BasicAi
attr_accessor :depth, :board, :loops
NEUTRAL = 0
WIN = 1000
LOSS = -1000
def initialize(depth=0)
@depth = depth
@loops = 0
end
# Steps:
# - random move, depth 1
# - random move but consider opponents best move, depth 2
# - arbitrary depth
def move(game_board)
@loops = 0
if @depth == 0
# makes a random move
moves = game_board.available_moves
moves.sample
else
# uses depth look ahead
scores = look_ahead(game_board, @depth)
moves = game_board.available_moves
move_from_scores moves, scores
end
end
def look_ahead(game_board, max_depth=5)
current_depth = 1
is_maximizer = true
moves = game_board.available_moves
scores = moves.map do |m|
@loops += 1
cloned = clone_board(game_board)
cloned.move(m)
if cloned.won? and is_maximizer
next WIN
elsif cloned.won? and !is_maximizer
next LOSS
elsif cloned.tie?
next NEUTRAL
elsif current_depth == max_depth
# two matched spots with open spots = 2
# three match (with open spots) = 3
next NEUTRAL
else
next score_board(cloned, !is_maximizer, current_depth + 1, max_depth)
end
end
scores
end
def score_board(game_board, is_maximizer, current_depth, max_depth=5)
@loops += 1
moves = game_board.available_moves
scores = moves.map do |m|
cloned = clone_board(game_board)
cloned.move(m)
if cloned.won? and is_maximizer
next WIN
elsif cloned.won? and !is_maximizer
next LOSS
elsif cloned.tie?
next NEUTRAL
elsif current_depth == max_depth
# two matched spots with open spots = 2
# three match (with open spots) = 3
next NEUTRAL
else
# TODO: this returns an array but needs to return value
# next 0
next score_board(cloned, !is_maximizer, current_depth + 1, max_depth)
end
end
if is_maximizer
score = LOSS
scores.each do |s|
score = s if s > score
end
return score
else
score = WIN
scores.each do |s|
score = s if s < score
end
return score
end
end
private
def move_from_scores(moves, scores)
score_to_beat = LOSS
move_indices = []
scores.each_with_index do |val, index|
if val == score_to_beat
move_indices << index
elsif val > score_to_beat
move_indices = [index]
score_to_beat = val
end
end
moves[move_indices.sample]
end
def clone_board(board)
cloned_board = Board.new(board.width, board.height)
cloned_board.board = board.board.clone
cloned_board.turn = board.turn
cloned_board
end
end