Fall2020Berkeleycs61aProjectsAnts答案

更新时间:2023-06-18 05:15:09 阅读: 评论:0

Fall2020Berkeleycs61aProjectsAnts答案Fall 2020 Berkeley cs61a Projects Ants答案
"""CS 61A prents Ants Vs. SomeBees."""
import random
from ucb import main, interact, trace
from collections import OrderedDict
>>>#
# Core Class #
>>>#
class Place:
"""A Place holds incts and has an exit to another Place."""
def__init__(lf, name, exit=None):
"""Create a Place with the given NAME and EXIT.
name -- A string; the name of this Place.
exit -- The Place reached by exiting this Place (may be None).
"""
lf.name = name
lf.bees =[]# A list of Bees
lf.ant =None# An Ant
# Pha 1: Add an entrance to the exit
# BEGIN Problem 2
"*** YOUR CODE HERE ***"
it !=None:
# END Problem 2
def add_inct(lf, inct):
"""
Asks the inct to add itlf to the current place. This method exists so
it can be enhanced in subclass.
"""
inct.add_to(lf)
def remove_inct(lf, inct):
"""
Asks the inct to remove itlf from the current place. This method exists so
it can be enhanced in subclass.
"""
def__str__(lf):
return lf.name
class Inct:
"""An Inct, the ba class of Ant and Bee, has armor and a Place."""
damage =0
is_watersafe =Fal
# ADD CLASS ATTRIBUTES HERE
def__init__(lf, armor, place=None):
"""Create an Inct with an ARMOR amount and a starting PLACE."""
lf.armor = armor
lf.place = place  # t by Place.add_inct ve_inct
def reduce_armor(lf, amount):
def reduce_armor(lf, amount):
"""Reduce armor by AMOUNT, and remove the inct from its place if it
has no armor remaining.
>>> test_inct = Inct(5)
>>> duce_armor(2)
>>> test_inct.armor
3
"""
lf.armor -= amount
if lf.armor <=0:
ve_inct(lf)
lf.death_callback()
def action(lf, gamestate):
"""The action performed each turn.
gamestate -- The GameState, ud to access game state information.
好人好事的新闻"""
def death_callback(lf):
# overriden by the gui
pass
def add_to(lf, place):
"""Add this Inct to the given Place
By default just ts the place attribute, but this should be overriden in the subclass            to manipulate the relevant attributes of Place
"""
lf.place = place
def remove_from(lf, place):
lf.place =None
def__repr__(lf):
儿童生日祝福cname =type(lf).__name__
return'{0}({1}, {2})'.format(cname, lf.armor, lf.place)
class Ant(Inct):
"""An Ant occupies a place and does work for the colony."""
implemented =Fal# Only implemented Ant class should be instantiated
food_cost =0
# ADD CLASS ATTRIBUTES HERE
def__init__(lf, armor=1):
"""Create an Ant with an ARMOR quantity."""
Inct.__init__(lf, armor)
def can_contain(lf, other):
return Fal
def contain_ant(lf, other):
asrt Fal,"{0} cannot contain an ant".format(lf)
def remove_ant(lf, other):
asrt Fal,"{0} cannot contain an ant".format(lf)
def add_to(lf, place):
好习惯
if place.ant is None:
place.ant = lf
el:
# BEGIN Problem Optional 2
asrt place.ant is None,'Two ants in {0}'.format(place)
# END Problem Optional 2
# END Problem Optional 2
杭州安全教育平台Inct.add_to(lf, place)
def remove_from(lf, place):
if place.ant is lf:
place.ant =None
elif place.ant is None:
asrt Fal,'{0} is not in {1}'.format(lf, place)
el:
# queen or container (optional) or other situation
ve_ant(lf)
class HarvesterAnt(Ant):
"""HarvesterAnt produces 1 additional food per turn for the colony."""
name ='Harvester'
implemented =True
# OVERRIDE CLASS ATTRIBUTES HERE
food_cost =2
def action(lf, gamestate):
"""Produce 1 additional food for the colony.
gamestate -- The GameState, ud to access game state information.
"""
# BEGIN Problem 1
"*** YOUR CODE HERE ***"
gamestate.food = gamestate.food +1
# END Problem 1
class ThrowerAnt(Ant):
"""ThrowerAnt throws a leaf each turn at the nearest Bee in its range."""
name ='Thrower'
implemented =True
damage =1
# ADD/OVERRIDE CLASS ATTRIBUTES HERE
food_cost =3
min_range =0
max_range =float('inf')
def nearest_bee(lf, beehive):
"""Return the nearest Bee in a Place that is not the HIVE (beehive), connected to        the ThrowerAnt's Place by following entrances.
This method returns None if there is no such Bee (or none in range).
"""
# BEGIN Problem 3 and 4
lf.prospective_bees_place = lf.place
lf.distance =0
while True:
if lf.prospective_bees_place.bees ==[]:
if lf.prospective_ance == beehive:
return None
el:
lf.prospective_bees_place = lf.prospective_ance
lf.distance = lf.distance +1
el:#discover a bee
if lf.min_range <= lf.distance <= lf.max_range:
return rANTdom_el_none(lf.prospective_bees_place.bees)
el:
if lf.prospective_ance == beehive:
小孩记性差怎么办return None
el:
lf.prospective_bees_place = lf.prospective_ance
lf.prospective_bees_place = lf.prospective_ance
lf.distance = lf.distance +1
# END Problem 3 and 4
def throw_at(lf, target):
"""Throw a leaf at the TARGET Bee, reducing its armor."""
if target is not None:
def action(lf, gamestate):
"""Throw a leaf at the nearest Bee in range."""
lf.throw_arest_bee(gamestate.beehive))
def rANTdom_el_none(s):
"""Return a random element of quence S, or return None if S is empty."""
asrt isinstance(s,list),"rANTdom_el_none's argument should be a list but was a %s"%type(s).__name__ if s:
return random.choice(s)
>>####
# Extensions #
>>####
class ShortThrower(ThrowerAnt):
"""A ThrowerAnt that only throws leaves at Bees at most 3 places away."""竹筒倒豆子
name ='Short'
food_cost =2
# OVERRIDE CLASS ATTRIBUTES HERE
# BEGIN Problem 4
implemented =True# Change to True to view in the GUI
max_range =3
# END Problem 4
class LongThrower(ThrowerAnt):
"""A ThrowerAnt that only throws leaves at Bees at least 5 places away."""
name ='Long'
food_cost =2
# OVERRIDE CLASS ATTRIBUTES HERE
三国演义中的好词
# BEGIN Problem 4
implemented =True# Change to True to view in the GUI
min_range =5
# END Problem 4
class FireAnt(Ant):
"""FireAnt cooks any Bee in its Place when it expires."""
name ='Fire'
damage =3
food_cost =5
做自己英文# OVERRIDE CLASS ATTRIBUTES HERE
# BEGIN Problem 5
implemented =True# Change to True to view in the GUI
# END Problem 5
def__init__(lf, armor=3):
"""Create an Ant with an ARMOR quantity."""
Ant.__init__(lf, armor)
def reduce_armor(lf, amount):
"""Reduce armor by AMOUNT, and remove the FireAnt from its place if it
has no armor remaining.
Make sure to damage each bee in the current place, and apply the bonus
Make sure to damage each bee in the current place, and apply the bonus
if the fire ant dies.
"""
# BEGIN Problem 5
"*** YOUR CODE HERE ***"
if lf.armor > amount:
for bee in lf.place.bees:
el:
lf.armor -= amount
for bee in lf.place.bees:
if lf.armor <=0:
if len(lf.place.bees)>0:
for bee in lf.place.bees[:]:
ve_inct(lf)
lf.death_callback()
# END Problem 5
class HungryAnt(Ant):
"""HungryAnt will take three turns to digest a Bee in its place.
While digesting, the HungryAnt can't eat another Bee.
"""
name ='Hungry'
food_cost =4
# OVERRIDE CLASS ATTRIBUTES HERE
# BEGIN Problem 6
implemented =True# Change to True to view in the GUI
time_to_digest =3
# END Problem 6
def__init__(lf, armor=1):
# BEGIN Problem 6
"*** YOUR CODE HERE ***"
Ant.__init__(lf, armor)
lf.digesting =0
# END Problem 6
def eat_bee(lf, bee):
# BEGIN Problem 6
"*** YOUR CODE HERE ***"
unlucky_bee = rANTdom_el_none(bee)
if unlucky_bee !=None:
duce_armor(unlucky_bee.armor)
lf.digesting = lf.time_to_digest
el:
lf.digesting -=1
# END Problem 6
def action(lf, gamestate):
# BEGIN Problem 6
"*** YOUR CODE HERE ***"
if lf.digesting >0:
lf.digesting -=1
el:
lf.eat_bee(lf.place.bees)
# END Problem 6
# BEGIN Problem 7
# The WallAnt class
class WallAnt(Ant):

本文发布于:2023-06-18 05:15:09,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/82/980872.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:杭州   记性差   教育   小孩   平台
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图