Lab - Week9 AI
Lab - Week9 AI
Object Oriented
Programming
As we dive into the complexities of programming, we
are introduced to OOP. OOP is large and very complex
topic, but today we will work on understand the basic
points of OOP, such as classes, objects, encapsulation,
inheritance, and polymorphism.
In this exercise, you will define a Hero class, create attributes for your character, and
display their stats. This will help you understand the fundamentals of classes and
objects in Python. Make sure to check the lectures for guidance on syntax.
[2] Define three attributes: name (string), level (integer), and health (integer).
[3] Define the initialization function (__init__) with these variables and assign them
values from the function parameters.
[5] Outside the class (directly to the left of the line without any indentation), create a
function game(), instantiate a hero object, and call display_stats() to showcase the
hero’s attributes.
[6] Call the game() function directly below where it is defined. This should be right
up against the left line with no indentation if you done everything correct.
A hero must manage their resources wisely. In this exercise, you will learn about
encapsulation by creating a private attribute _gold, ensuring controlled access
through methods.
[1] Modify the Hero class to include a private attribute _gold initialized to 0.
[3] Implement a method check_gold() that returns the hero’s current gold balance.
As I said in the lecture, the use of _ is a convention, but we enforce when using
python to do encapsulation.
[5] In the game() function, your hero encounter a villager, who ask him to do some
work. After finishing the work, the hearo will earn_gold(), and print the total gold
balance using check_gold().
In this exercise, you will use inheritance to create a Warrior class that extends the
Hero class and adds a weapon attribute.
[3] Modify the __init__ method to accept the weapon parameter and initialize it.
When we initialise an inherited object, we have to run the super __init__ . Check
the lecture for example.
[5] In the game() function, instantiate a Warrior object and call display_stats() and
show_weapon().
In this exercise, you will explore polymorphism, ensuring different characters can
have unique attack methods.
[1] Modify Hero and Warrior classes to also include an attack() method with
different attack styles.
[2] Create a list of fighters that includes instances of Hero, and Warrior.
Hopefully, these exercises have helped you think about how you can
In
A hero
earlier
must
incorporate weeks,
manage
thisespecially
intotheir
yourresources
up until we’ve
wisely.
assessment covered
to In this
get youaround
exercise,
going.40%youofwill
thelearn
marks,
about
we will
encapsulation
guide them a touchby creating
more. Inalater
private
weeks
attribute
(likely_gold,
weekensuring
5 and onwards),
controlled
weaccess
will advise
them on methods.
through implementations rather than saying do this, then this.
The[2]
• above
Add gives
an them the
inventory
Implement 40-49%
system
a method band,heroes
where and is better
earn_gold(amount) wordedand
can increases
that collect for the
theuse later criteria
items.
hero’s gold by a(less
guidance).
given amount.
[3] Implement a method check_gold() that returns the hero’s current gold balance.
As I said in the lecturer, the use of _ is a convention, but we enforce when using
python to do encapsulation.