Author Topic: Hamurabi  (Read 3889 times)

0 Members and 1 Guest are viewing this topic.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Hamurabi
« on: July 22, 2019, 12:09:11 pm »
Ken's Lemonade Stand reminded me of the old classic: Hamurabi
https://en.wikipedia.org/wiki/Hamurabi_(video_game)

Here is a QB64 translation of my SmallBASIC translation of that from someone else translation from...
Code: QB64: [Select]
  1. _TITLE "Hamurabi Classic"
  2. 'Hamuradi.bas for SmallBASIC 0.12.8 [B+=MGA] 2017-01-24   translated to QB64 B+ 2019-07-21
  3. ' translated from Hamuradi.bas for Tinycat BASIC by felixv7
  4. ' whose source to this classic was
  5. ' http://apollowebworks.com/russell/samples/hamurabi.html
  6.  
  7. DIM SHARED land, grain, starved, immigrants, population, rats, planted, yield, price, year, plague, buying, selling, fed
  8. land = 1000
  9. grain = 2800
  10. starved = 10
  11. immigrants = 5
  12. population = 100
  13. rats = 200
  14. planted = 300
  15. yield = INT(RND * 5) + 1
  16. price = INT(RND * 6) + 17
  17. year = 1
  18. plague = 0
  19.  
  20. WHILE population > 0
  21.     update 'report to H
  22.     decisions 'H input
  23.     calcStats 'recalc conditions
  24. PRINT "Hamurabi, I have bad news. Everyone is dead."
  25. PRINT "The city is lost."
  26. PRINT "If only you could try again..."
  27.  
  28. SUB update ()
  29.     CLS
  30.     PRINT "Hamurabi,"
  31.     PRINT
  32.     PRINT "I beg to report that in year "; year
  33.     PRINT "The people who starved to death was "; starved
  34.     PRINT "The people who came to the city was "; immigrants
  35.     IF plague THEN PRINT "The plague killed half of us!!!"
  36.     PRINT "                  The population is now "; population
  37.     PRINT
  38.     PRINT "The bushels we harvested was "; yield * planted
  39.     PRINT "as bushels of grain per acre was "; yield
  40.     PRINT "but bushels destroyed by rats was "; rats
  41.     PRINT "This leaves us with the granary bushels "; grain
  42.     PRINT
  43.     PRINT "The acres of land owned by the city is "; land
  44.     PRINT
  45.     PRINT "The price, bushels per acre, is "; price
  46.     PRINT
  47.  
  48. SUB decisions ()
  49.     WHILE 1
  50.         INPUT "How much land do you wish to buy? "; buying
  51.         buying = INT(ABS(buying))
  52.         IF buying = 0 THEN
  53.             EXIT WHILE
  54.         ELSEIF buying * price <= grain THEN
  55.             land = land + buying
  56.             grain = grain - buying * price
  57.             PRINT "Very well, we are left with "; grain; " bushels."
  58.             EXIT WHILE
  59.         ELSE
  60.             PRINT "But, Hamurabi! We don't have enough grain!"
  61.         END IF
  62.     WEND
  63.  
  64.     WHILE 1
  65.         INPUT "How much land do you wish to sell? "; selling
  66.         selling = INT(ABS(selling))
  67.         IF selling = 0 THEN
  68.             EXIT WHILE
  69.         ELSEIF selling <= land THEN
  70.             land = land - selling
  71.             grain = grain + selling * price
  72.             PRINT "Very well, we now have "; grain; " bushels."
  73.             EXIT WHILE
  74.         ELSE
  75.             PRINT "But, Hamurabi, we only have ", land, " acres!"
  76.         END IF
  77.     WEND
  78.  
  79.     WHILE 1
  80.         INPUT "How much grain should we feed our people? "; fed
  81.         fed = INT(ABS(fed))
  82.         IF fed <= grain THEN
  83.             grain = grain - fed
  84.             EXIT WHILE
  85.         ELSE
  86.             PRINT "But, Hamurabi! We only have ", grain, " bushels!"
  87.         END IF
  88.     WEND
  89.  
  90.     WHILE 1
  91.         INPUT "How many acres of land should we seed? "; planted
  92.         planted = INT(ABS(planted))
  93.         IF planted <= land THEN
  94.             IF planted <= grain * 2 THEN
  95.                 IF planted <= population * 10 THEN
  96.                     EXIT WHILE
  97.                 ELSE
  98.                     PRINT "But, Hamurabi! We only have "; population; " people!"
  99.                 END IF
  100.             ELSE
  101.                 PRINT "But, Hamurabi! We only have "; grain; " bushels!"
  102.             END IF
  103.         ELSE
  104.             PRINT "But, Hamurabi! We only have "; land; " acres!"
  105.         END IF
  106.     WEND
  107.  
  108. SUB calcStats
  109.     DIM immigrants1, immigrants2
  110.     yield = INT(RND * 5) + 1
  111.     grain = grain + yield * planted
  112.     rats = INT(RND * (grain * 0.07))
  113.     grain = grain - rats
  114.     starved = population - INT(fed / 20)
  115.     IF starved < 0 THEN starved = 0
  116.     population = population - starved
  117.     IF population <= 0 THEN EXIT SUB
  118.     immigrants1 = INT(starved / 2)
  119.     immigrants2 = INT((5 - yield) * grain / 600 + 1)
  120.     immigrants = immigrants1 + immigrants2
  121.     IF immigrants > 50 THEN immigrants = 50
  122.     IF immigrants < 0 THEN immigrants = 0
  123.     population = population + immigrants
  124.     plague = RND 'one in 10
  125.     IF plague < .1 THEN plague = -1 ELSE plague = 0
  126.     IF plague THEN population = INT(population / 2)
  127.     price = INT(RND * 6) + 17
  128.     year = year + 1
  129.  

Hamurabi fans, let me know if anything got lost in translation.

EDIT: added title
« Last Edit: July 22, 2019, 12:11:35 pm by bplus »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Hamurabi
« Reply #1 on: July 22, 2019, 12:15:32 pm »
Of course every good king has trusted advisers.

Here is "Hamurabi Helper":
Code: QB64: [Select]
  1. _TITLE "Hamurabi Helper" 'B+ from SmallBASIC trans 2019-07-21
  2. 'Humarabi helper.bas 2017-01-26
  3.  
  4. DIM p, g, a, bpa, seed, da, dg, feed, excess
  5.     PRINT: PRINT "=========================================": PRINT
  6.     INPUT "population "; p
  7.     INPUT "grain "; g
  8.     INPUT "acres "; a
  9.     INPUT "bushel per acre price"; bpa
  10.     IF a < 10 * p THEN
  11.         seed = a
  12.     ELSE
  13.         seed = 10 * p
  14.     END IF
  15.     IF g - seed - 20 * p < 0 THEN
  16.         IF g - seed - 10 * p < 0 THEN
  17.             da = a: dg = g
  18.             WHILE da > 20 * p
  19.                 da = da - 1 'sell acre
  20.                 dg = dg + bpa 'get grain
  21.                 IF dg - p * 20 > 0 THEN EXIT WHILE
  22.             WEND
  23.             PRINT "Sell "; a - da
  24.             PRINT "Feed "; 10 * p
  25.             PRINT "Seed "; 10 * p
  26.         ELSE
  27.             feed = g - seed
  28.             PRINT "No buy or sell"
  29.             PRINT "Feed "; feed
  30.             PRINT "Seed "; seed
  31.         END IF
  32.     ELSE
  33.         feed = 20 * p
  34.         excess = (g - seed - feed) \ bpa
  35.         PRINT "If positive buy else sell? "; excess
  36.         PRINT "Feed "; feed
  37.         PRINT "Seed "; seed
  38.     END IF
  39.  

Should this adviser be trusted?
« Last Edit: July 22, 2019, 12:16:45 pm by bplus »

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Hamurabi
« Reply #2 on: July 22, 2019, 04:28:37 pm »
I find it fantastic!
Thanks to share Bplus
Programming isn't difficult, only it's  consuming time and coffee

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: Hamurabi
« Reply #3 on: July 23, 2019, 08:27:10 am »
Cool!
if (Me.success) {Me.improve()} else {Me.tryAgain()}


My Projects - https://github.com/AshishKingdom?tab=repositories
OpenGL tutorials - https://ashishkingdom.github.io/OpenGL-Tutorials

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Hamurabi
« Reply #4 on: August 05, 2019, 07:22:10 pm »
Just a little mod, but I have an enthusiastic idea to put in field!
As I get some time to spend.
For now there is a spashscreen, a colirized text output,  a playagain function coded by using IF GOTO label
Code: QB64: [Select]
  1. _TITLE "Hamurabi Classic"
  2. 'Hamuradi.bas for SmallBASIC 0.12.8 [B+=MGA] 2017-01-24   translated to QB64 B+ 2019-07-21
  3. ' translated from Hamuradi.bas for Tinycat BASIC by felixv7
  4. ' whose source to this classic was
  5. ' http://apollowebworks.com/russell/samples/hamurabi.html
  6.  
  7. DIM SHARED land, grain, starved, immigrants, population, rats, planted, yield, price, year, plague, buying, selling, fed
  8. start:
  9. SplashScreen
  10. Init
  11. WHILE population > 0
  12.     update 'report to H
  13.     decisions 'H input
  14.     calcStats 'recalc conditions
  15. PRINT "Hamurabi, I have bad news. Everyone is dead."
  16. PRINT "100The city is lost."
  17. PRINT "If only you could try again..."
  18. COLOR 14, 1
  19. PRINT "But do you want try again? Y/N";
  20. COLOR 7, 0
  21. DIM ans AS STRING * 1
  22. WHILE NOT ans = "N"
  23.     ans = UCASE$(INKEY$)
  24.     IF ans = "Y" GOTO start
  25.  
  26. SUB Init
  27.     land = 1000
  28.     grain = 2800
  29.     starved = 10
  30.     immigrants = 5
  31.     population = 100
  32.     rats = 200
  33.     planted = 300
  34.     yield = INT(RND * 5) + 1
  35.     price = INT(RND * 6) + 17
  36.     year = 1
  37.     plague = 0
  38.  
  39. SUB update ()
  40.     CLS
  41.     COLOR 3, 0
  42.     PRINT "Hamurabi,"
  43.     PRINT
  44.     PRINT "I beg to report that in year "; year
  45.     PRINT "The people who starved to death was "; starved
  46.     PRINT "The people who came to the city was "; immigrants
  47.     IF plague THEN PRINT "The plague killed half of us!!!"
  48.     PRINT "                  The population is now "; population
  49.     PRINT
  50.     PRINT "The bushels we harvested was "; yield * planted
  51.     PRINT "as bushels of grain per acre was "; yield
  52.     PRINT "but bushels destroyed by rats was "; rats
  53.     PRINT "This leaves us with the granary bushels "; grain
  54.     PRINT
  55.     PRINT "The acres of land owned by the city is "; land
  56.     PRINT
  57.     PRINT "The price, bushels per acre, is "; price
  58.     PRINT
  59.     COLOR 7, 0
  60.  
  61. SUB decisions ()
  62.     WHILE 1
  63.         INPUT "How much land do you wish to buy? "; buying
  64.         buying = INT(ABS(buying))
  65.         IF buying = 0 THEN
  66.             EXIT WHILE
  67.         ELSEIF buying * price <= grain THEN
  68.             land = land + buying
  69.             grain = grain - buying * price
  70.             PRINT "Very well, we are left with "; grain; " bushels."
  71.             EXIT WHILE
  72.         ELSE
  73.             PRINT "But, Hamurabi! We don't have enough grain!"
  74.         END IF
  75.     WEND
  76.  
  77.     WHILE 1
  78.         INPUT "How much land do you wish to sell? "; selling
  79.         selling = INT(ABS(selling))
  80.         IF selling = 0 THEN
  81.             EXIT WHILE
  82.         ELSEIF selling <= land THEN
  83.             land = land - selling
  84.             grain = grain + selling * price
  85.             PRINT "Very well, we now have "; grain; " bushels."
  86.             EXIT WHILE
  87.         ELSE
  88.             PRINT "But, Hamurabi, we only have ", land, " acres!"
  89.         END IF
  90.     WEND
  91.  
  92.     WHILE 1
  93.         INPUT "How much grain should we feed our people? "; fed
  94.         fed = INT(ABS(fed))
  95.         IF fed <= grain THEN
  96.             grain = grain - fed
  97.             EXIT WHILE
  98.         ELSE
  99.             PRINT "But, Hamurabi! We only have ", grain, " bushels!"
  100.         END IF
  101.     WEND
  102.  
  103.     WHILE 1
  104.         INPUT "How many acres of land should we seed? "; planted
  105.         planted = INT(ABS(planted))
  106.         IF planted <= land THEN
  107.             IF planted <= grain * 2 THEN
  108.                 IF planted <= population * 10 THEN
  109.                     EXIT WHILE
  110.                 ELSE
  111.                     PRINT "But, Hamurabi! We only have "; population; " people!"
  112.                 END IF
  113.             ELSE
  114.                 PRINT "But, Hamurabi! We only have "; grain; " bushels!"
  115.             END IF
  116.         ELSE
  117.             PRINT "But, Hamurabi! We only have "; land; " acres!"
  118.         END IF
  119.     WEND
  120.  
  121. SUB calcStats
  122.     DIM immigrants1, immigrants2
  123.     yield = INT(RND * 5) + 1
  124.     grain = grain + yield * planted
  125.     rats = INT(RND * (grain * 0.07))
  126.     grain = grain - rats
  127.     starved = population - INT(fed / 20)
  128.     IF starved < 0 THEN starved = 0
  129.     population = population - starved
  130.     IF population <= 0 THEN EXIT SUB
  131.     immigrants1 = INT(starved / 2)
  132.     immigrants2 = INT((5 - yield) * grain / 600 + 1)
  133.     immigrants = immigrants1 + immigrants2
  134.     IF immigrants > 50 THEN immigrants = 50
  135.     IF immigrants < 0 THEN immigrants = 0
  136.     population = population + immigrants
  137.     plague = RND 'one in 10
  138.     IF plague < .1 THEN plague = -1 ELSE plague = 0
  139.     IF plague THEN population = INT(population / 2)
  140.     price = INT(RND * 6) + 17
  141.     year = year + 1
  142.  
  143.  
  144. SUB SplashScreen
  145.     COLOR 14, 0
  146.     CLS
  147.     LOCATE 3, 12: PRINT "Û   Û " + " ÛÛÛ  " + "Û   Û " + "Û   Û " + "Û   Û " + "ÛÛÛÛ  " + " ÛÛÛ  " + "ÛÛÛÛ  " + " ÛÛÛ  "
  148.     LOCATE , 12: PRINT "Û   Û " + "Û   Û " + "ÛÛ ÛÛ " + "ÛÛ ÛÛ " + "Û   Û " + "Û   Û " + "Û   Û " + "Û   Û " + "  Û   "
  149.     LOCATE , 12: PRINT "ÛÛÛÛÛ " + "ÛÛÛÛÛ " + "Û Û Û " + "Û Û Û " + "Û   Û " + "ÛÛÛÛ  " + "ÛÛÛÛÛ " + "ÛÛÛÛ  " + "  Û   "
  150.     LOCATE , 12: PRINT "Û   Û " + "Û   Û " + "Û   Û " + "Û   Û " + "Û   Û " + "Û   Û " + "Û   Û " + "Û   Û " + "  Û   "
  151.     LOCATE , 12: PRINT "Û   Û " + "Û   Û " + "Û   Û " + "Û   Û " + "Û   Û " + "Û   Û " + "Û   Û " + "Û   Û " + "  Û   "
  152.     LOCATE , 12: PRINT "Û   Û " + "Û   Û " + "Û   Û " + "Û   Û " + "Û   Û " + "Û   Û " + "Û   Û " + "Û   Û " + "  Û   "
  153.     LOCATE , 12: PRINT "Û   Û " + "Û   Û " + "Û   Û " + "Û   Û " + " ÛÛÛ  " + "Û   Û " + "Û   Û " + "ÛÛÛÛ  " + " ÛÛÛ  "
  154.     COLOR 7, 0
Programming isn't difficult, only it's  consuming time and coffee