Chess board

Share your advanced PureBasic knowledge/code with the community.
Olli
Addict
Addict
Posts: 1240
Joined: Wed May 27, 2020 12:26 pm

Chess board

Post by Olli »

Small code to display a chess board. Open bar to the cheat !
:mrgreen:
If display problem, please tell me the OS version.

Code: Select all

h = 3 * DesktopHeight(winB + ExamineDesktops() * 0) / 4
winB = OpenWindow(#PB_Any, 0, 0, h, h, "Chess board", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
hb = h / 8
SetGadgetFont(#PB_Default, FontID(LoadFont(#PB_Any, "courier", hb / 2) ) )
For i = 0 To 63
    x = i & 7
    y = (i & 56) >> 3
    StringGadget(i, x * hb, y * hb, hb, hb, Chr(32 + (9779 + ((($76232B >> (3 * x) & 7) * Bool(Not(y) Or Not(y - 7) ) ) | (Bool(Not(y - 1) Or Not(y - 6) ) * 6) ) ) * Bool((((y >> 2) ! (y >> 1)) & 1) ! 1) ) )
    SetGadgetColor(i, #PB_Gadget_BackColor, RGB(108, 128, 108) + ((x & 1) ! (y & 1) ) * RGB(147, 64, 20) )
    SetGadgetColor(i, #PB_Gadget_FrontColor, RGB(255, 255, 255) * Bool(y & 4) )
Next
Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow
BarryG
Addict
Addict
Posts: 4173
Joined: Thu Apr 18, 2019 8:17 am

Re: Chess board

Post by BarryG »

Olli wrote:StringGadget(i, x * hb, y * hb, hb, hb, Chr(32 + (9779 + ((($76232B >> (3 * x) & 7) * Bool(Not(y) Or Not(y - 7) ) ) | (Bool(Not(y - 1) Or Not(y - 6) ) * 6) ) ) * Bool((((y >> 2) ! (y >> 1)) & 1) ! 1) ) )
I'll never understand how that works (and I don't want to know). Just amazing. You have a good brain!
dige
Addict
Addict
Posts: 1405
Joined: Wed Apr 30, 2003 8:15 am
Location: Germany
Contact:

Re: Chess board

Post by dige »

This makes me crazy! :shock: great small code!
"Daddy, I'll run faster, then it is not so far..."
User avatar
jacdelad
Addict
Addict
Posts: 2010
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: Chess board

Post by jacdelad »

@Olli, this is cool!
How about using a TextGadget, or do you want the StringGadget to be able to move the pieces by hand?

Code: Select all

h = 3 * DesktopHeight(winB + ExamineDesktops() * 0) / 4
winB = OpenWindow(#PB_Any, 0, 0, h, h, "Chess board", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
hb = h / 8
SetGadgetFont(#PB_Default, FontID(LoadFont(#PB_Any, "courier", hb / 1.5) ) )
For i = 0 To 63
    x = i & 7
    y = (i & 56) >> 3
    TextGadget(i, x * hb, y * hb, hb, hb, Chr(32 + (9779 + ((($76232B >> (3 * x) & 7) * Bool(Not(Mod(y,7) ) ) ) | (Bool(Not(Mod(y-1,5) ) ) * 6) ) ) * Bool((((y >> 2) ! (y >> 1)) & 1) ! 1) ), #PB_Text_Center)
    SetGadgetColor(i, #PB_Gadget_BackColor, RGB(108, 128, 108) + ((x & 1) ! (y & 1) ) * RGB(147, 64, 20) )
    SetGadgetColor(i, #PB_Gadget_FrontColor, RGB(255, 255, 255) * Bool(y & 4) )
Next
Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow
BTW, you can save some more chars in your monster formula by using mod two times.
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
Olli
Addict
Addict
Posts: 1240
Joined: Wed May 27, 2020 12:26 pm

Re: Chess board

Post by Olli »

1) I apologize for the formula : I will simplify.
2) I use StringGadget as a good personnal choice.

I find this gadget is well adapted to this game (excepted for XP and older)

And I wanted the "ergonomy" part stays short to be concentrated for the game, and to allow everybody to code what we want then.
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Chess board

Post by Kwai chang caine »

Incredible , you never finish to etonashing me :shock:
Thanks for sharing this little jewel
BarryG wrote:You have a good brain!
It's not the first time i think the same thing 8)
ImageThe happiness is a road...
Not a destination
Post Reply