iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🍵

Managing 5 Enemies at Once: Using Arrays for Data Structures - All-Role Programmer Plan C Language #4

に公開

Review of the Previous Article

https://zenn.dev/mattya3713/articles/f76f39ea74a103

Last time, we learned about "repetition" in programming.

  • while statements (repeat while a condition is met)
  • for statements (when the number of repetitions is determined)

With this, we have covered the four fundamental concepts:

  • Create values (variables)
  • Calculate (arithmetic operations)
  • Make decisions (if)
  • Repeat (while / for)

However, a problem arises here.

"There are 5 enemies. I want to manage their HP individually."

With our current knowledge, we can only write it like this:

int EnemyHP1 = 100;
int EnemyHP2 = 100;
int EnemyHP3 = 100;
int EnemyHP4 = 100;
int EnemyHP5 = 100;

If there are 100 enemies... would you write 100 lines? That is troublesome, isn't it?

So, this time, we will learn a mechanism to manage data of the same type together.

It is an array.

We will look at:

  • What is an array?
  • How to use an array
  • Combining with for loops

We will learn the mechanism of "lining up data of the same type together."


Please try coding and testing this chapter as well.

Rather than just reading,

Code it yourself
 ↓
Run it
 ↓
Fix errors / Ask someone ← Level up here
 ↓
See the result and understand it

This is the fastest way to master it.

Before Learning Arrays

Let's try running the code first.

#include <stdio.h>

int main()
{
    int EnemyHP[5] = {100, 80, 60, 40, 20};

    for (int i = 0; i < 5; i++)
    {
        printf("HP of Enemy %d: %d\n", i + 1, EnemyHP[i]);
    }

    return 0;
}

Copy and paste the code and press F5 to execute. If the HP of enemies 1 to 5 is displayed in order, it's a success.

Once you have verified that it works, delete the black window with the X button in the top right, and let's start learning.

What is an Array?

In a nutshell:

"Numbered boxes of the same type lined up together"

This is an array.

If a variable is a single box,
an array is a numbered shelf.

int EnemyHP[5] = {100, 80, 60, 40, 20};

Breaking it down, it looks like this:

int

→ The type of box to hold integers

EnemyHP

→ The name of the array

[5]

→ Number of boxes (prepared for 5 items)

= {100, 80, 60, 40, 20}

→ Initial values to put in each box

It looks something like this:

EnemyHP[0] == 100
EnemyHP[1] == 80
EnemyHP[2] == 60
EnemyHP[3] == 40
EnemyHP[4] == 20
The numbering starts from 0

Array numbering (index) starts from 0.

If you prepare 5 items, they will be [0] to [4].
Note that [5] does not exist!

int EnemyHP[5]; // 5 items: [0][1][2][3][4]

EnemyHP[5];     // ← Does not exist. This will cause a bug

Accessing Array Values

When you want to retrieve the HP of a specific enemy:

printf("HP of Enemy 1: %d\n", EnemyHP[0]); // 100.
printf("HP of Enemy 3: %d\n", EnemyHP[2]); // 60.

You just retrieve it using [number].

You can also change the values.

// 30 damage to Enemy 1
EnemyHP[0] -= 30;

printf("HP of Enemy 1: %d\n", EnemyHP[0]); // 70

You can use them just like variables.

Combining with for loops

Arrays are even more powerful when combined with for loops.

"I want to deal 10 damage to everyone."

for (int i = 0; i < 5; i++)
{
    EnemyHP[i] -= 10;
    printf("HP of Enemy %d: %d\n", i + 1, EnemyHP[i]);
}

Every time i changes from 0→1→2→3→4,
EnemyHP[i] switches in order.

Whether there are 5 enemies or 100, the shape of the code does not change.
You only need to change one number.

for (int i = 0; i < 100; i++) // Just increase to 100 enemies
{
    EnemyHP[i] -= 10;
}

Processing Only Living Enemies

Finally, let's combine it with if statements.

"I want to display damage only for enemies whose HP is greater than 0."

for (int i = 0; i < 5; i++)
{
    if (EnemyHP[i] > 0)
    {
        printf("Enemy %d is still alive. HP: %d\n", i + 1, EnemyHP[i]);
    }
    else
    {
        printf("Enemy %d has fallen\n", i + 1);
    }
}

Variables, if, for, and arrays,
everything we have learned so far has come together.

Summary

What we did this time is:

Grouping data of the same type (Array)

Accessing by number (Index)

Processing in bulk by combining with for loops


You who have come this far have acquired these 5 skills:

  • Create values (variables)
  • Calculate (arithmetic operations)
  • Make decisions (if)
  • Repeat (while / for)
  • Grouping and lining up (array)

Groups of enemies, item lists, and map tiles.
All of them are combinations of arrays and for loops.

From here on, you will see more "combined techniques" with what you learned in previous chapters.
When you feel it is difficult, it is also good to go back to the previous chapter and review.

Thank you for your hard work.

Next Time

We now know how to group values.

However, if you keep writing code,
this problem will arise:

"I am writing the same process in multiple places."

Grouping processes into one block,
naming them, and making them callable many times.

We will look at functions.

Next Time

https://zenn.dev/mattya3713/articles/10d4d3f921b41d


All-Position Programmer Plan #4

Discussion