Lookup Table in Programming: A Plain Guide With Game Code

A lookup table in programming is a list of answers you store ahead of time, so your code can read a result by its position instead of working it out. You hand it a number, and it hands back whatever sits at that spot. This guide shows the idea with real lines from a browser tank game, so you can see one in use.

What is a lookup table in programming?

Think of a price list on a cafe wall. Nobody works out the price of a coffee each time someone orders one. They read it off the list. A lookup table does the same job inside a program.

The Wikipedia page on lookup tables describes one as an array that replaces a calculation with a simpler step: reading one slot of the array by its number. That step is called direct addressing. The number you know is the key. The answer stored at that slot is the value.

The idea is older than computers. People once kept printed tables of values to speed up hand calculations, such as trigonometry and logarithms. A program simply keeps its table in memory.

How the index finds the answer

Most lookup tables are plain arrays. An array is a row of slots, each with a number. In C#, the language this game is written in, arrays are zero indexed. An array with six items has slots 0 to 5.

So the first answer lives at slot 0, the second at slot 1, and so on. To read one, you write the array name and the slot number in square brackets, like Names[3]. There is no search and no loop. The number points straight at the answer.

That is the whole trick. Your job as the programmer is to turn whatever you know into the right slot number.

A worked example: the stage names in Tank City Reboot

Tank City Reboot is a browser game where you defend a CPU core from malware tanks. Each stage is named after a part of a computer. In the game's code, the file Stages.cs holds two arrays side by side.

  • Names holds six words: FIREWALL, CACHE, COOLANT, ENCRYPTION, DATA BUS and KERNEL.
  • Facts holds six short sentences, one for each name, in the same order.

When a stage starts, the game shows a card with the stage name and the fact the stage opens with. It does not hunt for them. It works out one number, called map, and reads both arrays at that slot: Names[map] and Facts[map].

Here is how to follow it for stage 4:

  1. The game computes map as the stage number minus 1, then takes the remainder after dividing by 6. For stage 4 that is (4 - 1) % 6, which is 3.
  2. It reads Names[3]. Count from 0: FIREWALL, CACHE, COOLANT, ENCRYPTION. The answer is ENCRYPTION.
  3. It reads Facts[3] and gets "Encrypted data looks like random noise to anyone who does not hold the key."
Stage 4 reading index 3 of the Names and Facts arrays in Tank City Reboot

Stage 4 becomes index 3, and both tables answer from slot 3.

Now try stage 10. The sum is (10 - 1) % 6, which is 3 again. So stage 10 shows ENCRYPTION too. That remainder step is why the stages loop after stage 6. The table stays six items long, and the math keeps the index inside it.

The game teaches the idea behind each name. It is not a course, but each card gives you one plain sentence about a real part of a computer.

More lookup tables in the same game

Once you know the shape, you start to see tables everywhere in the code.

Enemy names. Game.cs has KindNames = { "", "WORM", "TROJAN", "SPYWARE", "ROOTKIT" }. The enemy types come from an enum called Kind, where Player is 0, Worm is 1, Trojan is 2, Spyware is 3 and Rootkit is 4. The code turns a kind into its number and reads the name at that slot. Slot 0 is an empty string because the player never needs a malware name.

Power-up carriers. The same file has Carriers = { 4, 11, 18 }. Each time an enemy tank appears, the game counts it and asks whether that count is in the list, using Array.IndexOf(Carriers, spawned) >= 0. So the 4th, 11th and 18th tank of each stage carry a power-up. This is the other common use of a table: checking whether a value is on a list of allowed items.

Tank shapes. In Art.cs, each tank shape is an array of 16 text rows, and each letter stands for a part, such as T for tread or C for cannon. The drawing code reads shape[r][c], row first, then column. That is a table with two numbers instead of one, much like a tile map.

Lookup table or a chain of checks?

Not every rule in the game uses a table, and that is a fair contrast. The points for each enemy come from a function called Points. It is a chain of conditions: if the kind is Worm, give 100; if Trojan, 200; if Spyware, 300; if Rootkit, 400; otherwise 0.

That works fine. With four cases, the chain is short and easy to read. But you could also write it as a table, such as { 0, 100, 200, 300, 400 }, and read it with the kind's number, just like KindNames.

A few plain points can help you choose:

  • A table keeps the data in one place. Adding a stage means adding one name and one fact.
  • A table only works when the key can become a slot number in range.
  • A chain of checks can hold logic that is not just a stored value.
  • Two tables that must line up, like Names and Facts, need the same order. If one slips, every stage shows the wrong fact.

Lookup tables and hash tables

You will also meet tables keyed by things that are not small numbers, like words. Those are often built with a hash table. The Wikipedia page draws the line clearly. In a lookup table, the value for key k sits in slot k. In a hash table, a hash function first turns the key into a slot number.

The game uses this kind too. In Art.cs, the power-up pictures sit in a Dictionary keyed by the power-up type, and the code reads Glyphs[p]. You write it the same way, with square brackets, but the key is a power-up type, not a slot number you worked out.

If you like seeing how numbers map to answers, the guide to bitwise operators is a good next read.

Frequently asked questions

Is a lookup table the same as an array?

Not quite. A lookup table is the idea of stored answers read by key. An array is the most common place to keep one when the keys are small whole numbers.

Why does the first slot start at 0?

In C#, arrays are zero indexed, so a six-item array runs from 0 to 5. That is why stage 4 reads slot 3.

Can a lookup table be filled while the program runs?

Yes. Wikipedia notes that tables may be worked out ahead of time and stored, or filled in when the program starts up.

Do I need to know code to play Tank City Reboot?

No. You just play in your browser, and playing never needs an account. The cards on the game page explain each name in one plain sentence.

Get started

Open Tank City Reboot and play to stage 4. When the ENCRYPTION card appears, you are looking at slot 3 of two lookup tables. Then try writing the Points rule as a five-slot array yourself, and check that a rootkit still gives 400.

0 likes

Comments

No comments yet.

Sign in or make an account to comment.