From: Svjatoslav Agejenko Date: Wed, 23 Oct 2024 17:26:22 +0000 (+0300) Subject: Using AI to improve code readability X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=commitdiff_plain;h=ac3f3d53d137e0cf3cfd7f1f2c851ba2db75e51d;p=qbasicapps.git Using AI to improve code readability --- diff --git a/Games/Worm/worm.bas b/Games/Worm/worm.bas index be70a23..5248821 100755 --- a/Games/Worm/worm.bas +++ b/Games/Worm/worm.bas @@ -8,7 +8,7 @@ DECLARE SUB stat () ' Changelog: ' 2002, Initial version -' 2024.09, Improved program readability using AI +' 2024, Improved program readability using AI DECLARE FUNCTION cnum$ (a%) DECLARE SUB putworm (a%) @@ -26,6 +26,7 @@ DECLARE SUB show () DECLARE SUB sc (x%, y%) DEFINT A-Z +' Shared arrays for the game grid and worms DIM SHARED buf(0 TO 36, 0 TO 36) DIM SHARED buf2(0 TO 36, 0 TO 36) DIM SHARED ussx(1 TO 2000, 1 TO 5) @@ -36,30 +37,35 @@ DIM SHARED usss(1 TO 5) DIM SHARED ussk(1 TO 2000, 1 TO 5) DIM SHARED usskp(1 TO 5) +' Variables for worm positions and game state DIM SHARED ux(1 TO 5), uy(1 TO 5), uxp(1 TO 5), uyp(1 TO 5) DIM SHARED mtm -DIM SHARED ussm +DIM SHARED playerCount DIM SHARED elud(1 TO 5) DIM SHARED auto(1 TO 5) -DIM SHARED ail, lvl, wai +DIM SHARED ail +DIM SHARED lvl +DIM SHARED wai DIM SHARED elum DIM SHARED spd -ussm = 1 +playerCount = 1 ail = 10 lvl = 1 wai = 20 +' Array defines if player is human (0) or computer (1). auto(1) = 0 auto(2) = 0 auto(3) = 0 auto(4) = 0 auto(5) = 0 + CLS -INPUT "How many players (1 - 5):", ussm +INPUT "How many players (1 - 5):", playerCount INPUT "How many of them are computers:", a -FOR b = ussm TO ussm - a + 1 STEP -1 +FOR b = playerCount TO playerCount - a + 1 STEP -1 auto(b) = 1 NEXT b @@ -376,7 +382,7 @@ SUB level (a%) ' Initialize the worms for each player. show - FOR b = 1 TO ussm + FOR b = 1 TO playerCount ussl(b) = 0 putworm b NEXT b @@ -386,43 +392,43 @@ END SUB SUB prc (a%) ' This subroutine handles the main game loop for each worm. - subt a + subt a% - ussp(a) = ussp(a) + 1 + ussp(a%) = ussp(a%) + 1 ' If the worm has no lives left, end its turn. - IF elud(a) = 0 THEN + IF elud(a%) = 0 THEN GOTO 4 END IF ' If the worm is controlled by AI, recalculate its path. - IF auto(a) = 1 THEN - autop a + IF auto(a%) = 1 THEN + autop a% END IF ' Move the worm based on user input or AI direction. - ux(a) = ux(a) + uxp(a) - uy(a) = uy(a) + uyp(a) + ux(a%) = ux(a%) + uxp(a%) + uy(a%) = uy(a%) + uyp(a%) ' Wrap around the edges of the grid. - IF ux(a) = 35 THEN - ux(a) = 2 + IF ux(a%) = 35 THEN + ux(a%) = 2 END IF - IF uy(a) = 35 THEN - uy(a) = 2 + IF uy(a%) = 35 THEN + uy(a%) = 2 END IF - IF ux(a) = 1 THEN - ux(a) = 34 + IF ux(a%) = 1 THEN + ux(a%) = 34 END IF - IF uy(a) = 1 THEN - uy(a) = 34 + IF uy(a%) = 1 THEN + uy(a%) = 34 END IF - x = ux(a) - y = uy(a) + x = ux(a%) + y = uy(a%) 3 IF buf(x, y) = 2 THEN @@ -432,11 +438,11 @@ SUB prc (a%) stuff - ussl(a) = ussl(a) + mtm + ussl(a%) = ussl(a%) + mtm - usss(a) = usss(a) + mtm + usss(a%) = usss(a%) + mtm - FOR b = 1 TO ussm + FOR b = 1 TO playerCount IF (elud(b) > 0) AND (auto(b) = 1) THEN ai b END IF @@ -448,65 +454,65 @@ SUB prc (a%) END IF IF buf(x, y) > 0 THEN - dead a + dead a% GOTO 4 END IF - IF a = 1 THEN + IF a% = 1 THEN buf(x, y) = 10 - ELSEIF a = 2 THEN + ELSEIF a% = 2 THEN buf(x, y) = 11 - ELSEIF a = 3 THEN + ELSEIF a% = 3 THEN buf(x, y) = 12 - ELSEIF a = 4 THEN + ELSEIF a% = 4 THEN buf(x, y) = 13 - ELSEIF a = 5 THEN + ELSEIF a% = 5 THEN buf(x, y) = 14 END IF sc x, y - IF ussp(a) > 2000 THEN - ussp(a) = ussp(a) - 2000 + IF ussp(a%) > 2000 THEN + ussp(a%) = ussp(a%) - 2000 END IF - ussx(ussp(a), a) = x + ussx(ussp(a%), a%) = x - ussy(ussp(a), a) = y + ussy(ussp(a%), a%) = y 4 END SUB SUB putworm (a%) ' This subroutine initializes a new worm for a player. - b = ussl(a) + b = ussl(a%) ' Move the worm back to its starting position. FOR c = b TO 1 STEP -1 - ussl(a) = c + ussl(a%) = c - subt a + subt a% NEXT c 9 - uy(a) = INT(RND * 30 + 2) + uy(a%) = INT(RND * 30 + 2) - ux(a) = INT(RND * 10 + 5) + ux(a%) = INT(RND * 10 + 5) ' Ensure that the worm starts in an empty space. - FOR b = ux(a) TO ux(a) + 10 - IF buf(b, uy(a)) <> 0 THEN + FOR b = ux(a%) TO ux(a%) + 10 + IF buf(b, uy(a%)) <> 0 THEN GOTO 9 END IF NEXT b ' Set the initial direction of the worm. - uxp(a) = 1 + uxp(a%) = 1 - uyp(a) = 0 + uyp(a%) = 0 - ussl(a) = 3 + ussl(a%) = 3 stat END SUB @@ -647,7 +653,7 @@ SUB start uy(5) = 25 - FOR a = 1 TO ussm + FOR a = 1 TO playerCount ux(a) = 15 uxp(a) = 1 @@ -755,7 +761,7 @@ END SUB SUB subt (b%) ' This subroutine updates the worm's position on the grid. - a = ussp(b) - ussl(b) + a = ussp(b%) - ussl(b%) IF a < 1 THEN a = a + 2000 @@ -838,7 +844,7 @@ SUB tkt stat END IF - FOR a = 1 TO ussm + FOR a = 1 TO playerCount prc a NEXT a END SUB