/
MiniMaxOpeningImproved.py
65 lines (49 loc) · 1.84 KB
/
MiniMaxOpeningImproved.py
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
from MorrisGame import *
from Utils import *
import sys
statesReached = 0
def MinMax(board, depth, player1):
"""
:param board: a board position
:param depth: depth to check using min max
:param player1: player1 or not player 1
:return: object with next board position and evalutor.
"""
global statesReached
finalEvaluator = evaluator()
if depth != 0:
depth -= 1
currentEvaluator = evaluator()
if player1:
v = -sys.maxsize-1
possiblePositions = generateAdd([i for i in board])
else:
v = sys.maxsize
possiblePositions = generateAdd(InvertBoard([i for i in board]))
for position in possiblePositions:
if player1:
currentEvaluator = MinMax(position, depth, False)
if v < currentEvaluator.evaluator:
v = currentEvaluator.evaluator
finalEvaluator.board = position
else:
position = "".join(InvertBoard([i for i in position]))
currentEvaluator = MinMax(position, depth, True)
if v > currentEvaluator.evaluator:
v = currentEvaluator.evaluator
finalEvaluator.board = position
finalEvaluator.evaluator = v
else:
finalEvaluator.evaluator = staticEstimationImprovedOpening([i for i in board])
statesReached += 1
return finalEvaluator
if __name__ == '__main__':
file1 = open(sys.argv[1], "r")
file2 = open(sys.argv[2], "w")
depth = int(sys.argv[3])
inBoard = file1.read()
val = MinMax(inBoard, depth, True)
file2.write(val.board)
print("Input Position: "+inBoard+" Output Position: "+val.board)
print("Position Evaluated by static estimation: "+str(statesReached))
print("MINMAX estimate: "+str(val.evaluator))