Jump to content

[SOLVED] Prob Simple.. How to work out farmers on farms.


AbydosGater

Recommended Posts

Hi, Ive been doing some work on a basic game.. and it is all working out well. Except for this bit.

 

Every player gets a planet.. and on these planets the can have farmers and farms.

 

So lets say $farms is the number of farms and $farmers is the number of farmers.

If you want to use X as farms and Y as farmers thats ok :).

 

Each farm can man 1 and only 1 farmer.. so every farm gets one farmer.. How would i go about working out how many farms have a farmer and how many farms dont have farmers?

 

Wow i just realized how confusing that all sounds.

Just for example .. lets say there are 10 farms.. and 8 farmers.. straight off we know that there will be 8 manned farms and 2 unmanned farms.

 

But how could i go about doing this inside a script with different numbers for each user?

 

Thank you

 

Andy

Link to comment
Share on other sites

this can be done many ways depending on your application and preference, here are 2 ideas. the first generates 2 global variables $manned, and $unmanned set to their respective values after calling the function. the second echos the numbers. the third returns an array containing "manned" and "unmanned" and their respective integers. this should give you some ideas.

 

function farmers_calculate($farms, $farmers){

  global $unmanned, $manned;

  $unmanned = $farms - $farmers;
  $manned = $farms - $unmanned;

}

farmers_calculate(10,;

echo "$unmanned<br>";
echo "$manned<br>";

 

function farmers_calculate($farms, $farmers){

  $unmanned = $farms - $farmers;
  $manned = $farms - $unmanned;

  echo "There are $manned manned farm(s) and $unmanned unmanned farm(s)";

}

farmers_calculate(10,;

 

function farmers_calculate($farms, $farmers){


  $numbers['unmanned'] = $farms - $farmers;
  $numbers['manned'] = $farms - $numbers['unmanned'];

  return $numbers;

}

$numbers = farmers_calculate(10,;

Link to comment
Share on other sites

function farmers_calculate($farms, $farmers){

  global $unmanned, $manned;

  $unmanned = $farms - $farmers;
  $manned = $farms - $unmanned;

}

farmers_calculate(10,;

echo "$unmanned<br>";
echo "$manned<br>";

 

You shouldn't use globals like that. It'll make your code less reusable. I'd probably go with the last example.

Link to comment
Share on other sites

function farmers_calculate($farms, $farmers){

  global $unmanned, $manned;

  $unmanned = $farms - $farmers;
  $manned = $farms - $unmanned;

}

farmers_calculate(10,;

echo "$unmanned<br>";
echo "$manned<br>";

 

You shouldn't use globals like that. It'll make your code less reusable. I'd probably go with the last example.

 

A better alternative to globals could be variables passed by reference....

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.