Intro to Computer Science

Overview

This course is designed to be a gentle introduction to basic programming concepts, such as algorithms, syntax, loops, and variables, through the engaging and familiar experience of a video game. In each level, players use computational thinking and computer programming to navigate the hero to the goal without running into spikes or being spotted by ogres.

Lessons

LESSON 1: BASIC SYNTAX

Level 1:Dungeons of Kithgard

The journey begins! To escape the dungeon of Kithgard, your hero has to move. You tell them where to move by writing code.

Type a code into the editor to give your hero instructions. Heroes read and execute these instructions themselves when told to. To direct your hero, refer to them in code with hero.

Now that you know how to call on your hero, instruct them to move with direction commands, moveDown and moveRight. Write it like this:

hero.moveDown();

hero.moveRight();

To pass this level, move right, down, and right to grab the gem. Remember, you only need three lines of code.

The code you write here is very similar to the code you might write to tell a computer how to do all kinds of things, from playing music to displaying a web page. You're taking your first steps towards being a coder!

Dungeons of Kithgard Solution

// Move towards the gem.

// Don’t touch the spikes!

// Type your code below and click Run when you’re done.

hero.moveRight();

hero.moveDown();

hero.moveRight();

Level 2: Gems in the Deep

Gems in the DeepSolution

// Grab all the gems using your movement commands.

hero.moveRight();

hero.moveDown();

hero.moveUp();

hero.moveUp();

hero.moveRight();

Can you remember the lessons from the last level? This will be the same, but you will need to move a lot more. Remember, hero refers to you, the hero.

When you move, you only move as far as the next movement square (look for the small tiles on the ground), so you might have to moveUp twice in a row to get to the top of this level from the bottom.

Or you can pass a number as an argument to the movement command, to instruct your hero to move more than one space at a time.

For example, you can move up twice by typing:

hero.moveUp(2);

Level 3a: Kounter Kithwise

Sometimes it's all about timing. Grab the gem, but move the same way the ogre patrol does, so that they don't see you.

You can drag the playback timeline to see what happened the last time you ran your code.

Kounter Kithwise Solution

// Avoid the ogres and grab the gem.

hero.moveDown(2);

hero.moveRight();

hero.moveUp();

hero.moveRight();


Level 3b: Crawlways of Kithgard

You must use your wits to solve the puzzle. How can you move such that you aren't crossing the hallway at the same time as the ogre munchkins?

Remember, you can always backtrack the way you came.

Crawlways of Kithgard Solution

// Avoid being seen by the ogres.

hero.moveRight();

hero.moveLeft();

hero.moveRight(2);


Concept Challenge: Careful Steps

Sorry, no hints here :-(

This is where you use what you've learned in the previous levels.

If you have problems with this task, go back and review the past few levels to make sure you understand them!

Careful Steps Solution

// This is a CONCEPT challenge about basic syntax.

// Collect all gems and return to the exit (the red X).

// Avoid the spikes.

hero.moveUp();

hero.moveRight();

hero.moveDown();

hero.moveRight();

hero.moveLeft();

hero.moveDown();

hero.moveUp();

hero.moveUp();

hero.moveLeft();

hero.moveDown();

hero.moveLeft();

hero.moveLeft();

Level 4: Enemy Mine

The floor is littered with Fire Traps, but there's a safe path through to the gem.

When you call a method like moveRight() you can sometimes give extra information to the method to modify what it does. This extra information is referred to as arguments or parameters.

You can pass an argument to the moveRight() method like this: moveRight(3). This tells moveRight() to make your hero move 3 spaces to the right instead of 1.

Enemy MineSolution

// Use arguments with move statements to move farther.

hero.moveRight(3);

hero.moveUp();

hero.moveRight();

hero.moveDown(3);

hero.moveRight(2);

Level 4a: Illusory Interruption

Overview

You can't sneak past the guards unless they're distracted. Luckily, someone left a decoy nearby.

Stepping on the X will activate the decoy.

Tip: You can move multiple spaces by passing arguments to the move command, like moveRight(3).

Illusory Interruption Solution

// Use the Decoy to distract the guards by stepping on the X.

hero.moveRight();

hero.moveDown(2);

hero.moveUp(2);

hero.moveRight(3);

Level 4b: Forgetful Gemsmith

Overview

This one takes several commands, so make sure to use the autocomplete to write your code faster. You can type r, Enter to autocomplete a moveRight() command.

After you're done practicing simple movement with this level, it's time to learn attacking, so keep trying!

Forgetful Gemsmith Solution

// Grab the gems and go to the exit.

hero.moveRight();

hero.moveDown();

hero.moveRight(2);

hero.moveUp();

hero.moveRight();

Concept Challenge: Long Steps

This is a CONCEPT Challenge about arguments.

Collect all the gems and escape from the dungeon (the red arrow marks the exit).

Use no more than 7 commands.

You will need to use arguments with your movement commands!

Long Steps Solution

// This is a concept challenge level about arguments.

// Grab the gems and go to the exit.

// Your code should have no more than 7 commands.

// Use movement commands with arguments.

hero.moveRight(1);

hero.moveUp(3);

hero.moveRight(2);

hero.moveDown(3);

hero.moveLeft(1);

hero.moveUp(2);

hero.moveRight(3);

Level 5: True Names

Keep in mind a few things to beat this level:

  1. You need to attack each ogre munchkin twice to defeat it.

  2. Spell the names properly, with capitalization! "Brak" and "Treg".

  3. Put the names in quotes to make them into strings. Strings are a type of programming data. They represent text.

  4. After you defeat "Brak" you should moveRight() to get the gem.

  5. Then, defeat "Treg" by attacking them twice.

  6. It's no problem if you die; you can always keep trying.

True Names Solution

// Defend against "Brak" and "Treg"!

// You must attack small ogres twice.

hero.moveRight();

hero.attack("Brak");

hero.attack("Brak");

hero.moveRight();

hero.attack("Treg");

hero.attack("Treg");

Level 5a: Favorable Odds

Remember to hit ogre munchkins twice, and to put their names capitalized in quotes: "Krug" and "Grump".

If you buy a powerful sword, you can take down munchkins in one hit.

Favorable Odds Solution

// Attack both ogres and grab the gem.

hero.moveRight();

hero.attack("Krug");

hero.attack("Krug");

hero.moveRight();

hero.moveUp();

hero.attack("Grump");

hero.attack("Grump");

hero.moveLeft();

hero.moveLeft();

Level 5b: The Raised Sword

Remember, each munchkin takes two hits to defeat. Try to defeat them in the order they come at you so that they don't land extra hits on you.

Make sure to have enough armor to win the fight!

The Raised Sword Solution

// Defeat the ogres.

// Remember that they each take two hits.

hero.attack("Rig");

hero.attack("Rig");

hero.attack("Gurt");

hero.attack("Gurt");

hero.attack("Ack");

hero.attack("Ack");

Concept Challenge: Dangerous Steps

Dangerous Steps Solution

// Defeat the ogres using their names.

hero.moveRight();

// Defeat the first pair of ogres.

hero.attack("Sog");

hero.attack("Sog");

hero.attack("Gos");

hero.attack("Gos");

hero.moveRight(2);

// Defeat the second pair of ogres.

hero.attack("Kro");

hero.attack("Kro");

hero.attack("Ergo");

hero.attack("Ergo");

Combo Challenge: Sleep Hour

This solution uses all concepts: basic syntax, movement arguments, and strings.

// This is a COMBO challenge level.

// Defeat ogres, collect gems, and escape to the red X

// using strings and movement arguments!

hero.moveRight(2);

hero.attack("Ursa");

hero.attack("Ursa");

hero.moveDown();

hero.moveLeft(2);

hero.moveUp(2);

hero.attack("Rexxar");

hero.attack("Rexxar");

hero.moveRight();

hero.attack("Brack");

hero.attack("Brack");

hero.moveRight(2);

Level 6: Cell Commentary

Comments are a way for two programmers to communicate with each other. They are even useful for a single programmer to remember what they were doing to resume later!

In CodeCombat, we add comments to help you structure your code and give vital hints. Read them to understand what the objective is and what code you need to write to accomplish it.

In this level you will need to read the comments we provide to find the answer, to escape the prison cell!

Cell Commentary Solution

hero.say("What's the password?");

// Use the "say()" function to say the password.

// The password is: "Achoo"

hero.say("Achoo");

hero.moveUp();

hero.moveUp();

Level 7: Kithgard Librarian

Kithgard Librarian Solution

// You need to say a password to open the Library Door!

// The password is in the Hints!

// Click the blue Hints button above the code window.

// If you ever get stuck, click Hints!

hero.moveRight();

hero.say("Hush");

hero.moveRight();

Welcome to Hints!

Most levels have hints that contain extra information to help you complete your goals and learn new concepts.

Like this:

The password for the Library Door is "Hush".

Tip: Use the say command to say the password!

Tip: Remember to use quotes around the password to make it a string, and use correct capitalization.

Be sure to check Hints on future levels if you're having trouble!

Level 8: The Prisoner

You can attack the "Weak Door" by name to free your ally, then help him fight the ogre "Two".

When you attack something by name, be sure to include the quotes around the name (this is how you specify a String) and be sure to capitalize the names correctly.

Depending on your weapon and your strategy, you may have to attack the ogre more than once.

The Prisoner Solution

// Free the prisoner, defeat the guard and grab the gem.

// Free Patrick from behind the "Weak Door".

hero.moveRight();

hero.attack("Weak Door");

// Defeat the guard, named "Two".

hero.moveRight(2);

hero.moveDown(2);

hero.attack("Two");

hero.attack("Two");

hero.attack("Two");

hero.attack("Two");

hero.attack("Two");

hero.attack("Two");

// Get the gem.

hero.moveRight();

hero.moveDown();

LESSON 2: Loops

Level 9: Fire Dancing

Code normally executes in the order it's written. Loops allow you to repeat a block of code multiple times without having to re-type it.

How To Use while-true Loops

First, we start a loop with the while keyword. This tells your program WHILE something is true, repeat the body of the loop.

For now we want our loops to continue forever, so we'll use a while-true loop. Because true is always true!

Don't worry about this true stuff too much for now. We'll explain it more later. Just remember that a while-true loop is a loop that never ends.

This is how you code a while-true loop:

// Start the while-true loop with "while(true) {"

// Anything between the { and } is considered INSIDE the loop.

while(true) {

hero.moveRight();

hero.moveLeft();

}

hero.say("This line is not inside the loop!");

// Tip: the indentation (spaces at the start of the lines) is optional, but makes

your code easier to read!

Tip: You can put whatever you want inside a while-true loop! But for this level, we only need to repeat two commands: moveRight() and moveLeft()!

Fire Dancing Solution

// Code normally executes in the order it's written.

// Loops repeat a block of code multiple times.

while(true) {

hero.moveRight();

// Add a moveLeft command to the loop here

hero.moveLeft();

}

Level 10: Haunted Kithmaze

Loops let you repeat the same code over and over. You can do this level in just four commands with a while-true loop.

Tip: the hallway needs two movements to the right, and then two movements up. From there, you can just let the while-true loop repeat to do the rest.

Make sure that your movement commands are inside the loop so that they repeat!

Haunted Kithmaze Solution

// Loops are a better way of doing repetitive things.

while(true) {

// Add commands in here to repeat.

hero.moveRight();

hero.moveRight();

hero.moveUp();

hero.moveUp();

}


Level 11. Descending Further

For the bonus, use a loop to do this level in only four code statements. (Empty lines and comments don't count, and multiple statements on the same line do count.)

Hover over the loop documentation in the bottom right to see examples of the loop syntax.

Descending Further Solution

// Navigate the maze in less than 5 statements.

while(true) {

hero.moveRight(2);

hero.moveDown();

}

Level 11a: Riddling Kithmaze

Count carefully how many movements you need inside your loop to solve the maze, and where you need to repeat your loop. Remember, you should only use one loop per level, and make sure all your code is inside the loop.

Hover over the loop documentation to see an example of the loop syntax.

Riddling Kithmaze Solution

// Loops are a better way of doing repetitive things.

while(true) {

// Add commands in here to repeat.

hero.moveRight();

hero.moveDown();

hero.moveRight(2);

hero.moveUp();

}

Level 11b: Radiant Aura

Skeletons are too tough to fight. The Lightstones can keep them away from you, but only for a little while!

All you need to do is:

  • Grab a Lightstone

  • Move past a skeleton

  • Repeat

Radiant Aura Solution

// Pick up a lightstone to make skeletons flee from you for a short time.

while(true) {

hero.moveUp();

hero.moveDown();

hero.moveRight(2);

}

Concept Challenge: Loop Warehouse

Find a looping pattern to move through the warehouse, collect all gems, and exit at the white X mark.

Use no more than 5 statements.

Loop Warehouse Solution

// Collect all gems and escape at the white X mark.

// Use no more than 5 statements.

while(true) {

hero.moveUp(2);

hero.moveRight(2);

hero.moveDown();

hero.moveLeft();

}

Level 12: Dread Door

You can combine while-true loops and attack to easily destroy things that take more than one hit. Like this door.

while(true) {

hero.attack("Door");

}

You can attack the door by its name, which is "Door".

With looping and attacking, you can do this level in just two lines of code.

Dread Door Solution

// Attack the door!

// It will take many hits, so use a "while-true" loop.

while (true) {

hero.attack("Door");

}

Level 13: Hack and Dash

You're not fast enough to outrun the Sprite without drinking the Speed Potion.

Before the while-loop, you'll want to moveUp() and attack the "Chest", then moveDown() back into the main hallway.

Inside the while-loop, you need to add moveDown(). Use an argument to moveDown() multiple times wthout having write more lines of code.

Don't worry, the Sprite is activated by stepping on the X, so you'll have time to grab the potion!

Hack and Dash Solution

// You can write code before a loop.

hero.moveRight();

// Break open the "Chest" before using the loop to escape the maze!

hero.moveUp();

hero.attack("Chest");

// Return back back into the main hallway.

hero.moveDown();

while(true) {

// Move 3 times.

hero.moveRight(3);

// Move 3 times more.

hero.moveDown(3);

}

Level 14: Cupboards of Kithgard

You can perform any action before a while-true loop.

The ogre guards might be too much for you to handle. Maybe you'll find something useful in the "Cupboard"?

First, move close to the "Cupboard" (stand on the red X). It looks locked, so you'll have to attack it repeatedly using a while-true loop to break it open.

Cupboards of Kithgard Solution

// There may be something around to help you!

// First, move to the Cupboard.

hero.moveUp();

hero.moveRight(2);

hero.moveDown(2);

// Then, attack the "Cupboard" inside a while-true loop.

while (true) {

hero.attack("Cupboard");

}

LESSON 3: VARIABLES

Level 15: Known Enemy

Up until now, you have been doing three things:

  1. Calling methods (commands like moveRight())

  2. Passing strings (quoted pieces of text like "Treg") as arguments to the methods

  3. Using while-true loops to repeat your methods over and over.

Now you are learning how to use variables: symbols that represent data. The variable's value can vary as you store new data in it, which is why it's called a variable.

It's a pain to type the names of ogres multiple times, so in this level you use three variables to store the ogre names. Then when you go to attack, you can use the variable (enemy1) to represent the string that is stored in it ("Kratt").

Declare variables like so:

var enemy1 = "Kratt";

When you use quotes: "Kratt", you are making a string.

When you don't use quotes: enemy1, you are referencing the enemy1 variable.

Known Enemy Solution

// You can use a variable like a nametag.

var enemy1 = "Kratt";

var enemy2 = "Gert";

var enemy3 = "Ursa";

hero.attack(enemy1);

hero.attack(enemy1);

hero.attack(enemy2);

hero.attack(enemy2);

hero.attack(enemy3);

hero.attack(enemy3);

Level 16: Master of Names

Remember from the last level, variables are symbols that represent data. The variable's value can vary as you store new data in it, which is why it's called a variable.

Now instead of using the names of the enemies, you can use your glasses and the findNearestEnemy() method to store references to the ogres in variables. You don't need to use their names.

When you call the findNearestEnemy() method, you must store the result in a variable, like enemy3 (you can name it whatever you want). The variable will remember what the nearest enemy was when you called the findNearestEnemy() method, so make sure to call it when you see a nearby enemy.

Remember: when you use quotes, like "Kratt", you are making a string. When you don't use quotes, like enemy1, you are referencing the enemy1 variable.

Ogre munchkins still take two hits to defeat.

Master of Names Solution

// Your hero doesn't know the names of these enemies!

// Use the findNearestEnemy method on your new glasses.

// Assign the result of hero.findNearestEnemy() to the variable enemy1:

var enemy1 = hero.findNearestEnemy();

// enemy1 now refers to the nearest enemy. Use the variable to attack!

hero.attack(enemy1);

hero.attack(enemy1);

// Now that enemy1 is defeated, calling hero.findNearestEnemy() again will result in

finding the new nearest enemy.

var enemy2 = hero.findNearestEnemy();

hero.attack(enemy2);

hero.attack(enemy2);

// Assign the result of hero.findNearestEnemy() to the variable enemy3:

var enemy3 = hero.findNearestEnemy();

// Now attack using the enemy3 variable:

hero.attack(enemy3);

hero.attack(enemy3);

Level 16a: Lowly Kithmen

Because you don't know the names of these ogres, you can use the findNearestEnemy method from your glasses to store references to the ogres in variables.

When you call the findNearestEnemy method, you must store the result in a variable, like enemy2 (you can name it whatever you want). The variable will remember what the nearest enemy was when you called the findNearestEnemy method, so make sure to call it when you see a nearby enemy.

Remember: when you use quotes, like "Kratt", you are making a string. When you don't use quotes, like enemy1, you are referencing the enemy1 variable.

Lowly Kithmen Solution

// Create a second variable and attack it!


var enemy1 = hero.findNearestEnemy();

hero.attack(enemy1);

hero.attack(enemy1);

enemy1 = hero.findNearestEnemy();

hero.attack(enemy1);

hero.attack(enemy1);

hero.moveDown();

hero.moveRight();

hero.moveRight();

Level 16b: Closing the Distance

Like in the last level, you can use the findNearestEnemy method from your glasses to store references to the ogres in variables.

When you call the findNearestEnemy method, you must store the result in a variable, like enemy2 (you can name it whatever you want). The variable will remember what the nearest enemy was when you called the findNearestEnemy method, so make sure to call it when you see a nearby enemy.

Remember: when you use findNearestEnemy, you need to be in line of sight of an enemy, or you won't be able to see her.

This level introduces a new type of ogre, the ogre thrower, who does much more damage than the ogre munchkins you have been fighting. There are many more kinds of ogres awaiting you in future worlds.

Closing the Distance Solution

hero.moveRight();

// You should recognize this from the last level.

var enemy1 = hero.findNearestEnemy();

// Now attack enemy1.

hero.attack(enemy1);

hero.attack(enemy1);

hero.moveRight();

enemy1 = hero.findNearestEnemy();

hero.attack(enemy1);

hero.moveRight(2);

Concept Challenge: Master Of Names Debug

Variables are symbols that represent data.

The variable's value can vary as you store new data in it, which is why it's called a variable.

Now instead of using the names of the enemies, you can use your glasses and the findNearestEnemy() method to store references to the ogres in variables. You don't need to use their names.

When you call the findNearestEnemy() method, you must store the result in a variable, like enemy3 (you can name it whatever you want). The variable will remember what the nearest enemy was when you called the findNearestEnemy() method, so make sure to call it when you see a nearby enemy.

Remember: when you use quotes, like "Kratt", you are making a string. When you don't use quotes, like enemy1, you are referencing the enemy1 variable.

Master Of Names Debug Solution

// There is a mistake in the code below.

// Try to figure out what it is, then fix it!

var enemy1 = hero.findNearestEnemy();

hero.attack(enemy1);

hero.attack(enemy1);

var enemy2 = hero.findNearestEnemy();

hero.attack(enemy2);

hero.attack(enemy2);

var enemy3 = hero.findNearestEnemy();

hero.attack(enemy3);

hero.attack(enemy3);

Level 17: The Final Kithmaze

This level combines while-true loops and variables to both solve a maze and attack enemies.

Now you see why you need variables, because you're actually going to vary the data in the variable. Inside your while-true loop, if you define an enemy variable, it will refer to each of the three ogre munchkins in the level as the loop repeats. Cool, huh?

Pay attention to where your while-true loop should repeat so that you don't move further than you need to.

Make sure that you call findNearestEnemy() when you can actually see the ogre munchkin with clear line of sight.

The Final Kithmaze Solution

// Use a while-true loop to both move and attack.

while (true) {

hero.moveRight();

hero.moveUp();

var enemy = hero.findNearestEnemy();

hero.attack(enemy);

hero.attack(enemy);

hero.moveRight();

hero.moveDown(2);

hero.moveUp();

}

Combo Challenge: Safe Place

This is a COMBO challenge!

You need to defeat the ogres using at least one of the programming skills you've learned so far:

  • Variables

  • While Loops

  • Arguments

If you can, complete ALL the goals!

Safe Place Solution

This solution uses all concepts: arguments, while loops, and variables.

// Move to the treasure room and defeat all the ogres.

hero.moveUp(4);

hero.moveRight(4);

hero.moveDown(3);

hero.moveLeft(2);

while(true) {

var enemy = hero.findNearestEnemy();

hero.attack(enemy);

hero.attack(enemy);

}

Level 18: Kithgard Gates

When you use a builder's hammer, instead of the attack method, you get the buildXY method. buildXY takes three arguments, instead of one: buildType, x, and y. So you can decide what to build and where to build it.

  • buildType: either the string "fence", to build fences, or the string "fire-trap", to build fire traps.

  • x: the horizontal position at which to build. You can hover over the map to find coordinates.

  • y: the vertical position at which to build. x and y are both in meters.

buildXY("fence", x, y) allows you to build a fence at a certain spot, like this:

hero.buildXY("fence", 40, 20);

This level is much easier to beat with "fence" than with "fire-trap". It's almost impossible to use fire traps to defeat the ogres. If you want to try it, fine, but it took us fifteen minutes to figure it out, and we built the level.

You only need to build three fences to stop the ogres and escape the dungeon to the right.

Kithgard Gates Solution

// Build 3 fences to keep the ogres at bay!

hero.moveDown();

hero.buildXY("fence", 36, 34);

hero.buildXY("fence", 36, 30);

hero.buildXY("fence", 36, 26);

hero.moveRight(3);

Level 19: Wakka Maul

Battle your friends, coworkers and classmates in this all out brawl through the Kithgard dungeons!

Break out allies, summon more units, and evade the enemy's advances!

The doors are labeled "a","b","c","d","e","f","g","h","i","j". Use these strings to attack the specific door you want!

The human side can summon soldier and archer while the ogre side can summon scout and thrower. All either side needs to do is to say the unit name, and have enough gems, to summon the units. To summon units you'll want to say their name:


// If on the human side:

hero.say("soldier"); // To summon a soldier for 20 gold.

hero.say("archer"); // To summon an archer for 25 gold.

// If on the ogre side:

hero.say("scout"); // To summon a scout for 18 gold.

hero.say("thrower"); // To summon 2 throwers for 9 gold each.

Gems

There are several types of gem collections lying around. Each is worth a different amount of gold.

Single Gem = 5 Gold

Small Pile = 15 Gold

Medium Pile = 40 Gold

Chest = 100 Gold

Haste Potion

You may also collect the Haste Potion in the center of the level, which will double your movement speed.

Wakka Maul Solution

// Welcome to Wakka Maul! Prepare for combat!

// Venture through the maze and pick up gems to fund your war-chest.

// Break down doors to unleash allies (or enemies).

// For example, to attack the door labeled "g" use:

//hero.attack("g")

// If you have enough gold, you can call out for help by saying the type of unit you

would like to summon!

//hero.say("soldier") to summon a Soldier at the cost of 20 gold!

//hero.say("archer") to summon an Archer at the cost of 25 gold!

hero.moveDown();

hero.moveRight();

hero.attack("g");

hero.moveRight(4);

hero.moveUp();

hero.attack("h");

hero.attack("i");

hero.moveUp(2);

while (true) {

hero.say("archer");

}