Author Topic: Lemonade Stand 2  (Read 5496 times)

0 Members and 1 Guest are viewing this topic.

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Lemonade Stand 2
« on: September 01, 2020, 06:08:32 pm »
A year ago I posted on here a Lemonade Stand game simulation that I made back in the 1990's. It has over 200 lines of code and I never liked the in-code sequence it uses for each turn. So today I decided to make Lemonade Stand 2. This one is very similar to the last one, but less than 100 lines of code and a bit easier to play I believe. This one also uses a DO/LOOP on the coding so any programmer could figure out the flow of commands. It uses a lot of RND statements but don't let that throw you off, I just wanted to make it as real as I could. I'll post the URL to last years's here too in case you want to have both. Tell me what you think. Enjoy!

Here is today's:

Code: QB64: [Select]
  1. _TITLE "Lemonade Stand 2"
  2. SCREEN _NEWIMAGE(800, 600, 32)
  3. begin:
  4. PRINT "                                        Lemonade Stand 2"
  5. PRINT "                                         by  SierraKen"
  6. PRINT "You start with $20."
  7. PRINT "There are 5 cups per pitcher."
  8. PRINT "There are 30 cups per package of cups."
  9. PRINT "There will be weather, donations, and advertising."
  10. PRINT "Cup packages and pitchers can only be used for one day."
  11. INPUT "                                    Press Enter to begin.", a$
  12. money = 20
  13. ads = 0
  14. day = 0
  15.     day = day + 1
  16.     LOCATE 1, 1: PRINT "Day: "; day
  17.     weather = INT(RND * 8) + 1
  18.     IF weather = 1 THEN w$ = "Hot"
  19.     IF weather = 2 THEN w$ = "Sunny"
  20.     IF weather = 3 THEN w$ = "Cloudy"
  21.     IF weather = 4 THEN w$ = "Windy"
  22.     IF weather = 5 THEN w$ = "Rainy"
  23.     IF weather = 6 THEN w$ = "Cold"
  24.     IF weather = 7 THEN w$ = "Snowy"
  25.     IF weather = 8 THEN w$ = "Hail"
  26.     LOCATE 2, 1: PRINT "Weather: "; w$
  27.     pitcher = INT(5 / weather) + 2
  28.     LOCATE 4, 1: PRINT "Cost Of One Pitcher Of Lemonade Today: $"; pitcher
  29.     cups = INT(RND * 7) + 2
  30.     LOCATE 5, 1: PRINT "Cost Of One Package Of Cups Today: $"; cups
  31.     LOCATE 6, 1: INPUT "Price Of Each Glass $", price
  32.     sales1 = INT((INT(RND * 92) + 8) / weather)
  33.     donations = INT(RND * 4) + 1
  34.     IF donations = 2 THEN
  35.         donatedmoney = INT(RND * 20) + 1
  36.         money = money + donatedmoney
  37.     END IF
  38.     LOCATE 7, 1: INPUT "Amount to spend on advertising: $", ads
  39.     sales2 = (INT(RND * sales1) / price) + sales1
  40.     sales3 = INT(RND * (ads * 2)) + 3
  41.     sales4 = INT(sales2 + sales3)
  42.     sales5 = sales4 * price
  43.     LOCATE 9, 1: PRINT "Cups Of Lemonade Sold: "; sales4; " glasses today."
  44.     pitchersused = INT(sales4 / 5) + 1
  45.     LOCATE 10, 1: PRINT "Pitchers Used"; pitchersused
  46.     pitchersold = pitchersused * pitcher
  47.     cupsold = INT(sales4 / 30) + 1
  48.     LOCATE 11, 1: PRINT "Cup Packages Used:"; cupsold
  49.     LOCATE 12, 1: PRINT "You have made $"; sales5 - pitchersold
  50.     money = money + sales5 - ads
  51.     money = money - pitchersold
  52.     money = money - (cupsold * cups)
  53.     LOCATE 13, 1: PRINT "Donations: "; donatedmoney
  54.     LOCATE 14, 1: PRINT "Total Income: $"; money
  55.     IF money = 0 OR money < 0 THEN GOTO done:
  56.     LOCATE 16, 1: PRINT "Do you want to sell tomorrow (Y/N)?"
  57.     yesno:
  58.     yn$ = INKEY$
  59.     IF yn$ = "Y" OR yn$ = "y" THEN GOTO more:
  60.     IF yn$ = "N" OR yn$ = "n" THEN GOTO done:
  61.     GOTO yesno:
  62.     more:
  63.     CLS
  64. done:
  65. PRINT "Your Total Days: "; day
  66. PRINT "Your Total Money: "; money
  67. PRINT "Do you want to play again (Y/N)"
  68. again:
  69. again$ = INKEY$
  70. IF again$ = "y" OR again$ = "Y" THEN GOTO begin:
  71. IF again$ = "n" OR again$ = "N" THEN END
  72. GOTO again:
  73.  

Here is last year's: https://www.qb64.org/forum/index.php?topic=1523.msg107319#msg107319
« Last Edit: September 01, 2020, 06:18:10 pm by SierraKen »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Lemonade Stand 2
« Reply #1 on: September 01, 2020, 06:19:41 pm »
Hi @SierraKen

Wow nice reduction of Lines Of Code, did you have to sacrifice any of it functions?

I remember that was a pretty cute game.

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Lemonade Stand 2
« Reply #2 on: September 01, 2020, 07:09:01 pm »
It's funny B+, I made this today without even trying out my old version and I probably did that many months ago. So I just tried it out and the old one has 2 steps per turn. One step to buy the lemonade (or advertisements, etc from the menu) and the next one to sell it. Today's version only has 1 step per turn and you automatically buy as much lemonade and cups as you need to sell. So they are very different, but also very similar. I like today's better because it's more simple and basic. Today's one is also more realistic I believe because I added cups to buy and it also uses random weather in which the lemonade pitchers you buy cost more when it's hotter outside. But also you can raise the price and get more people on hotter days. :) But it's also random, just like the real world. Oh, I also tossed in random donations from people. LOL
« Last Edit: September 01, 2020, 07:13:24 pm by SierraKen »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Lemonade Stand 2
« Reply #3 on: September 01, 2020, 10:17:47 pm »
Yeah! It's funny I did a similar thing with DANILIN's "rebus". I wrote the program writer from scratch after seeing DANILIN's example and trying about 3 by hand. I kept saying to myself, "dang this is familiar.", but it was like from 3 years ago or more. I dug around for the code but didnt find anything except stuff similar but not quite same. Then I dug really deep and found the old code and now I have a much better idea to do the inputs for the Program Writer, my next project for tonight.

It's just funny how deeply this stuff gets set in your brain.

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Lemonade Stand 2
« Reply #4 on: September 01, 2020, 10:42:36 pm »
Yep B+. That's one of the best reasons I like programming, because it's factual, black and white. It's teaching our minds logical steps to create some awesome things. And those things build on top of older things and it goes from there. That is why we remember it.

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: Lemonade Stand 2
« Reply #5 on: September 02, 2020, 12:09:08 am »
You had to use the word, "remember".

This is completely off topic... but I thought it was funny...

An elderly couple were have a get-together with a bunch of friends. Having a good time and remembering past experiences.

One friend asked the old guy, "How have you been and what have you been doing with yourself?"

He replied, "Getting on just fine. I've started some classes on how to improve the memory."

"That's great! Which school are you going to?"

A few um's and ah's and the old guy mutters to himself, "Flower... Long stem... um... Thorns..."

His friend interrupts, "You mean a rose?"

"That's it!", says the old guy and turns to his wife... "Hey, Rose! What's the name of that school I'm attending?"
Logic is the beginning of wisdom.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Lemonade Stand 2
« Reply #6 on: September 02, 2020, 02:38:51 am »
I hear that's what happens when you eat swiss cheese you get holes in your memory. :-))

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Lemonade Stand 2
« Reply #7 on: September 02, 2020, 03:40:09 am »
@SierraKen

It seems you are NOT deducting the cost of the cups when calculating net for day.

You know what the real big pain of the Lemonade business is predicting how many pitchers you will need.
Order too many and have a slow day or order a few and be sold out before noon! Yikes

Wait we don't have to pre-order or make up the pitchers before going to the stand???

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Lemonade Stand 2
« Reply #8 on: September 02, 2020, 01:03:38 pm »
Here is a tip to save more lines of code (LOC).

Code: QB64: [Select]
  1. _TITLE "Choose from multiple items with 2 lines of code" ' b+ 2020-09-02
  2.  
  3. weather = INT(RND * 8) + 1
  4. IF weather = 1 THEN w$ = "Hot"
  5. IF weather = 2 THEN w$ = "Sunny"
  6. IF weather = 3 THEN w$ = "Cloudy"
  7. IF weather = 4 THEN w$ = "Windy"
  8. IF weather = 5 THEN w$ = "Rainy"
  9. IF weather = 6 THEN w$ = "Cold"
  10. IF weather = 7 THEN w$ = "Snowy"
  11. IF weather = 8 THEN w$ = "Hail"
  12.  
  13.  
  14. '                    Get random choice of multiple items in 2 lines of code!
  15.  
  16. 'Setup: 11 items seperate them by equal amount, this is one more than longest 6 chars so can read items
  17. '           12345671234567123456712345671234567123456712345671234567123456712345671234567
  18. weather$ = "Hot    Sunny  Cloudy Windy  Rainy  Stormy Cold   Snowy  Hail   Sleet  Foggy"
  19.  
  20.  
  21. FOR i = 1 TO 20 '    demo random choice
  22.  
  23.     ' In the Run, call for an random item  MID$ gets the section in the string, _TRIM$ removes spaces
  24.  
  25.     '                          length      * items    length
  26.     w$ = _TRIM$(MID$(weather$, 7 * INT(RND * 11) + 1, 7)) '<< dial weather report
  27.  
  28.     PRINT "Weather forecast: "; w$
  29.  
  30.  

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Lemonade Stand 2
« Reply #9 on: September 02, 2020, 01:08:48 pm »
B+, it deducts the price of the cups with line 60:  money = money - (cupsold * cups)

Yeah, I think I will work on this app a bit more soon to make a second step like my old Lemonade Stand did where you have to choose how much lemonade and cups to buy before you sell them. In this one I just deducted the cost based on how many you sell. But you are right, it's part of a business to keep inventory flowing. I'll add that soon, thanks for the tip!

Wow, that's a pretty cool trick for RND and text using MID$. I'll put that on a Notepad and try to use that instead on the next upgrade.

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Lemonade Stand 2
« Reply #10 on: September 02, 2020, 01:13:37 pm »
Oh wait, I can't use that weather trick because I need the weather numbers to add to the equations later on with less and more lemonade being sold and also the price of lemonade goes higher when it's hotter outside. And if I use both, it would actually be more code than what I have now.
« Last Edit: September 02, 2020, 01:15:59 pm by SierraKen »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Lemonade Stand 2
« Reply #11 on: September 02, 2020, 01:25:23 pm »
Oh wait, I can't use that weather trick because I need the weather numbers to add to the equations later on with less and more lemonade being sold and also the price of lemonade goes higher when it's hotter outside.

So do the random number outside the string lookup:

weatherNum = Int(Rnd * nItems) ' this will be 0 to (nItems - 1)    '  = nItems random weather conditions
w$ = _TRIM$(MID$(weather$, 7 * weatherNum +1 , 7))  ' for weather name string

The first MID$ item has to start at 1 but 7 * 0 = 0 so add 1 in MID$ call.

Oh boy, that is getting confusing I bet!  :)


Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Lemonade Stand 2
« Reply #12 on: September 02, 2020, 01:35:13 pm »
OK I am looking at my net Sales for the day and there the price of cups has not been deducted.

You should take gross sales = cups sold * price

then subtract expenses:

- whole pitchers dipped into * cost (no partial credit for unused part of pitcher)
This is where having a pitcher count at start of day helps you have you cost calc'd, you don't have to figure if cups sold mod 5 = 0 then pitcher cost = cups/5 * cost of pitcher but if cups mod 5 <> 0 then total pitcher cost =( int(cups/5) + 1) * pitcher$ (if you started a pitcher you used the whole thing)

- cups for whole packs used to cover pitchers made I assume, since cup packs cant be used next day according to your rules. Me, I'd go 5 cents per cup sold because you can use leftover cups the next day.


- ads  I think ad expense is spead out over several days since ads have to be placed well before time of sales.
====                         You could have all expenses calc'd before you sell 1 cup of lemonade!

net sales for day

and for money: money = money + netsales

You seem to be making 2 sets of calculations.

Hey do I sound like I managed a restuarant? ;-))

« Last Edit: September 02, 2020, 01:47:26 pm by bplus »

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Lemonade Stand 2
« Reply #13 on: September 02, 2020, 02:08:07 pm »
Edit: Skip this and the next post, I finally figured it out. lol

cupsold is a package of 30. cups is the price of the package so it times it together to get the price overall of how many packages you had to buy. Then it deducts that from your total amount from the money variable. money = money - (cupsold * cups)

It works differently than buying the lemonade in which one pitcher costs this:  pitcher = INT(5 / weather) + 2
Then pitchers used is this: pitchersused = INT(sales4 / 5) + 1
So pitcher price total old is this: pitchersold = pitchersused * pitcher
 money = money - pitchersold
« Last Edit: September 02, 2020, 02:34:30 pm by SierraKen »

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Lemonade Stand 2
« Reply #14 on: September 02, 2020, 02:09:38 pm »
But I'm going to re-work some of this later on and make it so you can first buy the cups and lemonade before you sell it. So don't worry about it. :) I got a lot on my mind today. Thanks though!