Craft Basic

Started by lucidapogee, February 07, 2023, 00:48:55

Previous topic - Next topic

lucidapogee

#60
Working on Commando Basic.

I added the command ERASEARRAY to clear the contents of any numerical or string array.

DIM list[1,3,4,6,7,43,43,43,65,7,8,9,0]
ERASEARRAY list

I also added the ability to break up dim lists to multiple lines.

dim l[2, 3, 5, 6, 8,
10, 11, 15, 19, 20]

Example:
dim l[
2, 3, 5, 6, 8,
10, 11, 15, 19, 20]

dim l[
2, 3, 5, 6, 8,
10, 11, 15, 19, 20
]


These features will probably be worked into the next Craft Basic update. Whenever that may be.

Also, here's a code preview of a snake game in Commando Basic.

rem Snake example game for Commando Basic
rem by Gemino Smothers 2023
rem www.lucidapogee.com

title "Hungry Snake!"

define gfxx = 320, gfxy = 200
define upkey = 0, rightkey = 0, downkey = 0, leftkey = 0, esckey = 0
define speed = .001, delay = 0, score = 0, game = 1
define maxsize = 1000, size = 19, direction = int(rnd * 4) + 1
define rx = int(rnd * (gfxx - 4)) + 1
define ry = int(rnd * (gfxy - 11)) + 8

gosub setup
gosub intro

do

        gosub handleinput
        gosub drawsnake
        gosub movesnake
        gosub checkgame
        gosub drawmouse
        gosub drawhud
        gosub flipgraphics

loop k <> 27 and game = 1

gosub outro
end

sub handleinput

        let k = key

        if (k = 87 or k = 119) and  direction <> 3 then

                let direction = 1

        endif

        if (k = 68 or k = 100) and direction <> 4 then

                let direction = 2

        endif

        if (k = 83 or k = 115) and direction <> 1 then

                let direction = 3

        endif

        if (k = 65 or k = 97) and direction <> 2 then

                let direction = 4

        endif

return

sub drawsnake

        fgcolor 0, 255, 0

        let i = size + 1

        do

                let i = i - 1
                let c = i - 1

                if sx[0] = sx[i] and sy[0] = sy[i] then

                        let game = 0

                endif

                let sx[i] = sx[c]
                let sy[i] = sy[c]

                dot sx[i], sy[i]

        loop i > 1

return

sub movesnake

        if direction = 1 then

                let sy[0] = sy[0] - 2

        endif

        if direction = 2 then

                let sx[0] = sx[0] + 2

        endif

        if direction = 3 then

                let sy[0] = sy[0] + 2

        endif

        if direction = 4 then

                let sx[0] = sx[0] - 2

        endif

return

sub checkgame

        if sx[0] < 0 or sx[0] >= gfxx or sy[0] < 0 or sy[0] >= gfxy then

                let game = 0

        endif

        if (sx[0] >= rx  and sy[0] >= ry and sx[0] <= rx + 3 and sy[0] <= ry + 3) then

                cls

                let rx = int(rnd * (gfxx - 4)) + 1
                let ry = int(rnd * (gfxy - 11)) + 8

                let size = size + 5
                let score = score + 1

        endif

return

sub drawmouse

        fgcolor 255, 255, 255
        circle rx, ry, 1

return

sub drawhud

        fgcolor 255, 255, 0
        cursor 1, 1
        print "Score: ", score, " ", rx, " ", ry

return

sub flipgraphics

        let delay = clock
        do
        loop clock < delay + speed
        flip
        cls

return

sub setup

        dim sx[maxsize]
        dim sy[maxsize]

        let sx[0] = gfxx / 2
        let sy[0] = gfxy / 2

        print "Enter 0 for windowed", comma, " 1 for fullscreen", comma, " or 2 to exit: ",
        input w

        if w = 2 then

                end

        endif

        close console
        open graphics, gfxx, gfxy, 32, w

return

sub intro

        bgcolor 128, 64, 0
        cls
        fgcolor 255, 255, 0
        cursor 15, 3
        print "Hungry Snake!"
        cursor 10, 5
        print "by Gemino Smothers 2023"
        cursor 12, 6
        print "www.lucidapogee.com"
        fgcolor 0, 200, 255
        cursor 15, 9
        print "Get the mouse."
        cursor 15, 10
        print "Avoid crashing."
        cursor 14, 12
        print "W", comma, "A", comma, "S", comma, " D to move."
        cursor 16, 13
        print "Esc to exit."
        fgcolor 255, 255, 255
        cursor 10, 16
        print "Press any key to play..."
        flip
        pause

return

sub outro

        cls
        cursor 12, 5
        print "Game Over! Score: ", score
        cursor 12, 7
        print "Thanks for playing!"
        flip

        let t = clock

        do
        loop clock < t + 5 or key

        cursor 12, 10
        print "Press any key to exit..."
        flip
        pause

return
Ebox Thin Client with Windows 95
EEE PC 701SD with Windows XP
Atari 1040STFM with GEM/TOS
Playstation 2 with FreeMcBoot Yabasic
Keyboard Famiclones with GBasic and FBasic
Xerox Sunrise 1800 with MSBasic and CP/M

lucidapogee

#61
I am working on the KEY function in Commando Basic. It is based on QBasic style INKEY$, expect KEY returns a number and not a string. This works by returning the ASCII value of any key being pressed or 0 if no key is pressed.

The problem I was having is that only keys that mach their ascii character were showing up. This is due to the fact that some keys such as the arrows have an extra null character in front and are actually two characters long. I was getting a 0 for these keys.

What I did to "fix" this was to check the length of input and return a negative equivalent when there's a nbsp character. This allows to differentiate between keys. Now the language may detect arrow keys and not just wasd.

Here's what I did in FreeBasic:

executekey:

        LET keyinput$ = INKEY$

        IF LEN(keyinput$) = 1 THEN

                LET evalstack$(pointer) = STR$(ASC(keyinput$))

        ELSEIF LEN(keyinput$) = 2 THEN

                LET evalstack$(pointer) = STR$(-ASC(RIGHT$(keyinput$, 1)))

        ELSE

                LET evalstack$(pointer) = "0"

        ENDIF

RETURN

Here's an example of the key function in use:
do

        let k = key

        if k then

                print k

         endif

loop k <> 27

end

If you press a, b, c, d, you get 97, 98, 99, and 100.
If you press up, right, down, left arrow keys, you get -72, -77, -80, and -75.

I will document all of the keys for easy reference.
Ebox Thin Client with Windows 95
EEE PC 701SD with Windows XP
Atari 1040STFM with GEM/TOS
Playstation 2 with FreeMcBoot Yabasic
Keyboard Famiclones with GBasic and FBasic
Xerox Sunrise 1800 with MSBasic and CP/M

lucidapogee

I made another ridiculous mistake with the last version of Craft Basic, so this time I made some major changes with the IDE. No more line numbers. There's now a status bar that pretty much mimics Notepad. You can get the line number from there. When the interpreter is running a program, the status bar shows the current line being executed. When in distribution mode, programs do not show the status bar.

This release also comes with a new example program demonstrating a 3d star field.

The reason this is a major update is I accidentally included a commercial font with the last update. I had to rush this update to fix that. I had also broken some compatibility with the IDE in Windows 95. Craft Basic should work perfectly on 95 to 10 now.

I consider this to be its most stable release yet. Download it.
I made another ridiculous mistake with the last version, so this time I made some major changes with the IDE. No more line numbers. There's now a status bar that pretty much mimics Notepad. You can get the line number from there. When the interpreter is running a program, the status bar shows the current line being executed. When in distribution mode, programs do not show the status bar.

This release also comes with a new example program demonstrating a 3d star field.

The reason this is a major update is I accidentally included a commercial font with the last update. I had to rush this update to fix that. I had also broken some compatibility with the IDE in Windows 95. Craft Basic should work perfectly on 95 to 10 now.

I consider this to be its most stable release yet. Download it.
Ebox Thin Client with Windows 95
EEE PC 701SD with Windows XP
Atari 1040STFM with GEM/TOS
Playstation 2 with FreeMcBoot Yabasic
Keyboard Famiclones with GBasic and FBasic
Xerox Sunrise 1800 with MSBasic and CP/M

lucidapogee

#63
I have solved more tasks on Rosetta Code with Craft Basic. These snippets will be included with the next version of Craft Basic (whenever it is released), but are uploaded to Rosetta code for now.

'roots of unity example

define theta = 0, real = 0, imag = 0
define pi = 3.14, n = 5

for m = 0 to n - 1

 let theta = m * (pi * 2) / n
 let real = cos(theta)
 let imag = sin(theta)

 if imag >= 0 then

 print real, comma, " ", imag, "i"

 else

 print real, comma, " ", imag * -1, "i"

 endif

 wait

next m

end

'department numbers example

print "P S F"

for p = 2 to 7 step 2

 for s = 1 to 7

 if s <> p then

 let f = (12 - p) - s

 if f > 0 and f <= 7 and f <> s and f <> p then

 print  p, " ", s, " ", f

 endif

 endif

 next s

next p

end

'permutations example

let n = 3
let i = n + 1

dim a[i]

for i = 1 to n

 let a[i] = i

next i

do

 for i = 1 to n

 print a[i]

 next i

 print

 let i = n

 do

 let i = i - 1
 let b = i + 1

 loopuntil (i = 0) or (a[i] < a[b])

 let j = i + 1
 let k = n

 do

 if j < k then

 let t = a[j]
 let a[j] = a[k]
 let a[k] = t
 let j = j + 1
 let k = k - 1

 endif

 loop j < k

 if i > 0 then

 let j = i + 1

 do

 if a[j] < a[i] then

 let j = j + 1

 endif

 loop a[j] < a[i]

 let t = a[j]
 let a[j] = a[i]
 let a[i] = t

 endif

loopuntil i = 0

end

'sum of digits example

define number = 0, base = 0, sum = 0

input "number: ", number
input "base: ", base

if number < 0 then

 let number = number * -1

endif

if base < 2 then

 let base = 2

endif

do

 if number > 0 then

 let sum = sum + number % base
 let number = int(number / base)

 endif

loop number > 0

print "sum of digits in base ", base, ": ", sum

end

'iterated digits squaring example

for i = 1 to 1000

 let j = i

 do

 let k = 0

 do

 let k = int(k + (j % 10) ^ 2)
 let j = int(j / 10)

 wait

 loop j <> 0

 let j = k

 loopuntil j = 89 or j = 1

 if j > 1 then

 let n = n + 1

 endif

 print "iterations: ", i

next i

print "count result: ", n

end

'munchausen numbers example

for i = 0 to 5

for j = 0 to 5

for k = 0 to 5

for l = 0 to 5

let s = i
gosub sign
let m = int(i ^ i * s)

let s = j
gosub sign
let m = m + int(j ^ j * s)

let s = k
gosub sign
let m = m + int(k ^ k * s)

let s = l
gosub sign
let m = m + int(l ^ l * s)

let n = 1000 * i + 100 * j + 10 * k + l

if m = n and m > 0 then

print m

endif
               
wait

next l

next k

next j

next i

end

sub sign

if s <> 0 then

if s < 0 then

let s = -1

else

let s = 1

endif

endif

return
Ebox Thin Client with Windows 95
EEE PC 701SD with Windows XP
Atari 1040STFM with GEM/TOS
Playstation 2 with FreeMcBoot Yabasic
Keyboard Famiclones with GBasic and FBasic
Xerox Sunrise 1800 with MSBasic and CP/M

lucidapogee

Here's a few more.

'munching squares example

let s = 255

for y = 0 to s

for x = 0 to s

let r = x ~ y
fgcolor  r, r * 2, r * 3
dot x, y

wait

next x

next y

end

'harshard numbers example

for i = 1 to 1002

let t = i
let s = 0

do

let s = s + t % 10
let t = int(t / 10)

wait

loop t > 0

if i % s = 0 and (c < 20 or i > 1000) then

let c = c + 1
print c, " : ", i

endif

next i

end

Ebox Thin Client with Windows 95
EEE PC 701SD with Windows XP
Atari 1040STFM with GEM/TOS
Playstation 2 with FreeMcBoot Yabasic
Keyboard Famiclones with GBasic and FBasic
Xerox Sunrise 1800 with MSBasic and CP/M

Pfaber11

Craft basic looks like a great language to learn for people starting out with BASIC. Very nice to read. Commando Basic would require some learning on my part. 
HP 15s i3 1.2 upto 3.4 ghz 128 gb ssd 16 gb ram 15.6 inch screen. Windows 11 home edition .  2Tb external hard drive dedicated to Linux Mint .
  PureBasic 6 and AppGameKit studio
ASUS Vivo book 15 16gb ram 256gb storage  cpu upto 4.1 ghz
HP Desktop AMD 3700 16GB ram 2 GB graphics card windows 10

lucidapogee

#66
Quote from: Pfaber11 on June 11, 2023, 09:04:44Craft basic looks like a great language to learn for people starting out with BASIC. Very nice to read. Commando Basic would require some learning on my part.
My goal is to continue developing Craft Basic over time.
I am still working on Commando Basic. Trying to not rush it. Right now, I am mostly focused on writing a book that will come with the whole package. The book will be a beginners introduction to programming using Commando Basic.



Here's a few more examples translated from Rosetta Code tasks. I'm almost up to 50 tasks complete with Craft Basic! I also have a to do list going for the next version. The next version will include new keywords and more examples.

'dot product example

dim a[1, 3, -5]
dim b[4, -2, -1]

arraysize n, a

for i = 0 to n - 1

    let s = s + a[i] * b[i]

next i

print s

end

'van eck sequence example

define limit = 1000

dim list[limit]

print "calculating van eck sequence..."

for n = 0 to limit - 1

    for m = n - 1 to 0 step -1

        if list[m] = list[n] then

            let c = n + 1
            let list[c] = n - m

            break m

        endif

        wait

    next m

next n

print "first 10 terms: "

for i = 0 to 9

    print list[i]

next i

print "terms 991 to 1000: "

for i = 990 to 999

    print list[i]

next i

end

'narcissistic decimal numbers example

dim p[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

let l = 10
let n = 25

do

    if c < n then

        if x >= l then

            for i = 0 to 9

                let p[i] = p[i] * i

            next i

            let l = l * 10

        endif

        let s = 0
        let y = x

        do

            if y > 0 then

                let t = y % 10
                let s = s + p[t]
                let y = int(y / 10)

            endif

            wait

        loop y > 0

        if s = x then

            print x
            let c = c + 1

        endif

        let x = x + 1

    endif

loop c < n

end

'casting out nines example

precision 4

define base = 10, c1 = 0, c2 = 0

for k = 1 to (base ^ 2) - 1

let c1 = c1 + 1

if k % (base - 1) = (k * k) % (base - 1) then

let c2 = c2 + 1
print k

endif

next k

print "trying ", c2, " numbers instead of ", c1, " numbers saves ", 100 - (100 * c2 / c1), "%"

end
Ebox Thin Client with Windows 95
EEE PC 701SD with Windows XP
Atari 1040STFM with GEM/TOS
Playstation 2 with FreeMcBoot Yabasic
Keyboard Famiclones with GBasic and FBasic
Xerox Sunrise 1800 with MSBasic and CP/M

lucidapogee

I have been busy doing Rosetta code tasks. It's fun.
While running doing these tasks, I also test them in Commando Basic. The results are great. It can do a lot of things that Craft Basic cannot. Faster too.

'factors of an integer example

do

    input "enter an integer", n

loop n = 0

let a = abs(n)

for i = 1 to int(a / 2)

    if a = int(a / i) * i then

        print i

    endif

next i

print a

end

'primality by wilson's theorem example

for i = 2 to 100

    let f = 1

    for j = 2 to i - 1

        let f = (f * j) % i
        wait

    next j

    if f = i - 1 then

        print i

    endif

next i

end

'arithmetic mean with array example

dim a[3, 1, 4, 1, 5, 9]

arraysize s, a

for i = 0 to s - 1

    let t = t + a[i]

next i

print t / s

end

arithmetic mean without array example


input "how many numbers to average?", n

for i = 1 to n

    input "enter a number: ", s
    let a = a + s

next i

print "average: ", a / n

end

'root mean square example

precision 8

let n = 10

for i = 1 to n

    let s = s + i * i

next i

print sqrt(s / n)

end

'perfect numbers example

print "calculating..."

for n = 1 to 10000

    let s = 0

    for i = 1 to n / 2

        if n % i = 0 then

            let s = s + i

        endif

    next i

    if s = n then

        print n, " is perfect."

    endif

    wait

next n

print "done"

end

'geometric mean example

let a = 1
let g = 1 / sqrt(2)

do

    let t = (a + g) / 2
    let g = sqrt(a * g)
    let x = a
    let a = t
    let t = x

loopuntil a = t

print a

end

'integer square root example

alert "integer square root of first 65 numbers:"

for n = 1 to 65

    let x = n
    gosub isqrt
    print r

next n

alert "integer square root of odd powers of 7"
cls
cursor 1, 1

for n = 1 to 21 step 2

    let x = 7 ^ n
    gosub isqrt
    print "isqrt of 7 ^ ", n, " = ", r 

next n

end

sub isqrt

    let q = 1

    do

        if q <= x then

            let q = q * 4

        endif

        wait

    loop q <= x

    let r = 0

    do

        if q > 1 then

            let q = q / 4
            let t = x - r - q
            let r = r / 2

            if t >= 0 then

                let x = t
                let r = r + q

            endif

        endif

    loop q > 1

    let r = int(r)

return

'primality by trial division example

for i = 1 to 50

    if i < 2 then

        let p = 0

    else

        if i = 2 then

            let p = 1

        else

            if i % 2 = 0 then

                let p = 0

            else

                let p = 1

                for j = 3 to int(i ^ .5) step 2

                    if i % j = 0 then

                        let p = 0
                        break j

                    endif

                    wait

                next j

            endif

        endif

    endif

    if p <> 0 then

        print i

    endif

next i

end

'nth root example

precision 6

let a = int(rnd * 5999) + 2

print "calculating nth root of ", a, "..."

for n = 1 to 10

    gosub nroot
    print n, " : ", y

next n

end

sub nroot

    let p = .00001

    let x = a
    let y = a / n

    do

        if abs(x - y) > p then

            let x = y
            let y = ((n - 1) * y + a / y ^ (n - 1)) / n

        endif

        wait

    loop abs(x - y) > p

return
Ebox Thin Client with Windows 95
EEE PC 701SD with Windows XP
Atari 1040STFM with GEM/TOS
Playstation 2 with FreeMcBoot Yabasic
Keyboard Famiclones with GBasic and FBasic
Xerox Sunrise 1800 with MSBasic and CP/M

lucidapogee

Craft Basic now also has over 60 entries on Rosetta Code and version 1.5 has been released!

With this latest update to Craft Basic, a few things have changed while many new features have been added. There's now over 20 new examples.

Among the changes, the print command now displays to an output window. The new custom output window uses RTF and is similar to the one Just Basic has. You may copy, paste, save, and print with the output window. Due to this, the cursor command is no longer supported. To display text in the graphics window, use static text forms. The result of the new output window is that Craft Basic now has much more stable data output capabilities. With this, now data may be continuously printed on a single line like in a console.

The cls command now has the graphics keyword to open the graphics window explicitly. This is useful as the interpreter now runs programs over the source code so you may view the code of text based programs as they are running. The cls command clears both the text and graphics windows when the graphics keyword is not specified.

There's also now an update option in the program menu. This allows you to modify the source code of a program while it's running without stopping execution.

The end command is no longer required to prevent the lag when source code has reached the end. This was done with a silly, but effective hack. If 100 or more consecutive lines are blank, that will be interpreted as an end command. This saves thousands of loops.

There's now SGN, PRIME, and FACTORIAL functions.

The ! operator may also be written as NOT() now.

The ~ operator may also be written as MOD now.

Dimming of array lists now supports expressions in the list during declaration.

Other general issues were fixed. All examples were updated.

There's still a lot on the to do list, but we will save the rest for the next version.

'https://rosettacode.org/wiki/Ultra_useful_primes

for n = 1 to 10

let k = -1

do

let k = k + 2
wait

loop prime(2 ^ (2 ^ n) - k) = 0

print "n = ", n, " k = ", k

next n

'https://rosettacode.org/wiki/Attractive_numbers

for x = 1 to 120

let n = x
let c = 0

do

if int(n mod 2) = 0 then

let n = int(n / 2)
let c = c + 1

endif

wait

loop int(n mod 2) = 0

for i = 3 to sqrt(n) step 2

do

if int(n mod i) = 0 then

let n = int(n / i)
let c = c + 1

endif

wait

loop int(n mod i) = 0

next i

if n > 2 then

let c = c + 1

endif

if prime(c) then

print x, " ",

endif

next x

'https://rosettacode.org/wiki/Prime_decomposition

define limit = 20, loops = 0

dim list[limit]

input "loops?", loops

for x = 1 to loops

let n = x
print n, " : ",
gosub collectprimefactors

for y = 0 to c

if list[y] then

print list[y], " ",
let list[y] = 0

endif

next y

print ""

next x

end

sub collectprimefactors

let c = 0

do

if int(n mod 2) = 0 then

let n = int(n / 2)
let list[c] = 2
let c = c + 1

endif

wait

loop int(n mod 2) = 0

for i = 3 to sqrt(n) step 2

do

if int(n mod i) = 0 then

let n = int(n / i)
let list[c] = i
let c = c + 1

endif

wait

loop int(n mod i) = 0

next i

if n > 2 then

let list[c] = n
let c = c + 1

endif

return

Ebox Thin Client with Windows 95
EEE PC 701SD with Windows XP
Atari 1040STFM with GEM/TOS
Playstation 2 with FreeMcBoot Yabasic
Keyboard Famiclones with GBasic and FBasic
Xerox Sunrise 1800 with MSBasic and CP/M

lucidapogee

#69
I am creating some promo videos to demonstrate Craft Basic examples.

Ebox Thin Client with Windows 95
EEE PC 701SD with Windows XP
Atari 1040STFM with GEM/TOS
Playstation 2 with FreeMcBoot Yabasic
Keyboard Famiclones with GBasic and FBasic
Xerox Sunrise 1800 with MSBasic and CP/M

Derron

Is there a reason to create 480p "squeezed" 4:3 videos?

Showing some Windows 95 looks might put off people too. Why? because most of todays users will think: "oh, someone uploaded a video they recorded >25yrs ago, skipping...". So if you intent reaching out to the "majority of people" then you should consider using a more recent OS - and there the "standard theme".



bye
Ron 

lucidapogee

#71
High quality video so the text may be read easily.

My work computer is a eee pc 701sd netbook with a 7" screen. Resolution 800x480. I guess I could have hooked up to an external monitor for better resolution.

Craft Basic is designed on and for Windows 95 and XP, although it supports 10 and 11. I don't own anything newer than XP.

I am not worried about themes as that has nothing to do with compatibility and my target audience will understand that. I am after retro enthusiasts and hobbyists, not so much people who are obsessed with shiny new operating systems. Craft Basic will run on newer systems and that's good enough.

Ebox Thin Client with Windows 95
EEE PC 701SD with Windows XP
Atari 1040STFM with GEM/TOS
Playstation 2 with FreeMcBoot Yabasic
Keyboard Famiclones with GBasic and FBasic
Xerox Sunrise 1800 with MSBasic and CP/M

SToS

Quote from: lucidapogee on June 30, 2023, 15:45:02I am not worried about themes as that has nothing to do with compatibility and my target audience will understand that. I am after retro enthusiasts and hobbyists, not so much people who are obsessed with shiny new operating systems. Craft Basic will run on newer systems and that's good enough.
Hear, hear!
👏

lucidapogee

In the latest update for Craft Basic to version 1.6, there's new keywords, examples, and features.


The TAB keyword works along with the COMMA, QUOTE, and NEWLINE keywords for printing tabs in the output window.

Arrays may now be dimmed with multi line lists.

The recursion limit was raised. This ability is still limited.

The CONFIRM command provides an input dialog with a prompt, a yes button, and a no button. Yes returns 1 and no returns 0.

When defining variables, it is now optional to assign an expression or value. This way you don't have to type the = 0.

The ERASEARRAY command zeroes the contents of an array.

There's 5 new examples in version 1.6.
Ebox Thin Client with Windows 95
EEE PC 701SD with Windows XP
Atari 1040STFM with GEM/TOS
Playstation 2 with FreeMcBoot Yabasic
Keyboard Famiclones with GBasic and FBasic
Xerox Sunrise 1800 with MSBasic and CP/M