C
C#8mo 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
Pobiega8mo 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
YuuKi8mo 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
Pobiega8mo 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
YuuKi8mo ago
yea something like this, so this is polymorphic JSON deserialization ?
Pobiega
Pobiega8mo ago
it would need that, yes
YuuKi
YuuKi8mo ago
okay ill search ty
Pobiega
Pobiega8mo 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
YuuKi8mo ago
okay thanks ill do more research on that ^^
Accord
Accord8mo 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
More Posts
❔ Is this possible in WEBAPI to add logic between JWT token validation and the authorization?I read a blog about the AspNetCore WebApi: https://jasonwatmore.com/post/2022/02/18/net-6-role-based✅ docker build with dotnet 8ERROR: Service 'vmg-dashboards-api' failed to build: The command '/bin/sh -c dotnet restore "vmg.dasMake every textblock in an itemscontrol with an OCollection<string> as the source have a click eventI have an itemscontrl with a OCollection as the source which means it can be updated i have a textbl❔ Validation of appsettings configuration before running the applicationHello. I would like to validate entries in the appsettings configuration at application startup to ❔ Migrate AppDomain (.net framework) to AssemblyLoadContext (.net core)I need help converting this code to the .net core equivalent by using AppDomain here, ensured that t❔ Form goes fully transparent please helpi was trying to make blurry panel then i searched and i found something but the problem is i see squ❔ ✅ Mind explaining a code to me?```cs public static int? Closest(int[] arr) { var min = arr.Distinct().Where(x => Math.Abs(x) ❔ (MVC web app) How can I handle exceptions/errors in this case?So I was thinking of placing all this code (the one inside the function) inside a try catch block, a❔ `<code>` xml docs not showing nicely in VSCodeI want to use `<code>` in my XML docs to provide examples documentation. However, extra `\` appears ❔ Console RPG GameHello. Im currently in middle school, and our project is to make an RPG game using console. Sadly we