AI LEAGUE
EQUINOX ARENA
Strategy 2
Game Strategy
2
Command Allies
Game Strategy #2: Command Allies
Command your Mage and Ranger
Skills needed:
Concept Video: Variables
Variables
A variable allows us to save information for later use in our programs! The values saved in a variable can change over time.
coins = 3
Value
(stored inside the container)
Variable
(the container)
Variable Name
(name of container)
coins
In order to save a value in a variable, you must assign the value to a unique name using the assignment operator (=).
The integer 3 is assigned to the variable named coins.
Assigning to Method Calls
enemy = hero.findNearestEnemy()
Assigning to Method Calls
distance = hero.getDistanceTo(enemy)
Defining Variables: Python
The nearest enemy to your base
Let’s put variables into action!
Find the nearest enemy and assign it to a variable
Keep the enemy in a variable to attack later!
Defining Variables: JavaScript
The nearest enemy to your base
Let’s put variables into action!
Find the nearest enemy and assign it to a variable
Keep the enemy in a variable to attack later!
Game Strategy #2: Command Allies
Command your Mage and Ranger
Skills needed:
Conditionals
If
Then
condition
action
We use conditionals to help us plan and make decisions. We check a condition to see if it’s true or false. If the condition is true, then we perform an action.
It is raining.
Bring an umbrella!
Comparators & Conditionals
TRUE
FALSE
The way conditionals work is a little more complicated...
IF distance < 4
hero sneakRight
IF distance > 4
hero moveRight
hero jumpRight
Only happens if distance < 4
IF distance to enemy < 4
hero moveRight
TRUE
FALSE
hero sneakRight
IF distance to enemy > 4
hero jumpRight
Only happens if distance > 4
Will always happen after the hero sneaks right or moves right
Using Conditionals: Python
Let’s protect our base!
Use an ability if there’s an enemy nearby
Now let’s Shockwave if there’s an enemy!
Only happens if there’s an enemy around
Using Conditionals: JavaScript
Let’s protect our base!
Use an ability if there’s an enemy nearby
Only happens if there’s an enemy around
Now let’s Shockwave if there’s an enemy!
Game Strategy #2: Command Allies
Command your Mage and Ranger
Skills needed:
Functions
We are always looking for new ways to make our code faster, efficient, and error proof.
We achieve this with structured programming.
Structured programming uses reusable elements, like variables, to make a program clearer and easy to update.
With regular programming, although it requires less planning ahead, a program is prone to errors and hard to update.
Structured
Regular
Function Parts
A function contains a piece of code that you can use over and over again in your program. It provides a shortcut that helps your code stay organized and easier to read.
FUNCTION path
hero moveRight one
hero moveUp one
hero moveRight one
path
hero moveUp three times
path
hero moveDown two times
path
function call
function definition
function code
function name
Introducing Champions
Champions
champion.place(“a”)
Ranger
Mage
Defining Functions: Python
Now let’s see functions in action!
Let’s define the actions of our ranger!
2
1
Start with this keyword
Choose a unique and valid name
The steps your computer needs to execute.
4
The indentation and colon (:) indicate the start of the function block
3
Next we’ll learn how to use this function!
Defining Functions: JavaScript
Now let’s see functions in action!
Let’s define the actions of our ranger!
2
1
Start with this keyword
Choose a unique and valid name
The steps your computer needs to execute.
4
The curly braces { } indicate the start and end of the function block
3
Next we’ll learn how to use this function!
Game Strategy #2: Command Allies
Command your Mage and Ranger
Skills needed:
Events
We can write programs that wait for particular events to occur.
If the event occurs, then a block of code will be executed.
Events are based on the inputs we provide a program! Example events include: mouse clicks, key presses, starting a program, etc.
Event Syntax
CodeCombat has special methods that can listen for things in the game.
When the event occurs, a special function is executed.��For example:
pet.on(eventType, functionName)
“hears” “spawn”
fetchPotion
attackEnemy
Functions Called by Event Handlers
When an event occurs, it will call a function that you define. This function needs a special parameter, named event, in order to work.
def championAct(data):� champion = data.champion;� while True:� en = champion.findNearestEnemy()� if en:� champion.attack(en)��hero.on('spawn-ranger', championAct)
Python
Adding an event listener
A normal function that’ll be our event handler
Functions Called by Event Handlers
When an event occurs, it will call a function that you define. This function needs a special parameter, named event, in order to work.
function championAct(data) {� const champion = data.champion;� while (true) {� let en = champion.findNearestEnemy();� if (en) {� champion.attack(en);� }� }�}�hero.on('spawn-ranger', championAct);
JavaScript
Adding an event listener
A normal function that’ll be our event handler
Handling Events
Now let’s use our functions as an event handler for the spawn event of each of our ranger and mage!
2
1
Event name
Event Handler Function
Let’s repeat the steps for our mage!
3
For JavaScript:
Don’t forget the semi-colon (;)
Now, let’s use the function we created!
4
Extra Methods!
Let’s take a look at some more methods we can use at the Equinox!
hero.findNearestEnemy
Calling this function returns the nearest enemy to your base, we need to store that in a variable
Python
hero.findNearestEnemy
Calling this function returns the nearest enemy to your base, we need to store that in a variable
JavaScript
hero.distanceTo
Gets the distance from your base to the target
Python
hero.distanceTo
Gets the distance from your base to the target
JavaScript
hero.setTargeting
Sets the target of the tower
Python
hero.setTargeting
Sets the target of the tower
JavaScript
hero.getTowerAt
Calling this function returns the tower at the current place, we need to store that in a variable
Python
hero.getTowerAt
Calling this function returns the tower at the current place, we need to store that in a variable
JavaScript
champion.findNearestEnemy
Calling this function returns the nearest enemy to your champion, we need to store that in a variable
Python
champion.findNearestEnemy
Calling this function returns the nearest enemy to your champion, we need to store that in a variable
JavaScript
champion.attack
Commands you champion to attack the target
Python
champion.attack
Commands you champion to attack the target
JavaScript
Object Properties
Enemies, Champions, and Hero all have properties that you can use, we’ll use enemy.isBoss in the next slide
enemy.isBoss
false
true
Returns true if the enemy is a boss enemy.
Python
JavaScript
champion.moveTo
We’re telling the archer to move from their location to ‘G’ if their enemy is not a boss
Python
champion.moveTo
We’re telling the archer to move from their location to ‘G’ if their enemy is not a boss
JavaScript
champion.special
Both archer and mage have special abilities that affect your opponent’s towers
Python
champion.special
Both archer and mage have special abilities that affect your opponent’s towers
JavaScript
champion.isReady
To use the ability without affecting the flow of your code we check if the champion isReady to use it!
Python
champion.isReady
To use the ability without affecting the flow of your code we check if the champion isReady to use it!
JavaScript
hero.on
Once the archer is spawn they will run our function that tells them to keep attacking their nearest enemy
Python
hero.on
Once the archer is spawn they will run our function that tells them to keep attacking their nearest enemy
JavaScript
Game Strategy 2 In Action
Let’s put everything into action! ⚔️
Python
Game Strategy 2 In Action
Let’s put everything into action! ⚔️
JavaScript
Time to Compete
Submit Your Code
Let’s Check the Leaderboard
Time to analyze!
Let’s battle an opponent!
What did you discover?
What did we learn?
What went well?
What can be improved?
Iteration Time!
Now let’s go back and improve on our code before we move on!
Check your code and strategy
Explore more strategies
Ask for feedback from your friends!
Improve your code!�Some ideas: explore more methods like hero.findNearestOpponentEnemy() or hero.getEnemyHero()