CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jul 2012
    Location
    .NET4.0 / VS2010
    Posts
    5

    Need help with a calculator of sorts

    Hi..

    I'm new to C#, so I figured I'd create a user on this forum and see how this plays out.

    I'm trying to create a resource calculator for Travian. I've seen quite a few out there, but none that do exactly what I want it to do, so I decided to make my own.

    What I want is this:

    I need to train a unit. The unit costs x amount of wood, clay, iron and crop.
    I need to find out how to fastest gain access to that amount of resources.
    If my hero produces all resources, he gains (3 * points in resource production) resources of each kind per hour.
    If my produces any other resource, he gains (10 * points in resource production) resources of that kind per hour.

    I want my program to figure out if it is faster to for example take wood production for x hours and then switch to some other production, or which route would be the fastest. I guess I'll need some sort of algorithm for this, I'm not really sure how this can be done.

    I put in the form of my program the following:

    -Points put in resource production
    -How many resources I get per hour
    -How many resources I have currently
    -How many resources I need
    -What type of resource is currently being produces by the hero (wood/clay/iron/crop/all)

    I have this code so far:
    Code:
            private void btnCalc_Click(object sender, EventArgs e)
            {
                // Initialize variables
                int baseWood = 0;
                int baseClay = 0;
                int baseIron = 0;
                int baseCrop = 0;
                int missingWood = 0;
                int missingClay = 0;
                int missingIron = 0;
                int missingCrop = 0;
    
                // Check if all fields are filled out
                foreach (TextBox tb in this.Controls.OfType<TextBox>())
                {
                    if (tb.Text == string.Empty)
                    {
                        // Textbox empty
                        MessageBox.Show("Please fill out all fields.", "Missing number");
                        return;
                    }
                }
    
                if (rbWood.Checked)
                {
                    baseWood = Convert.ToInt32(txtWoodHour.Text) - (Convert.ToInt32(txtRes.Text) * 10);
                }
                else if (rbClay.Checked)
                {
                    baseClay = Convert.ToInt32(txtClayHour.Text) - (Convert.ToInt32(txtRes.Text) * 10);
                }
                else if (rbIron.Checked)
                {
                    baseIron = Convert.ToInt32(txtIronHour.Text) - (Convert.ToInt32(txtRes.Text) * 10);
                }
                else if (rbCrop.Checked)
                {
                    baseCrop = Convert.ToInt32(txtCropHour.Text) - (Convert.ToInt32(txtRes.Text) * 10);
                }
                else if (rbAll.Checked)
                {
                    baseWood = Convert.ToInt32(txtWoodHour.Text) - (Convert.ToInt32(txtRes.Text) * 3);
                    baseClay = Convert.ToInt32(txtClayHour.Text) - (Convert.ToInt32(txtRes.Text) * 3);
                    baseIron = Convert.ToInt32(txtIronHour.Text) - (Convert.ToInt32(txtRes.Text) * 3);
                    baseCrop = Convert.ToInt32(txtCropHour.Text) - (Convert.ToInt32(txtRes.Text) * 3);
                }
                else
                {
                    // No radio buttons selected.
                    MessageBox.Show("Please select which resource your hero produces.", "Missing selection.");
                    return;
                }
    
                // Calculate how many resources are needed
                missingWood = Convert.ToInt32(txtTargetWood.Text) - Convert.ToInt32(txtWood.Text);
                missingClay = Convert.ToInt32(txtTargetClay.Text) - Convert.ToInt32(txtClay.Text);
                missingIron = Convert.ToInt32(txtTargetIron.Text) - Convert.ToInt32(txtIron.Text);
                missingCrop = Convert.ToInt32(txtTargetCrop.Text) - Convert.ToInt32(txtCrop.Text);
    
                // Calculate fastest way to target
            }
    Any help would be appreciated, and I'll gladly explain in more detail, if any other info is needed. My mind is a bit foggy after explaining this, which seemed to be a bit harder to explain than I thought

    Thanks in advance!
    // Jákup

  2. #2
    Join Date
    Jun 2011
    Location
    Buenos Aires, Argentina
    Posts
    130

    Re: Need help with a calculator of sorts

    If... I understood correctly and didn't miss something.
    Let's say a unit will take the same amount of all 4 resources (R)

    Code:
    3 resources -> 1 hour
    R resources -> T Time
    
    Time needed:
    T = R * 1 / 3 hours -> T = R * (1/3)
    or
    Code:
    10 resources -> 1 hour
    R resources -> T Time
    times 4 kinds of resource
    
    Time needed:
    T = 4 * (R * 1 / 10) hours -> T = R * (4/10)
    Rearranging numbers and comparing, you can see that gathering all resources at the same time is always faster than separetdly (4/10 > 1/3) if all are needed.

    Once you have enough of (any) one resource, then the scenario becomes (3/10 < 1/3), and then it becomes more efficient to gather separetly, regardless of what you have in stock. So all you have to do is gather until at least one the resource's quota has been achieved.

    In your case you'd gatherAll() until any of missingWood/Clay/Iron/Crop is zero. Or at least I think so =)

    PS: I'd suggest using numericUpDowns or maskedTextBoxes if you're going to enter numbers only. It's safer and avoids you the trouble of validating.

  3. #3
    Join Date
    Jul 2012
    Location
    .NET4.0 / VS2010
    Posts
    5

    Re: Need help with a calculator of sorts

    Ok, say that we're using your example.

    So when we need 4 resources, we use all 4 until we're missing 3.. After that just raise any that's missing?
    What I'm thinking is, that you have to keep in mind that the base production is still going on while your hero is producing one kind.

    Take my own hero for example, here are the values:

    My production with 18 points in resources, and crop chosen:
    Wood: 165
    Clay: 165
    Iron: 165
    Crop: 336

    So while I'm saving up crop, the other 3 still get 165 higher each hour. So I was wondering how to calculate when to stop producing something with your hero, considering that the base value will get your where you want to be in the end.
    For instance, I change my crop production to wood, when I need 345 and 156 crop, so that in one hour I will get (165 + (18 * 10) = 345) wood and (336 - (18 * 10) = 156) crop.

    Do you know what I mean? I don't know how to explain this =)
    Last edited by Jakup; July 13th, 2012 at 08:03 PM.

  4. #4
    Join Date
    Jul 2012
    Location
    .NET4.0 / VS2010
    Posts
    5

    Re: Need help with a calculator of sorts

    anyone?

    is the above example understandable or do I need to clarify?

  5. #5
    Join Date
    Jul 2012
    Location
    .NET4.0 / VS2010
    Posts
    5

    Re: Need help with a calculator of sorts

    *bump*

  6. #6
    Join Date
    Jul 2012
    Location
    .NET4.0 / VS2010
    Posts
    5

    Re: Need help with a calculator of sorts

    I created a method, but it's not giving me the right results..
    http://pastebin.com/7Va7R7u9
    If I pass the parameters: 461 wood per hour, 5000 target wood (1800 current wood), it says it will take 154 hours?? That can't be right, when 3200/461=6.9?

    I don't know what I'm doing wrong Isn't there an easier way? I've been looking at this for so long, that I can't even figure out the simplest of tasks..

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured