-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcity.py
218 lines (184 loc) · 6.07 KB
/
city.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#Keyik Annagulyyeva
#Advised by Jianghao Wang
import numpy as np
import random
import os
import time
#Displays my city
def Display_array(arr):
for y in range(len(arr)):
for x in range(len(arr[y])):
print(arr[y][x], end='')
print()
size = int(input("Enter a city size: "))
arr = np.full((size,size),'.')
population = 0
revenue = 0
a = 0.5
#Cash flood that updates the city to a dollar sign (can be considered a 'natural' disaster.)
def Disaster(arr, population, revenue):
for y in range(len(arr)):
for x in range(len(arr[y])):
arr[y][x] = '$'
time.sleep(a)
os.system('clear||cls')
Display_array(arr)
population -= 2
revenue += 200000
return population, revenue
#Monster that eats the part of the city (these monsters come once in a while as they get hungry)
def Disaster_upgrade(arr, population, revenue):
y = random.randint(0, size - 2)
x = 0
for times in range(size - 1):
arr[y][x] = '@'
arr[y][x + 1] = '@'
arr[y + 1][x] = '@'
arr[y + 1][x + 1] = '@'
x = x + 1
time.sleep(a)
os.system('clear||cls')
Display_array(arr)
arr[y][x - 1] = '.'
arr[y + 1][x - 1] = '.'
arr[y][x] = '.'
arr[y][x - 1] = '.'
arr[y + 1][x] = '.'
arr[y + 1][x - 1] = '.'
os.system('clear||cls')
Display_array(arr)
population -= 2
revenue -= 1000
#Turn the city back to '.' symbols, back to normal.
def clearDisaster(arr):
for y in range(len(arr)):
for x in range(len(arr[y])):
arr[y][x] = '.'
time.sleep(a)
os.system('clear||cls')
Display_array(arr)
#Allow the user to choose their own building.
def choose_building():
options = ''' What would you like to build?
1. HOUSE.
2. FOUNTAIN.
3. LIBRARY.
4. BANK.
5. RIVER.
6. STREET.
'''
print(options)
user_building = input("Please choose a building (1/2/3/4/5/6): ")
return user_building
#Define each of my buildings, including the demolish feature of the game.
def house(arr,coord_y, coord_x, population, revenue):
if arr[coord_y][coord_x] != '.':
arr[coord_y][coord_x] = "X"
arr[coord_y][coord_x + 1] = "X"
population -= 3
revenue -= 5000
else:
arr[coord_y][coord_x] = "["
arr[coord_y][coord_x + 1] = "]"
population += 10
revenue += 10000
return population, revenue
def fountain(arr, coord_y, coord_x, population, revenue):
if arr[coord_y][coord_x] != '.':
arr[coord_y][coord_x] = "X"
population -= 2
revenue -= 8000
else:
arr[coord_y][coord_x] = "%"
arr[coord_y][coord_x + 1 ] = "%"
arr[coord_y + 1][coord_x] = "%"
arr[coord_y + 1][coord_x + 1] = "%"
population += 5
revenue += 10500
return population, revenue
def library(arr, coord_y, coord_x, population, revenue):
if arr[coord_y][coord_x] != '.':
arr[coord_y][coord_x] = "X"
population -= 8
revenue -= 5200
else:
arr[coord_y][coord_x] = "#"
arr[coord_y][coord_x + 1 ] = "#"
arr[coord_y + 1][coord_x] = "#"
arr[coord_y + 1][coord_x + 1] = "#"
population += 5
revenue += 10000
return population, revenue
def bank(arr, coord_y, coord_x, population, revenue):
if arr[coord_y][coord_x] != '.':
arr[coord_y][coord_x] = "X"
population -= 10
revenue += 5000
else:
arr[coord_y][coord_x] = "$"
arr[coord_y][coord_x + 1 ] = "$"
population += 10
revenue += 20000
return population, revenue
def river(arr, coord_y, coord_x):
if arr[coord_y][coord_x] != '.':
arr[coord_y][coord_x] = "X"
else:
arr[coord_y][coord_x] = "~"
arr[coord_y + 1 ][coord_x] = "~"
arr[coord_y + 2 ][coord_x] = "~"
def street(arr, coord_y, coord_x):
if arr[coord_y][coord_x] != '.':
arr[coord_y][coord_x] = "X"
else:
arr[coord_y][coord_x] = '='
arr[coord_y][coord_x+1] = '='
arr[coord_y][coord_x + 2] = '='
#Call out all of my previosus functions into the CityGame.
def cityGame(arr, population, revenue):
#Randomize the 'natural' disasters.
disaster = random.randint(0,4)
if disaster == 0:
population, revenue = Disaster(arr, population, revenue)
clearDisaster(arr)
elif disaster == 1:
Disaster_upgrade(arr, population, revenue)
else:
Display_array(arr)
if population < 0:
population = 0
#Allow the user to quit the game.
print()
print("Revenue: $", revenue)
print("Population: ", population)
leave_the_city = input("Please type 'QUIT' if you want to quit.\nOr press anything on your keyboard to continue: ")
print()
if leave_the_city == "Quit" or leave_the_city == "quit":
exit()
user_building = choose_building()
#Promt the user to try again if the input is out of size of the matrix or simply invalid.
try:
coord_y, coord_x = [int(i) for i in input("Please pick TWO coordinates: ").split()]
except:
print("Not correct input.")
return population, revenue
if coord_y > size or coord_x > size or coord_y < 0 or coord_x < 0:
print("Input out of bounds.")
return population, revenue
user_building = user_building.strip()
if user_building == "1":
population, revenue = house(arr, coord_y, coord_x, population, revenue)
elif user_building == "2":
population, revenue = fountain(arr, coord_y, coord_x, population, revenue)
elif user_building == "3":
population, revenue = library(arr, coord_y, coord_x, population, revenue)
elif user_building == "4":
population, revenue = bank(arr, coord_y, coord_x, population, revenue)
elif user_building == "5":
river(arr, coord_y, coord_x)
else:
street(arr, coord_y, coord_x)
return population, revenue
#Main while loop within 10 lines of code.
while True:
population, revenue = cityGame(arr, population, revenue)