-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.py
66 lines (57 loc) · 2.3 KB
/
Main.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
65
66
from Gameboard import*
from Pacman_ghavi import*
from Ghost import *
from Gameboard import Gameboard
import copy
def minimax(board, depth, isMaximizing, alpha, beta):
if depth == 0 or board.is_game_over():
return board.evaluate()
if isMaximizing:
best_score = float('-inf')
for direction in ["up", "down", "left", "right"]:
new_board = copy.deepcopy(board)
new_board.pacman.move(direction)
score = minimax(new_board, depth - 1, False, alpha, beta)
best_score = max(best_score, score)
alpha = max(alpha, best_score)
if beta <= alpha:
break
return best_score
else:
best_score = float('inf')
for direction in ["up", "down", "left", "right"]:
for direction2 in ["up", "down", "left", "right"]:
new_board = copy.deepcopy(board)
new_board.ghosts[0].move(direction)
new_board.ghosts[1].move(direction2)
score = minimax(new_board, depth - 1, True, alpha, beta)
best_score = min(best_score, score)
beta = min(beta, best_score)
if beta <= alpha:
break
return best_score
def run_game():
board = Gameboard()
pacman = Pacman_ghavi(board)
ghosts = [Ghost(board) for _ in range(2)]
board.update_state(pacman, ghosts) # مقداردهی اولیه pacman و ghosts در GameBoard
while not board.is_game_over():
board.display()
best_move = None
best_score = float('-inf')
for direction in ["up", "down", "left", "right"]:
new_board = copy.deepcopy(board)
new_board.pacman.move(direction)
score = minimax(new_board, 4, False, float('-inf'), float('inf'))
if score > best_score:
best_score = score
best_move = direction
print(best_move)
if best_move:
pacman.move(best_move)
for ghost in ghosts:
ghost.move()
# اضافه کردن متد بهروزرسانی وضعیت بازی
board.update_state(pacman, ghosts)
print(sum(10 for x in range(board.height) for y in range(board.width) if board.board[x][y] == '#') - board.pacman.moves_count)
run_game()