Practical Code Generation and Game Logic Development Assisted by Lightweight LLMs

    This article explores the practical application of large language models (LLMs) in assisting code generation and game logic development. Aimed at programmers and game designers, it outlines the appropriate use cases for LLMs, criteria for evaluating generated code drafts, and provides structural examples for open-world maps, reputation systems, and turn-based combat mechanics.

    This article is intended for programmers and game designers using LLMs to assist development. The included code snippets are for educational purposes to illustrate structure and validation logic, and are not guaranteed to be production-ready.

    I. Defining the Scope of Application

    Tasks suitable for LLM assistance include boilerplate code, data class definitions, unit test drafts, common API call examples, and drafting classes and functions based on verbal requirements.

    Tasks unsuitable for deployment without human review include financial calculations, permission checks, encryption and authentication, user privacy data processing, and core business rules requiring long-term maintenance.

    Regarding efficiency gains, while claims of reducing development time from “days to minutes” exist, without specific model names, versions, task sets, and comparison baselines, such claims cannot be verified. To properly evaluate benefits, teams should record the completion time, rework frequency, and defect count for the same set of tasks with and without LLM assistance, and then compare the results.

    II. Criteria for Usable Code Drafts

    LLM-generated code generally conveys the main idea, but common issues fall into three categories: undefined dependent functions or classes, missing boundary conditions, and naming inconsistencies with project standards. A three-step review process is recommended:

    1. Run the code to confirm there are no syntax or import errors.
    2. Add tests for critical branches, especially boundary values.
    3. Have individuals familiar with the business rules verify the code line by line.

    III. Open-World Maps: Define Before Assemble

    Maps are typically divided into three layers: regions, villages, and cities. The code below defines Region before GameWorld. The initialization method of GameWorld directly creates Region objects; Python must know this class name when executing that line, or it will raise an error. This code requires Python 3.9 or higher.

    1. class Region:
    2. def __init__(self, name):
    3. self.name = name
    4. self.villages = []
    5. self.cities = []
    6. self.npcs = []
    7. class GameWorld:
    8. def __init__(self):
    9. self.regions = {
    10. "East": Region("Eastern Plains"),
    11. "West": Region("Western Mountains"),
    12. }

    Non-linear storylines and moral factions belong to gameplay design and should not rely solely on model inference. Designers should first provide the enumeration values for factions and their switching conditions, allowing developers to implement based on these specifications.

    IV. Reputation System: State Source for Every Branch

    This article assumes the reputation value ranges from 0 to 100, increasing or decreasing based on task completion and event outcomes. In the original example, a “newcomer” corresponds to a lower reputation.

    In the code below, get_player_input only reads an integer; actual projects require input validation. initiate_combat only prints a prompt; the actual combat entry needs to connect to the combat system in Section V.

    1. class Player:
    2. def __init__(self, reputation=0):
    3. self.name = "Player"
    4. self.reputation = reputation # Range from 0 to 100
    5. class NPC:
    6. def __init__(self, name, min_reputation_for_info):
    7. self.name = name
    8. self.min_reputation_for_info = min_reputation_for_info
    9. def provide_quest_info(self, player):
    10. print(f"{self.name}: There have been unusual movements outside the city recently. You can go check it out.")
    11. def get_player_input():
    12. return int(input("Please select: "))
    13. class ReputationSystem:
    14. def interact_with_npc(self, player, npc):
    15. if player.reputation < npc.min_reputation_for_info:
    16. print(f"{npc.name}: You are just a newcomer. Come back when you have made a name for yourself.")
    17. self.offer_alternative_options(player, npc)
    18. else:
    19. npc.provide_quest_info(player)
    20. def offer_alternative_options(self, player, npc):
    21. print("1. Leave and take a small quest to increase reputation")
    22. print("2. Force the NPC to talk through violence")
    23. choice = get_player_input()
    24. if choice == 2:
    25. self.initiate_combat(player, npc)
    26. def initiate_combat(self, player, npc):
    27. # Placeholder implementation, should call the combat system in practice
    28. print(f"Combat initiated with {npc.name}")

    Several design questions must also be confirmed: whether forcing an NPC through violence reduces reputation, whether it affects other NPCs’ attitudes toward the player, and after which events the reputation value changes. These rules are not reflected in the code and should be documented in the design specification.

    V. Turn-Based Combat: Turn Order Rules and Value Boundaries

    The original text states players initially have 30 attribute points, allowing one attribute to be raised to a “good” level. However, it does not specify what 30 points represent, the numerical upper limit for “good,” or the range for agility. The code below assumes agility is an integer from 1 to 100; attribute allocation rules should follow the design specification.

    1. class Character:
    2. def __init__(self, name, agility):
    3. self.name = name
    4. self.agility = agility # Agility, range from 1 to 100
    5. def get_attribute(self, key):
    6. if key == "Agility":
    7. return self.agility
    8. raise KeyError(key)
    9. class TurnBasedCombat:
    10. MAX_CONSECUTIVE = 4
    11. def calculate_turn_order(self, player, enemy):
    12. p = player.get_attribute("Agility")
    13. e = enemy.get_attribute("Agility")
    14. # If the difference is less than 5, both sides alternate turns
    15. if abs(p - e) < 5:
    16. return ["player", "enemy"]
    17. if p > e:
    18. fast, slow, fast_side, slow_side = p, e, "player", "enemy"
    19. else:
    20. fast, slow, fast_side, slow_side = e, p, "enemy", "player"
    21. ratio = fast / max(slow, 1)
    22. # If the ratio exceeds 2, the faster side acts 4 consecutive times
    23. if ratio > 2:
    24. return [fast_side] * self.MAX_CONSECUTIVE + [slow_side]
    25. # In other cases, round to the nearest integer, acting at least 2 consecutive times
    26. count = max(2, min(self.MAX_CONSECUTIVE, round(ratio)))
    27. return [fast_side] * count + [slow_side]

    Action rules can broadly