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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
|
from typing import List
import pygame
import math
from Direction import DIRECTION
import map as Map
import random
class Ghost():
def __init__(self):
self.x = 75
self.y = 75
self.tx = 0
self.ty = 0
self.speed = 3
def heuristic(self, pacman_pos, next_pos):
return abs(next_pos[0] - pacman_pos[0]) + abs(next_pos[1] - pacman_pos[1])
# checks if the current position of pacman is either a dot, big dot or free
def is_valid(self, maze, x, y):
is_dot = maze[y][x] == Map.D
is_big_dot = maze[y][x] == Map.BD
is_free = maze[y][x] == 0
return (is_dot or is_free or is_big_dot)
# checks collision with pacman and obstacles returns false if there is a collision and true otherwise
def check_collision(self, nx, ny, gx, gy, maze):
direct_x = [1, 0, -1, 0, 1, 1, -1, -1]
direct_y = [0, 1, 0, -1, -1, 1, -1, 1]
# print(f"nx: {nx}, ny: {ny}")
# print()
# print(maze[3][13], self.is_valid(maze, 13, 3))
print()
if nx == 391 and ny == 79:
print("----------------------")
for i in range(len(direct_x)):
px = nx + direct_x[i] * 14
py = ny + direct_y[i] * 14
print(px, py)
x = px // gx
y = py // gy
# print(f"x: {x}, y: {y} is_valid({x}, {y}) = {self.is_valid(maze, x, y)}")
if not self.is_valid(maze, x, y):
return False
return True
def get_possible_moves(self, maze):
possible_directions = {}
up = (0, -self.speed)
down = (0, self.speed)
right = (self.speed, 0)
left = (-self.speed, 0)
if self.check_collision(up[0], up[1], 30, 30, maze):
possible_directions[up] = DIRECTION.UP
if self.check_collision(down[0], down[1], 30, 30, maze):
possible_directions[down] = DIRECTION.DOWN
if self.check_collision(right[0], right[1], 30, 30, maze):
possible_directions[right] = DIRECTION.RIGHT
if self.check_collision(left[0], left[1], 30, 30, maze):
possible_directions[left] = DIRECTION.LEFT
return possible_directions
# def get_next_move(self, pacman_pos, maze):
# possible_directions = self.get_possible_moves(maze)
#
# next_move = DIRECTION.RIGHT
# mn = 1000000 # really big number
#
# for pos, dir in possible_directions.items():
# h = self.heuristic(pacman_pos, pos)
# if h < mn:
# mn = h
# print("dir: ", dir)
# next_move = dir
#
# print("Min h:", mn)
# # print("Left: ", self.heuristic(pacman_pos))
# print("Best: next_move", next_move)
# return next_move
def get_next_move(self, pacman_pos, maze):
dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]
ret = len(dx) * [math.inf]
for i in range(len(dx)):
nx = self.x + dx[i] * self.speed
ny = self.y + dy[i] * self.speed
if self.check_collision(nx, ny, 30, 30, maze):
ret[i] = self.heuristic((nx, ny), pacman_pos)
min_idx = ret.index(min(ret))
# if min_idx == 1:
# print(f"({self.x}, {self.y})")
# print(self.check_collision(391, 79, 30, 30, maze))
return (dx[min_idx] * self.speed, dy[min_idx] * self.speed)
def move(self, maze, player_pos):
next_move = self.get_next_move(player_pos, maze)
dx = 0
dy = 0
self.x += next_move[0]
self.y += next_move[1]
# if next_move == DIRECTION.UP:
# self.ty = -self.speed
# self.tx = 0
# elif next_move == DIRECTION.DOWN:
# self.ty = self.speed
# self.tx = 0
# elif next_move == DIRECTION.RIGHT:
# self.tx = self.speed
# self.ty = 0
# elif next_move == DIRECTION.LEFT:
# self.tx = -self.speed
# self.ty = 0
#
#
# if self.check_collision(self.tx, self.ty, 30, 30, maze):
# dx = self.tx
# dy = self.ty
#
# if dx < 0:
# self.direction = DIRECTION.LEFT
# elif dx > 0:
# self.direction = DIRECTION.RIGHT
# elif dy < 0:
# self.direction = DIRECTION.UP
# elif dy > 0:
# self.direction = DIRECTION.DOWN
#
# if self.check_collision(dx, dy, 30, 30, maze):
# self.x += dx
# self.y += dy
def draw(self, screen):
radius = 30 // 2
pos = (self.x , self.y)
pygame.draw.circle(screen, 'green', pos, radius)
|