-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpectimax algorithim plus code.py
More file actions
53 lines (45 loc) · 1.74 KB
/
expectimax algorithim plus code.py
File metadata and controls
53 lines (45 loc) · 1.74 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
import copy
#
def evaluate(board):
return sum(sum(row) for row in board)
def get_empty_cells(board):
return [(i, j) for i in range (SIZE) for j in range (SIZE) if board[i][j] == 0]
def get_possible_moves(board):
return ['w', 's', 'a', 'd']
def make_move(board, move):
new_board = copy.deepcopy(board)
apply_move(new_board, move)
return new_board
def expectimax(board, depth, player_turn = True):
if depth == 0 or is_game_over(board):
return evaluate(board)
if player_turn:
max_scire = float('-inf')
for move in get_possible_moves(board):
new_board = make_move(board, move)
score = expectimax(new_board, depth - 1, False )
max_score = max(max_score, score)
return max_score
else:
empty_cells = get_empty_cells(board)
if not empty_cells:
return evaluate(board)
expected_score = 0
for cell in empty_cells:
for title_value in [2 , 4]:
new_board = copy.deepcopy(board)
new_board[cell[0]][cell[1]] = tile_value
probability = 0.9 if title_value == 2 else 0.1
score = expectimax(new_board, depth - 1, True)
expected_score += probability * score
return expected_score / len(empty_cells)
def get_best_move(board, depth):
best_move = None
max_score = float('-inf')
for move in get_possible_moves(board):
new_board = make_move(board, move)
score = expectimax(new_board, depth - 1 , False)
if score > max_score:
max_score = score
best_move = move
return best_move