# [C language ] Arithmetic progressions and MMORPGs

I am an avid MMORPG player and for me learning to program becomes much more fun and interesting if the programs I write have some connection to the games. This is especially important when it comes to some abstract mathematical things that are very difficult for me (humanities) to perceive.

Today we will look at such a thing as mathematical progressions. First, let’s write an arithmetic progression in C language, which will count how many monsters we need to kill to get a new level 😉 The program asks us to enter our initial experience (start), the amount of experience for killing one monster (step) and the amount of experience that is needed to get a new level (stop):

```#include <stdio.h>

int main (void)
{
int start, step, stop;
int mob = 1;

printf("Enter your initial XP, exp per fight, "
"and exp needed for lvl up\n");

scanf("%d%d%d", &start, &step, &stop);

while (start < stop)
{
start += step;
printf("Killing %d monster: %d\n", mob++, start);
}

printf("Ding! Level up! You've finally got %d exp.\n", start);

getchar();
getchar();

return 0;
}```

As an example, let’s take real values from my online roguelike Tangaria: the first value is the experience at level 22, the second value is the experience for killing an average monster, and the third value is the experience needed to get to level 23:

```Enter your initial XP, exp per fight, and exp needed for lvl up
6800 75 8400
Killing 1 monster: 6875
Killing 2 monster: 6950
Killing 3 monster: 7025
Killing 4 monster: 7100
Killing 5 monster: 7175
Killing 6 monster: 7250
Killing 7 monster: 7325
Killing 8 monster: 7400
Killing 9 monster: 7475
Killing 10 monster: 7550
Killing 11 monster: 7625
Killing 12 monster: 7700
Killing 13 monster: 7775
Killing 14 monster: 7850
Killing 15 monster: 7925
Killing 16 monster: 8000
Killing 17 monster: 8075
Killing 18 monster: 8150
Killing 19 monster: 8225
Killing 20 monster: 8300
Killing 21 monster: 8375
Killing 22 monster: 8450
Ding! Level up! You've finally got 8450 exp.```

In total, we see that to go from level 22 to 23, we need to kill 22 monsters on average 🙂 This simple calculator can be used for any game. Of course, you can just use the calculator (divide the difference between the final and initial experience by the amount of experience for one mob) – but this way is clearer .. and it will teach you how to make progressions in the C language! 😀

Now let’s write a program that will help you find out the farm efficiency of a particular location. Enter the current experience (cur_xp), the amount of experience per minute (exp_per_min) and the game session in minutes (game_session_min) there – and the program will calculate how much experience you get per hour:

```#include <stdio.h>

int main (void)
{
int cur_xp, exp_per_min, game_session_min;
int min = 0; // counter; how long do we play already

printf("Enter your initial (current) XP, exp per minute, "
"and your gaming session (in minutes)\n");

scanf("%d%d%d", &cur_xp, &exp_per_min, &game_session_min);

while (min < game_session_min)
{
printf("Fighting %d minutes: %d\n", min++, cur_xp);
cur_xp += exp_per_min;
}

printf("After %d minutes you will get %d exp.\n", game_session_min, cur_xp);

getchar();
getchar();

return 0;
}```

Sample output:

```Enter your initial XP, exp per minute, and your gaming session (in minutes)
0 470 15
Fighting 0 minutes: 0
Fighting 1 minutes: 470
Fighting 2 minutes: 940
Fighting 3 minutes: 1410
Fighting 4 minutes: 1880
Fighting 5 minutes: 2350
Fighting 6 minutes: 2820
Fighting 7 minutes: 3290
Fighting 8 minutes: 3760
Fighting 9 minutes: 4230
Fighting 10 minutes: 4700
Fighting 11 minutes: 5170
Fighting 12 minutes: 5640
Fighting 13 minutes: 6110
Fighting 14 minutes: 6580
After 15 minutes you will get 7050 exp.```

Now let’s make a program, a fight with a monster – how many rounds your character will last if he is dealt a certain amount of damage. You need to enter your character’s health and incoming damage per round:

```#include <stdio.h>

int main (void)
{
int cur_hp, damage;
int round = 1;

printf("Enter your HP and incoming damage per round\n");

scanf("%d%d", &cur_hp, &damage);

while (cur_hp > 0)
{
printf("Fighting %d round: %d hp left\n", round++, cur_hp);
cur_hp -= damage;
}

printf("You will survive %d blows\n", round - 1);

getchar();
getchar();

return 0;
}```

Output:

```Enter your HP and incoming damage per round
240 17
Fighting 1 round: 240 hp left
Fighting 2 round: 223 hp left
Fighting 3 round: 206 hp left
Fighting 4 round: 189 hp left
Fighting 5 round: 172 hp left
Fighting 6 round: 155 hp left
Fighting 7 round: 138 hp left
Fighting 8 round: 121 hp left
Fighting 9 round: 104 hp left
Fighting 10 round: 87 hp left
Fighting 11 round: 70 hp left
Fighting 12 round: 53 hp left
Fighting 13 round: 36 hp left
Fighting 14 round: 19 hp left
Fighting 15 round: 2 hp left
You will survive 15 blows```

This entry was posted in C language (en). Bookmark the permalink.