-
Notifications
You must be signed in to change notification settings - Fork 0
/
hw8_part1.py
100 lines (68 loc) · 2.57 KB
/
hw8_part1.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
# -*- coding: utf-8 -*-
"""
Created on Fri Apr 23 23:19:12 2021
@author: LiWeiJun
"""
import json
class Bear(object):
def __init__(self,x0=0,y0=0):
self.x=x0
self.y=y0
def __str__(self):
bear='Active Bears:'
for i in data["active_bears"]:
a='('+str(i[0])+','+str(i[1])+')'
bear=bear+'\n'+'Bear at {0} moving {1}'.format(a,i[2])
return bear
class BerryField(object):
def __init__(self,x0=0,y0=0):
self.x=x0
self.y=y0
def __str__(self):
b=0
for i in data["berry_field"]:
for j in i:
b+=j
form='Field has {0} berries.'.format(b)
for list1 in range(len(data["berry_field"])):
for list2 in data["active_tourists"]:
for list3 in data["active_bears"]:
if list1==list2[0]:
data["berry_field"][list1][list2[1]]='T'
if list1==list3[0]:
data["berry_field"][list1][list3[1]]='B'
for ni in data["active_tourists"]:
for nk in data["active_bears"]:
if ni[0]==nk[0] and nk[1]==ni[1]:
data["berry_field"][nk[0]][nk[1]]='X'
for number in data["berry_field"]:
form=form+'\n'
for j in number:
form=form+str(j).rjust(4)
return form
class Tourist(object):
def __init__(self,x0=0,y0=0):
self.x=x0
self.y=y0
def __str__(self):
tour="Active Tourists:"
for i in data["active_tourists"]:
a='('+str(i[0])+','+str(i[1])+')'
tour=tour+'\n'+'Tourist at {}, 0 turns without seeing a bear.'.format(a)
return tour
if __name__=="__main__":
file=input('Enter the json file name for the simulation => ').strip()
print(file)
print()
f = open(file)
data = json.loads(f.read())
print(BerryField())
print()
print(Bear())
print()
print(Tourist())
#print(data["berry_field"])
#print(data["active_bears"])
#print(data["reserve_bears"])
#print(data["active_tourists"])
#print(data["reserve_tourists"])