From a291f325ff4dd60a5e3a48acc614dbd1ee38aaa5 Mon Sep 17 00:00:00 2001 From: Svjatoslav Agejenko Date: Mon, 12 Aug 2024 13:14:04 +0300 Subject: [PATCH] Using AI to improve code readability --- Miscellaneous/font.bas | 142 +++++++++++++++++++++++++++------------- Miscellaneous/key.bas | 30 +++++++-- Miscellaneous/modes.bas | 80 +++++++++++++--------- 3 files changed, 168 insertions(+), 84 deletions(-) diff --git a/Miscellaneous/font.bas b/Miscellaneous/font.bas index 0c005e5..4370f6a 100644 --- a/Miscellaneous/font.bas +++ b/Miscellaneous/font.bas @@ -1,61 +1,111 @@ +' Program author: ' Svjatoslav Agejenko -' year: 2001 -' alien text +' email: svjatoslav@svjatoslav.eu +' homepage: https://svjatoslav.eu +' +' Program attempts to render imaginary alien text. +' Text is composed by subdividing square into 4 triangles. +' +' 2001, Initial version. +' 2024.08, Improved code readability. DEFINT A-Z -DECLARE SUB prch (x, y, n, s) -DIM SHARED co(1 TO 4) -co(1) = 7 -co(2) = 7 -co(3) = 0 -co(4) = 0 +' Declare the subroutine which will draw a character at a given position with a given size and color +DECLARE SUB DrawCharacter (characterX, characterY, characterColor, CharacterSize) +' Define shared array for colors +DIM SHARED characterColors(1 TO 4) AS INTEGER + +' Initialize the color palette +characterColors(1) = 7 ' LightGray +characterColors(2) = 7 ' LightGray +characterColors(3) = 0 ' Black +characterColors(4) = 0 ' Black + +' Set the screen mode and seed the random number generator SCREEN 12 RANDOMIZE TIMER +' Clear the screen with black color PAINT (1, 1), 0 -siz = 4 +' Define the size of each character +CONST CharacterSize = 4 +' Initialize counters for drawing characters +DIM tmp AS INTEGER tmp = 0 -FOR y = 1 TO 480 - siz - 2 STEP siz + (siz \ 2) -tmp1 = 0 -FOR x = 1 TO 640 - siz - 2 STEP siz + (siz \ 2) -prch x, y, RND * 16, siz -tmp1 = tmp1 + 1 -IF tmp1 > 20 THEN tmp1 = 0: x = x + (siz) -NEXT x -tmp = tmp + 1 -IF tmp > 5 THEN tmp = 0: y = y + (siz) -NEXT y - -SUB prch (x, y, n, s) -sp = s \ 2 - -c = co(RND * 3 + 1) -LINE (x, y)-(x + s, y), c -LINE (x, y)-(x + sp, y + sp), c -LINE (x + s, y)-(x + sp, y + sp), c -PAINT (x + 2, y + 1), c - -c = co(RND * 3 + 1) -LINE (x, y)-(x, y + s), c -LINE (x, y)-(x + sp, y + sp), c -LINE (x, y + s)-(x + sp, y + sp), c -PAINT (x + 1, y + 2), c - -c = co(RND * 3 + 1) -LINE (x + s, y)-(x + s, y + s), c -LINE (x + s, y)-(x + sp, y + sp), c -LINE (x + s, y + s)-(x + sp, y + sp), c -PAINT (x + s - 1, y + 2), c - -c = co(RND * 3 + 1) -LINE (x, y + s)-(x + s, y + s), c -LINE (x, y + s)-(x + sp, y + sp), c -LINE (x + s, y + s)-(x + sp, y + sp), c -PAINT (x + 2, y + s - 1), c +' Outer loop for vertical positioning of characters +FOR characterY = 1 TO 480 - CharacterSize - 2 STEP CharacterSize + (CharacterSize \ 2) + DIM tmp1 AS INTEGER + tmp1 = 0 + + ' Inner loop for horizontal positioning of characters + FOR characterX = 1 TO 640 - CharacterSize - 2 STEP CharacterSize + (CharacterSize \ 2) + ' Draw a character with random color and specified size at the current position + CALL DrawCharacter(characterX, characterY, INT(RND * 16), CharacterSize) + + ' Increment the inner loop counter + tmp1 = tmp1 + 1 + + ' Add spaces to emulate visual character clusters + IF tmp1 > 20 THEN + tmp1 = 0 + characterX = characterX + (CharacterSize) + END IF + NEXT characterX + + ' Increment the outer loop counter + tmp = tmp + 1 + + ' Add space to group caracters visually into clusters + IF tmp > 5 THEN + tmp = 0 + characterY = characterY + (CharacterSize) + END IF +NEXT characterY + +' Subroutine to draw a character at a given position with a given size and color +SUB DrawCharacter (characterX AS INTEGER, characterY AS INTEGER, characterColor AS INTEGER, CharacterSize AS INTEGER) + ' Calculate half the size of the character for drawing diagonals + DIM halfSize AS INTEGER + halfSize = CharacterSize \ 2 + + ' Randomly select a color from the palette + DIM randomColor AS INTEGER + randomColor = characterColors(INT(RND * 3) + 1) + + ' Draw the top horizontal line and diagonals of the character + LINE (characterX, characterY)-(characterX + CharacterSize, characterY), randomColor + LINE (characterX, characterY)-(characterX + halfSize, characterY + halfSize), randomColor + LINE (characterX + CharacterSize, characterY)-(characterX + halfSize, characterY + halfSize), randomColor + ' Fill the top right corner of the character + PAINT (characterX + 2, characterY + 1), randomColor + + ' Draw the left vertical line and diagonals of the character + randomColor = characterColors(INT(RND * 3) + 1) + LINE (characterX, characterY)-(characterX, characterY + CharacterSize), randomColor + LINE (characterX, characterY)-(characterX + halfSize, characterY + halfSize), randomColor + LINE (characterX, characterY + CharacterSize)-(characterX + halfSize, characterY + halfSize), randomColor + ' Fill the middle left of the character + PAINT (characterX + 1, characterY + 2), randomColor + + ' Draw the right vertical line and diagonals of the character + randomColor = characterColors(INT(RND * 3) + 1) + LINE (characterX + CharacterSize, characterY)-(characterX + CharacterSize, characterY + CharacterSize), randomColor + LINE (characterX + CharacterSize, characterY)-(characterX + halfSize, characterY + halfSize), randomColor + LINE (characterX + CharacterSize, characterY + CharacterSize)-(characterX + halfSize, characterY + halfSize), randomColor + ' Fill the middle right of the character + PAINT (characterX + CharacterSize - 1, characterY + 2), randomColor + + ' Draw the bottom horizontal line and diagonals of the character + randomColor = characterColors(INT(RND * 3) + 1) + LINE (characterX, characterY + CharacterSize)-(characterX + CharacterSize, characterY + CharacterSize), randomColor + LINE (characterX, characterY + CharacterSize)-(characterX + halfSize, characterY + halfSize), randomColor + LINE (characterX + CharacterSize, characterY + CharacterSize)-(characterX + halfSize, characterY + halfSize), randomColor + ' Fill the bottom left corner of the character + PAINT (characterX + 2, characterY + CharacterSize - 1), randomColor END SUB diff --git a/Miscellaneous/key.bas b/Miscellaneous/key.bas index e55f01b..35202cb 100644 --- a/Miscellaneous/key.bas +++ b/Miscellaneous/key.bas @@ -1,10 +1,26 @@ -1 -a$ = INKEY$ -IF a$ = "" THEN GOTO 1 -PRINT a$ -PRINT ASC(LEFT$(a$, 1)) -PRINT ASC(RIGHT$(a$, 1)) +' Utility to print information about pressed keyboard buttons +' +MainLoop: + ' Wait for a key press and store it in userInput$ + userInput$ = INKEY$ -GOTO 1 + ' If no key has been pressed, jump back to the main loop + IF userInput$ = "" THEN GOTO MainLoop + ' Print the character that was typed by the user + PRINT "You typed: "; userInput$ + + ' Calculate the ASCII value of the first character (if input is more than one character) + ' and print it + IF LEN(userInput$) > 0 THEN + PRINT "ASCII value of the first character ('"; LEFT$(userInput$, 1); "'): "; ASC(LEFT$(userInput$, 1)) + END IF + + ' Calculate the ASCII value of the last character and print it + IF LEN(userInput$) > 0 THEN + PRINT "ASCII value of the last character ('"; RIGHT$(userInput$, 1); "'): "; ASC(RIGHT$(userInput$, 1)) + END IF + + ' Jump back to the main loop to wait for another key press + GOTO MainLoop \ No newline at end of file diff --git a/Miscellaneous/modes.bas b/Miscellaneous/modes.bas index aef4458..cbd5d8b 100644 --- a/Miscellaneous/modes.bas +++ b/Miscellaneous/modes.bas @@ -1,35 +1,53 @@ -' Determine avaiable video modes -' made by Svjatoslav Agejenko -' in 2001 -' homepage: svjatoslav.eu -' email: svjatoslav@svjatoslav.eu - -' program to determine avaiable video modes -' it assumes that 1 is always avaiable - -DIM SHARED mo(1 TO 100) -ON ERROR GOTO 1 -b = 1 -a = 0 -2 -SCREEN a -'PRINT "mode", a -'a$ = INPUT$(1) -mo(b) = a -b = b + 1 -a = a + 1 -GOTO 2 - - -1 -a = a + 1 -IF a > 1000 THEN +' Determine available video modes +' Improved and commented version of the original program by Svjatoslav Agejenko (2001) +' Homepage: svjatoslav.eu +' Email: svjatoslav@svjatoslav.eu + +' This program determines the available video modes on a computer. +' It is based on the assumption that video mode 1 is always available. + +DIM SHARED AvailableModes(1 TO 100) AS INTEGER +ON ERROR GOTO ErrorHandler + +' Initialize the video mode counter and the current mode number. +currentModeIndex = 1 +currentModeNumber = 0 + +' Start the loop to test each video mode. +DO + ' Attempt to set the screen to the current video mode. + SCREEN currentModeNumber + + ' Increment the mode number for the next iteration. + currentModeNumber = currentModeNumber + 1 + + ' Store the successful video mode in the array. + AvailableModes(currentModeIndex) = currentModeNumber - 1 + + ' Move to the next index in the array. + currentModeIndex = currentModeIndex + 1 + +LOOP + +' Error handling routine when an error occurs (e.g., invalid video mode). +ErrorHandler: +' Increment the mode number to continue testing after an error. +currentModeNumber = currentModeNumber + 1 + +' Check if we have reached the maximum number of modes to test. +IF currentModeNumber > 1000 THEN + ' Reset the screen to text mode (usually mode 1). SCREEN 1 - PRINT "Avaiable video modes on this computer:" - FOR a = 1 TO b - 1 - PRINT mo(a) - NEXT a + + ' Display the list of available video modes. + PRINT "Available video modes on this computer:" + FOR modeIndex = 1 TO currentModeIndex - 1 + PRINT AvailableModes(modeIndex) + NEXT modeIndex + + ' End the program after displaying the results. END END IF -RESUME +' Resume execution after an error to continue testing modes. +RESUME \ No newline at end of file -- 2.20.1