-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmaze.py
109 lines (87 loc) · 3.34 KB
/
maze.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
102
103
104
105
106
107
108
109
"""
Author: freezed <[email protected]> 2018-03-17
Version: 0.1
Licence: `GNU GPL v3` GNU GPL v3: http://www.gnu.org/licenses/
This file is part of [_ocp3_ project](https://github.com/freezed/ocp3)
"""
import os
import random
from conf import elmt_val, ERR_FILE, ERR_LINE, MAZE_SIZE
class Maze:
"""
Provides a usable maze from a text file
Checks the maze compatibility
Moves the player to it
"""
def __init__(self, filename):
"""
Initialise maze
The Maze object has given attributes:
:var bool status: False = End of game (file error, end of game or quit)
:var int COL_NB: column number of the maze
:var int RANGE: string range
:var int MAX_ITEMS: number of items
:var str string: contain maze without EOL in astring
:param filename: maze filename
"""
# Loading maze file
if os.path.isfile(filename) is False:
self.status = False
print(ERR_FILE.format(filename))
else:
with open(filename, "r") as maze_data:
splited_maze = maze_data.read().splitlines()
if self.check_file(splited_maze):
# Builds a square maze (end-line spaces are missing in file)
self.string = '\n'.join(
(self.check_line(line) for line in splited_maze)
)
# Place randomly 'item' on the maze
for symbol_to_place in elmt_val('symbol', 'item', True):
position = random.choice(
[idx for (idx, value) in enumerate(self.string)
if value == elmt_val('symbol', 'name', 'floor', 0)]
)
self.set_symbol(symbol_to_place, position)
self.MAX_ITEMS = sum(
1 for _ in elmt_val('name', 'item', True)
)
self.COL_NB = MAZE_SIZE + 1 # List starts to zero
self.RANGE = range(self.COL_NB * MAZE_SIZE - 1) # remove last EOL
self.status = True
else:
self.status = False
@staticmethod
def check_file(splited_maze):
"""
Checks the maze conformity before starting the game
:param list/str splited_maze: Maze splited in a list (line = index)
"""
if len(splited_maze) != MAZE_SIZE:
print(ERR_LINE.format(len(splited_maze)))
return False
# ++Add other checks here: elements inside, exit possible, etc++
else:
return True
@staticmethod
def check_line(line):
"""
Checks if a line has a good length (configured in MAZE_SIZE const).
Fill it if it's too small, truncate if it's too long.
"""
differance = MAZE_SIZE - len(str(line))
if differance < 0:
return line[:MAZE_SIZE]
elif differance > 0:
return line + (differance * elmt_val('symbol', 'name', 'floor', 0))
else:
return line
def set_symbol(self, symbol, pos):
"""
Set a symbol on the maze
Used for 'player' and 'floor' after collecting items
:param str symbol: the symbol to set
:param str pos: index in the string
"""
txt = self.string
self.string = txt[:pos] + symbol + txt[pos + 1:]