Author Topic: How I convert VERY VERY OLD spaghetti code (example included)  (Read 4594 times)

0 Members and 1 Guest are viewing this topic.

Offline xra7en

  • Seasoned Forum Regular
  • Posts: 284
    • View Profile
So I have a love for old PC games. Maybe because it grew up with them and it is also how I learned to program. While the modern games are very nice with all the eye candy, there is just something about having to type activity in your pc to progress a game that just works for me.

This month I am taking on a larger conversion that I ever have when it comes to VERY old code. Some things to note about the exerpt code below, is that my PERSONAL theory on why it is such a mess, is because back in the 70's (and before) there really was not much of a way to distro your programs. You were really just punching in these games SO YOU COULD play. And as such, the devs at the time, (including yourself when you start programing) wanted to make sure that you could not figure out the game while you were typing it in.
Secondly, many computers back then were limited to 1 or 2 character variables. Me personally, I learned with a TIMEX SINCLAIR (2K RAM non MB OR GB 2,000 characters was it!!!) Then I went to the tandy deskmate (One of the FIRST windows - I think GEO WORKS CAME UP NEXT), THEN bypassed the comodore and went with the COLOR COMPUTER I,II, III . OK I digress


Code: QB64: [Select]
  1. 3720 IF H2 < 9 THEN 4030
  2. 3730 IF H2 < 11 THEN 3870
  3. 3740 IF H2 < 13.9 THEN 4670
  4. 3750 IF H1 > 10.9 THEN 4780
  5. 3760 IF H1 > 8.9 THEN 3820
  6.  
  7. '// FROM LINE # 4020 ALSO
  8. 3770 IF R < 200 * RND THEN 4830
  9. 3780 GOSUB 4880
  10. 3790 R = ABS(R + Y)
  11. 3800 IF R > 5000 THEN 4780
  12. 3810 GOTO 1380 ' *** M A I N  L O O P
  13.  
  14. 3820 IF RND < .5 THEN 3780
  15. 3830 GOSUB 4940
  16. 3840 R = ABS(R - Y)
  17. 3850 IF R > 5000 THEN 4780
  18. 3860 GOTO 1380 ' *** M A I N  L O O P
  19.  
  20.  
  21. 3870 IF H1 < 7 THEN 4000
  22. 3880 IF H1 < 9 THEN 3770
  23. 3890 IF H1 > 10.9 THEN 4780
  24. 3900 IF RND < .5 THEN 3820
  25. 3910 IF RND < .5 THEN 3960
  26.  
  27. '//FROM 4010,4060, 4110, 4270, 4290
  28. 3920 GOSUB 4980
  29. 3930 R = ABS(R + 2 * Y)
  30. 3940 IF R > 5000 THEN 4780
  31. 3950 GOTO 1380 ' *** M A I N  L O O P
  32.  
  33. 3960 GOSUB 5040
  34. 3970 R = ABS(R - 2 * Y)
  35. 3980 IF R > 5000 THEN 4780
  36. 3990 GOTO 1380 ' *** M A I N  L O O P
  37.  
  38. 4000 IF R > 700 THEN 3960
  39. 4010 IF R > 200 THEN 3920
  40. 4020 GOTO 3770
  41.  
  42.  
  43. 4030 IF H2 < 6 THEN 4200
  44. 4040 IF H1 < 7 THEN 4120
  45. 4050 IF R < 300 THEN 3960
  46. 4060 IF R > 700 THEN 3920
  47. 4070 IF H1 > 7.9 THEN 4090
  48.  
  49. '// FROM 4360
  50. 4080 IF fnd(B1) > fnd(B) THEN 3960
  51. 4090 IF ABS(B1 - 90) >= ABS(B - 90) - 20 THEN 5080
  52. 4100 IF RND < .5 THEN 3960
  53. 4110 GOTO 3920
  54.  
  55. 4120 R9 = h
  56. 4130 B9 = B1
  57. 4140 GOSUB 6130
  58. 4150 R9 = R
  59. 4160 B9 = B1
  60. 4170 GOSUB 5080
  61. 4180 IF F8 > f9 THEN 3960
  62. 4190 GOTO 4050
  63.  
  64. 4200 IF H1 < 7 THEN 4290
  65. 4210 IF R > 150 THEN 4240
  66. 4220 IF RND < .5 THEN 3830
  67. 4230 GOTO 3960
  68. 4240 IF R >= 400 THEN 4270
  69. 4250 IF ABS(B1) < 30 THEN 5150
  70. 4260 GOTO 3830
  71.  
  72. 4270 IF R > 700 THEN 3920
  73. 4280 GOTO 4080
  74.  
  75.  
  76. '// FROM 4200
  77. 4290 IF R > 700 THEN 3920
  78. 4300 R9 = R
  79. 4310 B9 = B1
  80. 4320 GOSUB 6080
  81. 4330 R9 = R
  82. 4340 B9 = B1
  83. 4350 GOSUB 6130
  84.  
  85. 4360 IF f9 > F8 THEN 4080
  86. 4370 IF H1 > 6.9 THEN 4390
  87. 4380 IF fnd(B1) > fnd(B) THEN 3960
  88. 4390 IF ABS(B1 - 90) >= ABS(B - 90) - 20 THEN 5150
  89. 4400 GOTO 3960
  90.  

Normally you can almost, in your head, when converting an old "typed" game from a bookm pretty much convert it to QB64 pretty easy, since they are at most maybe 50 - 200 lines of code and very easy to follow.  This particular one is almost a 1,000 lines of code incremented by 10.

  • First thing I do, when I run across something this big, i just type it in exactly as shown in the book. I do this for a couple reasons, to get to understand the vars, and various chucks that seem to repeat, qb64 does a wonderful job of running almost any game you copy over.
  • once the game is typed in - I, of course, play it LOL. So I can see the flow much better
  • Once I have a basic (no pun) understanding of the program, I go through it and do a SEARCH AND CHANGE for variable I know what they are and change them to human readable variable - example the var "P". when I find that it means ships power, I will probably change all the P's to "shipsPower", and continue for every var I am pretty sure what they are.
  • the "ON GOTO/GOSUB" routines become very easy to deal with. I change those to SELECT CASE and thin for each var, I make a sub for it instead of putting the routine inside the SELECT CASE.  What I mean is, instead of CASE x: [ DO several lines of code for shooting an arrow] I will have it go to SUB shoot_arrow()
  • The main game loop I shove into a DO LOOP UNTIL GAMEOVER. 

Now this is very abbreviated version of what I do, but might help someone who likes the old games and wants to play them. Yes you can directly just type the game into QB64 and it will play fine, but later you will find it is NOT readable LOL. We call that "READ-ONCE-CODE"

At the time of this writing, the above code is a mystery to me as to what it does. I have a general idea, but the actual mechanism of the variables and what thye apply to is a mystery.

This is a STAR TREK game I have never seen before and I want to play it. No (text) graphics like all the standard clones of the 70's and 80's this is like ELIZA meets STAR TREK. These games in here have some of the most horrifying spaghetti code I have ever seen. Makes DAVID H. AHL code actually readable LOL


https://www.amazon.com/Tested-Ready-Run-Programs-BASIC/dp/0830610855/ref=sr_1_1?keywords=24+tested+ready+to+run+game+programs+in+basic&qid=1560093358&s=gateway&sr=8-1

I just like re-writing old DOS book games into modern QB64 code - weird hobby, I know!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: How I convert VERY VERY OLD spaghetti code (example included)
« Reply #1 on: June 09, 2019, 12:05:51 pm »
Hi xra7en,

You wouldn't happen to have a copy of David AHL's Blackjack typed and running in QB64?

I would like a copy but those magazine prints are hard on my eyes.

Wait... I just found this, is it similar to David's?
Code: QB64: [Select]
  1.  
  2. '1 REM THIS PROGRAM HAS BEEN MODIFIED TO INCREASE THE BETS
  3. 2 REM LINE 340,360 HAVE BEEN CHANGED TO UP THE LIMIT TO 100K
  4. 3 REM LINE 345 MODIFIED ALSO LET IT EQUAL THE # OF CHAR THE $ AMT IS
  5. 10 PRINT "DO YOU WANT INSTRUCTIONS";
  6. 15 INPUT K$
  7. 25 IF (LEFT$(K$, 1) <> "Y") AND (LEFT$(K$, 1) <> "y") THEN 85
  8. 50 PRINT "THIS IS A GAME OF BLACKJACK, LAS VEGAS STYLE."
  9. 55 PRINT "HERE ARE THE RULES OF THE HOUSE.  THE DEALER"
  10. 60 PRINT "MUST HIT ON 16 OR LESS AND WILL STAY ON 17 OR"
  11. 65 PRINT "MORE.  YOU MAY SPLIT TWO CARDS IF THEY ARE THE"
  12. 70 PRINT "SAME AND PLAY ONE HAND WITH EACH OF THEM.  ALSO,"
  13. 75 PRINT "YOU MAY DOUBLE YOUR BET AND RECEIVE EXACTLY ONE "
  14. 80 PRINT "MORE CARD ANY TIME ON YOUR FIRST HIT.  THE TYPING"
  15. 85 PRINT "INSTRUCTIONS ARE: S-STAND; H-HIT; D-DOUBLE; AND"
  16. 90 PRINT "P-SPLIT A PAIR."
  17. 91 PRINT "THE HOUSE LIMIT IS $100,000"
  18. 95 IF K <> 1 THEN 155
  19. 100 PRINT "WHEN THE DEALER HAS AN EXPOSED ACE HE WILL ASK"
  20. 105 PRINT "YOU FOR AN INSURANCE BET.  AN INSURANCE BET WILL"
  21. 110 PRINT "RISK HALF YOUR BET FOR AN AMOUNT EQUAL TO YOUR BET"
  22. 115 PRINT "IF YOU WIN.  YOU WIN IF THE DEALER HAS A BLACKJACK"
  23. 120 PRINT "AND LOSE IF HE DOESN''T! THE HOUSE LIMIT IS 50,000>"
  24. 125 PRINT "GOOD LUCK.  OH, BY THE WAY, THE DEALER IS NOTED FOR"
  25. 130 PRINT "DEALING OFF THE BOTTOM OF THE DECK.  WATCH HIM VERY"
  26. 135 PRINT "CLOSELY.  HERE HE IS NOW."
  27. 140 PRINT
  28. 145 PRINT
  29. 150 PRINT
  30. 155 LET K = 0
  31. 160 LET W1 = 0
  32. 170 LET N = INT(1945 * RND + 1)
  33. 175 PRINT
  34. 180 PRINT "ANY TIME YOU WANT ME TO RESHUFFLE THE CARDS SIMPLY"
  35. 185 PRINT "TYPE 7777 WHEN I ASK FOR YOUR WAGER AND I''LL BE VERY"
  36. 190 PRINT "HAPPY TO OBLIGE.  O.K., HERE IS THE FIRST HAND."
  37. 200 LET X = INT(10 * RND)
  38. 210 DIM D(52), E(5), V(5), T(5), WA(5)
  39. 215 FOR A = 1 TO 52
  40.    220 LET D(A) = 0
  41. 225 NEXT A
  42. 230 DIM Q(52)
  43. 235 FOR A = 0 TO 39 STEP 13
  44.    240 FOR C = 1 TO 13
  45.        245 LET Q(A + C) = C
  46.    250 NEXT C
  47. 255 NEXT A
  48. 260 PRINT
  49. 265 LET K = K + 1
  50. 270 FOR P = 1 TO 5
  51.    275 LET E(P) = 0
  52.    280 LET V(P) = 0
  53.    285 LET T(P) = 0
  54. 290 NEXT P
  55. 295 LET V(3) = 1
  56. 300 PRINT
  57. 305 PRINT
  58. 310 PRINT
  59. 315 PRINT "WAGER";
  60. 320 LET P = 1
  61. 325 INPUT W
  62. 330 LET WA(2) = W
  63. 335 IF W <= 0 THEN 1685
  64. 340 IF W <= 100000 THEN 370
  65. 345 IF W <> 7777 THEN 360
  66. 350 GOSUB 1645
  67. 355 GOTO 315
  68. 360 PRINT "HEY THE HOUSE LIMIT IS $100 G ''S"
  69. 365 GOTO 315
  70. 370 PRINT
  71. 375 PRINT "I SHOW",
  72. 380 GOSUB 865
  73. 385 IF E(1) = 0 THEN 395
  74. 390 LET V(4) = 1
  75. 395 LET V(5) = 1
  76. 400 GOSUB 865
  77. 405 LET M = X
  78. 410 LET P = 2
  79. 415 PRINT "FIRST CARD IS",
  80. 420 GOSUB 865
  81. 425 LET G = X
  82. 430 PRINT "NEXT CARD IS",
  83. 435 GOSUB 865
  84. 440 IF V(2) > 0 THEN 605
  85. 445 LET S = X
  86. 450 IF V(3) <> 1 THEN 605
  87. 455 IF T(P) <> 21 THEN 505
  88. 460 PRINT "   ***BLACKJACK***   "
  89. 465 PRINT
  90. 470 PRINT " THAT''S GETTING A BIT RIDICULOUS::::"
  91. 475 PRINT
  92. 480 PRINT "MY HOLE CARD WAS ",
  93. 485 LET X = M
  94. 490 GOSUB 1035
  95. 495 LET W1 = W1 + 1.5 * W
  96. 500 GOSUB 1335
  97. 505 IF V(4) = 0 THEN 570
  98. 510 PRINT "INSURANCE ANYONE";
  99. 515 INPUT I$
  100. 520 PRINT
  101. 525 IF LEFT$(I$, 1) <> "y" THEN 570
  102. 530 IF T(1) <> 21 THEN 555
  103. 535 LET W1 = W1 + W
  104. 540 PRINT
  105. 545 PRINT "YOU WIN $"; W; " ON YOUR INSURANCE BET"
  106. 550 GOTO 570
  107. 555 LET W1 = W1 - W / 2
  108. 560 PRINT
  109. 565 PRINT "YOU LOST $"; W / 2; " ON YOUR INSURANCE BET - I DON''T HAVE BLACKJACK"
  110. 570 IF T(1) <> 21 THEN 605
  111. 575 PRINT
  112. 580 PRINT "**I HAVE BLACKJACK**"
  113. 585 PRINT "MY HOLE CARD WAS ",
  114. 590 LET X = M
  115. 595 GOSUB 1035
  116. 600 GOTO 1300
  117. 605 IF T(P) <= 21 THEN 650
  118. 610 IF E(P) > 0 THEN 640
  119. 615 PRINT "YOU BUSTED, ";
  120. 620 PRINT "YOUR TOTAL IS "; T(P)
  121. 625 LET C1 = T(P) - 5 * (INT(T(P) / 5))
  122. 630 IF V(2) = 1 THEN 775
  123. 635 GOTO 1175
  124. 640 LET E(P) = E(P) - 1
  125. 645 LET T(P) = T(P) - 10
  126. 650 IF V(1) = 2 THEN 620
  127. 655 LET V(3) = V(3) + 1
  128. 660 PRINT "WHAT DO YOU WANT TO DO: (S)TAND (H)IT (D)OUBLE S(P)LIT";
  129. 661 INPUT I$: V(1) = -1
  130. 662 REM CONVERT SYMBOLIC TO NUMERIC INPUT
  131. 666 IF (LEFT$(I$, 1) = "S") OR (LEFT$(I$, 1) = "s") THEN V(1) = 0
  132. 667 IF (LEFT$(I$, 1) = "H") OR (LEFT$(I$, 1) = "h") THEN V(1) = 1
  133. 668 IF (LEFT$(I$, 1) = "D") OR (LEFT$(I$, 1) = "d") THEN V(1) = 2
  134. 669 IF (LEFT$(I$, 1) = "P") OR (LEFT$(I$, 1) = "p") THEN V(1) = 3
  135. 670 IF V(1) < 0 THEN PRINT "WHAT ?": GOTO 660
  136. 671 IF V(1) <> 3 THEN 830
  137. 675 IF V(2) > 0 THEN 820
  138. 680 IF V(3) <> 2 THEN 820
  139. 685 IF Q(G) = Q(S) THEN 700
  140. 690 PRINT "NOW IS THAT A PAIR?"
  141. 695 GOTO 660
  142. 700 LET V(2) = 1
  143. 705 IF Q(G) <> 1 THEN 715
  144. 710 LET V(1) = 2
  145. 715 LET P = 3
  146. 720 PRINT "       PLAY HAND ONE NOW"
  147. 725 PRINT "FIRST CARD IS ",
  148. 730 LET WA(3) = W
  149. 735 LET X = G
  150. 740 GOSUB 750
  151. 745 GOTO 430
  152. 750 GOSUB 990
  153. 755 GOSUB 1035
  154. 760 LET V(3) = 1
  155. 765 LET T(P) = C
  156. 770 RETURN
  157. 775 LET P = 2
  158. 780 LET V(2) = 2
  159. 785 PRINT "       PLAY HAND TWO NOW"
  160. 790 PRINT "FIRST CARD IS",
  161. 795 LET X = S
  162. 800 GOSUB 750
  163. 805 IF Q(G) = 1 THEN 815
  164. 810 LET V(1) = 0
  165. 815 GOTO 430
  166. 820 PRINT "NO SPLITS NOW -- TRY AGAIN"
  167. 825 GOTO 660
  168. 830 IF V(1) <> 2 THEN 855
  169. 835 IF V(3) = 2 THEN 850
  170. 840 PRINT "TOO LATE TO DOUBLE, CHARLIE."
  171. 845 GOTO 660
  172. 850 LET WA(P) = 2 * WA(P)
  173. 855 IF V(1) > 0 THEN 430
  174. 860 GOTO 620
  175. 865 GOSUB 900
  176. 870 LET T(P) = T(P) + C
  177. 875 IF V(5) = 0 THEN 890
  178. 880 LET V(5) = 0
  179. 885 RETURN
  180. 890 GOSUB 1035
  181. 895 RETURN
  182. 900 IF R >= 50 THEN 945
  183. 905 LET N = 10 * (1 + ABS(COS(N + W1)))
  184. 910 FOR A = 1 TO N
  185.    915 LET X = INT(53 * RND)
  186.    920 IF X = 0 THEN 915
  187. 925 NEXT A
  188. 930 IF D(X) = 0 THEN 980
  189. 935 LET R = R + 1
  190. 940 IF R < 50 THEN 900
  191. 945 FOR A = 1 TO 52
  192.    950 IF D(A) = K THEN 960
  193.    955 LET D(A) = 0
  194. 960 NEXT A
  195. 965 LET R = 0
  196. 970 PRINT "   I RESHUFFLED   ";
  197. 975 GOTO 900
  198. 980 LET R = 0
  199. 985 LET D(X) = K
  200. 990 IF Q(X) <> 1 THEN 1010
  201. 995 LET C = 11
  202. 1000 LET E(P) = E(P) + 1
  203. 1005 RETURN
  204. 1010 IF Q(X) > 10 THEN 1025
  205. 1015 LET C = Q(X)
  206. 1020 RETURN
  207. 1025 LET C = 10
  208. 1030 RETURN
  209. 1035 GOSUB 1050
  210. 1040 GOSUB 1120
  211. 1045 RETURN
  212. 1050 IF Q(X) <> 1 THEN 1065
  213. 1055 PRINT " ACE ";
  214. 1060 RETURN
  215. 1065 IF Q(X) > 10 THEN 1080
  216. 1070 PRINT Q(X); " ";
  217. 1075 RETURN
  218. 1080 IF Q(X) > 11 THEN 1095
  219. 1085 PRINT " JACK ";
  220. 1090 RETURN
  221. 1095 IF Q(X) > 12 THEN 1110
  222. 1100 PRINT " QUEEN ";
  223. 1105 RETURN
  224. 1110 PRINT " KING ";
  225. 1115 RETURN
  226. 1120 IF X > 39 THEN 1145
  227. 1125 IF X > 26 THEN 1155
  228. 1130 IF X > 13 THEN 1165
  229. 1135 PRINT "OF SPADES"
  230. 1140 RETURN
  231. 1145 PRINT "OF CLUBS"
  232. 1150 RETURN
  233. 1155 PRINT "OF HEARTS"
  234. 1160 RETURN
  235. 1165 PRINT "OF DIAMONDS"
  236. 1170 RETURN
  237. 1175 LET P = 2
  238. 1180 PRINT "MY HOLE CARD WAS ",
  239. 1185 LET X = M
  240. 1190 GOSUB 1035
  241. 1195 IF T(2) < 22 THEN 1210
  242. 1200 IF V(2) = 0 THEN 1300
  243. 1205 IF T(3) > 21 THEN 1300
  244. 1210 LET P = 1
  245. 1215 IF T(1) < 17 THEN 1360
  246. 1220 IF T(1) > 17 THEN 1230
  247. 1225 IF E(1) > 0 THEN 1360
  248. 1230 IF T(1) > 21 THEN 1375
  249. 1235 LET P = 2
  250. 1240 PRINT "MY TOTAL IS "; T(1)
  251. 1245 IF T(P) > 21 THEN 1300
  252. 1250 IF T(1) > 21 THEN 1265
  253. 1255 IF T(1) > T(P) THEN 1300
  254. 1260 IF T(1) = T(P) THEN 1330
  255. 1265 LET W1 = W1 + WA(P)
  256. 1270 IF C1 > 3 THEN 1445
  257. 1275 IF C1 > 2 THEN 1465
  258. 1280 IF C1 > 1 THEN 1485
  259. 1285 IF C1 > 0 THEN 1505
  260. 1290 GOTO 1525
  261. 1295 GOTO 1330
  262. 1300 LET W1 = W1 - WA(P)
  263. 1305 IF C1 > 3 THEN 1545
  264. 1310 IF C1 > 2 THEN 1565
  265. 1315 IF C1 > 1 THEN 1585
  266. 1320 IF C1 > 0 THEN 1605
  267. 1325 GOTO 1625
  268. 1330 IF V(2) > 0 THEN 1345
  269. 1335 GOSUB 1405
  270. 1340 GOTO 265
  271. 1345 LET P = 3
  272. 1350 LET V(2) = 0
  273. 1355 GOTO 1245
  274. 1360 PRINT "I DRAW",
  275. 1365 GOSUB 865
  276. 1370 GOTO 1210
  277. 1375 IF E(1) = 0 THEN 1395
  278. 1380 LET E(1) = E(1) - 1
  279. 1385 LET T(1) = T(1) - 10
  280. 1390 GOTO 1210
  281. 1395 PRINT "I BUSTED*****"
  282. 1400 GOTO 1235
  283. 1405 IF W1 < 0 THEN 1425
  284. 1410 IF W1 = 0 THEN 1435
  285. 1415 PRINT "YOU''RE AHEAD $"; W1
  286. 1420 RETURN
  287. 1425 PRINT "YOU''RE BEHIND $"; W1
  288. 1430 RETURN
  289. 1435 PRINT "YOU''RE EVEN"
  290. 1440 RETURN
  291. 1445 PRINT
  292. 1450 PRINT "I MUST HAVE DEALT WRONG."
  293. 1455 PRINT
  294. 1460 GOTO 1330
  295. 1465 PRINT
  296. 1470 PRINT "YOU LUCKED OUT AGAIN:"
  297. 1475 PRINT
  298. 1480 GOTO 1330
  299. 1485 PRINT
  300. 1490 PRINT "YOU MUST HAVE BEEN PEEKING."
  301. 1495 PRINT
  302. 1500 GOTO 1330
  303. 1505 PRINT
  304. 1510 PRINT "I COULD LOSE MY JOB THIS WAY."
  305. 1515 PRINT
  306. 1520 GOTO 1330
  307. 1525 PRINT
  308. 1530 PRINT "THE CARDS HAVE TURNED AGAINST ME."
  309. 1535 PRINT
  310. 1540 GOTO 1330
  311. 1545 PRINT
  312. 1550 PRINT "THE BOTTOM OF THE DECK STRIKES AGAIN."
  313. 1555 PRINT
  314. 1560 GOTO 1330
  315. 1565 PRINT
  316. 1570 PRINT "A VICTORY FOR US GOOD GUYS."
  317. 1575 PRINT
  318. 1580 GOTO 1330
  319. 1585 PRINT
  320. 1590 PRINT "YOU CAN''T BEAT SKILL."
  321. 1595 PRINT
  322. 1600 GOTO 1330
  323. 1605 PRINT
  324. 1610 PRINT "MUST BE MY LUCKY DAY."
  325. 1615 PRINT
  326. 1620 GOTO 1330
  327. 1625 PRINT
  328. 1630 PRINT "YOU CAN''T BEAT THE BEST."
  329. 1635 PRINT
  330. 1640 GOTO 1330
  331. 1645 PRINT
  332. 1650 PRINT "I''M SUPPOSED TO SHUFFLE, BUT I DON''T KNOW HOW!"
  333. 1660 PRINT
  334. 1665 RETURN
  335. 1685 PRINT
  336. 1690 PRINT "WELL, SEE YOU NEXT TIME!"
  337. 1700 PRINT
  338. 1710 END
  339.  
... and it works!

My opinion of why the spaghetti code is so crazy is not because they want to make it hard to understand while typing in but because they did not have modern subs and functions and ways to exit loops or jump around inside loops.
« Last Edit: June 09, 2019, 12:19:05 pm by bplus »

Offline xra7en

  • Seasoned Forum Regular
  • Posts: 284
    • View Profile
Re: How I convert VERY VERY OLD spaghetti code (example included)
« Reply #2 on: June 09, 2019, 12:20:13 pm »
I have over the years of searching the net, have gathered a collection over 340+ BASIC books in PDF.
however with that said, not sure on the ethics of QB64 forums if it is allowed to share those.

I have pdf file of all these books
https://www.atariarchives.org/

and then another 300+ LOL

Does D.H.A have a blackjack version - I think he doesn in his BIG COMPUTER GAMES
I just like re-writing old DOS book games into modern QB64 code - weird hobby, I know!

Offline xra7en

  • Seasoned Forum Regular
  • Posts: 284
    • View Profile
Re: How I convert VERY VERY OLD spaghetti code (example included)
« Reply #3 on: June 09, 2019, 12:21:34 pm »
Hi xra7en,

You wouldn't happen to have a copy of David AHL's Blackjack typed and running in QB64?

I would like a copy but those magazine prints are hard on my eyes.

Lemme see what I can find. I need a break from this spaghetti I am working on LOL
I'll see if i can type a version up
« Last Edit: June 09, 2019, 12:22:56 pm by xra7en »
I just like re-writing old DOS book games into modern QB64 code - weird hobby, I know!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: How I convert VERY VERY OLD spaghetti code (example included)
« Reply #4 on: June 09, 2019, 12:27:36 pm »
Yeah I found a game here, it looks different, even older than one I posted!
https://www.atariarchives.org/basicgames/showpage.php?page=19

I was curious how it  played, yikes! more than a 2 pages!

Ah! another one from Creative Computing Morristown, NJ

« Last Edit: June 09, 2019, 12:31:51 pm by bplus »

Offline xra7en

  • Seasoned Forum Regular
  • Posts: 284
    • View Profile
Re: How I convert VERY VERY OLD spaghetti code (example included)
« Reply #5 on: June 09, 2019, 06:38:17 pm »
feel free to join in
This is a scratch built one - DOUBLE DOWN and split IS NOT DONE yet, but you get the idea.
There is some quirky code I have to fix, but hey, this is like a 2hr slam together LOL

Code: QB64: [Select]
  1. '// BLACKJACK
  2. '// SCRATCH CODED.
  3. '// DESIGN:
  4. '//     DECK IS SHUFFLED AFTER EVERY HAND
  5. '//     DEALER HITS ON 16
  6. '//     DEALER STANDS ON 17
  7. '//
  8.  
  9. RANDOMIZE (-TIMER) '                    OLD SCHOOL RANDOMIZER
  10.  
  11. '// SUITS TILES
  12. CONST heart = ""
  13. CONST diamond = ""
  14. CONST club = ""
  15. CONST spade = ""
  16.  
  17.  
  18. '// BOOLEAN HACK
  19. CONST TRUE = 1
  20. CONST FALSE = 0
  21.  
  22. '// CREATE BLANK CARD
  23. TYPE tCard
  24.     cardNum AS INTEGER '                CARD NUMBER IN DECK 1-52
  25.     suit AS INTEGER '                   SUITS 1-4
  26.     value AS INTEGER '                  VALUE OF THIS CARD
  27.     tile AS STRING '                   TILE ART THIS CARD HAS
  28.     discarded AS INTEGER '              HAS THIS CARD BEEN DISCARDED
  29.  
  30. TYPE tPLAYER
  31.     cash AS INTEGER '                   CASH ON HAND
  32.     wins AS INTEGER '                   HOW MANY TIMES WON
  33.     bust AS INTEGER '                   BUST COUNT
  34.     push AS INTEGER '                   PUSH COUNT
  35.     cTotal AS INTEGER '                 RUNNING CARD TOTAL
  36.     aceUp AS INTEGER '                  HOW MANY ACES DOES PLAYER HAVE?
  37.  
  38.  
  39.  
  40.  
  41. DIM SHARED GAMEOVER AS INTEGER '        BOOLEAN FOR MONITORING GAME STATUS
  42. DIM SHARED I, J AS INTEGER '            GLOBLA LOOP COUNTERS
  43. DIM SHARED PLAYER AS tPLAYER '          PLAYER DB
  44. DIM SHARED DECK(52) AS tCard '          HERE IS OUR COOL DECK
  45. DIM SHARED ROUND_OVER AS INTEGER '      EACH ROUND
  46. DIM SHARED DEALER_TOTAL AS INTEGER '    HOW MUCH DOES DEALER HAVE SHOWING
  47.  
  48.  
  49.  
  50.  
  51. 'GAME_TITLE
  52. GAME_INIT
  53. GAME_RUN
  54. 'GAME_END
  55.  
  56.  
  57. '$INCLUDE: 'UTILS.BI'
  58.  
  59. SUB GAME_INIT
  60.  
  61.     NEW_DECK '                          CREATE THE DECK
  62.  
  63.     GAMEOVER = FALSE '                  GAME IS NOT OVER (JUST STARTING)
  64.  
  65.     PLAYER.cash = 100 '                 START PLAYER WITH $100
  66.     PLAYER.aceUp = 0 '                  PLAYER HAS NO ACES YET
  67.  
  68.  
  69.  
  70. SUB GAME_TITLE
  71.  
  72.  
  73.  
  74. SUB SHOW_CARD (CARDNUM AS INTEGER)
  75.  
  76.     IF DECK(CARDNUM).value > 10 THEN DECK(CARDNUM).value = 10
  77.     IF DECK(CARDNUM).value = 1 THEN
  78.         PRINT "A" + DECK(CARDNUM).tile
  79.     ELSE
  80.         PRINT STR$(DECK(CARDNUM).value) + DECK(CARDNUM).tile
  81.     END IF ' IF THIS IS AN ACE
  82.  
  83.  
  84.  
  85. FUNCTION CALC_TOTAL (TOTAL AS INTEGER)
  86.     IF TOTAL > 10 THEN TOTAL = 10
  87.     IF TOTAL = 1 THEN TOTAL = 11 ' START OFF WITH ACE = 11
  88.     CALC_TOTAL = TOTAL
  89.  
  90.  
  91. SUB BROKE
  92.     CLS
  93.     PRINT
  94.     PRINT "Well it looks like you are out of money!"
  95.     PRINT "why not call it a night!"
  96.     PRINT
  97.     PAUSE
  98.     END
  99.  
  100.  
  101.  
  102. SUB GAME_RUN
  103.  
  104.     DIM CARDNUM AS INTEGER
  105.     DIM PlayerTotal(10) AS INTEGER '        TRACK EACH CARD THEY GET
  106.     DIM pCOUNT AS INTEGER '                 CARDS IN HAND
  107.     DIM ans AS STRING '                     PLAYER CHOICE
  108.     DIM BET AS INTEGER '                               PLAYER BET
  109.  
  110.     DO '                                    START THE GAME
  111.  
  112.         DO '                                START A NEW HAND
  113.             PLAYER.aceUp = 0
  114.             PLAYER.cTotal = 0
  115.             ROUND_OVER = FALSE
  116.             DEALER_TOTAL = 0
  117.             pCOUNT = 1
  118.             FOR I = 1 TO 10: PlayerTotal(I) = 0: NEXT
  119.  
  120.  
  121.             CLS
  122.             IF PLAYER.cash <= 0 THEN BROKE
  123.  
  124.             PRINT "Cash on hand: "; PLAYER.cash;
  125.             PRINT "House Max Bet: 1000" ' TODO: MAKE THIS A VARIABLE
  126.  
  127.             DO
  128.  
  129.                 PRINT "Place your bet please: ";: INPUT BET
  130.             LOOP UNTIL BET <= PLAYER.cash
  131.  
  132.             '// SHOW DEALERS CARD UP
  133.             CARDNUM = DEAL_A_CARD
  134.             PRINT "DEALER UP-CARD: ";
  135.             SHOW_CARD (CARDNUM)
  136.             DEALER_TOTAL = DEALER_TOTAL + CALC_TOTAL(DECK(CARDNUM).value)
  137.             PRINT "Dealer Total: "; DEALER_TOTAL
  138.  
  139.             '// SHOW PLAYERS CARDS
  140.             CARDNUM = DEAL_A_CARD
  141.             PRINT
  142.             PRINT "PLAYER CARD: ";
  143.             SHOW_CARD (CARDNUM)
  144.             PlayerTotal(pCOUNT) = CALC_TOTAL(DECK(CARDNUM).value)
  145.             IF PlayerTotal(pCOUNT) = 1 THEN PLAYER.aceUp = INC(PLAYER.aceUp)
  146.  
  147.             pCOUNT = INC(pCOUNT)
  148.  
  149.  
  150.             CARDNUM = DEAL_A_CARD
  151.             PRINT "PLAYER CARD: ";
  152.             SHOW_CARD (CARDNUM)
  153.             PlayerTotal(pCOUNT) = CALC_TOTAL(DECK(CARDNUM).value)
  154.             IF PlayerTotal(pCOUNT) = 1 THEN PLAYER.aceUp = INC(PLAYER.aceUp)
  155.             pCOUNT = INC(pCOUNT)
  156.  
  157.             FOR I = 1 TO UBOUND(PLAYERTOTAL)
  158.                 PLAYER.cTotal = PLAYER.cTotal + PlayerTotal(I)
  159.             NEXT
  160.             PRINT
  161.             PRINT "Player Showing: "; PLAYER.cTotal
  162.             IF PLAYER.cTotal = 21 THEN
  163.                 QBJACK (BET)
  164.             ELSE
  165.  
  166.  
  167.                 PRINT: PRINT
  168.                 PRINT "[S]tand, [H]it, [D]ouble ";
  169.                 IF PlayerTotal(1) = PlayerTotal(2) THEN PRINT ", S[p]lit: ";
  170.  
  171.                 INPUT ans: ans = UCASE$(ans)
  172.  
  173.                 SELECT CASE ans
  174.                     CASE "S": CALL results(DEALER_TOTAL, PLAYER.cTotal, BET)
  175.                     CASE "H":
  176.                         DO
  177.                             CARDNUM = DEAL_A_CARD
  178.                             PRINT
  179.                             PRINT "Next Card: ";: SHOW_CARD (CARDNUM)
  180.                             PlayerTotal(pCOUNT) = CALC_TOTAL(DECK(CARDNUM).value): pCOUNT = INC(pCOUNT)
  181.                             PLAYER.cTotal = PLAYER.cTotal + PlayerTotal(pCOUNT - 1)
  182.                             PRINT "Player Showing: "; PLAYER.cTotal
  183.                             IF PLAYER.cTotal > 21 THEN BUST (BET)
  184.                             IF PLAYER.cTotal = 21 THEN WIN (BET) 'NOT SAME AS BLACKJAC
  185.                             PRINT
  186.                             PRINT "[S]tand or [H]it?";
  187.                             INPUT ans: ans = UCASE$(ans)
  188.  
  189.                         LOOP UNTIL UCASE$(ans) = "S"
  190.                         CALL results(DEALER_TOTAL, PLAYER.cTotal, BET)
  191.                 END SELECT
  192.  
  193.             END IF ' NOT QBJACK
  194.  
  195.  
  196.         LOOP UNTIL ROUND_OVER
  197.  
  198.     LOOP UNTIL GAMEOVER '                   ARE WE DONE PLAYING?
  199.  
  200.  
  201. SUB PAUSE
  202.     PRINT "Press [ENTER] to continue..."
  203.     INPUT I
  204.  
  205.  
  206. SUB BUST (bet AS INTEGER)
  207.     PLAYER.cash = PLAYER.cash - bet
  208.     PRINT
  209.     PRINT "Player BUSTS! Sorry chum! Better luck next time"
  210.     ROUND_OVER = TRUE
  211.  
  212.     PAUSE
  213.  
  214.  
  215. SUB WIN (bet AS INTEGER)
  216.     PLAYER.cash = PLAYER.cash + bet
  217.     PRINT
  218.     PRINT "Player WINS!! Congradulations!! Keep it up!"
  219.     ROUND_OVER = TRUE
  220.     PAUSE
  221.  
  222.  
  223. SUB QBJACK (bet AS INTEGER)
  224.     PLAYER.cash = INT(PLAYER.cash * 1.5)
  225.     PRINT
  226.     PRINT "PLAYER HAS QBJACK!!! ROCK ON!! Don't get cocky!!"
  227.     ROUND_OVER = TRUE
  228.     PAUSE
  229.  
  230.  
  231. SUB PUSH
  232.     PRINT
  233.     PRINT "Player PUSH. No win/loss. Luck!!"
  234.     ROUND_OVER = TRUE
  235.     PAUSE
  236.  
  237.  
  238.  
  239.  
  240. SUB results (dealerTotal, PlayerTotal, bet)
  241.     DIM CARDNUM AS INTEGER
  242.  
  243.     '// player is standing on an odd total
  244.     DO
  245.         CARDNUM = DEAL_A_CARD
  246.         PRINT "DEALER NEXT CARD: ";
  247.         SHOW_CARD (CARDNUM)
  248.         dealerTotal = dealerTotal + CALC_TOTAL(DECK(CARDNUM).value)
  249.         PRINT "Dealer Total: "; dealerTotal
  250.         _DELAY (2)
  251.         PRINT
  252.  
  253.     LOOP UNTIL dealerTotal > 16
  254.  
  255.     SELECT CASE dealerTotal
  256.         CASE IS > 21: WIN (bet)
  257.         CASE IS = PlayerTotal: PUSH
  258.         CASE IS < PlayerTotal: WIN (bet)
  259.         CASE IS > PlayerTotal: BUST (bet)
  260.  
  261.     END SELECT
  262.  
  263.  
  264.  
  265. '//
  266. '// DRAW A CARD FROM THE DECK
  267. '//
  268. FUNCTION DEAL_A_CARD
  269.  
  270.     DIM X, Y AS INTEGER
  271.  
  272.     DO
  273.         X = INT(RND * 52) + 1
  274.  
  275.     LOOP UNTIL DECK(X).discarded = 0
  276.  
  277.     DECK(X).discarded = 1 ' BURN THIS CARD
  278.  
  279.     DEAL_A_CARD = DECK(X).cardNum
  280.  
  281.  
  282.  
  283. SUB NEW_DECK
  284.  
  285.     DIM COUNT, VALUE, SUIT AS INTEGER
  286.  
  287.     COUNT = 1 '                         CARD NUMBER IN THE DECK (1-52)
  288.     VALUE = 1 '                         VALUE 1 -13
  289.     SUIT = 1 '                          SUIT 1 - 4
  290.     FOR I = 1 TO 52
  291.         DECK(I).cardNum = COUNT
  292.         DECK(I).suit = SUIT
  293.         DECK(I).value = VALUE
  294.         DECK(I).discarded = 0
  295.         SELECT CASE SUIT
  296.             CASE 1: DECK(I).tile = club
  297.             CASE 2: DECK(I).tile = heart
  298.             CASE 3: DECK(I).tile = spade
  299.             CASE 4: DECK(I).tile = diamond
  300.         END SELECT
  301.         COUNT = INC(COUNT)
  302.         SUIT = INC(SUIT): IF SUIT > 4 THEN SUIT = 1
  303.         VALUE = INC(VALUE): IF VALUE > 13 THEN VALUE = 1
  304.  
  305.     NEXT
  306.  
  307.  



You will also need this code snippet
Code: QB64: [Select]
  1. '// DEC(X) -1 TO X
  2. '// INC(X) +1 TO X
  3. '// DISTANCE: DISTANCE BETWEEN 2 X,Y COORDS
  4.  
  5.     '============================================================================
  6.     '--- PASCAL FUNCTION: DECREASES X BY 1)
  7.     '============================================================================
  8.     DEC = X - 1
  9.  
  10.     '============================================================================
  11.     '--- PASCAL FUNCTION: INCREASE X BY 1)
  12.     '============================================================================
  13.     INC = X + 1
  14.  
  15. FUNCTION DISTANCE (X1, Y1, X2, Y2 AS INTEGER)
  16.     '============================================================================
  17.     '--- DIST BETWEEN 2 POINTS
  18.     '============================================================================
  19.     DISTANCE = SQR((X1 - X2) ^ 2 + (Y1 - Y2) ^ 2)
  20.  
  21.  
  22. FUNCTION yesNo (default AS STRING)
  23.     '// @default = when ENTER is pressed what will be the default answer
  24.  
  25.     DIM ans AS STRING
  26.  
  27.     DO
  28.         ans = INKEY$
  29.         ans = LCASE$(ans)
  30.     LOOP UNTIL ans = "y" OR ans = "n" OR ans = CHR$(13)
  31.     IF ans = CHR$(13) THEN ans = default
  32.  
  33.     IF ans = "y" THEN
  34.         yesNo = 1
  35.     ELSE
  36.         yesNo = 0
  37.     END IF
  38.  
  39.  
  40. FUNCTION COUNT_TAGS (TEXT AS STRING)
  41.     '//
  42.     '// RETURNS THE NUMBER OF LORD TAGS IN A STRING
  43.     '//
  44.     DIM I AS INTEGER
  45.     IF LEN(TEXT) = 0 THEN COUNT_TAGS = 0
  46.     DIM COUNT AS INTEGER
  47.     DIM LENGTH AS INTEGER
  48.     LENGTH = LEN(TEXT)
  49.     COUNT = 0
  50.  
  51.     FOR I = 1 TO LENGTH
  52.         IF MID$(TEXT, I, 1) = "`" THEN COUNT = COUNT + 1
  53.  
  54.     NEXT
  55.     COUNT_TAGS = COUNT
  56.  
  57.  
  58.  
  59.  
  60. SUB CENTER (text AS STRING, row AS INTEGER)
  61.     '//
  62.     '// CENTER TEXT AS A SPECIFIED ROW
  63.     '//
  64.     DIM textLen, col AS INTEGER
  65.     DIM TAGS AS INTEGER
  66.  
  67.     IF INSTR(text, "`") >= 1 THEN ' There is a tag
  68.  
  69.         TAGS = COUNT_TAGS(text) * 2
  70.         textLen = INT((LEN(text) - TAGS) / 2)
  71.     ELSE
  72.         textLen = INT(LEN(text) / 2)
  73.  
  74.     END IF
  75.  
  76.     col = 40 - textLen
  77.     LOCATE row, col
  78.     IF TAGS > 0 THEN
  79.         LWRITE text
  80.     ELSE
  81.         PRINT text
  82.     END IF
  83.  
  84.  
  85.  
  86. SUB LWRITE (TXT AS STRING)
  87.     DIM i AS INTEGER
  88.     '======================================================
  89.     '--- THIS IS A POPULAR COLOR ROUTINE BASED ON THE
  90.     '--- SETH ABLE L.O.R.D. BBS GAME COLOR FORMATTING
  91.     '=====================================================
  92.     COLOR 7 ' DEFAULT COLOR
  93.     FOR i = 1 TO LEN(TXT$)
  94.         ' CHECK FOR A COLOR CHANGE
  95.         IF MID$(TXT$, i, 1) = "`" THEN
  96.             i = i + 1
  97.             SELECT CASE MID$(TXT, i, 1)
  98.                 CASE "0": COLOR 10
  99.                 CASE "1": COLOR 1
  100.                 CASE "2": COLOR 2
  101.                 CASE "3": COLOR 3
  102.                 CASE "4": COLOR 4
  103.                 CASE "5": COLOR 5
  104.                 CASE "6": COLOR 6
  105.                 CASE "7": COLOR 7
  106.                 CASE "8": COLOR 8
  107.                 CASE "9": COLOR 9
  108.                 CASE "!": COLOR 11
  109.                 CASE "@": COLOR 12
  110.                 CASE "#": COLOR 13
  111.                 CASE "$": COLOR 14
  112.                 CASE "%": COLOR 15
  113.                 CASE "n": PRINT '           new line
  114.                 CASE "t": PRINT , ; '       TAB
  115.             END SELECT
  116.             i = i + 1
  117.  
  118.         END IF
  119.         PRINT MID$(TXT$, i, 1);
  120.     NEXT
  121.     PRINT ;
  122.  
  123. SUB LWRITELN (TXT AS STRING)
  124.     LWRITE (TXT$)
  125.     PRINT
  126.  

I might move this to another thread. (mod??)
I just like re-writing old DOS book games into modern QB64 code - weird hobby, I know!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: How I convert VERY VERY OLD spaghetti code (example included)
« Reply #6 on: June 09, 2019, 09:37:11 pm »
Hi xra7en,

Your Blackjack start is welcome on the Blackjack thread I started, I like comparing coding techniques.

BTW is that RANDOMIZE (-TIMER) ? or are my eyes going bluey...

Also I am not encountering any problems by just copy / paste the 2nd code into the bottom of the first and commenting out the $INCLUDE line in the first.

Right buggy, I get 21 and congratulated then I am quizzed to Hit or Stand wo any other new info, like a card or a new hand and then I am told I busted... yikes!

Offline xra7en

  • Seasoned Forum Regular
  • Posts: 284
    • View Profile
Re: How I convert VERY VERY OLD spaghetti code (example included)
« Reply #7 on: June 10, 2019, 06:56:18 pm »
Hi xra7en,

Your Blackjack start is welcome on the Blackjack thread I started, I like comparing coding techniques.

BTW is that RANDOMIZE (-TIMER) ? or are my eyes going bluey...

Also I am not encountering any problems by just copy / paste the 2nd code into the bottom of the first and commenting out the $INCLUDE line in the first.

Right buggy, I get 21 and congratulated then I am quizzed to Hit or Stand wo any other new info, like a card or a new hand and then I am told I busted... yikes!

thanks for taking a look! LOL
yes that is (negative timer) do not remember where I learned that, but it was many years ago. and seemed to produce a better random. But we're talking basic games so, not to big issue.

Ya that "include" is mostly for a LORD color text i use a lot. and a few pascal tweaks that sorta work the way I want.

as far as the blackjack - I was home on break for a bit this weekend , so started a blackjack game - hopefully someone can finish it up, I may tonight, totally slammed with work this week(Im a maint tech for several properties) .  Its not too bad for a "seat-of-the-pants" coding LOL
The loop needs a tweak, I think there needs to be a "ROUND_OVER" true put somewhere, but I got a call so had to run.

If you want I can copy it over to the blackjack thread, I did not know there was one! I used to play BJ as a part time income, but my method got to the point I was winning good, but it was a method you don't want to use live :-)


but thanks for looking!!!!! :-)

I just like re-writing old DOS book games into modern QB64 code - weird hobby, I know!

Offline keybone

  • Forum Regular
  • Posts: 116
  • My name a Nursultan Tulyakbay.
    • View Profile
Re: How I convert VERY VERY OLD spaghetti code (example included)
« Reply #8 on: June 12, 2019, 03:36:37 am »
I have over the years of searching the net, have gathered a collection over 340+ BASIC books in PDF.
however with that said, not sure on the ethics of QB64 forums if it is allowed to share those.

I have pdf file of all these books
https://www.atariarchives.org/

and then another 300+ LOL

Does D.H.A have a blackjack version - I think he doesn in his BIG COMPUTER GAMES

That's your site? I love that site, been there a million times. :-D
I am from a Kazakhstan, we follow the hawk.

Offline xra7en

  • Seasoned Forum Regular
  • Posts: 284
    • View Profile
Re: How I convert VERY VERY OLD spaghetti code (example included)
« Reply #9 on: June 14, 2019, 09:03:25 am »
That's your site? I love that site, been there a million times. :-D

LOL
I left out a word I think wold have clarified this.


I have "ALSO" pdf file of all these books
https://www.atariarchives.org/
as well as another 300+ pdf

Either the books are considered "abandon" or atari archives got permission for all of those to be posted publicly.

I just like re-writing old DOS book games into modern QB64 code - weird hobby, I know!