Simple math problem

This is probably very simple but for some reason I can’t figure it out.
Trying to make my first game, the easiest I could think of, oldschool drugwars.

So I have maxInventory, cash, price.
How can I calculate the max amount of items I can purchase?

Normally to the inventory is a List or something so you can just check if it has any space left. Depends on how you’re managing inventory size and capacity.

Once you know if there is room just check if there is enough cash and subtract the price from cash.

I need to check both how many I can afford and if there is enough space but let me fill in the banks. maybe it will be easier to understand.

cash=100;
currentInventory=2;
maxInventory=10;
price=10;

Okay so now if I hit my “Buy Max” button I want to buy as many items I can afford whils still not overfilling my maxInventory. Do you have an idea :slight_smile: I feel so stupid, this should be a very simple math problem but I’m drawing a blank.

can afford = cash / price
max without overshooting = maxInv - current Inventory.
:slight_smile:

That’s how far I got.
maxAfford = cash/price
maxSlots = maxInventory-currentInventory.

But…i still dont get it. what’s my end line here.

well, you’re basically there.
if you go to buy, check that MaxCanBuy > 0
then, if maxAfford >= MaxCanBuy bought MaxCanBuy, otherwise bought maxAfford.

what value does MaxCanBuy have then?

Sorry for being so thick.

I think I got it. Hopefully. Thx man. I’ll bother you again if I got it wrong. Gotta go make dinner for the kids.

Just clamp the value of the amount they can buy

        //Amount you can afford to buy
        var afford = cash / price; 
        //Clamp to the max amount of avaiable slots
        var maxCanBuy = Mathf.Clamp(afford, 0, maxInventory - currentInventory);
        //Final cost
        var totalCost = maxCanBuy * price;

if (maxAfford > 0)
{
cash -= maxSlots*price;
currentInventory += maxSlots;
}

that is about it huh?

MaxCanBuy was the same as your ‘maxSlots’

Looks good.

Thx folks.

No prob. take it easy :slight_smile:

Wait. What I had is wrong still.

inventoryLeft = maxInventory - cocaineInventory;
maxAfford = cash / cocainePrice;

if (maxAfford > 0)
{
cash -= maxAfford*cocainePrice;
cash -= cash/cocainePrice;
}

hehe :slight_smile: I messed it up. This script will be able to purchase more than cash value.

nevermind. solved it. By myself this time. hurray.

Hmm? :slight_smile:
That doesn’t look right.

int availSpace = maxInventory - currentInventory;
int canAfford = cash / price;
if(canAfford > 0 && availSpace > 0) {
    int bought = 0;
    if(canAfford >= availSpace) bought = availSpace;
    else bought = canAfford;
    inventorySpaces += bought;
    cash -= bought * price;
   }

Something like that.

You responded as I was writing this out. You could post your updated/correct solution if you want. :slight_smile:
Glad you solved it…

Oh, yes of course. close the thread or whatever. Thank you.

There is no thread closing (barring some horrible rule breaking). :slight_smile: They just stay here for future readers (and writers).