-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimulation.py
101 lines (82 loc) · 3.94 KB
/
Simulation.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
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
from pathfinding import optimalPath
import random
from building import Person
def getIsDead(victim, intruder, building):
#check if victim made it out of building
if(victim.position_x > len(building) or victim.position_x < 0 or victim.position_y > len(building[0]) or victim.position_y < 0):
return False
#victim and intruder same position
if(victim.position_x == intruder.position_x and victim.position_y == intruder.position_y):
return True
#victim or intruder in a room
if(building[victim.position_x][victim.position_y].type == "Room" or building[intruder.position_x][intruder.position_y].type == "Room"):
return False
if(intruder.position_y == victim.position_y): #same y plane
dist = intruder.position_y - victim.position_y
if(dist < 0): #intruder is north
for i in range(intruder.position_y, victim.position_y):
if(building[intruder.position_x][i].type == "Room"):
return False
return True
else: #intruder is south
for i in range(victim.position_y, intruder.position_y):
if(building[intruder.position_x][i].type == "Room"):
return False
return True
def checkDeaths(victims, shooter, building):
deadVictims = []
for victim in victims:
if(getIsDead(victim, shooter, building)):
deadVictims.append(victim)
victim.die()
return deadVictims
def move_shooter(shooter, building):
directions = [(-1, 0), (1, 0), (0, -1), (0, 1)] # N, S, W, E
possible_moves = []
for dx, dy in directions:
nx, ny = shooter.position_x + dx, shooter.position_y + dy
if 0 <= nx < len(building) and 0 <= ny < len(building[0]):
# Allow movement into both halls and rooms
possible_moves.append((nx, ny))
if possible_moves:
# Randomly choose a move from the available options
shooter.last_position = (shooter.position_x, shooter.position_y) # Update last position before moving
shooter.position_x, shooter.position_y = random.choice(possible_moves)
else:
# Move backward if there are no possible moves
shooter.position_x, shooter.position_y = shooter.last_position
def simulate(building, exits, victims, shooter):
total_deaths = 0
total_escape = 0
turn_limit = 10 # Set a limit to avoid infinite loops
for turn in range(turn_limit):
# Move the shooter
move_shooter(shooter, building)
# Evacuate victims
for victim in victims:
if victim.status == "Alive":
path_to_exit = optimalPath(victim, building, exits)
if path_to_exit:
next_pos = path_to_exit[1] # Get the next position on the path
if len([v for v in victims if (v.position_x, v.position_y) == next_pos and v.status == "Alive"]) < 20:
victim.position_x, victim.position_y = next_pos
# Check for deaths
dead_victims = checkDeaths(victims, shooter, building)
total_deaths += len(dead_victims)
for v in victims:
if v.status == "Safe":
total_escape +=1
# Remove dead victims from the list
victims = [v for v in victims if v.status == "Alive"]
# Print current positions
for v in victims:
print(f"{v.type} at ({v.position_x}, {v.position_y})")
print(f"Shooter at ({shooter.position_x}, {shooter.position_y})")
print(f"Total deaths so far: {total_deaths}\n")
print(f"Still alive in building: {len(victims)}\n")
print(f"Made it out the building: {total_escape}\n")
# End simulation if all victims are dead or evacuated
if not victims:
print("All victims are dead.")
break
return total_deaths