-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweather.py
More file actions
121 lines (109 loc) · 4.97 KB
/
Copy pathweather.py
File metadata and controls
121 lines (109 loc) · 4.97 KB
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
import random
# 定义天气条件
temperatures = ['极冷', '冷', '凉爽', '温和', '温暖', '热', '极热']
weathers = ['晴朗', '多云', '阴天', '小雨', '中雨', '大雨', '雷暴', '飓风']
wind_directions = ['北', '东北', '东', '东南', '南', '西南', '西', '西北']
wind_speeds = ['无风', '微风', '和风', '强风', '大风', '狂风', '暴风', '台风']
with open('seed.txt', 'r') as f:
seed = int(f.read())
random.seed(seed)
def read_weather_from_file(filename):
"""从文件中读取天气数据"""
with open(filename, 'r', encoding='utf-8') as file:
lines = file.readlines()
if len(lines) != 4:
raise ValueError("文件中应包含四行天气数据")
return {
'temperature': lines[0].strip(),
'weather': lines[1].strip(),
'wind_direction': lines[2].strip(),
'wind_speed': lines[3].strip()
}
def write_weather_to_file(weather, filename):
"""将天气数据写入文件"""
with open(filename, 'w', encoding='utf-8') as file:
file.write(f"{weather['temperature']}\n")
file.write(f"{weather['weather']}\n")
file.write(f"{weather['wind_direction']}\n")
file.write(f"{weather['wind_speed']}\n")
def adjust_value(current_value, options, condition=None):
"""根据D20的结果调整值,并考虑特定条件"""
roll = random.randint(1, 20)
current_index = options.index(current_value)
# 其他条件下的处理
if condition is None and options != temperatures and options != wind_directions:
value = current_index
if value <= len(options) / 2:
if roll in range(1, 7):
return max(0, value - 1)
elif roll in range(17, 21):
return min(len(options) - 1, value + 1)
else:
if roll in range(1, 14):
return max(0, value - 1)
elif roll in range(19, 21):
return min(len(options) - 1, value + 1)
elif options == temperatures:
if roll in range(1, 7):
return max(0, current_index - 1)
elif roll in range(15, 21):
return min(len(options) - 1, current_index + 1)
elif options == wind_directions:
return random.randint(0, len(wind_directions) - 1)
else:
if roll in range(1, 13):
return max(0, current_index + 1)
return current_value
def adjust_weather(current_weather):
"""根据当前天气生成新的随机天气并应用特殊规则"""
# 调整温度
temperature_index = adjust_value(current_weather['temperature'], temperatures)
# 调整天气
weather_index = adjust_value(current_weather['weather'], weathers)
# 当天气下雨且温度高于冷时,温度可能下降
if current_weather['weather'] not in ['晴朗', '多云', '阴天'] and current_weather['temperature'] not in ['极冷']:
temperature_index = adjust_value(current_weather['temperature'], temperatures, True)
# 当天气下雨且风速低于雨量时,风速可能增加
if current_weather['weather'] in ['小雨', '中雨', '大雨', '雷暴', '飓风'] and \
current_weather['wind_speed'] not in ['大风', '狂风', '暴风', '台风']:
wind_speed_index = adjust_value(current_weather['wind_speed'], wind_speeds, True)
else:
wind_speed_index = adjust_value(current_weather['wind_speed'], wind_speeds)
# 确保 `wind_speed_index` 是整数
if type(weather_index) == str:
weather_index = weathers.index(current_weather['weather'])
if type(temperature_index) == str:
temperature_index = temperatures.index(current_weather['temperature'])
if type(wind_speed_index) == str:
wind_speed_index = wind_speeds.index(current_weather['wind_speed'])
# 如果天气为飓风或台风,则需要检查风速是否匹配
if current_weather['weather'] == '飓风' and current_weather['wind_speed'] != '暴风':
weather_index = max(0, weather_index - 1)
elif current_weather['wind_speed'] == '台风' and current_weather['weather'] != '雷暴':
wind_speed_index = max(0, wind_speed_index - 1)
# 返回结果
return {
'temperature': temperatures[temperature_index],
'weather': weathers[weather_index],
'wind_direction': wind_directions[random.randint(0, len(wind_directions) - 1)],
'wind_speed': wind_speeds[wind_speed_index]
}
# 主程序
filename = "weather.txt"
try:
# 读取当前天气
current_weather = read_weather_from_file(filename)
print("当前天气:")
for key, value in current_weather.items():
print(f"{key}: {value}")
# 生成随机天气
new_weather = adjust_weather(current_weather)
print("\n随机生成的新天气:")
for key, value in new_weather.items():
print(f"{key}: {value}")
# 写入新天气
write_weather_to_file(new_weather, filename)
except Exception as e:
print(f"发生错误: {e}")
with open('seed.txt', 'w') as f:
f.write(str(seed+1))