Python代写:CS2003 Bears and Fish

代写Python基础问题,模拟熊吃鱼的场景。

Part 1

Write a Python program to simulate an ecosystem containing two types of creatures, bears and fish, using a “Creature”class as an abstract base class. The ecosystem consists of a two dimensional “rectangular grid” of water having vertical dimension m 2 and horizontal dimension n 2 and, consequently, containing m x n cells. Each cell is either empty (i.e. has value None, which is a reserved word in Python) or is occupied by one and only one creature, either a bear or a fish. In each time step, based on a random process, each animal attempts to move into an adjacent grid location or stay where it is. A “move” is either horizontal (left or right) or vertical (up or down) and “move” is “cyclic.” (e.g. the cell adjacent to the right of a cell on the right boundary should be considered to be the cell on the left boundary of the same row; similarly for the other three boundary considerations.)

You program should be able to read any data file in the following format:

5 8      #any integers >=2 representing number of rows and number of columns
23       #number of random moves
B 3 6 2  #creature type (B for bear, F for fish), hori and vert cell location, strength)
P 2 5 7  #creature type, hori and vert cell location, strength
...
B 1 3 5  #creature type hori and vert cell location, strength
<eof>

Your program should also contain a section of working code, “commented out”, that could replace the read of a data file with interactive input.

Obviously, you improve the quality of your program by including exception handling for data and other error checking and recovery. For this project, you can assume that your program is only fed valid data and you are not required to do exception handling; however, include as much exception handling as you can.

Part 2

Add a Boolean gender field (character M or F) and an integer strength field (integer in the range 0 to 10) to Creature. If two creatures of the same type try to collide, then only create a new instance of that type if they are of difference genders. Otherwise, if two creatures of the same type and gender try to collide then only the one of the larger strength survives (in case of a tie, choose only one arbitrarily).

Deliverables

Please submit a zip file containing your source code file(s); your text data file containing at least 40 cells and at least, initially, 10 creatures; and a word or a txt file containing execution output of your program that prints the array status after the initial placement of all creatures and, subsequently, after every change in status.

Hints: It may be helpful to take note of the following working programs:

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
class stuff:
def __init__(self):
self.x = 1
self.y = 1
stuffarray = [[stuff() for i in range(0,9)] for j in range(0,9)]
print ((stuffarray[1][4]).x)
__
def sanitised_input(prompt, type_=None, min_=None, max_=None, range_=None):
if min_ is not None and max_ is not None and max_ < min_:
raise ValueError("min_ must be less than or equal to max_.")
while True:
ui = input(prompt)
if type_ is not None:
try:
ui = type_(ui)
except ValueError:
print("Input type must be {0}.".format(type_.__name__))
continue
if max_ is not None and ui > max_:
print("Input must be less than or equal to {0}.".format(max_))
elif min_ is not None and ui < min_:
print("Input must be greater than or equal to {0}.".format(min_))
elif range_ is not None and ui not in range_:
if isinstance(range_, range):
template = "Input must be between {0.start} and {0.stop}."
print(template.format(range_))
else:
template = "Input must be {0}."
if len(range_) == 1:
print(template.format(*range_))
else:
print(template.format(" or ".join((", ".join(map(str, range_[:-1])), str(range_[-1])))))
else:
return ui

age = sanitised_input("Enter your age: ", int, 1, 101)
answer = sanitised_input("Enter your answer: (P) Polygon, ", str.lower, range_=('a', 'b', 'c', 'd'))
__
from random import randint, choice
for i in (randint(1,5), randint(9,15), randint(21,27)):
print (i)