C
C#13mo ago
YuuKi

❔ Modular programming ?

Hello, I am currently studying video game programming in C#. As part of this, we have a project to recreate the board game Mini-Ville. Our goal is to implement cards, including potential future cards, without modifying the code. We plan to achieve this by using JSON or YAML files. These files would contain information such as the card's name, type, and all other relevant details. Additionally, the files would include instructions to be executed when the card is activated. I would like to know if there is a specific term for what we are trying to accomplish. I have been researching online using the keyword "modular programming," but I'm not sure if that's the right term.
9 Replies
Pobiega
Pobiega13mo ago
Not sure if there is a specific term for this, but it's not super hard to implement. To could use polymorphic JSON deserialization to create a list of "effects" that run when the card is activated, making each card essentially a collection of effects
YuuKi
YuuKiOP13mo ago
So, a card that has to take 5 coins from a player and contains the instruction in the JSON as takePiece(target, 5), when the instruction is read in the code, will it execute the takePiece(target, nb) method? *if you need any more info btw, say it *
Pobiega
Pobiega13mo ago
Something like this
[
{
"name": "Squire",
"effects": [
{
"identifier": "takeCoins",
"target": "player",
"value": 5
},
{
"identifier": "spawnUnit",
"target": "player",
"unitType": "squire"
}
]
}
]
[
{
"name": "Squire",
"effects": [
{
"identifier": "takeCoins",
"target": "player",
"value": 5
},
{
"identifier": "spawnUnit",
"target": "player",
"unitType": "squire"
}
]
}
]
YuuKi
YuuKiOP13mo ago
yea something like this, so this is polymorphic JSON deserialization ?
Pobiega
Pobiega13mo ago
it would need that, yes
YuuKi
YuuKiOP13mo ago
okay ill search ty
Pobiega
Pobiega13mo ago
public interface IEffect
{
string Identifier { get; }
bool Handle(GameState state);
}

public class TakeCoins : IEffect
{
public string Identifier => "TakeCoins";

public string Target { get; set; }
public int Value { get; set; }

public bool Handle(GameState state)
{
var target = state.GetPlayer(Target);

if (target.Coins < Value)
{
return false;
}

target.Coins -= Value;
return true;
}
}
public interface IEffect
{
string Identifier { get; }
bool Handle(GameState state);
}

public class TakeCoins : IEffect
{
public string Identifier => "TakeCoins";

public string Target { get; set; }
public int Value { get; set; }

public bool Handle(GameState state)
{
var target = state.GetPlayer(Target);

if (target.Coins < Value)
{
return false;
}

target.Coins -= Value;
return true;
}
}
so you could instantiate a TakeCoins effect via polymorphic deserialization, then apply it like so how you structure this exactly is up to you, but the core idea remains the same
YuuKi
YuuKiOP13mo ago
okay thanks ill do more research on that ^^
Accord
Accord13mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server