-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmontecarlo.py
32 lines (22 loc) · 951 Bytes
/
montecarlo.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
from game import Game
import random
import numpy as np
from copy import deepcopy
MOVES = ['up','down','left','right']
class MonteCarlo:
def __init__(self, board, simulations):
self.simulations = simulations
self.original_board = board
self.move_scores = [0,0,0,0] # scores by move
def solve(self):
for i, move in enumerate(MOVES):
for c in range(self.simulations):
simulation = Game()
simulation.board = [row[:] for row in self.original_board] # deepcopy(self.original_board)
simulation.call_move(move)
while not simulation.is_loss():
simulation.call_move(random.choice(MOVES))
self.move_scores[i] += simulation.board_sum()
index = np.argmax(self.move_scores)
best_move = MOVES[index]
return best_move