léger avancement de la carte

This commit is contained in:
DylanVsn 2019-11-06 22:45:29 +01:00
parent 7d52e0a85c
commit a75f5298a0
1 changed files with 91 additions and 3 deletions

View File

@ -1,5 +1,93 @@
#!/usr/bin/env python3
from enum import IntEnum
"""
Les cartes sont sur 28×31
"""
class Tile(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
class Map:
"""
Pacman maps size is 28×31
"""
width = 28
height = 31
def __init__(self, list_ = []):
"""
list_ 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
be at the opposite map tile)
6: fully inaccessible tile (basically tiles that represent the "in-wall"
space)
"""
self.map = list_ # in the first part we assume list_ is correct and no need to verify
def verify(self, list_) -> boolean:
"""
This method will verify if a given map is valid or not
Return True if correct else False
we will assume
there are only:
- 1 ghost door
- 4 big pac-dots
- 240 pac-dots
Each ground tile must have at least 2 ground tile neighboors because
there is no dead-end tile in pac-man
"""
# 1 ghost door verification
if sum(sub_arr.count(Tile.GSD) for sub_arr in list_) != 1:
return False
# 4 big pac dots
if sum(sub_arr.count(Tile.BPD) for sub_arr in list_) != 4:
return False
# 240 small pac-dots
if sum(sub_arr.count(Tile.SPD) for sub_arr in list_) != 240:
return False
# odd number of teleporter tiles
teleporter_count = sum(sub_arr.count(Tile.TPT) for sub_arr in list_)
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]
# must verify teleporters are on edges
teleporter_count -= edges.count(Tile.TPT)
if teleporter_count: # not all teleporters are on edges
return False
# no ground tile on border
if any(Tile.GRD in edges):
return False
# now we have to verify there is no dead-end ground tile
for col in range(1, width-1):
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):
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)
return True