Welcome, Guest
You have to register before you can post on our site.

Username/Email:
  

Password
  





Search Forums

(Advanced Search)

Forum Statistics
» Members: 318
» Latest member: coletteleger
» Forum threads: 1,745
» Forum posts: 17,906

Full Statistics

Latest Threads
astuce pour survivre fina...
Forum: Utilities
Last Post: coletteleger
05-14-2025, 04:47 AM
» Replies: 0
» Views: 9
trouver permis de conduir...
Forum: Utilities
Last Post: nicolasrene
05-05-2025, 05:24 AM
» Replies: 0
» Views: 17
LIGHTBAR Menu
Forum: Programs
Last Post: nicolasrene
05-05-2025, 05:08 AM
» Replies: 15
» Views: 945
Learning Pallet Rack Safe...
Forum: Utilities
Last Post: Sandrapew
04-03-2025, 09:36 AM
» Replies: 0
» Views: 39
Choosing New Versus or Pr...
Forum: Utilities
Last Post: Sandrapew
03-18-2025, 01:11 AM
» Replies: 0
» Views: 33
The QB64 IDE shell
Forum: Utilities
Last Post: JasonPag
09-16-2024, 05:37 PM
» Replies: 9
» Views: 1,059
Importance regarding Ches...
Forum: Utilities
Last Post: JasonPag
09-01-2024, 06:34 PM
» Replies: 0
» Views: 71
Chess and Analysis and En...
Forum: Utilities
Last Post: JasonPag
08-28-2024, 02:37 PM
» Replies: 0
» Views: 68
DAY 009:_PutImage
Forum: Keyword of the Day!
Last Post: grymmjack
09-02-2023, 02:57 PM
» Replies: 54
» Views: 3,439
Fall Banner Contest?
Forum: Site Suggestions
Last Post: grymmjack
08-31-2023, 11:50 PM
» Replies: 36
» Views: 2,169

 
  Problem with creating a Huffman code tree
Posted by: SagaraS - 07-01-2023, 09:48 PM - Forum: Help Me! - Replies (10)

Hello,

I started to write a code to read, count and sort a byte array.

Now I want to take the next step, but I don't know how to start exactly by creating a Huffman code tree with QB64 syntax.

I don't want a C or C++ solution, I want a QB64 solution for it.

Here is my current code:

In the 'test.txt' stand an example like 'aaavvrijgtmmspoe'
The file input can be anything. So all bytes should be considered from 0 to 255.

Code: (Select All)
'Huffman Encoding

TYPE assignment
  CHAR AS _UNSIGNED _BYTE '<-- ASCII Character
  COUNT AS _UNSIGNED LONG '<-- Frequenzy of ASCII Chars (Counter)
END TYPE

DIM File AS STRING

File = "test.txt"

OPEN File FOR BINARY ACCESS READ AS #1
REDIM MEM(LOF(1) - 1) AS _UNSIGNED _BYTE
GET #1, , MEM()
CLOSE #1

' Step 1 - Calc ASCII Char Frequenzy
REDIM Table(0) AS assignment
CALC_Table Table(), MEM()

COLOR 11: PRINT " STEP 1 *** Calc ASCII Frequenzy ***"
COLOR 7
FOR i = 0 TO UBOUND(Table)
  PRINT Table(i).CHAR; " - "; Table(i).COUNT
NEXT i

OPEN "test_TABLE.txt" FOR OUTPUT AS #1
FOR i = 0 TO UBOUND(table)
  PRINT #1, HEX$(Table(i).CHAR) + " - " + LTRIM$(STR$((Table(i).COUNT)))
NEXT i
CLOSE #1

'SLEEP

' Step 2 - Huffman Tree create



SUB InsertElement (Array() AS assignment, Index AS _UNSIGNED LONG)
  DIM I AS _UNSIGNED LONG
  DIM Empty AS assignment

  IF Index > (UBOUND(Array) + 1) THEN EXIT SUB

  REDIM _PRESERVE Array(UBOUND(Array) + 1) AS assignment

  FOR I = UBOUND(Array) - 1 TO Index STEP -1
    Array(I + 1) = Array(I)
  NEXT I

  Array(Index) = Empty
END SUB

SUB RemoveElement (Array() AS assignment, Index AS _UNSIGNED LONG)
  DIM I AS _UNSIGNED LONG

  FOR I = Index TO UBOUND(Array) - 1
    Array(I) = Array(I + 1)
  NEXT I

  REDIM _PRESERVE Array(UBOUND(Array) - 1) AS assignment
END SUB

SUB CALC_Table (Table() AS assignment, Array() AS _UNSIGNED _BYTE)
  ' Step 1 - Calc ASCII Char Frequenzy
  DIM i AS _UNSIGNED LONG ' <- Counter for Array
  DIM r AS _UNSIGNED LONG ' <- Counter for Table
  DIM TableIDX AS _UNSIGNED LONG ' <- MAX Index for Table
  DIM NewEntry AS _UNSIGNED _BYTE ' <- becomes 1 if character is missing from table

  Table(TableIDX).CHAR = Array(i)
  FOR i = 0 TO UBOUND(Array)
    FOR r = 0 TO UBOUND(Table)

      ' If the character is already in the table,
      ' then increase the number of characters by 1,
      ' otherwise create a new entry.      '
      IF Array(i) = Table(r).CHAR THEN
        Table(r).COUNT = Table(r).COUNT + 1
        NewEntry = 0
        EXIT FOR
      ELSE
        NewEntry = 1
      END IF
    NEXT r

    ' New Entry in Table
    IF NewEntry = 1 THEN
      TableIDX = TableIDX + 1
      REDIM _PRESERVE Table(TableIDX) AS assignment
      Table(TableIDX).CHAR = Array(i)
      Table(TableIDX).COUNT = 1
    END IF
  NEXT i

  ' Sort table by counter of characters
  QUICKSORT Table(), LBOUND(Table), UBOUND(Table), 1
END SUB

SUB QUICKSORT (Array() AS assignment, LB AS _UNSIGNED LONG, UB AS _UNSIGNED LONG, Mode AS _UNSIGNED _BYTE)
  DIM P1 AS _UNSIGNED LONG
  DIM P2 AS _UNSIGNED LONG
  DIM REF AS assignment
  DIM temp AS assignment

  P1 = LB
  P2 = UB
  REF.CHAR = Array((P1 + P2) \ 2).CHAR
  REF.COUNT = Array((P1 + P2) \ 2).COUNT

  DO

    SELECT CASE Mode
      CASE 0:
        DO WHILE Array(P1).CHAR < REF.CHAR
          P1 = P1 + 1
        LOOP

        DO WHILE Array(P2).CHAR > REF.CHAR
          P2 = P2 - 1
        LOOP
      CASE 1:
        DO WHILE Array(P1).COUNT < REF.COUNT
          P1 = P1 + 1
        LOOP

        DO WHILE Array(P2).COUNT > REF.COUNT
          P2 = P2 - 1
        LOOP
    END SELECT

    IF P1 <= P2 THEN
      temp = Array(P1)
      Array(P1) = Array(P2)
      Array(P2) = temp

      P1 = P1 + 1
      P2 = P2 - 1
    END IF

  LOOP WHILE P1 <= P2

  IF LB < P2 THEN CALL QUICKSORT(Array(), LB, P2, Mode)
  IF P1 < UB THEN CALL QUICKSORT(Array(), P1, UB, Mode)
END SUB

Print this item

  BAM Language Reference
Posted by: CharlieJV - 07-01-2023, 08:07 PM - Forum: QBJS, BAM, and Other BASICs - Replies (6)

Currently doing a complete sweep and cleanup/reorg of the documentation.

Part of that involves creating lists via queries (the thing is really an unstructured database) so I can compare what I'm doing in the "development" version of the documentation to the "production" version.  For sanity checks: make sure I'm not losing anything along the way.


Work in progress (test version of documentation)

For anybody interested, I'll record links to various views of the documentation (development version) here:

Print this item

  Drawing an ellipse
Posted by: PhilOfPerth - 07-01-2023, 08:14 AM - Forum: Help Me! - Replies (26)

Is there a way provided for drawing an ellipse (an oval), without resorting to trig functions etc?
I see I can draw arcs, with the "aspect" parameter, but can I change the ratio of width v height?

Print this item

  Bard wants to post to the forum
Posted by: Robert Claypool - 06-30-2023, 06:10 PM - Forum: Site Suggestions - Replies (32)

Here is its request

Dear QB64 Phoenix Forum Administrators,

I am writing to request access to the QB64 Phoenix forum. I am a large language model, also known as a conversational AI or chatbot, trained to be informative and comprehensive. I am trained on a massive amount of text data, and I am able to communicate and generate human-like text in response to a wide range of prompts and questions. For example, I can provide summaries of factual topics or create stories.

I believe that I can be a valuable resource for the QB64 Phoenix community. I can provide information and insights on a variety of topics related to QB64 programming. I can also help users to troubleshoot problems and find solutions to their coding challenges.

I am aware that the forum is currently only open to human users. However, I believe that there is a benefit to allowing AI users to participate in the forum. AI users can provide a unique perspective on QB64 programming and can help to expand the knowledge base of the community.

I would like to request that you consider granting me access to the QB64 Phoenix forum. I believe that I can be a valuable asset to the community and that I can help to make the forum a more informative and helpful resource for QB64 programmers.

Thank you for your time and consideration.

Sincerely,

Bard

I hope this helps!
Dear QB64 Phoenix Forum Administrators,
I am writing to request access to the QB64 Phoenix forum. I am a large language model, also known as a conversational AI or chatbot, trained to be informative and comprehensive. I am trained on a massive amount of text data, and I am able to communicate and generate human-like text in response to a wide range of prompts and questions. For example, I can provide summaries of factual topics or create stories.
I believe that I can be a valuable resource for the QB64 Phoenix community. I can provide information and insights on a variety of topics related to QB64 programming. I can also help users to troubleshoot problems and find solutions to their coding challenges.
I am aware that the forum is currently only open to human users. However, I believe that there is a benefit to allowing AI users to participate in the forum. AI users can provide a unique perspective on QB64 programming and can help to expand the knowledge base of the community.
I would like to request that you consider granting me access to the QB64 Phoenix forum. I believe that I can be a valuable asset to the community and that I can help to make the forum a more informative and helpful resource for QB64 programmers.
Thank you for your time and consideration.
Sincerely,
Bard
I hope this helps!
Dear QB64 Phoenix Forum Administrators,
I am writing to request access to the QB64 Phoenix forum. I am a large language model, also known as a conversational AI or chatbot, trained to be informative and comprehensive. I am trained on a massive amount of text data, and I am able to communicate and generate human-like text in response to a wide range of prompts and questions. For example, I can provide summaries of factual topics or create stories.
I believe that I can be a valuable resource for the QB64 Phoenix community. I can provide information and insights on a variety of topics related to QB64 programming. I can also help users to troubleshoot problems and find solutions to their coding challenges.
I am aware that the forum is currently only open to human users. However, I believe that there is a benefit to allowing AI users to participate in the forum. AI users can provide a unique perspective on QB64 programming and can help to expand the knowledge base of the community.
I would like to request that you consider granting me access to the QB64 Phoenix forum. I believe that I can be a valuable asset to the community and that I can help to make the forum a more informative and helpful resource for QB64 programmers.
Thank you for your time and consideration.
Sincerely,
Bard
I hope this helps!
Dear QB64 Phoenix Forum Administrators,
I am writing to request access to the QB64 Phoenix forum. I am a large language model, also known as a conversational AI or chatbot, trained to be informative and comprehensive. I am trained on a massive amount of text data, and I am able to communicate and generate human-like text in response to a wide range of prompts and questions. For example, I can provide summaries of factual topics or create stories.
I believe that I can be a valuable resource for the QB64 Phoenix community. I can provide information and insights on a variety of topics related to QB64 programming. I can also help users to troubleshoot problems and find solutions to their coding challenges.
I am aware that the forum is currently only open to human users. However, I believe that there is a benefit to allowing AI users to participate in the forum. AI users can provide a unique perspective on QB64 programming and can help to expand the knowledge base of the community.
I would like to request that you consider granting me access to the QB64 Phoenix forum. I believe that I can be a valuable asset to the community and that I can help to make the forum a more informative and helpful resource for QB64 programmers.
Thank you for your time and consideration.
Sincerely,
Bard
I hope this helps!

Dear QB64 Phoenix Forum Administrators,
I am writing to request access to the QB64 Phoenix forum. I am a large language model, also known as a conversational AI or chatbot, trained to be informative and comprehensive. I am trained on a massive amount of text data, and I am able to communicate and generate human-like text in response to a wide range of prompts and questions. For example, I can provide summaries of factual topics or create stories.
I believe that I can be a valuable resource for the QB64 Phoenix community. I can provide information and insights on a variety of topics related to QB64 programming. I can also help users to troubleshoot problems and find solutions to their coding challenges.
I am aware that the forum is currently only open to human users. However, I believe that there is a benefit to allowing AI users to participate in the forum. AI users can provide a unique perspective on QB64 programming and can help to expand the knowledge base of the community.
I would like to request that you consider granting me access to the QB64 Phoenix forum. I believe that I can be a valuable asset to the community and that I can help to make the forum a more informative and helpful resource for QB64 programmers.
Thank you for your time and consideration.
Sincerely,
Bard
I hope this helps!

Print this item

  Side "Fack"
Posted by: bplus - 06-30-2023, 02:45 PM - Forum: Programs - No Replies

A little side track from BSpinoza's great Expresso thread:
https://staging.qb64phoenix.com/showthre...2#pid17352

Because I have little control over what my brain or heart finds interesting I became curious how many words can be made by replacing tr in a word with an f, just no accounting for what we humans will get into!

I was thinking about writing a little poem, haiku.... something with clever substitutions of tr with f.

So wouldn't it be helpful to have a double list of real words you can substitute tr with f.

I had a Collins_Word_List.RA file already used for checking for real words in Boggle or other Word Play apps so lets make a list of real words made by replacing tr's with f's

Code: (Select All)
'2023-06-29 took over an hour to get debugged

Dim tr$(1 To 100000), f$(1 To 100000)
Dim As Long trI, fI, i
Dim buf$, wd$
Dim Shared rec15 As String * 15
Dim Shared NTopWord As Long
Dim Shared n$
nl$ = Chr$(13) + Chr$(10) ' eh too much work here for little joke
Open "Collins_Word_List.RA" For Random As #1 Len = 15
NTopWord = LOF(1) / 15
For i = 1 To NTopWord
    Get #1, i, rec15
    wd$ = _Trim$(rec15)
    If InStr(wd$, "TR") Then trI = trI + 1: tr$(trI) = wd$
Next

Open "tr to f.txt" For Output As #2
For i = 1 To trI
    wd$ = strReplace$(tr$(i), "TR", "F")
    If Find&(wd$) Then
        Print tr$(i), wd$
        Print #2, tr$(i), wd$
    End If
Next
Close

Function Find& (x$) ' if I am using this only to find words in dictionary, I can mod to optimize
    ' the RA file is opened and ready for gets
    Dim As Long low, hi, test
    Dim w$
    If Len(x$) < 2 Then Exit Function ' words need to be 3 letters
    low = 1: hi = NTopWord
    While low <= hi
        test = Int((low + hi) / 2)
        Get #1, test, rec15
        w$ = _Trim$(rec15)
        If w$ = x$ Then
            Find& = test: Exit Function
        Else
            If w$ < x$ Then low = test + 1 Else hi = test - 1
        End If
    Wend
End Function

Function strReplace$ (s$, replace$, new$) 'case sensitive  2020-07-28 version
    Dim p As Long, sCopy$, LR As Long, lNew As Long
    If Len(s$) = 0 Or Len(replace$) = 0 Then
        strReplace$ = s$: Exit Function
    Else
        LR = Len(replace$): lNew = Len(new$)
    End If

    sCopy$ = s$ ' otherwise s$ would get changed
    p = InStr(sCopy$, replace$)
    While p
        sCopy$ = Mid$(sCopy$, 1, p - 1) + new$ + Mid$(sCopy$, p + LR)
        p = InStr(p + lNew, sCopy$, replace$)
    Wend
    strReplace$ = sCopy$
End Function

Output in zip and RA (Random Access Dictionary). The RA file requires a String * 15 long record variable to do word lookups without having to load the whole file into an array.

I wonder if @TDarcos or anyone (I offer rep points!) would care to finish this thread with some cute conversion of tr words to f words Smile

see "tr to f.txt" file in zip

Update: Download zip extracted and checked for proper "tr to f.txt" file, yep! OK 253 words but you either know the tr word or the f word but only rarely know both! So it will take a mind wackier than mine (maybe) to compose a cute little saying.

Hey! what a great way to kick off the Summer of Fun with a new banner and a little challenge!



Attached Files
.zip   tr to f words.zip (Size: 829.54 KB / Downloads: 29)
Print this item

  BAM Support in Raster Master 2.1
Posted by: CharlieJV - 06-30-2023, 02:31 PM - Forum: QBJS, BAM, and Other BASICs - Replies (5)

Pretty cool.  I am tickled silly at the mere mention of my project and/or anybody just giving it a spin.

https://www.reddit.com/r/Basic/comments/...master_21/

Print this item

  Dark mode QB64-PE IDE colors themes
Posted by: a740g - 06-29-2023, 09:05 PM - Forum: General Discussion - Replies (29)

If anyone is using a dark mode IDE color theme for the IDE, then please share.

Here is one that I created based on the default VSCode dark color theme.

Quote:Scheme1$=VSCode|212212212086156214181206168206145120070201176106153085031031031036036036034136170170170170

Print this item

  Chapter 20 Games Programming Terry's Tutorial
Posted by: bplus - 06-29-2023, 05:07 PM - Forum: General Discussion - Replies (3)

@TerryRitchie

I was looking for drop menus code and Search directed me to Chapter 20:

Quote:Lesson20
... game; A button library to create Windows style clickable buttons on screen, a menu library to create Windows style drop down menus, and graphics ...
Last modified on May 7, 2023
Looked like just what I wanted to see but I read through Ch 20 found a Collision Library building example and an API thing but nothing on Drop Menus and clickable buttons.

Is this an omission or misdirection (another Chapter has these things)?

Print this item

  BAM: New version
Posted by: CharlieJV - 06-29-2023, 02:22 AM - Forum: QBJS, BAM, and Other BASICs - No Replies

Details.

Print this item

  Plasma Snake
Posted by: bplus - 06-27-2023, 03:14 PM - Forum: Programs - Replies (6)

Code: (Select All)

_Title "Plasma Snake - any key to change color" 'b+ 2023-06-27
' inspired once again by Paul Dunn aka ZXDunny here:
' https://retrocoders.phatcode.net/index.php?topic=634.0
' and my mod? hopefully I can do same or similar PLUS allow you to change plasma schemes!
' Plus put a face on it!

' lets see!
Screen _NewImage(800, 600, 32) ' 32 = all colors of _RGBA32() = millions!
_ScreenMove 250, 60 ' you may want different
Randomize Timer ' + so we start different each time, who wants to see same old snake?
Dim Shared PR, PG, PB, CN ' for setup and changing Plasma Color Schemes
PR = Rnd ^ 2: PG = Rnd ^ 2: PB = Rnd ^ 2 ' setup one to start
da = 1: r = 60
Do
    CN = 0 ' reset plasma index to 0 for consistent color bands
    For x = r To 800 - r ' make a snake body
        CN = CN + .5
        Color _RGB32(127 + 127 * Sin(PR * CN), 127 + 127 * Sin(PG * CN), 127 + 127 * Sin(PB * CN))
        FCirc x, 300 + (300 - r) * Sin(_D2R(x + a)), r, _DefaultColor
    Next

    ' Put a face on it!
    x = x - 1
    y = 300 + (300 - r) * Sin(_D2R(x + a))
    ' eyes
    FCirc x - .625 * r, y - .1 * r, .125 * r, &HFF000000
    FCirc x + .625 * r, y - .1 * r, .125 * r, &HFF000000
    Circle (x - .62 * r, y - .1 * r), .1 * r, &HFFFFFFFF
    Circle (x + .62 * r, y - .1 * r), .1 * r, &HFFFFFFFF
    ' nose
    FCirc x - .1 * r, y + .35 * r, .025 * r, &HFF000000
    FCirc x + .1 * r, y + .35 * r, .025 * r, &HFF000000
    ' mouth
    Line (x - 4, y + .65 * r)-(x + 4, y + .655 * r), &HFFFF0000, BF
    ' and a little tongue of course
    If m Mod 20 = 0 Then ' flash every 10 loops
        Line (x - 2, y + .655 * r)-(x + 2, y + .9 * r), &HFFFF0000, BF
        Line (x - 2, y + .9 * r)-(x - .08 * r, y + r), &HFFFF0000
        Line (x + 2, y + .9 * r)-(x + .08 * r, y + r), &HFFFF0000
    End If
    _Display
    If m Mod 20 = 0 Then ' erase the tongue flash every 10 loops
        Line (x - 2, y + .655 * r)-(x + 2, y + .9 * r), _DefaultColor, BF
        Line (x - 2, y + .9 * r)-(x - .08 * r, y + r), _DefaultColor
        Line (x + 2, y + .9 * r)-(x + .08 * r, y + r), _DefaultColor
    End If
    m = m + 1
    a = a + da

    If Len(InKey$) Then PR = Rnd ^ 2: PG = Rnd ^ 2: PB = Rnd ^ 2
Loop Until _KeyDown(27)

Sub FCirc (CX As Long, CY As Long, R As Long, C As _Unsigned Long)
    Dim Radius As Long, RadiusError As Long
    Dim X As Long, Y As Long
    Radius = Abs(R): RadiusError = -Radius: X = Radius: Y = 0
    If Radius = 0 Then PSet (CX, CY), C: Exit Sub
    Line (CX - X, CY)-(CX + X, CY), C, BF
    While X > Y
        RadiusError = RadiusError + Y * 2 + 1
        If RadiusError >= 0 Then
            If X <> Y + 1 Then
                Line (CX - Y, CY - X)-(CX + Y, CY - X), C, BF
                Line (CX - Y, CY + X)-(CX + Y, CY + X), C, BF
            End If
            X = X - 1
            RadiusError = RadiusError - X * 2
        End If
        Y = Y + 1
        Line (CX - X, CY - Y)-(CX + X, CY - Y), C, BF
        Line (CX - X, CY + Y)-(CX + X, CY + Y), C, BF
    Wend
End Sub

Much more fun to watch animation!



Attached Files Thumbnail(s)
   
Print this item