From 45c937af1d757617c124d89be65a799dff9cd76a Mon Sep 17 00:00:00 2001 From: Svjatoslav Agejenko Date: Sun, 8 Sep 2024 15:11:45 +0300 Subject: [PATCH] Using AI to improve code readability --- Math/Lottery/loto.bas | 363 ++++++++++++++++--------------- Simulation/Explosion/explode.bas | 164 ++++++++++---- 2 files changed, 309 insertions(+), 218 deletions(-) diff --git a/Math/Lottery/loto.bas b/Math/Lottery/loto.bas index 629b9c5..b34eb94 100755 --- a/Math/Lottery/loto.bas +++ b/Math/Lottery/loto.bas @@ -1,23 +1,14 @@ -CHDIR ".\qbasicapps\math\loto" - - -' Lottery analyzer -' made by Svjatoslav Agejenko -' in 2001 -' homepage: svjatoslav.eu -' email: svjatoslav@svjatoslav.eu - -DECLARE SUB ennus () -DECLARE SUB menu () -DECLARE SUB gr1 () -DECLARE SUB gr2 () -DECLARE SUB getson (a$) -DECLARE SUB loaddata () -DECLARE SUB wai () -DECLARE SUB gr3 () +' Program to analyze lottery winning numbers. +' By Svjatoslav Agejenko. +' Email: svjatoslav@svjatoslav.eu +' Homepage: http://www.svjatoslav.eu +' +' Changelog: +' 2001, Initial version +' 2024.09, Improved program readability using AI + DEFINT A-Y -DECLARE SUB start () DIM SHARED mitus, sona$(1 TO 50) DIM SHARED num(1 TO 500, 1 TO 7) DIM SHARED mitun @@ -28,188 +19,216 @@ loaddata menu SUB ennus -DIM buf(1 TO 48) -PRINT "viimase 10 loosimise ajal:" - -FOR a = mitun - 10 TO mitun -FOR b = 2 TO 7 -buf(num(a, b)) = buf(num(a, b)) + 1 -NEXT b -NEXT a - -FOR b = 1 TO 6 -c = 0 -FOR a = 1 TO 48 -IF buf(a) > c THEN c = buf(a): d = a -NEXT a -PRINT d; " esines: "; c; " korda" -buf(d) = 0 -NEXT b - -PRINT "--------------------------------------" - -FOR a = 1 TO mitun -FOR b = 2 TO 7 -buf(num(a, b)) = a -NEXT b -NEXT a - -FOR b = 1 TO 6 -c = 30000 -FOR a = 1 TO 48 -IF buf(a) < c THEN c = buf(a): d = a -NEXT a -PRINT d; " esines viimati: "; mitun - c; " loosimist tagasi" -buf(d) = 30000 -NEXT b - + DIM buf(1 TO 48) + PRINT "During the last 10 draws:" + + ' Count occurrences of each number in the last 10 draws + FOR a = mitun - 10 TO mitun + FOR b = 2 TO 7 + buf(num(a, b)) = buf(num(a, b)) + 1 + NEXT b + NEXT a + + ' Print the most frequent numbers + FOR b = 1 TO 6 + c = 0 + FOR a = 1 TO 48 + IF buf(a) > c THEN c = buf(a): d = a + NEXT a + PRINT d; " appeared: "; c; " times" + buf(d) = 0 + NEXT b + + PRINT "--------------------------------------" + + ' Find when each number last appeared + FOR a = 1 TO mitun + FOR b = 2 TO 7 + buf(num(a, b)) = a + NEXT b + NEXT a + + FOR b = 1 TO 6 + c = 30000 + FOR a = 1 TO 48 + IF buf(a) < c THEN c = buf(a): d = a + NEXT a + PRINT d; " appeared last time: "; mitun - c; " draws ago" + buf(d) = 30000 + NEXT b END SUB SUB getson (a$) -mitus = 0 - -d = 1 -FOR b = 1 TO LEN(a$) -c$ = RIGHT$(LEFT$(a$, b), 1) -IF c$ = " " THEN -d = 1 -ELSE -IF d = 1 THEN -mitus = mitus + 1 -sona$(mitus) = "" -d = 0 -END IF -sona$(mitus) = sona$(mitus) + c$ -END IF -NEXT b + mitus = 0 + + d = 1 + FOR b = 1 TO LEN(a$) + c$ = RIGHT$(LEFT$(a$, b), 1) + IF c$ = " " THEN + d = 1 + ELSE + IF d = 1 THEN + mitus = mitus + 1 + sona$(mitus) = "" + d = 0 + END IF + sona$(mitus) = sona$(mitus) + c$ + END IF + NEXT b END SUB SUB gr1 -CLS -LINE (0, 0)-(600, 0), 1 -LINE (0, 49)-(600, 49), 1 -LINE (0, 50)-(600, 50), 1 -LINE (0, 48 * 6 + 51)-(600, 48 * 6 + 51), 1 -FOR a = 1 TO mitun -c = 0 -FOR b = 2 TO 7 -c = c + num(a, b) -PSET (a, num(a, b)), 15 -NEXT b -LINE (a, c + 50)-(a, 24 * 6 + 50), 10 -NEXT a -wai + CLS + LINE (0, 0)-(600, 0), 1 + LINE (0, 49)-(600, 49), 1 + LINE (0, 50)-(600, 50), 1 + LINE (0, 48 * 6 + 51)-(600, 48 * 6 + 51), 1 + + ' Draw vertical lines for each draw and plot numbers + FOR a = 1 TO mitun + c = 0 + FOR b = 2 TO 7 + c = c + num(a, b) + PSET (a, num(a, b)), 15 + NEXT b + LINE (a, c + 50)-(a, 24 * 6 + 50), 10 + NEXT a + + wai END SUB SUB gr2 -CLS -DIM buf(1 TO 20000) - -FOR x = 1 TO mitun - 1 -FOR a = 2 TO 7 -LINE (600, num(x, a))-(610, num(x + 1, a)), 3 -NEXT a -LINE (600, 1)-(600, 48), 1 -SOUND 0, 2 -GET (10, 1)-(610, 50), buf(1) -PUT (1, 1), buf(1), PSET -LINE (601, 1)-(610, 50), 0, BF -IF INKEY$ <> "" GOTO 5 -NEXT x -wai -5 + CLS + DIM buf(1 TO 20000) + + ' Draw lines connecting consecutive lottery numbers + FOR x = 1 TO mitun - 1 + FOR a = 2 TO 7 + LINE (600, num(x, a))-(610, num(x + 1, a)), 3 + NEXT a + LINE (600, 1)-(600, 48), 1 + SOUND 0, 2 + + ' Scroll the screen to the left + GET (10, 1)-(610, 50), buf(1) + PUT (1, 1), buf(1), PSET + LINE (601, 1)-(610, 50), 0, BF + + IF INKEY$ <> "" GOTO 5 + NEXT x + + wai + 5 END SUB SUB gr3 -CLS -PAINT (1, 1), 3 -DIM buf(1 TO 48) -DIM buf2(1 TO 20000) -c = 0 - -FOR a = 1 TO mitun -FOR b = 1 TO 48 -buf(b) = 0 -NEXT b - -FOR b = 2 TO 7 -buf(num(a, b)) = 10 -NEXT b - -FOR b = 1 TO 48 -c = c + 1 -buf2(c) = buf(b) -NEXT b -NEXT a - -FOR e = 2 TO 400 -d = 0 -FOR x = 1 TO 639 -FOR y = 1 TO e -d = d + 1 -IF d > c THEN GOTO 4 -PSET (x, y), buf2(d) -NEXT y -NEXT x -4 -LINE (x + 1, 1)-(x + 1, e), 14 -LINE (x + 1, y)-(x + 4, y), 12 -IF INKEY$ <> "" THEN GOTO 6 -NEXT e -wai -6 + CLS + PAINT (1, 1), 3 + DIM buf(1 TO 48) + DIM buf2(1 TO 20000) + + c = 0 + + ' Prepare data for drawing + FOR a = 1 TO mitun + FOR b = 1 TO 48 + buf(b) = 0 + NEXT b + + FOR b = 2 TO 7 + buf(num(a, b)) = 10 + NEXT b + + FOR b = 1 TO 48 + c = c + 1 + buf2(c) = buf(b) + NEXT b + NEXT a + + ' Draw the graph + FOR e = 2 TO 400 + d = 0 + FOR x = 1 TO 639 + FOR y = 1 TO e + d = d + 1 + IF d > c THEN GOTO 4 + PSET (x, y), buf2(d) + NEXT y + NEXT x + 4 + + LINE (x + 1, 1)-(x + 1, e), 14 + LINE (x + 1, y)-(x + 4, y), 12 + + IF INKEY$ <> "" THEN GOTO 6 + NEXT e + + wai + 6 END SUB SUB loaddata -PRINT "�ks hetk ...." - -mitun = 0 -OPEN "loos.txt" FOR INPUT AS #1 -1 -IF EOF(1) <> 0 THEN GOTO 2 -LINE INPUT #1, a$ -getson a$ -mitun = mitun + 1 -FOR b = 1 TO 7 -num(mitun, b) = VAL(sona$(b)) -NEXT b -GOTO 1 -2 -CLOSE #1 - -CLS + PRINT "One moment..." + + mitun = 0 + OPEN "loos.txt" FOR INPUT AS #1 + + 1 + IF EOF(1) <> 0 THEN GOTO 2 + + LINE INPUT #1, a$ + getson a$ + + mitun = mitun + 1 + + ' Parse and store lottery numbers + FOR b = 1 TO 7 + num(mitun, b) = VAL(sona$(b)) + NEXT b + + GOTO 1 + + 2 + CLOSE #1 + + CLS END SUB SUB menu 3 -CLS + CLS + 7 -LOCATE 1, 1 -PRINT " Viking loto statistika anal��s" -PRINT "1 - t�ppgraaf" -PRINT "2 - joongraaf" -PRINT "3 - sobitus" -PRINT "4 - statistika" -PRINT "5 - v�lja" -a$ = INPUT$(1) - -IF a$ = "1" THEN gr1 -IF a$ = "2" THEN gr2 -IF a$ = "3" THEN gr3 -IF a$ = "4" THEN ennus: GOTO 7 -IF a$ = "5" THEN SYSTEM -GOTO 3 + LOCATE 1, 1 + + PRINT " Lottery statistics analysis" + + PRINT "1 - Dot graph" + PRINT "2 - Line graph" + PRINT "3 - Combinatorics" + PRINT "4 - Statistics" + PRINT "5 - Exit program" + + a$ = INPUT$(1) + + IF a$ = "1" THEN gr1 + IF a$ = "2" THEN gr2 + IF a$ = "3" THEN gr3 + IF a$ = "4" THEN ennus: GOTO 7 + IF a$ = "5" THEN SYSTEM + + GOTO 3 END SUB SUB start -SCREEN 12 + SCREEN 12 END SUB SUB wai -a$ = INPUT$(1) -END SUB + a$ = INPUT$(1) +END SUB diff --git a/Simulation/Explosion/explode.bas b/Simulation/Explosion/explode.bas index 54e0482..a3c8e16 100755 --- a/Simulation/Explosion/explode.bas +++ b/Simulation/Explosion/explode.bas @@ -1,19 +1,22 @@ -' Material simulation, simulates shockwaves propagation in gas. -' made by Svjatoslav Agejenko -' in 2003 -' homepage: svjatoslav.eu -' email: svjatoslav@svjatoslav.eu - -DECLARE SUB saveit () -DECLARE SUB playit () -DECLARE SUB frmget () -DECLARE SUB frmsav () -DECLARE SUB spot (x!, y!, p!) -DECLARE SUB lin (x1!, y1!, x2!, y2!) -DECLARE SUB disp () -DECLARE SUB start () -DIM SHARED wal -wal = 9980 +' Program to simulate shock waves propagation in gas. +' By Svjatoslav Agejenko. +' Email: svjatoslav@svjatoslav.eu +' Homepage: http://www.svjatoslav.eu +' +' Changelog: +' 2003, Initial version +' 2024.09, Improved program readability using AI + +DECLARE SUB saveit() +DECLARE SUB playit() +DECLARE SUB frmget() +DECLARE SUB frmsav() +DECLARE SUB spot(x!, y!, p!) +DECLARE SUB lin(x1!, y1!, x2!, y2!) +DECLARE SUB disp() +DECLARE SUB start() +DIM SHARED wall +wall = 9980 DIM SHARED press(1 TO 100, 1 TO 100) DIM SHARED spdx(1 TO 100, 1 TO 100) @@ -29,7 +32,6 @@ PAINT (1, 1), 1 OPEN "tst.an0" FOR BINARY AS #1 - start 1 @@ -37,19 +39,37 @@ start FOR y = 2 TO 99 FOR x = 2 TO 99 - IF press(x, y) = wal THEN spdx(x - 1, y) = 0: spdy(x, y - 1) = 0: spdx(x, y) = 0: spdy(x, y) = 0: GOTO 3 - spdy(x, y) = spdy(x, y) - (press(x, y) / 500) ' gravitation + IF press(x, y) = wall THEN + spdx(x - 1, y) = 0 + spdy(x, y - 1) = 0 + spdx(x, y) = 0 + spdy(x, y) = 0 + GOTO 3 + END IF + + ' Apply gravitation + spdy(x, y) = spdy(x, y) - (press(x, y) / 500) + + IF press(x + 1, y) = wall THEN + spdx(x, y) = 0 + GOTO 2 + END IF - IF press(x + 1, y) = wal THEN spdx(x, y) = 0: GOTO 2 + ' Calculate x-direction speed spdx(x, y) = (press(x + 1, y) - press(x, y)) / 20 + spdx(x, y) 2 - IF press(x, y + 1) = wal THEN spdy(x, y) = 0: GOTO 3 + + IF press(x, y + 1) = wall THEN + spdy(x, y) = 0 + GOTO 3 + END IF + + ' Calculate y-direction speed spdy(x, y) = (press(x, y + 1) - press(x, y)) / 20 + spdy(x, y) 3 NEXT x NEXT y - 4 b = 0 FOR y = 2 TO 99 @@ -57,18 +77,43 @@ FOR y = 2 TO 99 a = press(x, y) + spdx(x, y) + spdy(x, y) - spdx(x - 1, y) - spdy(x, y - 1) IF a = 0 OR ((a < 0) AND (a > -.0001)) THEN - IF spdx(x, y) < 0 THEN spdx(x, y) = 0 - IF spdy(x, y) < 0 THEN spdy(x, y) = 0 - IF spdx(x - 1, y) > 0 THEN spdx(x - 1, y) = 0 - IF spdy(x, y - 1) > 0 THEN spdy(x, y - 1) = 0 + IF spdx(x, y) < 0 THEN + spdx(x, y) = 0 + END IF + + IF spdy(x, y) < 0 THEN + spdy(x, y) = 0 + END IF + + IF spdx(x - 1, y) > 0 THEN + spdx(x - 1, y) = 0 + END IF + + IF spdy(x, y - 1) > 0 THEN + spdy(x, y - 1) = 0 + END IF END IF IF a < 0 THEN - IF spdx(x, y) < 0 THEN spdx(x, y) = spdx(x, y) / 1.5 - IF spdy(x, y) < 0 THEN spdy(x, y) = spdy(x, y) / 1.5 - IF spdx(x - 1, y) > 0 THEN spdx(x - 1, y) = spdx(x - 1, y) / 1.5 - IF spdy(x, y - 1) > 0 THEN spdy(x, y - 1) = spdy(x, y - 1) / 1.5 + IF spdx(x, y) < 0 THEN + spdx(x, y) = spdx(x, y) / 1.5 + END IF + + IF spdy(x, y) < 0 THEN + spdy(x, y) = spdy(x, y) / 1.5 + END IF + + IF spdx(x - 1, y) > 0 THEN + spdx(x - 1, y) = spdx(x - 1, y) / 1.5 + END IF + + IF spdy(x, y - 1) > 0 THEN + spdy(x, y - 1) = spdy(x, y - 1) / 1.5 + END IF + b = 1 + + ' Display negative pressure LOCATE 20, 1 PRINT a END IF @@ -78,16 +123,33 @@ IF b = 1 THEN GOTO 4 FOR y = 2 TO 99 FOR x = 2 TO 99 - IF spdx(x, y) > 0 THEN spdxp(x - 1, y) = ((press(x, y) * spdx(x - 1, y)) + (spdx(x, y) * spdx(x, y))) / (press(x, y) + spdx(x, y)) - spdx(x - 1, y) - IF spdy(x, y) > 0 THEN spdyp(x, y - 1) = ((press(x, y) * spdy(x, y - 1)) + (spdy(x, y) * spdy(x, y))) / (press(x, y) + spdy(x, y)) - spdy(x, y - 1) - IF spdx(x - 1, y) < 0 THEN spdxp(x, y) = ((press(x, y) * spdx(x, y)) - (spdx(x - 1, y) * spdx(x - 1, y))) / (press(x, y) - spdx(x - 1, y)) - spdx(x, y) - IF spdy(x, y - 1) < 0 THEN spdyp(x, y) = ((press(x, y) * spdy(x, y)) - (spdy(x, y - 1) * spdy(x, y - 1))) / (press(x, y) - spdy(x, y - 1)) - spdy(x, y) + ' Update pressure based on speed + IF spdx(x, y) > 0 THEN + spdxp(x - 1, y) = ((press(x, y) * spdx(x - 1, y)) + (spdx(x, y) * spdx(x, y))) / + (press(x, y) + spdx(x, y)) - spdx(x - 1, y) + END IF + + IF spdy(x, y) > 0 THEN + spdyp(x, y - 1) = ((press(x, y) * spdy(x, y - 1)) + (spdy(x, y) * spdy(x, y))) / + (press(x, y) + spdy(x, y)) - spdy(x, y - 1) + END IF + + ' Handle negative speeds + IF spdx(x - 1, y) < 0 THEN + spdxp(x, y) = ((press(x, y) * spdx(x, y)) - (spdx(x - 1, y) * spdx(x - 1, y))) / + (press(x, y) - spdx(x - 1, y)) - spdx(x, y) + END IF + + IF spdy(x, y - 1) < 0 THEN + spdyp(x, y) = ((press(x, y) * spdy(x, y)) - (spdy(x, y - 1) * spdy(x, y - 1))) / + (press(x, y) - spdy(x, y - 1)) - spdy(x, y) + END IF NEXT x NEXT y - FOR y = 2 TO 99 FOR x = 2 TO 99 + ' Update pressure based on speed press(x + 1, y) = press(x + 1, y) - spdx(x, y) press(x, y + 1) = press(x, y + 1) - spdy(x, y) press(x, y) = press(x, y) + spdx(x, y) @@ -97,6 +159,7 @@ NEXT y FOR y = 2 TO 99 FOR x = 2 TO 99 + ' Update speed based on previous speed spdx(x, y) = spdx(x, y) + spdxp(x, y) spdxp(x, y) = 0 spdy(x, y) = spdy(x, y) + spdyp(x, y) @@ -104,18 +167,15 @@ FOR y = 2 TO 99 NEXT x NEXT y - FOR y = 1 TO 100 FOR x = 1 TO 100 + ' Draw pixel based on pressure PSET (x, y), press(x, y) + 16 NEXT x NEXT y saveit - - - GOTO 1 CLOSE #1 @@ -123,6 +183,7 @@ CLOSE #1 SUB disp FOR y = 47 TO 53 FOR x = 47 TO 53 + ' Display pressure values in a grid LOCATE y - 46, (x - 46) * 4 PRINT press(x, y) NEXT x @@ -132,38 +193,46 @@ a$ = INPUT$(1) END SUB -SUB lin (x1, y1, x2, y2) +SUB lin(x1, y1, x2, y2) m = ABS(x1 - x2) m1 = ABS(y1 - y2) -IF m1 > m THEN m = m1 +IF m1 > m THEN + m = m1 +END IF +' Calculate line coordinates x3 = x2 - x1 y3 = y2 - y1 FOR a = 0 TO m + ' Interpolate points along the line x5 = x3 * a / m + x1 y5 = y3 * a / m + y1 - press(x5, y5) = wal -NEXT a + ' Set pressure to wall value + press(x5, y5) = wall +NEXT a END SUB SUB saveit FOR y = 1 TO 100 a$ = "" + FOR x = 1 TO 100 + ' Save pixel color as string a$ = a$ + CHR$(POINT(x, y)) NEXT x + linb = a$ PUT #1, , linb NEXT y - END SUB -SUB spot (x, y, p) +SUB spot(x, y, p) +' Set pressure to value `p` in a 2x2 area press(x, y) = p press(x + 1, y) = p press(x, y + 1) = p @@ -175,6 +244,7 @@ frm = 0 FOR a = 1 TO 100 FOR b = 1 TO 100 + ' Initialize pressure and speed variables press(a, b) = 3 spdx(a, b) = 0 spdy(a, b) = 0 @@ -185,18 +255,20 @@ NEXT a FOR y = 30 TO 60 FOR x = 10 TO 50 + ' Create initial pressure spots spot x, y, 30 NEXT x NEXT y +' Draw boundary lines lin 2, 2, 2, 99 lin 99, 2, 99, 99 lin 2, 99, 99, 99 lin 2, 2, 99, 2 FOR x = 5 TO 40 STEP 5 + ' Draw additional lines for testing lin x, 80, x + 50, 80 - x NEXT x END SUB - -- 2.20.1