-
Notifications
You must be signed in to change notification settings - Fork 0
/
CreateWorld.py
68 lines (51 loc) · 1.78 KB
/
CreateWorld.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
'''Script to create a random input file of the correct format'''
import random
import string
import sys
def generateName(length):
'''Generate a random name of a given length.
A minimum length of 1 needs to be input.
'''
VOWELS = "aeiou"
CONSONANTS = "".join(set(string.ascii_lowercase) - set(VOWELS))
name = ""
for i in range(length):
if i % 2 == 0:
name += random.choice(CONSONANTS)
else:
name += random.choice(VOWELS)
return name.capitalize()
def makeWorldFile(worldSize):
'''Create a file with the initial world map.
Each row has the city name followed by 1 to 4 directions and the city they lead to.
Row example:
Barkouzi north=Milini west=Sunsi
'''
if(worldSize<5):
print("Please make a world with more than 4 cities: The input must be a positive integer bigger than 4.")
return
directions = ["north","east","west","south"]
cities = []
for i in range(worldSize):
cityName = generateName(random.randint(2,6))
cities.append(cityName)
with open("World_Map.txt","w") as f:
for city in cities:
f.write(city + ' ')
for j in range(random.randint(1,4)):
connections = []
nextConnection = random.choice(cities)
while nextConnection in connections:
nextConnection = random.choice(cities)
f.write(directions[j] +'='+nextConnection+' ')
connections.append(nextConnection)
f.write('\n')
def main():
try:
cityNumber = int(sys.argv[1])
except ValueError:
print("Please use an integer as an argument")
return
makeWorldFile(cityNumber)
if __name__ == "__main__":
main()