@ADoctor/

Farming Simulator

Python

Original name: BraveDarkAfkgaming

fork
loading
Files
  • main.py
  • 4x42YearsOn.txt
  • previous10YearsOn.txt
  • previous6YearsOn.txt
  • previousFile.txt
  • sample3YearsOn.txt
  • sampleField.txt
main.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
import random

def printField(field): #Outputs the field in a particular format
	for f in field:
		for e in f:
			print(e,end=' ')
		print()
	print("")

def placeObject(field,element,length,width): #Randomly places an object in the field
	placed = False
	while placed == False:
		randrow = random.randint(0,length) #Chooses random co-ordinates for the object
		elementRow = []
		for i in field[randrow-1]:
			elementRow.append(i)
		randcol = random.randint(0,width)
		if elementRow[randcol-1] == '_': #Checks if the co-ordinates are occupied
			elementRow[randcol-1] = element
			field[randrow-1] = elementRow #Places in the object
			placed = True

def createField(seeds,rocks,length,width): #Creates a randomised field with rocks and seeds
	field = []
	fieldRow = []
	for w in range(0,width):
		fieldRow.append('_') #Creates an empty field row
	for l in range(0,length):
		field.append(fieldRow) #Creates an empty field
	for r in range(0,rocks):
		placeObject(field,'R',length,width) #Adds the rocks
	for s in range(0,seeds):
		placeObject(field,'.',length,width) #Adds the seeds
	printField(field)
	return field
	#L4
	pass

def getVariables(): #Retrieves the size of the field and the amount of rocks and seeds wanted in the randomised field
	while True:
		try: #Gets inputs
			seeds = int(input("How many seeds would you like to plant? "))
			rocks = int(input("How many rocks are present in the field? "))
			length = int(input("What is the length of your field? "))
			width = int(input("What is the width of your field? "))
			if seeds + rocks <= length * width: #Validation
				break
			else:
				print("Sorry, but there are too many seeds and rocks overall. They will not fit on the board.")
		except ValueError:
			print("Sorry, but that is not a valid input. Please input a number.")
	print("")
	return seeds,rocks,length,width
	#L4
	pass

def createRandomField(): #Creates a randomised field
	seeds,rocks,length,width = getVariables() #Gets the variables
	field = createField(seeds,rocks,length,width) #Creates the field
	return field,length,width
	#L3
	pass

def setUpField(filename): #Turns a file into a working field
	fieldlength = 0
	fieldtxt = open(filename,'r') #Turns a file into a 1D array
	field = fieldtxt.readlines()
	for row in range(0,len(field)):
		field[row] = field[row].strip('\n')
		fieldlength += 1
	fieldwidth = len(field[0])
	newField = []
	for i in field: #Turns the 1D array into a 2D array
		fieldRow = []
		for j in i:
			fieldRow.append(j)
		newField.append(fieldRow)
	printField(newField)
	return newField,fieldlength,fieldwidth
	#L4
	pass

def getPreviousField(): #Creates a field from a previous file
	filename = input("What is the name of the file you wish to access? Do not include the .txt at the end. ") #Gets the name of the file being used
	filename += '.txt'
	print("File name: "+filename)
	field,length,width = setUpField(filename) #Creates the field from the file
	return field,length,width
	#L3
	pass

def getSampleField(): #Creates a field from a sample file
	field,length,width = setUpField('sampleField.txt')
	return field,length,width
	#L3
	pass

def generateField(): #Generates a field
	while True:
		backToMenu = False
		try:
			option = int(input("""	Which of the following would you like to do?
	1. Use a pre-generated sample field
	2. Use a field from a text file
	3. Generate a random field
	0. Quit
	Input the number corresponding to your chosen option: """)) #Gets the user's choice for what type of field they want to use
			print("")
			if option == 1:
				field,length,width = getSampleField() #Gets a field from the sample file
				break
			elif option == 2:
				field,length,width = getPreviousField() #Gets field from a previous file
				break
			elif option == 3:
				field,length,width = createRandomField() #Creates a random field
				break
			elif option == 0: #Quits the program
				backToMenu = True
				field = ''
				length = 0
				width = 0
				break
			else: #Validation
				print("""Sorry, that's not a valid option. Please try again.
	""")
		except ValueError:
			print("""Sorry, that's not a valid option. Please try again.
	""")
	return backToMenu,field,length,width
	#L2
	pass

def simulateWinter(field): #Kills all potatoes
	print("""Winter
	""")
	for i in range(0,len(field)):
		for j in range(0,len(field[i])):
			if field[i-1][j-1] == 'O':
				field[i-1][j-1] = '_'
	return field
	#L3
	pass

def simulateFrost(field): #Kills a certain amount of seeds depending on the severity
	frostSeverity = random.randint(0,3) #Randomly chooses the severity
	if frostSeverity == 0:
		print("""No frost
		""")
	elif frostSeverity == 1:
		print("""Light frost
		""")
	elif frostSeverity == 2:
		print("""Frost
		""")
	else:
		print("""Severe frost
		""")
	frostCounter = 0
	for i in range(0,len(field)): #Kills every x seed
		for j in range(0,len(field[i])):
			if field[i-1][j-1] == '.':
				frostCounter += 1
				if frostCounter % 6 < frostSeverity:
					field[i-1][j-1] = '_'
	return field
	#L4
	pass

def simulateSpring(field): #Simulates a frost, then turns all seeds into potatoes
	print("""Spring
	""")
	field = simulateFrost(field) #Simulates frost
	for i in range(0,len(field)): #Turns all seeds into potatoes
		for j in range(0,len(field[i])):
			if field[i-1][j-1] == '.':
				field[i-1][j-1] = 'O'
	return field
	#L3
	pass

def simulateDrought(field): #Kills a certain proportion of potatoes, depending on a random variable
	droughtSeverity = random.randint(0,2) #Gets the random variable
	if droughtSeverity == 0:
		print("""No drought
		""")
	elif droughtSeverity == 1:
		print("""Drought
		""")
	else:
		print("""Severe drought
		""")
	droughtCounter = 0
	for i in range(0,len(field)): #Kills the potatoes
		for j in range(0,len(field[i])):
			if field[i-1][j-1] == 'O':
				if droughtCounter % 4 < droughtSeverity:
					field[i-1][j-1] = '_'
				droughtCounter += 1
	return field
	#L4
	pass

def simulateSummer(field): #Simulates a drought
	print("""Summer
	""")
	field = simulateDrought(field)
	return field
	#L3
	pass

def simulateWind(): #Creates and displays the wind
	windS = random.randint(-1,1) #Gets the wind
	windE = random.randint(-1,1)
	windSstr = ''
	windEstr = ''
	if windS == 1: #Creates a string for the wind
		windSstr = 'south'
	elif windS == -1:
		windSstr = 'north'
	if windE == 1:
		windEstr = 'east'
	elif windE == -1:
		windEstr = 'west'
	if windE == 0 and windS == 0:
		print("No wind is present.")
	else:
		print("The wind is travelling to the",windSstr+windEstr+'.') #Displays the string
	return windS,windE
	#L4
	pass

def simulateSeeds(field,y,x,windY,windX): #Drops seeds
	for i in range(y-1+windY,y+2+windY):
		for j in range(x-1+windX,x+2+windX):
			if i+1>len(field) or i<0 or j<0:
				print('',end='')
			elif j+1>len(field[i]):
				print('',end='')
			elif field[i][j] != '_':
				print('',end='')
			else:
				field[i][j] = '.'
	return field

def simulateAutumn(field): #Creates wind and drops seeds
	print("""Autumn
	""")
	windS, windE = simulateWind() #Creates wind
	for i in range(0,len(field)):
		for j in range(0,len(field[i])):
			if field[i][j] == 'O': #Drops seeds
				field = simulateSeeds(field,i,j,windS,windE)
	return field
	#L3
	pass

def simulateSeasons(field): #Simulates one year
	field = simulateWinter(field)
	printField(field)
	field = simulateSpring(field)
	printField(field)
	field = simulateSummer(field)
	printField(field)
	field = simulateAutumn(field)
	printField(field)
	return field
	#L2
	pass

def saveField(field): #Saves the field if requested
	while True:
		toSave = input("Would you like to save this field? Y/N ").upper() #Gets the user's response as to if they want to save
		if toSave == 'N':
			break
		elif toSave == 'Y':
			fileName = input("What do you want your file's name to be? Do not include the .txt at the end. ")
			fileName += '.txt'
			try:
				fileToSave = open(fileName,'r')
				print("Sorry, that file name already exists.")
			except FileNotFoundError:
				print("File name: "+fileName)
				rows = len(field)-1
				fileToSave = open(fileName,'w+')
				for i in range(0,rows+1):
					for j in field[i]:
						fileToSave.write(j)
					if i != rows:
						fileToSave.write('\n')
				break
		else:
			print("Sorry, that's not a valid response.")
	#L2
	pass

def main(): #The main program
	while True:
		backToMenu,field,length,width = generateField()
		if backToMenu == False:
			try:
				years = int(input("How many years do you want to simulate? "))
				if years < 1:
					print("Since the amount of years you chose is less than 1, it will default to 1.")
					years = 1
			except ValueError:
				print("Since the amount of years you chose is not an integer, it will default to 1.")
				years = 1
			currentYear = 0
			while currentYear < years:
				field = simulateSeasons(field)
				currentYear += 1
			saveField(field)
		else:
			break
	#L1
	pass

main()