avancé un peu la carte, j'ai mis 2 layers, dans la vérif d'une map je dois m'assurer que 2 téléporteurs soient bien face à face + test connexité
This commit is contained in:
parent
ee749a4c04
commit
2c4971c3fe
74
src/carte.py
74
src/carte.py
@ -1,14 +1,17 @@
|
||||
#!/usr/bin/env python3
|
||||
from enum import IntEnum
|
||||
|
||||
class Tile(IntEnum):
|
||||
class DotTile(IntEnum):
|
||||
NDT = 0 # no dot
|
||||
SPD = 1 # small pac-dot
|
||||
BPD = 2 # big pac-dot
|
||||
|
||||
class PhysTile(IntEnum):
|
||||
GRD = 0 # ground
|
||||
WAL = 1 # wall
|
||||
SPD = 2 # small pac-dot
|
||||
BPD = 3 # big pac-dot
|
||||
GSD = 4 # ghost door
|
||||
TPT = 5 # teleporter tile
|
||||
FIT = 6 # fully inaccessible tile
|
||||
GSD = 2 # ghost door
|
||||
TPT = 3 # teleporter tile
|
||||
FIT = 4 # fully inaccessible tile
|
||||
# ghost-cell ground as FIT ? verify no pac-dot here
|
||||
|
||||
class Map:
|
||||
@ -19,23 +22,28 @@ class Map:
|
||||
width = 28
|
||||
height = 31
|
||||
|
||||
def __init__(self, list_ = []):
|
||||
def __init__(self, physic_map = [], dots_map = []):
|
||||
"""
|
||||
list_ is the array containing elements:
|
||||
physic_map is the array containing elements:
|
||||
0: ground tile
|
||||
1: wall
|
||||
2: small pac-dot
|
||||
3: big pac-dot
|
||||
4: ghost door
|
||||
5: teleporter (no need to precise which to go because we assume it will
|
||||
2: ghost door
|
||||
3: teleporter (no need to precise which to go because we assume it will
|
||||
be at the opposite map tile)
|
||||
6: fully inaccessible tile (basically tiles that represent the "in-wall"
|
||||
4: fully inaccessible tile (basically tiles that represent the "in-wall"
|
||||
space)
|
||||
|
||||
dots_map is a layer on top of the physic_map of the same dimension which contains
|
||||
0: no dot
|
||||
1: small pac-dot
|
||||
2: big pac-dot
|
||||
"""
|
||||
self.map = list_ # in the first part we assume list_ is correct and no need to verify
|
||||
self.phys_map = phys_map # in the first part we assume phys_map is correct and no need to verify
|
||||
self.dots_map = dots_map
|
||||
self.intersect_map = [] # TODO - the layer which contains intersections pre-calculated
|
||||
|
||||
|
||||
def verify(self, list_) -> boolean:
|
||||
def verify(self, phys_map, dots_map) -> boolean:
|
||||
"""
|
||||
This method will verify if a given map is valid or not
|
||||
Return True if correct else False
|
||||
@ -48,36 +56,50 @@ class Map:
|
||||
|
||||
Each ground tile must have at least 2 ground tile neighboors because
|
||||
there is no dead-end tile in pac-man
|
||||
Each dot must be on a ground tile (not even a teleporter one)
|
||||
"""
|
||||
|
||||
if not (len(phys_map) == len(dots_map) == 31):
|
||||
return False
|
||||
|
||||
for i in range(len(phys_map)):
|
||||
if not (len(phys_map[i]) == len(dots_map[i]) == 28):
|
||||
return False
|
||||
|
||||
# 1 ghost door verification
|
||||
if sum(sub_arr.count(Tile.GSD) for sub_arr in list_) != 1:
|
||||
if sum(sub_arr.count(PhysTile.GSD) for sub_arr in phys_map) != 1:
|
||||
return False
|
||||
|
||||
# 4 big pac dots
|
||||
if sum(sub_arr.count(Tile.BPD) for sub_arr in list_) != 4:
|
||||
# # 4 big pac dots
|
||||
if sum(sub_arr.count(DotTile.BPD) for sub_arr in dots_map) != 4:
|
||||
return False
|
||||
|
||||
# 240 small pac-dots
|
||||
if sum(sub_arr.count(Tile.SPD) for sub_arr in list_) != 240:
|
||||
# # 240 small pac-dots
|
||||
if sum(sub_arr.count(DotTile.SPD) for sub_arr in dots_map) != 240:
|
||||
return False
|
||||
|
||||
# dots are only in ground tile
|
||||
for row in range(len(phys_map)):
|
||||
for col in range(len(phys_map[0])):
|
||||
if dots_map[row][col] and phys_map[row][col]: # no dot = 0; ground = 0
|
||||
return False
|
||||
|
||||
# odd number of teleporter tiles
|
||||
teleporter_count = sum(sub_arr.count(Tile.TPT) for sub_arr in list_)
|
||||
teleporter_count = sum(sub_arr.count(PhysTile.TPT) for sub_arr in phys_map)
|
||||
if teleporter_count % 2:
|
||||
return False
|
||||
|
||||
edges = list_[0][1:] + list_[height-1][1:] + [sub[0] for sub in list_] + [sub[width-1] for sub in list_][1:-1]
|
||||
edges = phys_map[0][1:] + phys_map[height-1][1:] + [sub[0] for sub in phys_map] + [sub[width-1] for sub in phys_map][1:-1]
|
||||
|
||||
# must verify teleporters are on edges
|
||||
teleporter_count -= edges.count(Tile.TPT)
|
||||
teleporter_count -= edges.count(PhysTile.TPT)
|
||||
if teleporter_count: # not all teleporters are on edges
|
||||
return False
|
||||
|
||||
### ATTENTION - we need to verify teleporters are in front of the other
|
||||
|
||||
# no ground tile on border
|
||||
if any(Tile.GRD in edges):
|
||||
if any(PhysTile.GRD in edges):
|
||||
return False
|
||||
|
||||
# now we have to verify there is no dead-end ground tile
|
||||
@ -85,12 +107,12 @@ class Map:
|
||||
for row in range(1, height-1):
|
||||
cpt = 0
|
||||
for x, y in ((0, 1), (1, 0), (-1, 0), (0, -1)):
|
||||
if list[row+y][col+x] in (Tile.GRD, Tile.TPT, Tile.BPD, Tile.SPD):
|
||||
if list[row+y][col+x] in (PhysTile.GRD, PhysTile.TPT, PhysTile.BPD, PhysTile.SPD):
|
||||
cpt += 1
|
||||
if cpt == 2:
|
||||
break
|
||||
if cpt < 2:
|
||||
return False
|
||||
# we have to verify if there is only 1 connexity component of (Tile.GRD, Tile.TPT, Tile.BPD, Tile.SPD)
|
||||
# we have to verify if there is only 1 connexity component of (PhysTile.GRD, PhysTile.TPT, PhysTile.BPD, PhysTile.SPD)
|
||||
|
||||
return True
|
||||
|
Reference in New Issue
Block a user