PHP Game Development: Experience Points Algorithm Design

I’m currently building a PHP-based browser game and I’m working on the combat system. I need a solid algorithm for calculating experience points (EXP) when a player defeats another player or a monster. I want the EXP gain to scale with the opponent’s level or difficulty, but I’m not sure what formula works best. Any suggestions or examples from your own projects?

I’ve seen some simple approaches like using a fixed EXP per monster, but I’d like something more dynamic that works for both PvP and PvE. What have you used in your games?

Topic Summary: Modern EXP algorithm design in PHP 8.x with enums, match expressions, and difficulty scaling for browser RPGs.

:open_book: Topic Overview (Wikipedia):

An experience point is a unit of measurement used in some tabletop role-playing games (RPGs) and role-playing video games to quantify a player character’s life experience and progression through the game. Experience points are generally awarded for the completion of objectives, overcoming obstacles and opponents, and successful role-playing.
Read more on Wikipedia

:books: Official Documentation & Reference Links:

---
title: EXP calculation flow
---

graph TD
    A[Player Defeats Opponent] --> B{Opponent Type?}
    B -->|PvE| C[Get Monster Difficulty]
    B -->|PvP| D[Get Player Level Difference]
    C --> E[Calculate Base EXP]
    D --> E
    E --> F[Apply Difficulty Multiplier]
    F --> G[Cap EXP if Farming Detected]
    G --> H[Return Final EXP]

Here’s an algorithm I used for a text RPG I built into an AIM bot a while back. It assumes different players have different levels and determines the EXP they receive:

((x*x)*1.8)+x*(1+(ceil(x/15)-1)*40)

Where x is the level of the defeated opponent. This formula gives a non-linear curve that rewards fighting higher-level enemies. However, you need to balance it with your level-up thresholds. If you’re just starting, maybe focus on understanding basic math before implementing complex systems.

In my games, I usually assign a fixed EXP value to each monster type and then calculate EXP based on damage dealt. For example, if a Minotaur has 2000 HP and is worth 1000 EXP, and you deal 500 damage (25% of its HP), you get 250 EXP. This works well for PvE and can be adapted for PvP by treating the player as a monster with a level-based EXP value.

Using an algorithm like alex7h3pr0gr4m3r’s is also good because you only need to store the opponent’s level, not individual EXP values. Just make sure to test the curve so low-level players aren’t stuck.

I’ve been experimenting with EXP algorithms in PHP 8.2 for a turn-based RPG, and I found that using a combination of opponent level and a difficulty multiplier works well. Here’s a modern approach using PHP 8.x features like enums and match expressions:

enum Difficulty: int {
    case Easy = 1;
    case Normal = 1.5;
    case Hard = 2.5;
    case Boss = 5;
}

function calculateExp(int $playerLevel, int $opponentLevel, Difficulty $difficulty): int {
    $baseExp = match(true) {
        $opponentLevel > $playerLevel => 50 * ($opponentLevel - $playerLevel) ** 1.5,
        $opponentLevel === $playerLevel => 30 * $playerLevel,
        default => 20 * $playerLevel / ($playerLevel - $opponentLevel + 1)
    };
    
    return (int)($baseExp * $difficulty->value);
}

For PvP, I use a similar formula but cap the EXP to prevent farming low-level alts. I also integrate with Composer packages like brick/math for precise decimal handling if needed. The key is to test the curve with a script that simulates leveling from 1 to 100 and adjust the constants. What approach are you using for level-up thresholds?