Repository navigation
Expand file tree
/
Copy pathminimax.py
More file actions
63 lines (48 loc) · 2.16 KB
/
Copy pathminimax.py
File metadata and controls
63 lines (48 loc) · 2.16 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
58
59
60
61
62
63
from copy import deepcopy
import pygame
WHITE = (255, 255, 255)
RED = (255,0,0)
def minimax(board, depth, player, game):
if depth == 0 or board.winner() != None:
return board.evaluate(), board
if player.color == RED:
maxEval = float('-inf')
best_move = None
for move in get_all_moves(game.board, player, game):
evaluation = minimax(move, depth-1, player, game)[0][0]
maxEval = max(maxEval, evaluation)
if maxEval == evaluation:
best_move = move
return maxEval, best_move
else:
minEval = float('inf')
best_move = None
for move in get_all_moves(game.board, player, game):
evaluation = minimax(move, depth-1, player, game)[0][1]
minEval = min(minEval, evaluation)
if minEval == evaluation:
best_move = move
return minEval, best_move
def simulate_move(piece, move, board, game):
board.move(piece, move[0], move[1])
return board
def get_all_moves(board, agent, game):
moves = [] # boardların listesi
for piece in board.get_all_agents(): # 1. runnerı alacak, 2. chaser1 i alacak, sonra chaser2 yi alacak
valid_moves = board.get_valid_moves(agent)
moves_array = [valid_moves[key] for key in valid_moves.keys()] # [(1,0), (2,3), (1,4)] gibi bir liste
for i in range(len(moves_array)):
# moves_arrayin içindeki her kordinatı simulate_move ediyoruz
# draw_moves(game, board, piece)
temp_board = deepcopy(board) # geçici bir board yarattık
temp_agent = temp_board.get_agent(agent.row, agent.col)
new_board = simulate_move(temp_agent, moves_array[i], temp_board, game)
moves.append(new_board)
return moves
def draw_moves(game, board, piece):
valid_moves = board.get_valid_moves(piece)
board.draw(game.window)
pygame.draw.circle(game.window, (0,255,0), (piece.x, piece.y), 50, 5)
game.draw_valid_moves(valid_moves)
pygame.display.update()
pygame.time.delay(10)