Break The Prison, Python


#1

I’m a little confused by the === stuff.

I have completed the level using this coding:

# Write this isFriend(name) spell to tell friend from foe.
# Return true for friends' names, false for ogres' names.
if name == "William":
    return True
if name == "Krogg":
    return False;  # <-- Start here; Krogg is not a friend!
if name == "Gort":
    return False;
if name == 'Krog':
    return False;
if name == 'Marcus':
    return True;
if name == 'Robert':
    return True;
if name == "Grul'Thock":
    return False;
if name == "Brack":
    return False;
if name == "Lucas":
    return True;
if name == 'Gordon':
    return True;

Would that mean “if X = Y, outcome 1” and “if X = Z, outcome 2” as in, return true or return false? If so, does that mean that the use of ‘return’ would mean ‘implement action of…’ ?
I cannot understand where True or False are defined or what action will be taken by the recognition of this statement, e.g. ‘if True…’ or ‘if False…’ Where can I see that?

Also, it has “Read Only” spells.

var door = this.getNearestEnemy();
if(door) {
    this.say("Who goes there?");
}
else {
    this.moveRight();
}

What does ‘var’ mean?
What does ‘this’ mean in the var door = this.getNearestEnemy coding?
Would I be right to assume that by using door = this.getNearestEnemy, are we redefining the door as “NearestEnemy”? Thus the door = this.getNearestEnemy means minion redefined door as NearestEnemy, thus will attack door?

Also, what is the second part of that coding?

if(door) {
    this.say("Who goes there?");
}
else {
    this.moveRight();
}

Again, no reference to True or False.

Can someone please help clear this up for me?

Thank you.


#2

The interesting part of how return True and return False work here is how those values are used in the hear() method of Tharin (not one that you write):

if(!this.heard) this.heard = {};
if(this.heard[message]) return;
this.heard[message] = true;

if(this.isFriend(message)) {
    // A friend! Break the door down!
    this.bustDownDoor();
}
else {
    // An enemy! Jump past the door!
    this.setTargetPos({x: this.pos.x + 20, y: this.pos.y});
    this.jump();
}

This code first checks to see if it has heard the message before. If it has, it returns (we only want to process hearing each guy’s name once). If not, it stores that it has heard that name and then checks whether isFriend(message) is true.

isFriend() is the method you’re writing in the level. So when you return True or False, it either runs this.bustDownDoor() or it jumps past the door.

You are probably confused by the syntax difference between the methods you write and the read-only ones. The read-only ones are written in JavaScript. Ideally they’d be in Python, but we’re probably not doing too many levels like this, so we haven’t prioritized making those readable in Python.

var is a JavaScript keyword for declaring a variable. Python doesn’t need a keyword; you can just declare variables by assigning them. JavaScript: var door = this.getNearestEnemy();. Python: door = self.getNearestEnemy(). Python uses self instead of this and doesn’t need semicolons. What that statement does is creates a variable called door and stores the return value of getNearestEnemy() in it. (The nearest enemy happens to be a Dungeon Door.)

The return <something> statement will exit the method. Each time a method ends, it returns a value. (If it doesn’t explicitly return a value, it returns the special value None.) So when your code gets to a return True, it stops running the rest of the code in your method, and gives the True value to the hear() method.

This level is pretty complicated to understand fully! That’s part of the reason we took it out of the old beginner campaign; even there it was too hard.


#3

That does look cool, but I have a load of questions now. I understand it’s JavaScript, but I’m still going to quiz you…sorry.

A few questions on this:
You said “If it has, it returns (we only want to process hearing each guy’s name once)” What do you mean ‘returns’? Is that as in, starts again, returns to zero, or returns as in “I just clicked the return button.” or what?

Why are there [ ] around some ‘message’ and ( ) around other’s? What’s the difference?

What do the { and } symbols do as opposed to ( ) or [ ] ?

What do the // do? Is that the Java version of Python #'s or “”" ?

Does ‘else’ mean If not A then do B?

A question on this part here:

Is that all linked as a single function for the character? So in non coding language it would be seen as:
this.heard[message] = true; If Message Heard the outcome is true…meaning person is a friend?

if(this.isFriend(message)) {
// A friend! Break the door down! If person is a Friend (message is true) bust down the door?
this.bustDownDoor();
}

Then the ‘else’ {function 2 code in here} if the answer is FALSE, implement second command, jump away.
Is my understanding of that code right?


#4

return is a programming keyword that ends the current function’s execution. So if I do something like this:

def count():
    x = 0
    x += 1
    x += 1
    return x
    x += 1
    x += 1
    return x

Then the program will stop at the first return x and return the value 2. It won’t execute the rest of the code.

this.heard[message] = true; is using [] for object indexing; it’s looking up the value for the key message in the heard object. this.bustDownDoor() is using () to actually call the bustDownDoor method.

{} curly braces indicate blocks of code. They’re a JavaScript thing; Python uses indentation to indicate blocks. They can also indicate object literals. this.heard = {}; creates an empty object ({}) and stores it into this.heard.

// starts a single-line comment in JavaScript, like # starts a single-line comment in Python.

Yes, else is an optional second clause of an if-statement. Its code block runs if the if-condition is not met.

if(this.heard[message]) return; means, if we have heard the message before, return (stop executing). It doesn’t matter here if it was a friend, just whether we have heard it.

The else-clause on the if(this.isFriend(message)) conditional does mean, “if this isn’t a friend, jump away”.