Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
EDIT: How do I make a population simulator, that simulates population growth?

(Unnecessary)
Hi I'm Jake and recently I've started designing a population simulator. What would be the best way to make a population simulator running on a timeline? How would I incorporate random gender, age, height and weight. When people in this game have children how would I make the child have the last name of the male parent. How would I implement a count of my population.

So far I started to work on the Person class:
C#
public class Person
{
    public PName Name = new PName("", new List<string>(), "");
        
    public Gender Gender = Gender.Male;

    public Person(PName name, Gender gender)
    {
        Name = name;
        Gender = gender;
    }

    public Person(PName name)
    {
        Name = name;
        Random random = new Random();
        Gender = (Gender)random.Next(1, 2);
    }

    public string GetGender()
    {
        if (Gender == Gender.Female)
        {
            return "Female";
        }
        else
        {
            return "Male";
        }
    }
}


Here is the Gender enum:
C#
public enum Gender : int
{
    Male = 1,
    Female = 2,
}


Here is the PName class:
C#
public class PName
{
    public string FirstName = "";
    public List<string> MiddleNames = new List<string>();
    public string LastName = "";

    public PName(string firstName, List<string> middleNames, string lastName)
    {
        FirstName = firstName;
        MiddleNames = middleNames;
        LastName = lastName;
    }

    public string GetName()
    {
        string middleNames = "";
        int middleNameCount = 0;

        foreach (string middleName in MiddleNames)
        {
            if (middleNameCount == 0)
            {
                middleNames = " " + middleName;
            }
            else if (middleNameCount == MiddleNames.ToArray().GetLength(0))
            {
                middleNames = middleNames + " " + middleName;
            }
            else
            {
                middleNames = middleNames + middleName;
            }

            middleNameCount += 1;
        }

        return FirstName + middleNames + " " + LastName; 
    }

    public static implicit operator string(PName a)
    {
        return a.GetName();
    }
}
Posted
Updated 9-Dec-13 19:11pm
v3
Comments
amsga 8-Dec-13 5:38am    
You will need other classes to deal with the idea of family and to generate out the corresponding (pardon the pun) children. That class should be able to handle the family name issues.
Richard MacCutchan 8-Dec-13 7:48am    
You have created a few basic classes but you are starting from the wrong position. And why have a separate class for a person's name?
You need to think about the algorithms you need to make the population grow along your timeline. You can use the Random class to make events happen. And at each event you will need another randomiser to decide what event happens, birth, death, marriage etc.
Sergey Alexandrovich Kryukov 8-Dec-13 14:16pm    
This "how" question does not make much sense. The answer will be: "the way you design it". The code design should meet the model you design. So, the question is: "have you developed (or have for granted) the valid mathematical model meeting your requirement"? The question is way too vague, as the solutions can be very different. Well, try to figure out some specific concerns you have. The problem does not sound really difficult, but it requires that you act as a scientist and an engineer, not as a student trying to master programming...
—SA
Chris Ross 2 9-Dec-13 9:46am    
Re implementing a count of your population - whenever a new person is created, add it to a collection (class Population {...}). Whenever someone dies, remove if from the Population class (and add it to one or more of the Ghosts, Undead, Ascended, MerelyDead, Saints, FoodForWorms, etc. classes :) ).
So that you don't have references all over the place to the Population class, make the creation and death of a person static members of your Person class; those methods can then update the Population class. ... or - just have a static property TotalPopulation on the Person class; simpler if a little less OO clean.
BillWoodruff 10-Dec-13 2:30am    
This can be a great (and enjoyable) challenge for you. I'd suggest you start off by doing some research before you design your Classes.

Take a look at some of the links here for "c# population simulation:"

https://www.google.com/search?q=c%23+population+simulation

And, if you are not familiar with it, I think you can get many useful ideas from studying John Conway's "Game of Life:"

http://en.wikipedia.org/wiki/Conway's_Game_of_Life

There's a C# version of the game on CodeProject:

http://www.codeproject.com/Articles/329881/Code-solution-of-Conway-s-Game-of-life-prob

As you progress with writing your code, I'm sure you will get good answers to specific questions here.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900