1 of 58

AI LEAGUE

EQUINOX ARENA

Strategy 2

2 of 58

Game Strategy

2

Command Allies

3 of 58

Game Strategy #2: Command Allies

Command your Mage and Ranger

Skills needed:

  • Variables
  • If Statements
  • Functions
  • Event Handling

4 of 58

Concept Video: Variables

5 of 58

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.

6 of 58

Assigning to Method Calls

enemy = hero.findNearestEnemy()

7 of 58

Assigning to Method Calls

distance = hero.getDistanceTo(enemy)

8 of 58

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!

9 of 58

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!

10 of 58

Game Strategy #2: Command Allies

Command your Mage and Ranger

Skills needed:

  • Variables
  • If Statements
  • Functions
  • Event Handling

11 of 58

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!

12 of 58

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

13 of 58

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

14 of 58

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!

15 of 58

Game Strategy #2: Command Allies

Command your Mage and Ranger

Skills needed:

  • Variables
  • If Statements
  • Functions
  • Event Handling

16 of 58

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

17 of 58

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

18 of 58

Introducing Champions

Champions

  • Allies that can be placed next to towers
  • Controlled using functions and special commands that begin with champion

champion.place(“a”)

Ranger

  • Quick attacks & Disables enemy towers
  • Summon with: �hero.on('spawn-ranger', championAct)

Mage

  • Area damage & Freezes enemy towers
  • Summon with:�hero.on('spawn-mage', championAct)

19 of 58

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!

20 of 58

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!

21 of 58

Game Strategy #2: Command Allies

Command your Mage and Ranger

Skills needed:

  • Variables
  • If Statements
  • Functions
  • Event Handling

22 of 58

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.

23 of 58

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

24 of 58

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

25 of 58

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

26 of 58

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

27 of 58

Extra Methods!

Let’s take a look at some more methods we can use at the Equinox!

28 of 58

hero.findNearestEnemy

Calling this function returns the nearest enemy to your base, we need to store that in a variable

Python

29 of 58

hero.findNearestEnemy

Calling this function returns the nearest enemy to your base, we need to store that in a variable

JavaScript

30 of 58

hero.distanceTo

Gets the distance from your base to the target

Python

31 of 58

hero.distanceTo

Gets the distance from your base to the target

JavaScript

32 of 58

hero.setTargeting

Sets the target of the tower

Python

33 of 58

hero.setTargeting

Sets the target of the tower

JavaScript

34 of 58

hero.getTowerAt

Calling this function returns the tower at the current place, we need to store that in a variable

Python

35 of 58

hero.getTowerAt

Calling this function returns the tower at the current place, we need to store that in a variable

JavaScript

36 of 58

champion.findNearestEnemy

Calling this function returns the nearest enemy to your champion, we need to store that in a variable

Python

37 of 58

champion.findNearestEnemy

Calling this function returns the nearest enemy to your champion, we need to store that in a variable

JavaScript

38 of 58

champion.attack

Commands you champion to attack the target

Python

39 of 58

champion.attack

Commands you champion to attack the target

JavaScript

40 of 58

Object Properties

Enemies, Champions, and Hero all have properties that you can use, we’ll use enemy.isBoss in the next slide

41 of 58

enemy.isBoss

false

true

Returns true if the enemy is a boss enemy.

Python

JavaScript

42 of 58

champion.moveTo

We’re telling the archer to move from their location to ‘G’ if their enemy is not a boss

Python

43 of 58

champion.moveTo

We’re telling the archer to move from their location to ‘G’ if their enemy is not a boss

JavaScript

44 of 58

champion.special

Both archer and mage have special abilities that affect your opponent’s towers

Python

45 of 58

champion.special

Both archer and mage have special abilities that affect your opponent’s towers

JavaScript

46 of 58

champion.isReady

To use the ability without affecting the flow of your code we check if the champion isReady to use it!

Python

47 of 58

champion.isReady

To use the ability without affecting the flow of your code we check if the champion isReady to use it!

JavaScript

48 of 58

hero.on

Once the archer is spawn they will run our function that tells them to keep attacking their nearest enemy

Python

49 of 58

hero.on

Once the archer is spawn they will run our function that tells them to keep attacking their nearest enemy

JavaScript

50 of 58

Game Strategy 2 In Action

Let’s put everything into action! ⚔️

Python

51 of 58

Game Strategy 2 In Action

Let’s put everything into action! ⚔️

JavaScript

52 of 58

Time to Compete

53 of 58

Submit Your Code

  1. Click on run until you’re satisfied!

  • When ready click on�rank my game

54 of 58

Let’s Check the Leaderboard

  1. Go back to the Ladder

  • Check the leaderboard on the left of the screen and the matches you played on the right

55 of 58

Time to analyze!

Let’s battle an opponent!

  • Choose an opponent and click on battle to fight the other player

  • Click on Spectate to watch

56 of 58

What did you discover?

What did we learn?

  • What coding concepts did you apply today in the Arena?

What went well?

  • What was your favorite part about the Arena?

What can be improved?

  • How did you improve on the strategy?
  • What did you notice your opponent is doing that’s giving them an advantage?
  • Are you using placing champions in the optimal places?
  • Are you using specials when the boss enemies appear?
  • Are you using specials when the enemies get too close?

57 of 58

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()

58 of 58