-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminimax.rb
More file actions
57 lines (55 loc) · 1.5 KB
/
minimax.rb
File metadata and controls
57 lines (55 loc) · 1.5 KB
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
require './evaluate'
class Minimax
def execute(board,depth)
value=0
bestMove=nil
highestValue=-Float::INFINITY
lowestValue=Float::INFINITY
board.getCurrentPlayer.getLegalMoves.each do |move|
movedBoard=move.execute
if(board.getTurn.isWhite)
value=min(movedBoard,depth-1)
else
value=max(movedBoard,depth-1)
end
if(board.getTurn.isWhite && value>=highestValue)
highestValue=value
bestMove=move
elsif(board.getTurn.isBlack && value<=lowestValue)
lowestValue=value
bestMove=move
end
end
return bestMove
end
def min(board,depth)
eval=Evaluate.new
if(depth==0 || board.getCurrentPlayer.isGameDone)
return eval.evaluate(board,depth)
else
lowestValue=Float::INFINITY
board.getCurrentPlayer.getLegalMoves.each do |move|
currentValue=max(move.execute,depth-1)
if(currentValue<=lowestValue)
lowestValue=currentValue
end
end
return lowestValue
end
end
def max(board,depth)
eval=Evaluate.new
if(depth==0 || board.getCurrentPlayer.isGameDone)
return eval.evaluate(board,depth)
else
greatestValue=-Float::INFINITY
board.getCurrentPlayer.getLegalMoves.each do |move|
currentValue=min(move.execute,depth-1)
if(currentValue>=greatestValue)
greatestValue=currentValue
end
end
return greatestValue
end
end
end