Teacher's Guide
Chapter 5 / Level 5: Operator Overloading
Teaching Objective:
- Student understands how vectors operate and how to interface them with class objects
- Understands how Operator overloading works and it’s syntax
- Learns various use cases and nuances when overloading operators to optimize code
Guide
In this exercise we will be working with vectors in order to perform Operator Overloading in order to tally how many pounds of wine is being filtered in barrels.
Walk and grab all the metal water tins in the top of the room and make your way to the red carpet X marks. On the light and dark X marks face the barrels and use the water() and read() functions.
player.water()
await player.read()
Once you’ve identified how many pounds are in each set of barrels create objects of the class set up in the code editor to create vectors. A vector in this case is used to store too values in a single object, and Operator Overloading will be used to add together the vectors.
For each set of colored carpets there is a different type of wine being filtered. On the aforementioned red carpet, red_wine is filtered; on the yellow carpets, sparkling_wine is filtered; on the green carpets, white_wine is filtered; and on the orange carpets, orange_wine is being filtered.
The objects for each vector is denoted in light and dark in correlation with the X marks. Once the objects are created and populated, add them together to determine a total. On the gold X marks use the write() function in order to tally the numbers together by outlining the x and y values on the totals. For example:
red_wine_light = vector(345, 272)
red_wine_dark = vector(248, 432)
red_wine_total = red_wine_light + red_wine_dark
await player.write(
"There is a total of %s pounds on the left barrels and %s pounds on the right barrels"
% (red_wine_total.x, red_wine_total.y))
Do this for all four (4) sets of carpets using this same format in order to complete the level.
Map overview and path

Code Solution
import player
class vector:
def __init__(self, x = 0, y = 0):
self.x = x
self.y = y
def __add__(self, other):
x = self.x + other.x
y = self.y + other.y
return vector(x, y)
player.turn_left()
player.move_forward(2)
player.turn_right()
player.move_forward()
player.turn_left()
player.move_forward(6)
player.turn_left()
player.move_forward()
player.turn_right()
player.move_forward(2)
player.turn_right()
player.move_forward(5)
player.turn_right()
player.move_forward(2)
player.turn_right()
player.water()
await player.read()
player.turn_left()
player.move_forward()
player.turn_right()
player.water()
await player.read()
player.turn_left()
player.move_forward()
player.turn_right()
red_wine_right = vector(345, 272)
red_wine_left = vector(248, 432)
red_wine_total = red_wine_right + red_wine_left
await player.write(
"There is a total of %s pounds on the left barrels and %s pounds on the right barrels"
% (red_wine_total.x, red_wine_total.y))
player.turn_left()
player.move_forward(2)
player.turn_right()
player.water()
await player.read()
player.turn_left()
player.move_forward()
player.turn_right()
player.water()
await player.read()
player.turn_left()
player.move_forward()
player.turn_right()
sparkling_wine_light = vector(294, 349)
sparkling_wine_dark = vector(472, 295)
sparkling_wine_total = sparkling_wine_dark + sparkling_wine_light
await player.write(
"There is a total of %s pounds on the left barrels and %s pounds on the right barrels"
% (sparkling_wine_total.x, sparkling_wine_total.y))
player.turn_left()
player.move_forward(2)
player.turn_left()
player.move_forward(4)
player.turn_left()
player.move_forward(2)
player.turn_left()
player.water()
await player.read()
player.turn_right()
player.move_forward()
player.turn_left()
player.water()
await player.read()
player.turn_right()
player.move_forward()
player.turn_left()
white_wine_light = vector(427, 333)
white_wine_dark = vector(289, 378)
white_wine_total = white_wine_dark + white_wine_light
await player.write(
"There is a total of %s pounds on the left barrels and %s pounds on the right barrels"
% (white_wine_total.x, white_wine_total.y))
player.turn_right()
player.move_forward(2)
player.turn_left()
player.water()
await player.read()
player.turn_right()
player.move_forward()
player.turn_left()
player.water()
await player.read()
player.turn_right()
player.move_forward()
player.turn_left()
orange_wine_light = vector(342, 495)
orange_wine_dark = vector(317, 368)
orange_wine_total = orange_wine_dark + orange_wine_light
await player.write(
"There is a total of %s pounds on the left barrels and %s pounds on the right barrels"
% (orange_wine_total.x, orange_wine_total.y))