Many Enums in one Array C# - c#

I have been looking around how to make one array with many different enums.
What I am trying to do is have enums with for example
public enum playerTeam
{
Red,
Blue
};
and another with
public enum currentWeapon
{
Knife,
Gun,
Rifle,
Shotgun,
SniperRifle,
RocketLauncher,
Grenade,
Molotov,
FlameThrower,
ProximityMine,
RemoteMine
};
and then assign them to a array called something like
Players[]
Then being able to loop trough the array and set values of each enum. I have used the enums without array before. To set/get data of the player.
But now I am about to expand my project to multiplayer. And I cant figure out how to add enums to one array. As that would make code a bit easier to handle.
This was really hard to explain, hope you guys understand..

I'd suggest you create a class Player which has members Weapon and Team. Then use player instances to perform operations.
class Player
{
Weapon CurrentWeapon {get; set;}
Team Team {get; set;}
}

That's a weird way to look at things, considering what C# allows you to do. At best, to achieve exactly what you want, you could use something that maps keys to values (players to weapons/teams), like the System.Collections.Generic.Dictionary.
But there's really no need to do that. The Player class should contain that info in two fields:
class Player
{
...
private Team currentTeam;
private Weapon currentWeapon;
...
}
Judging by how you named your enums, and by your idea, I'm thinking you should also follow a learning resource for C# and OOP, from beginning to end.

This is a bit ridiculous because team is definably not a weapon (wtf is it currentWeapon. and not Weapon) but if you're asking how to use an enum and bit flags you could write
[Flags]
public enum Weapon
{
...
WeaponMask = 0xFF
IsBlueTeam = 0x0100
}
Which allows you to do
switch(player[i] & WeaponMask) { case SomeWeapon: ... }
isBlueTeam = (player[i] & IsBlueTeam) != 0 //assuming its either blue or read

Related

Writing a game: Dynamic attack logic in classes

I'm writing an RPG in C#, I have the character classes initialising nicely but I'm struggling with the attack classes.
Each attack will have its unique damage calculation formulas based off of multiple stats and eventually decorators.
Is there a way to dynamically create/append these damage formulas and decorators, or should I create a unique class for each attack that extends a generic attack class?
Here is some Pseudocode for what I'm after:
public class Attack
{
public string Name {get; set;}
List<Decorators> DamageCalc;
public Attack(string[] data, List<Decorators> decorators)
{
Name = data[DataList.Name]; //I'm using constants to keep my indexes readable
DamageCalc = data[DataList.Damage]; //i.e. strength * 10 + agility
Decorators = decorators
ApplyDecorators(Decorators)
}
public double DamageCalculation(Character attacker)
{
return DamageCalc; //attacker.Strength * 10 + attacker.Agility
}
...
}
Flexibility to add new features and behaviours is of particular interest to me.
Many thanks for your input!
Edit 1
Where I to use JSON, would there be any simple way to relate JSON fields to my Character class's stat variables or do I need write custom code to interoperate it?
Off hand I can't think of a tidy way to use a JSON field, or any field for that matter, that can dynamically collect information from my Character class.
Edit 2
Ideally this would allow me to dynamically create all my attacks as instances of a single attack class by taking data from a JSON file, string, XML file or database table.
I changed the code sample on the DamageCalculation method.
Closed
I the selected answer does provide a solution for my particular problem. It seems that I really would have to write code to parse formulas and generate the dynamic tool that I'm envisioning. As such I will see if I can get around this particular problem from a design perspective.
You need to write a base class that contain the signature of your calculations and on the derived classes implement them based on their specialty.
It is a solid, and you can find more about it under the title of "Strategy Design Pattern"

Repurposing the .net type system - bad idea?

I'm playing around with writing an item crafting system that I might want to put into a game someday. There are Recipes which specify the ingredients they require and what they produce.
I wanted the recipes to be flexible, such that they only required a broad category of ingredients, not an exact one. For example, a recipe for a weapon blade might just say it requires a metal, not specifically steel. The recipes have to verify that the ingredients given are within the acceptable category. Some materials might belong to multiple categories.
Then I had a possibly brilliant, possibly insane idea. The .net type system already implements that! So for each material, I add a property of type Type, and use IsAssignableFrom to verify the ingredients' compatibility.
I have a file that looks like this:
public interface ItemType { }
public interface Material : ItemType { }
public interface Metal : Material { }
public interface Gold : Metal { }
public interface Silver : Metal { }
public interface Iron : Metal { }
public interface Steel : Metal { }
public interface Wood : Material { }
public interface Coal : Material { }
And so on. None of those are ever implemented. I'm just borrowing the built in type checking for my own purposes.
Is there anything necessarily wrong with this?
edit: actual question
If I've been clear enough to explain what I'm trying to accomplish here, then what would you suggest is a good way to go about it, ignoring this whole type system abuse thing? Would you have also used this solution, or something else?
Second question, are there any pitfalls to watch out for in what I've done here?
Is there anything necessarily wrong with this?
Yes, everything.
Classes and interfaces are meant to express behavior. There is no behavior in your code. Your code is not miscomunicating the intentions. Usually, when you see interface, you expect it to have some method and that method is called. That is not the case here.
It will become impossible to define the materials and recipes in some kind of configuration/resource file, like most normal games do. So you have to recompile every time you want to change the materials or recipe a little.
It will become problematic to create items/materials that are somehow related. For example, lets say there are multiple tools and each tool can be from different materials. In your case, you have to write down every combination. In ideal case, you can just run few nested for loops which create each combination.
You cannot parametrize the materials in any way without creating classes of them. For example, you might want different colors of wool. How would you do it? Create interface for each color? Or use some kind of enum as parameter. But you have to create class for that.
Better way would be simple Item class that has collection of tags. Even simple strings should be enough.

What Data Structure would you use for a Curriculum of a Department in a University?

For my homework, I'm implementing a course registration system for a university and I implemented a simple class for Curriculum with list of semesters and other properties like name of the department, total credits etc.
But I'm wondering if I can inherit this class from a Graph Data Structure with Edges and vertices.
Anybody done similar things before?
My current design is something like this:
public class Curriculum
{
public string NameOfDepartment { get; set; }
public List<Semester> Semesters { get; set; }
public bool IsProgramDesigned { get; set; }
public Curriculum()
{
IsProgramDesigned = false;
}
//
public string AddSemester(Semester semester)
{
As an enterprise architect I would absolutely not use a graph structure for this data. This data is a list and nothing more.
For a problem similar to this, the only reason I would ever consider using a graph structure would be to potentially create the relationship of course requirements and prerequisites.
This way you could then use the graph algorithm to determine if it is valid for a student to register for a class by making sure it is a valid addition to the tree. Same for removing classes, it could be validated to make sure you aren't dropping a class and staying enrolled in the lab for the class example.
Now if I was going to actually implement this. I would still have an overall list of classes that have a Key to the vertex in the graph representation. One thing to keep in mind is that graph algorithms are about the biggest heavy hitter you can throw at a database so minimize the amount of work done to pull the graph out is always key. Depending on the size and scope, I would also evaluate if I could store entire graphs in a serialized form or to use a document database for the same reason.
Which in this example would be the most likely route I would take. I would store the entire object of prerequisites co-requisites and so on right inline with my course object. Since the graph is a set it and done event there's no need to do an actual graph traversal and you're better off storing the pre-calculated graph.
Yes you can inherit this class from a Graph data structure. You can make it a subclass of anything you want (except for a sealed class). The question of whether or not it is a wise design is entirely dependant on what you want to do. I assume you know how, so comment if you need an example of how to implement inheritance.
IF you are wanting to write your own graphing algorithms, why not just model it yourself? It would probably be a fun exercise.

Good code architecture for this problem?

I am developing a space shooter game with customizable ships. You can increase the strength of any number of properties of the ship via a pair of radar charts*. Internally, i represent each ship as a subclassed SpaceObject class, which holds a ShipInfo that describes various properties of that ship.
I want to develop a relatively simple API that lets me feed in a block of relative strengths (from minimum to maximum of what the radar chart allows) for all of the ship properties (some of which are simplifications of the underlying actual set of properties) and get back a ShipInfo class i can give to a PlayerShip class (that is the object that is instantiated to be a player ship).
I can develop the code to do the transformations between simplified and actual properties myself, but i would like some recommendations as to what sort of architecture to provide to minimize the pain of interacting with this translator code (i.e. no methods with 5+ arguments or somesuch other nonsense). Does anyone have any ideas?
*=not actually implemented yet, but that's the plan.
What about the Builder pattern? You could have a static FillDefaults method on your ShipInfo class and then assign each property of the ShipInfo via an instance method that returns the instance that you're working with, like this:
ShipInfo.FillDefaults().CalculateSomething(50).AssignName("Testing...").RelativeFiringPower(10).ApplyTo(myShip);
Within ShipInfo, this would look something like:
public static ShipInfo FillDefaults()
{
ShipInfo newInstance = ...;
// Do some default setup here
return newInstance;
}
public ShipInfo CalculateSomething(int basis)
{
// Do some calculation
// Assign some values internally
return this;
}
// Keep following this pattern of methods
public void ApplyTo(SpaceObject obj)
{
// Some checks here if you want
obj.ShipInfo = this;
}
I would say the Facade pattern is perfect for that kind of problem. If you have 5+ arguments on your methods, consider encapsulating at least part of them in a new type.
Seems like you want to set some properties but not the others, but not in a particular order of importance so that you could define overloads with incrementally more arguments.
You could implement a constructor with minimum required values that sets default values for the other, and then use object initializer to set the remaining relevant values:
// Didn't set properties 2 3 and 6, only set the ones needed in this case.
SpaceObject ship = new SpaceObject(someRequiredValue) {
Property1 = 50,
Property4 = Game.Settings.Ships.Armor.Strong,
Property5 = new PropertySet1{
Prop51 = "Enterprise",
Prop53 = true,
Prop57 = false
};
To me this looks like a case for the decorator pattern.

Best way to get object data?

I need to display some stats, numbers, and graphs about various game objects on the screen.
(examples: camera position, field of view, frames per second, fill rate, number of objects culled, etc... )
Currently any object which wants to be graphed or displayed implements an interface along these lines:
public interface IGraphable
{
float GraphableValue { get; set; }
}
Then that object can be sent to a graph component to be displayed. This has some obvious drawbacks like not being able to graph 2 different pieces of data which belong to the same class.
What I want is a way to pass a pointer to where the data is located or a pointer to a function which knows how to return the data instead of passing the object to the display component.
I believe that this is what delegates are for but I don't understand how to use them in this context (Actually I don't understand them very well at all). Also, is there another (smarter/better) way to do this?
Thanks!
Why not invert the control like this:
public interface IGraphable
{
    void BuildGraphable( IGraph g );
}
interface IGraph {
void AddValue( double value );
}
this is a preferred option in OO anyway as it hides details of the IGraphable implementation. Additionally you can now extend IGraph for added functionality without breaking compatibility.
Depending on how you're doing things, you could possibly use Reflection (attributes on accessors), although that can be relatively confusing at first too. But it's a very useful tool in your arsenal, so it's well worth spending the time on. Here is a great tutorial on how to use them:
http://www.brainbell.com/tutors/C_Sharp/Attributes.htm
But then, learning delegates is also very useful, and that does sound like a good solution. I haven't looked deeply into it, but this tutorial on the same site might be useful:
http://www.brainbell.com/tutors/C_Sharp/Delegates_and_Event_Handlers.htm
I have decided to do the following:
public class GraphComponent
{
private Func<flaot> _function;
public GraphComponent(Func<flaot> function, ...)
{ ... }
}
This allows me to specify how the data is retrieved by writing something like this:
FPSComponent fpsc = new FPSComponent();
GraphComponent fpsg = new GraphComponent(delegate() { return fpsc.ApproximateFPS; }, ...);
What I want is a way to pass a pointer to where the data is located or a pointer to a function which knows how to return the data instead of passing the object to the display component.
If you don't want to add your objects to your graph component BECAUSE
This has some obvious drawbacks like not being able to graph 2 different pieces of data which belong to the same class.
Maybe a list will solve your problem ?
public interface IGraphable
{
List<float> GraphableValues { get; }
}

Categories

Resources