Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
I'm trying to create a simple simulator in C#, which should simulate a very simple car engine. The simulator can either be on or off, and it will, for instance, have the following inputs (there will possibly come more in the future).
Use case:
In this example, the car engine will have the follow input parameter:
Thrust produced (0-100%)
Temperature (-50C to 300C)
NOS (enabled/disabled)
Driving mode (economy / normal / speed)
On/Off
These input parameter will alter the following two output parameter:
Thrust produced (0-150%)
Temperature (-50C to 300C)
If for instance the driving mode is set to economy, the maximum thrust which it can output will be e.g. 70%. NOS will also alternate the output somehow. The simulator will have an event handler, were the computed values for thrust and temperature will be announced.
Question:
I'm asking for some input on how this simulator easiest can be designed, so it can be extended with new input parameter later on. Any good design pattern you would recommend or articles? Feel free to ask questions or show your best solution :)
Thanks in advance
You should implement several patterns which will make your life easier. If you are familiar with Unity game engine use its concepts. I suggest you to use this game engine if you aiming to build games.
First of all, use Game Loop and Update patterns.
In simulation your need several FixedUpdate events for calculation in one Update event for rendering frame. FixedUpdate should not depend on current FPS.
Then you can implement some kind of Bus like in real cars CAN-BUS is used. It can be done with Observer/Observable or Subscriber/Publisher. You can learn about difference here Difference between Observer, Pub/Sub, and Data Binding but I suggest you to use Subscriber/Publisher as it is like message queue.
To implement different modes (economy, racing) you probably need Strategy pattern.
Also Component pattern will help to make things by analogy of devices and sensors in real car. Each component object reads messages from bus queue and sends events to bus. Some components like ECU or EMS can use current strategy to send commands for injectors.
State pattern will help you to maintain different states of components. For example, engine cycle state.
Your question is very broad, but I suggest you to choose some real-life concept and make code design by its analogy.
Please refer to this great book to read more about mentioned patterns: http://gameprogrammingpatterns.com/contents.html
Given the car engine simulator is bunch of input and output parameters which depends on change to other parameters I'd recommend to use Observer pattern,
For example ThrustProduced parameter would be an observer of DrivingMode, and when the DrivingMode parameter changed, it has to notify it's observers including ThrustProduced, which then recalculates the thrust and limit it to 70% and then fire the event to notify that the thrust has changed.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I'm trying to create a replica of the card game "Monopoly Deal" in C# Console, for educational purposes. I have a problem with my class design that I'm not sure how to resolve. I have two classes, Game and Player. Game controls the state of the game, and contains a list of Player objects.
Currently the way it works is that the Game class controls everything. The Player is basically a container class: it has all of the cards in the player's collections. The decision-making is done by the Game class because it can see all the players, and therefore knows which moves are possible.
For example, if Player 1 wanted to steal a card from Player 2, my program would have to first verify that Player 2 has any cards to steal. Player 1 doesn't know that, because it's just a single object in a list of Player objects. So the Game class has a method which checks all possible actions that a player could make, asks the player to choose which action they want to make, and then performs that action for them.
My problem: I don't think that this is good OOP class-design. I think that I should be delegating the decision-making to the Player class, not the Game class - otherwise my Game class will become bloated with lots of methods that relate to player actions and decisions, and not the game state itself. I don't know how to achieve this, however.
Possible solutions:
Create a GameState object containing just a limited view of other players' cards, and pass that to the Player object (Seems the easiest way but I guess it's creating redundant data which is a waste of memory.)
Pass a reference to the whole Game object to the Player class (I could do - it's not like I'm going to program by ComputerPlayers to cheat - but doesn't feel like good OOP design either)
Give up, and put all of the game logic (including the computer AI) in the Game class, and accept that Player will just be a data container, not a decision maker.
Redesign my classes a different way.
Something else I've missed?
Is there any obvious solution here? Thank you.
EDIT: I've attempted to trim down excessive detail in the question as it was closed for "Not enough focus". If further revisions are needed, please let me know.
How about creating an action-class that represents an action that you can take in the game. The Game then just has two methods Game.verifyAction(action) and Game.executeAction(action). The Game contains all the data necessary to verify and execute actions.
Next, treat players just as data containers, meaning a player does not think it's just contained in the state. A player might contain data like it's score, it's id, etc. To actually think you have an agent class that is responsible for choosing actions. So Agent.think(Game) returns an action that the agent wants to take in this state.
Now it's easy to move the main-loop outside the Game-class, you can also replace an Agent easily with an GUI that returns a valid action once the human chooses an action.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I want to read sms from my GSM modem.
I wrote C# code.
This code run when I click start button.
I want to my program read sms when received, not click button.
thanks.
Your program is keyed to activate when you press a button, some method is called. You need to call this method when SMS data is received instead. This could be done using threads (SMS thread and main thread showing data) although it could just as easily be done using a cycle. In pseudo code:
while (don't quit) {
display page;
check for sms data;
sleep for small time to allow other OS programs to run also;
}
This is a "tight loop" and can use excessive amounts of CPU time depending on the code of the actual steps. For a tight program loop one simple method is to apply some sort of sleep method.
There are other ways to do the same thing, visitor pattern could probably be used, threads, etc...
It seems that you are only lacking the cycle. Your programs is probably more like:
while (don't quit) {
display page.
wait for button press.
}
although that flow wouldn't be obviously apparent at first glance without studying your program flow.
If you are using triggers (the button press is probably a trigger) you can trigger on a timer that fires as often as you want (100ms, 1 second, whatever) that checks for SMS data when fired, if there is SMS data it updates the form.
Many, many ways to do this. A quick Google for "program flow" doesn't find any useful links at first glance that would explain the many ways you could do this. Perhaps looking at other's code would help. I've often searched open source repositories for code I could look at to see how someone else did something.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I have been programming for about 2 months now and I'm self-learned, while I get the basics about inheritance, polymorphism, interfaces, delegates, data & reference types, loops, if/switch, LINQ, xml, SQL, etc etc. I just cannot wrap my head around events!
I have read at least 4-5 different tutorials and writeups online but they are way too confusing to me, there's an Event type, EventHandler, delegates, event raise/subscribe, there's just too much stuff going on and, I don't know if I'm thickheaded or not but it's INCREDIBLY confusing for me.
Please explain events to me in a way that a complete beginner programmer like me can understand, many thanks!
Action and reaction.
As John said in your comments.. "When I click a button, something happens!"
It doesn't get much simpler than this:
http://en.wikipedia.org/wiki/Observer_pattern
At their core event simply contain a list of functions (or at least a way to access these functions) which will be called the event is raised. Raising an event is simply the act of triggering the subject to notify all the functions which are subscribed (read: contained in the list) to that event.
How this list of subscriptions is comprised varies greatly depending on the capabilities of the framework. In the observer pattern (commonly used in Java) you do this by passing in an object that has implemented the appropriate interface. The subject iterates through the list of observers and call the function defined by the interface. The drawback to this pattern is that you have to have the potential for a naming collision between two entirely different subjects which can be difficult (though not impossible) to work around.
Delegates remedy this issue by allowing you to pass in the function or method itself. A delegate is sort of like an interface in that it establishes a contract but instead of class members it just specifies a set of parameters for a function. The subject can then iterate through a list of these methods, which are commonly referred to as event handlers, and pass the appropriate parameters. Delegates are less troublesome than an observer pattern but they can still be time consuming.
More recently C# added the generic delegates Action and Func which are a bit easier to work with.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
i've been figuring a little about abilities... like in DOTA, LoL and other MOBA / MMORPG games. If i would make a game with character selection in the beginning, and in-game it would have spells / abilities and such..
What would a correct way of doing this be? Should i make one Champion class, with all values and textures, or initialize it in Game1.cs like Champion champ = new Champion(texture, name, Q-ability name, Q-cooldown, Q-damage, Q-manacost) - but that would take TONS of diffrent declarations...
Should i make a Champion folder and make a seperate class of them all? And also the abilities - i'm thinking about maaking an enum to keep them, or would a List or another variable work better? Thanks!
For such a game, I'd recommend the use of an entity system (http://entity-systems.wikidot.com/). These allow you to create separate spell, ability, player, character etc classes and to compose them into highly configurable entities. I ported the popular Ash entity framework to C#, which is available at https://github.com/DavidArno/Ash.NET, which may be of use to you.
Beside of an entity system which David Arno suggested, if I wanted to do this, I'd go with a single Hero class and then with a list of abilities inside it. Abilities are all the same, different thing is their factors. For instance, how much damage an ability can deliver and etc. This way you have to instantiate all of your champions and their abilities.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
all I am looking to develop a project in unity, it is for android! I was wondering if I could get some clarity on a few things. My problem involves me trying to creating a universe of stars, 150,000 individual stars to be exact, granted there would only be a certain percentage in view at any one time. What is the most efficient structure for being able to convince the user of a realistic environment while keeping the overhead to a minimum since it will be on a phone?
What type of objects do I want to use to represent the masses of stars vs. the likes of stars in close proximity that require finer details?
What sort of threading structures should I consider while planning this project?
How easily does a project port from unity to android, in such scenarios?
Any help is much appreciated as I am looking to better develop with unity, cheers
I would suggest not tracking all 150,000 stars, but only the ones that are in view. When the field of view changes, use a random number generator to define the stars that have just entered it, and drop from memory the ones that have left. To preserve consistency, you might want to retain the stars for a short period around the current field of view, if the user can do rapid switches in direction.
As for threading, that's less a function of the number of stars you are tracking, and more a function of what it is that you are doing with them - something you didn't mention.
1) This question is mainly a game development question and not unity regarding. I just point you in the direction, as a complete answer would be to much. Normally if you need to know where you are in a 3D scene with infinite objects or close (150k is close), you would use a octree for orientation. Constructed like a map, each node of the tree points a direction (West, South, Nord, East, NNW, ...) Then you each of your stars gets 1 node, and you can calculate what is where and how much do you want to see. More information can be found on google. (Quite complicated topic jfyi)
2) Dedicated to 1) with a mix of entity/component design. You will know what I mean after 1) is clear to you.
3) Absolutly Multithreaded Asynchron. 1 Thread Update, 1 Thread Draw, Few Worker Threads (position, ...)
4) The port of Unity Engine is actually working really good. Of course you should have an android peripherial to test and debug on, but most of the time, it will work for you.