Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
QBJS - Chess API
#21
Ok @Dbox 
it can be a interesting exercise doing a TUI for this engine, but you must accept that its max force is about 1000Elo (IMHO); 
as you can see from these screenshots, it gives an interesting playing at expert level if you play straight.
But as soon as you try some traps it leaves pieces for free.

Here it has got the pawn in D4 and now it will loose the queen after Bishop B5+
[Image: Qb-JS-cannot-see-a-simple-trap.png]


Here it gives a Knight for 2 pawns after Queen d4
[Image: Qb-JS-leaves-pieces-for-free.png]
In sum it is stronger than QBChess.bas (a demo that belongs with examples), but it is still very BASIC.
Reply
#22
(02-12-2026, 07:05 PM)bplus Wrote: @dbox once again QBJS does not like something that works perfectly in QB64...

QBJS has no screen 0. BTW _MouseX and _MouseY in screen 0 of QB64 does char cells not pixels, same with QBJS. Otherwise I dont know why the codes perfectly in QB64 but not QBJS????

...the point of all my labors today was todo mouse clicks! Sad
I can tell I've disappointed you @bplus, and I'm sorry.

The main issue was that the tight loop in the GetMove$ method was not giving enough time for UI events to be processed.  This can be remedied by adding a _Limit before the end of the loop.

The _MouseX and _MouseY returning character coordinates instead of pixel coordinates is an oversight on my part.  I tend to mostly work in graphics mode myself.  I'll add a fix for that in the next release.  In the meantime, it can be worked around by just dividing the mouse position by the font size.

Quote:BTW I had to Dim all my variables in the function GetMove$ to get it accepted by QBJS. I thought you fixed all that!
I'm pretty sure this was because of Option Explicit.  As you can see below the Dims are not necessary.

Here's version of your test program that might be a bit closer to what you were wanting:


(02-12-2026, 07:25 PM)TempodiBasic Wrote: Ok @Dbox 
it can be a interesting exercise doing a TUI for this engine, but you must accept that its max force is about 1000Elo (IMHO); 
I will accept it.
Reply
#23
Code: (Select All)
dbox:
The _MouseX and _MouseY returning character coordinates instead of pixel coordinates is an oversight on my part.  I tend to mostly work in graphics mode myself.  I'll add a fix for that in the next release.  In the meantime, it can be worked around by just dividing the mouse position by the font size.
 
Thanks @dbox should be an easy fix then. I can change my test to screen _NewImage(640, 400, 12)
and convert to pixels as you say by dividing by 8 and 16 for _MouseX and Y.
  724  855  599  923  575  468  400  206  147  564  878  823  652  556 bxor cross forever
Reply
#24
Not easy fix, I convert to pixels and JS is still NOT Being QB64!

Code: (Select All)
' test qbjs get move for chess ' b+ 2026-02-12
Screen _NewImage(640, 400, 12)
Do Until _KeyDown(27)
    PrintBoard
    gm$ = ""
    gm$ = GetMove$
    If gm$ <> "" Then
        Color 7, 0
        Locate 22, 50
        Print gm$; "   zzz... sleeping."
        Sleep
        Cls
        _KeyClear
    End If
Loop

Function GetMove$
    Dim As String boardX, boardY, retorn, k, b
    Dim As Long mx, my, mb, bx, by

    Do
        While _MouseInput: Wend
        mx = (_MouseX + 4) / 8: my = (_MouseY + 8) / 16 ' convert into char cells
        mb = _MouseButton(1)
        If mb Then
            Locate 14, 50: Print Space$(15)
            Locate 14, 50: Print mx, my
            boardX = "": boardY = ""
            ' check mx
            If (mx Mod 4) <> 0 Then
                If mx > 4 And mx < 39 Then
                    bx = Int((mx - 0) / 4)
                    boardX = Chr$(64 + bx)
                End If
            End If
            If my Mod 2 = 0 Then
                If my > 3 And my < 19 Then
                    by = Int((my - 2) / 2)
                    boardY = _Trim$(Str$(9 - by))
                End If
            End If
            Locate 15, 50: Print Space$(15)
            Locate 15, 50: Print boardX, boardY
        End If
        If (boardX <> "") And (boardY <> "") Then
            retorn = boardX + boardY
        Else
            k = InKey$
            If Len(k) = 1 And UCase$(k) = "Q" Then
                retorn = k
            Else
                b = b + k
                If Len(b) = 2 Then retorn = b
            End If
        End If
        _Limit 120
    Loop Until retorn <> ""
    GetMove$ = retorn
End Function


Sub PrintBoard ' rework in QB64 to model the board for get move

    Locate 4, 40: Print "Turn:  " '; Chess.Turn; "   "
    Locate 6, 40: Print "Level: " '; levels(aiLevel)
    'Locate 12, 40: Print "Enter 'Q' to Quit"
    'If Chess.IsCheckMate Then
    '    Locate 8, 40: Color 4: Print "Check Mate"
    'ElseIf Chess.IsCheck Then
    '    Locate 8, 40: Color 6: Print "Check"
    'End If

    'PrintHistory

    Locate 1, 1

    'Dim As String bstate, p, s
    'bstate = Chess.BoardPieces
    'Dim p$, s$
    Dim As Integer rank, file
    Color 7: Print
    Print "     A   B   C   D   E   F   G   H"
    Print "   ";
    Color 8, 7: Print "|-------------------------------|"
    For file = 8 To 1 Step -1
        Color 7, 0
        Color 7, 0: Print " " + _Trim$(Str$(file)) + " ";
        For rank = Asc("A") To Asc("H")
            '    s$ = Chr$(rank) + _Trim$(Str$(file))
            '    p$ = "" ' bstate(s)
            '    If p$ = "" Then p$ = " "
            '    Color 8, 7: Print " ";
            '    If p$ > "A" And p$ < "Z" Then Color 15 Else Color 0
            '    Print p$;
            '    Print " "
            Color 8, 7: Print "|---";
            'Color 7, 0
        Next rank
        Print "|"
        If file > 1 Then
            Color 7, 0: Print "   ";
            Color 8, 7: Print "|-------------------------------|"
        End If
    Next file
    Color 7, 0: Print "   ";
    Color 8, 7: Print "|-------------------------------|"
End SUB

Again works fine in QB64 but not so in QBJS (the GetMove() Function).

This is too frustrating to continue, I dont know what JS expects.
  724  855  599  923  575  468  400  206  147  564  878  823  652  556 bxor cross forever
Reply
#25
(02-12-2026, 10:55 PM)bplus Wrote: Not easy fix, I convert to pixels and JS is still NOT Being QB64!
...
Again works fine in QB64 but not so in QBJS (the GetMove() Function).

This is too frustrating to continue, I dont know what JS expects.
Sorry bplus, I feel like I've failed you again somehow.  It's true that QBJS is not 100% compatible with QB64, but the goal is to eventually have bplus-level QB64 compatibility.  Thanks for giving it a go anyway.

Incidentally, your last code snippet seems to work the same in QBJS and QB64 by just changing line 23 to use the integer division operator (as it was in the sample I provided):

Reply
#26
@Dbox

I have tried to do a different GetAILevel menu but I got this and I dunno where is the issue!


[Image: QBJS-error.png]

I hve just modded the GetAILevel so:

[qbjs]

levels(1) = "Beginner"
    levels(2) = "Easy"
    levels(3) = "Intermediate"
    levels(4) = "Advanced"
    levels(5) = "Expert"
    Dim i As Integer, aiLevel as integer
    aiLevel = 1
   
    'Print
    do
    _printstring (200,100)," Select a Difficulty Level" 'Print " Select a Difficulty Level"
   
    For i = 1 To 5
            '        Print "  -> "; i; "- "; levels(i)
            if i = aiLevel then color 3, 7 else color 15, 0
        _Printstring (200, 100 + (20 *i)), "  -> "; i; "- "; levels(i)
    Next i
    color 15, 0
    _printstring (200,220), " Enter Difficulty Level 1-5"
    do 'input
    if (_keyhit = 32) or (_keyhit = 13) then exit sub
    if _keyhit = 18432 then
        if aiLevel >1 then
            aiLevel = aiLevel -1
            exit do
        end if
    end if
    if _keyhit = 20480 then
        if aiLevel <5 then
            aiLevel = aiLevel +1
            exit do
        end if
    end if
    loop
   
    loop
   
    'Print
  'Input " Enter Difficulty Level 1-5: ", aiLevel
    'If aiLevel > 0 And aiLevel < 6 Then
        ' valid
  ' Else
    '    aiLevel = 1
    '  Print " Invalid selection, defaulting to level 1"
      '  Print
      ' Print " Press any key to continue..."
        'Sleep
  '  End If

[/qbjs]

Thanks for feedbacks
PS: is QBJS button box broken in the forum?
Reply
#27
Dbox: 
Code: (Select All)
Incidentally, your last code snippet seems to work the same in QBJS and QB64 by just changing line 23 to use the integer division operator (as it was in the sample I provided):

I don't care if my test code works in QBJS, it is only to get the GetMove$() Function working correctly with the Chess program.

I plug the function into the chess program in place of the 2 Input "enter move" lines. The keypresses work but it looks like the digit for the row number is not being passed?

dbox:
Code: (Select All)
Incidentally, your last code snippet seems to work the same in QBJS and QB64 by just changing line 23 to use the integer division operator (as it was in the sample I provided):

If I Dim a variable as Long why is QBJS showing a print of the variable as real number with decimals??? QB64 would drop the decimal automatically ie integer division should not be needed!
  724  855  599  923  575  468  400  206  147  564  878  823  652  556 bxor cross forever
Reply
#28
(02-13-2026, 09:39 AM)TempodiBasic Wrote: @Dbox

I have tried to do a different GetAILevel menu but I got this and I dunno where is the issue!
I saw two issues:
  1. You probably want to set the value of _KeyHit to a variable and then test for all of the matches.  Otherwise, it is going to basically ask and see if there has been another keypress for each call of _KeyHit.
  2. It is recommended that if you have a loop in QBJS that is testing for some kind of user input, you add a call to _Limit.  Otherwise, it can result in such a tight loop that the browser won't be able to process the input events in a timely manner.

Here is a version with the changes I described above applied.  I like what you've got so far!


Quote:Thanks for feedbacks
PS: is QBJS button box broken in the forum?
The [ qbjs ] tag works a little differently than the
[ code ] or [ qb ] tag.  It is expecting you to paste in the share link for the program.

Once you've got your program ready in the QBJS IDE, click the Share/Export button      .
This will display the following dialog with the share link highlighted:
   

Copy that link and paste it inside the [ qbjs ] tag.  It should look similar to this:
Code: (Select All)
[qbjs]https://qbjs.org/?code=SW1wb3J0IENvbnNvbGUgRnJvbSAi...g5ARXCg==[/qbjs]
Reply
#29
(02-13-2026, 02:30 PM)bplus Wrote: I don't care if my test code works in QBJS, it is only to get the GetMove$() Function working correctly with the Chess program.

Fair enough, here is a version plugged into the chess program:


Quote:If I Dim a variable as Long why is QBJS showing a print of the variable as real number with decimals??? QB64 would drop the decimal automatically ie integer division should not be needed!
Ah, well that is a documented difference related to flexible typing:
https://github.com/boxgaming/qbjs/wiki/Q...ble-typing

However, I think there is an opportunity to potentially improve the compatibility in the context of variable assignments.  I appreciate all of the great feedback!
Reply
#30
+1 @dbox I am so happy to see mouse input finally work! Yea oh yea! Smile
It looks like I needed to clear the mouse click along with the integer conversions.

I am surprised how quick I am getting use to letters for the chess pieces. It might come from using images for Sudoku Smile

Language differences bookmarked.
  724  855  599  923  575  468  400  206  147  564  878  823  652  556 bxor cross forever
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Amazing Grace to test out Web Audio API CharlieJV 0 438 11-30-2022, 03:09 AM
Last Post: CharlieJV

Forum Jump:


Users browsing this thread: 1 Guest(s)