Summertime, and the lemonade is easy

Date:June 21, 2017 / year-entry #143
Tags:other
Orig Link:https://blogs.msdn.microsoft.com/oldnewthing/20170621-00/?p=96436
Comments:    23
Summary:Unearthing an old game from my youth.

Back in my youth, my brother and I played many games of Lemonade Stand, a simple BASIC game that put you in charge of a virtual lemonade stand. You had to decide, based on the weather report and the cost of materials, how much lemonade to make, how many advertising signs to put up, and how much to charge. We had settled upon our idea of what an optimal configuration was, but I always wondered whether we had actually found it. (You too can play the game in an emulator and see how well you do.)

No fear, the game has been ported to Windows and Mac OS X. That web site also includes the original source code, so you can port it to JavaScript if you feel so motivated. As for me, I extracted the formulas and did my own calculations.

Here are the formulas. All money amounts are in cents.

Nominal demand is in two parts.

If price ≤ 10  then  nominal demand = 30 + 24 × (10 − price) / 10
If price ≥ 10  then  nominal demand = 30 × (10 / price

The marginal benefit of advertising decays exponentially.

advertising boost = 1 − ½ exp(−signs)

And the environment effect is as follows:

If hot  then  environment effect = 2.0
If sunny  then  environment effect = 1.0
If there is construction on the street  then  environment effect = 0.1
If cloudy  then  environment effect = 1.0 − probability of rain
If it rains  then  environment effect = 0.0

The total demand is the product of the factors.

total demand = nominal demand × (1 + advertising boost) × environment effect

with an exception noted below.

Based on these formulas, you can calculate the optimal inputs for each weather report. If it's cloudy, you need to take the probability of rain into account, because if rain actually falls, then nobody will buy any lemonade.

When we played this game as kids, we found the optimal values by a simple gradient descent. Since the demand graph is convex for non-cloudy days, gradient descent does find the optimal values. For cloudy days, we just flailed around.

The exception to the demand formula is that if there is construction on the street, then there is a 50% chance that the street crews will buy all your lemonade. We figured out that the way to make a killing was to jack up the price of lemonade as high as the program will let you ($1.00), spend nothing on advertising, and make a ton of lemonade. If you're lucky, then the street crews will buy all your lemonade at full price, and you are suddenly rolling in money.

We didn't realize it at the time, but when we played the game, it was basically just gambling on construction workers.

Bonus chatter: Some time ago, my daughter and her friend made signs for their lemonade stand. The first sign:

Lemonade $1.44

The second sign:

Pay extra get real lemonade


Comments (23)
  1. GL says:

    May I know what I would got if I paid only $1.44?

    1. parkrrrr says:

      Whatever you get, it’s definitely gross.

      1. Karellen says:

        Well, you definitely score internet points with that joke!

    2. Brian says:

      Crystal Light?

  2. Matt says:

    Ah, so that’s what game the bug porn section of Frog Fractions is based on.

  3. Ted Spence says:

    The version I played also had another factor: temperature forecast. It had some other logic that would scale demand based on the ambient temperature, which would vary from 60 degrees to about 105 degrees in American units.

    I recall my delight as a young hacker when I discovered how to modify the algorithm and I gradually got the weather system to repeatedly forecast temperatures as high as 32,000 degrees, which led to brisk demand for lemonade.

    1. cheong00 says:

      What? I don’t know [strike]charcoal[/strike] ash drinks lemonade. :O

  4. Pierre B. says:

    Well, the maxima is at 10 cents regardless of advertising or climate (unless it rains or there is construction, in each case the optima is the extreme), so I don’t see the interest of the game.

    1. pc says:

      Well, no, it’s not interesting once you’ve solved it. The fun was seeing how much you could get away with, and guessing whether the rain would hold off.

      I remember playing this on my Apple 2c as a child. I had thought that the algorithm behind how it figured out how many customers you got involved more randomness, but perhaps it didn’t. Thanks for the nostalgia trip.

    2. Antonio Rodríguez says:

      I played a lot with the Spanish version of it, called simply “Limonada”, also in my Apple //c (which I still own). I remember that I found (by trial and error) that the optimal price was around 10-12 cents. And in the later times, I had a lot of experience programming in Applesoft BASIC (I even wrote my own word processor, which I used to write all my essays at high school), but never dared to read Lemonade’s source code, not wanting to spoil the magic in it :-) .

    3. Scarlet Manuka says:

      How do you get that? From my figures you should never charge less than 10c, but if the cost of materials rises above 5c you should charge twice the cost. Also, there’s the number of signs to buy and the number of glasses to make to consider.

  5. That made my think of my old 1.44 MB floppy disks.

    1. Antonio Rodríguez says:

      “Old”? 1.44 MB? It took years to get there! The first version of Lemonade Stand was released in cassette tape (that was before the Apple II got a disk drive in 1978!). And after that, it was distributed in single-sided 5.25″ disks – with a whopping capacity of 140 KB! Now it would be difficult to fit a single *icon* in one of these napkin-sized disks. Those were the times :-) .

  6. cheong00 says:

    [Pay extra get real lemonade]

    Priceless.

  7. Patrick says:

    I used to run a dual-node PCBoard BBS out of my closet when I was 13.

    I couldn’t port it to JavaScript in those days … too busy flipping DIP switches, IRQ jumpers, and still messing around with the physics in GORRILA.BAS for fun.

    Now I deal with too many different things … times were simpler then.

    Great memories, thanks for the post!

  8. The BASIC game that I played when I was a child was QBASIC Gorillas, distributed with MS-DOS 5. It is an artillery game where the player has to input an angle (in degrees) and a velocity for throwing a banana at another gorilla. I am surprised how I did not lose interest in the game after discovering that the 60-70 combination almost always scored a hit.

    I remember having improved it. I moved to the “SCREEN 12” resolution (640×480, 16 colors?), replaced the “INPUT”-based reading of numbers with a dialer (involved complicated coding), greatly changed the variation of structures, and added a collision detection system that ensured the banana could not teleport past structures if the velocity was high enough. Performance was not an issue because I had an Intel 386DX!

    1. Richard says:

      I recall moving the gorillas along X in order to randomise the winning move.

      And also being amused by making the bang much, much bigger – so the only winning move was not to play…

      Unfortunately there are very few games like that these days. I also worry whether the youth of today will still really get into programming without the introduction of simple text and 2D games that ship with their source and compiler/interpreter.

      Minecraft educational edition is interesting, but is it too complex?

      1. Martin says:

        Never fear. While not the same as tweaking BASIC programs from our childhood, there are quite a few very accessible inroads for today’s generation. It is really much easier to get into now than back then. My kids started playing with Scratch at 4, 6, and 8 years of age (was introduced to them by their Great Aunt.) and have moved on to more traditional languages. It seems for them and a most of their friends, programming is just another thing they do.

    2. BZ says:

      Our high school had (I believe) an 8088 (along with a Mac Plus) even though this was in the late 486 / early Pentium days. We played gorilla sometimes, but the go-to game was Nibbles (a snake clone). Someone more clever than I managed to add a third player (the game was two player out of the box, one player controlled by arrow keys and one by the numeric keypad) controlled by letter keys (SDFC or something similar). My only contribution was to add a level with randomly generated obstacles. Of course, that made it unwinnable at times.

      It was a Jewish school so we gave the snake characters silly Jewish names and replaced all references to dying with being “out”.

      Then we got 486s and everybody moved on to playingg X-Men

      1. Oh, I wanted to start improving Nibbles too after I was done with Gorillas. But I didn’t have infinite time. Age and educational mandates caught up with me. So, I had to drop QBASIC and go on to learn Pascal, and later Object Pascal (Delphi).

  9. Dave Harris says:

    There’s a cartoon, probably Peanuts, in which one child is selling with a sign:

    Lemonade: $1.00

    and the other puts up a sign:

    Lemonade: $1.00
    In a clean glass: $1.50

Comments are closed.


*DISCLAIMER: I DO NOT OWN THIS CONTENT. If you are the owner and would like it removed, please contact me. The content herein is an archived reproduction of entries from Raymond Chen's "Old New Thing" Blog (most recent link is here). It may have slight formatting modifications for consistency and to improve readability.

WHY DID I DUPLICATE THIS CONTENT HERE? Let me first say this site has never had anything to sell and has never shown ads of any kind. I have nothing monetarily to gain by duplicating content here. Because I had made my own local copy of this content throughout the years, for ease of using tools like grep, I decided to put it online after I discovered some of the original content previously and publicly available, had disappeared approximately early to mid 2019. At the same time, I present the content in an easily accessible theme-agnostic way.

The information provided by Raymond's blog is, for all practical purposes, more authoritative on Windows Development than Microsoft's own MSDN documentation and should be considered supplemental reading to that documentation. The wealth of missing details provided by this blog that Microsoft could not or did not document about Windows over the years is vital enough, many would agree an online "backup" of these details is a necessary endeavor. Specifics include:

<-- Back to Old New Thing Archive Index