Page 1 of 1

Drawing a chessboard

Posted: Fri Jan 16, 2009 1:35 am
by Kaisen2100
Hello i am going crazy trying to draw a chessboard using loop ...

can anyone help please!!!!

thanks

Posted: Fri Jan 16, 2009 1:58 am
by Kaeru Gaman
a little Demo using strings...

Code: Select all

For t = 0 To 7
  Out$ = ""
  For n = 0 To 7
    Odd = (n+t) % 2
    If Odd
      Out$ + "X"
    Else
      Out$ + "O"
    EndIf
  Next
  Debug Out$
Next
the important part is the calculation of the Variable "Odd"


PS:
and here with grafix...

Code: Select all

CreateImage(0, 256, 256)

StartDrawing(ImageOutput(0))
  For t = 0 To 7
    For n = 0 To 7
      Odd = (n+t) % 2
      If Odd
        Col = $FF0000
      Else
        Col = $00FFFF
      EndIf
      Box(32*n, 32*t, 32, 32, Col )
    Next
  Next
StopDrawing()

OpenWindow( 0, 0,0, 256,256, "Checkers")
  ImageGadget( 0, 0,0, 256, 256, ImageID(0) )

Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow

Posted: Fri Jan 16, 2009 2:06 am
by netmaestro
Just happen to have something kicking around hehe..

Code: Select all

ProcedureDLL DrawChessBoard(flip)
  ; netmaestro November 2006
  dark  = RGB(121,141,167)
  light = RGB(239,239,231)
  LoadFont(0, "Arial", 12, #PB_Font_Bold|#PB_Font_HighQuality)
  img_board = CreateImage(#PB_Any, 580, 580, 32)
  StartDrawing(ImageOutput(img_board))
    Box(0,0,580,580,light)
    Box(24,24,532,532,dark)
    Box(29,29,522,522,light)
    Box(33,33,514,514,dark)
  
    For j=34 To 482 Step 128
      For i = 34 To 482 Step 128
        Box(i,j,64,64,light):Box(i+64,j,64,64,dark)
        Box(i,j+64,64,64,dark):Box(i+64,j+64,64,64,light)
      Next
    Next
    
    DrawingFont(FontID(0))
    Select flip
      Case 1
        DrawText(0,3,  "              H              G             F             E              D             C             B              A",dark,light)
        DrawText(0,558,"              H              G             F             E              D             C             B              A",dark,light)
      Case 0
        DrawText(0,3,  "              A              B             C             D              E             F             G              H",dark,light)
        DrawText(0,558,"              A              B             C             D              E             F             G              H",dark,light)
    EndSelect
    If flip=0: cc=9 : Else : cc=0 : EndIf
    For i=59 To 508 Step 64
      If flip=0:cc-1:Else:cc+1:EndIf
      DrawText(8,i,Chr(48+cc),dark,light)
      DrawText(561,i,Chr(48+cc),dark,light)
    Next
  StopDrawing()
  ProcedureReturn ImageID(img_board)
EndProcedure

;test...
OpenWindow(0,0,0,580,580,"",$CF0001)
ImageGadget(0,0,0,0,0,DrawChessBoard(0))
Repeat:Until WaitWindowEvent()=#WM_CLOSE
I was working on a routine to add a pebbled surface to it but I got sidetracked and never got back to it. Maybe sometime soon.

Posted: Fri Jan 16, 2009 9:41 am
by Michael Vogel
A little chess server for handling some games locally and via the internet would be fine, maybe there will be some time for it next winter (I just have to remember to find that nice board, netmaestro :wink: )
Michael

Posted: Fri Jan 16, 2009 11:09 am
by Kwai chang caine
I can't resist, it's more strong that me :oops:

How you do all, for create something also nice, with several line :shock:

When i copy and paste your code, i say to me.....it's surrely a begin of somes squares :roll:

And when i click run, It's all the chess :shock:

I know that PB is strong, but he have some masters who use it like gods 8)
I know too that what i say, use at nothing, but it's so much pleasure at all the time i try all your code, i can't contain my happiness, and i thanks you like this :D

Thanks for sharing 8)
I keep it preciously in my bag, for perhaps later :roll:

Good day

Posted: Fri Jan 16, 2009 2:27 pm
by Kaeru Gaman
to show off a bit...

netmaestro draws four boxes per loop, this is easy. it's like having an already checkered tile and multiplicate it.

mine instead draws only one box per loop, deciding the color by a flag that alternates the real checkers.

this is the true nerd's way! Image

Posted: Fri Jan 16, 2009 10:15 pm
by Kaisen2100
thanks it helped a lot!

and the netmaestro board is great, but i will use the code from Kaeru Gaman lol ... just to begin and experiment some stuff :)

thanks to both

Posted: Wed May 06, 2009 5:15 am
by Prod
As a rabid chessfan I have made hundreds of these. :roll:
Here is a pretty minimalistic method, so to speak. :P

Code: Select all

For y=0 To 7
  For x=0 To 7
    a=((x+y+1)&1)*255
    Plot(x,y,RGB(a,a,a))
  Next
Next
This one uses only bit-ops for a much better compile, tho it looks utterly insane to the human eye.

Code: Select all

For y=0 To 7
  For x=0 To 7
    a=(~x!y&1)*255
    Plot(x,y,RGB(a,a,a))
  Next
Next
Yes I know noone will ever come back to this post, but I am addicted. :wink:

Posted: Mon May 11, 2009 12:27 am
by einander
With perspective view:

Code: Select all

Structure PointF :  X.F : Y.F : EndStructure
Structure PF4 :  P.PointF[4] : EndStructure
nSteps=8 ; try different nSteps
 VX.PF4

With VX  ;position CLOCKWISE for chessboard corners 
   \P[0]\X=200 : \P[0]\Y=120
   \P[1]\X=500 : \P[1]\Y=60
   \P[2]\X=600 : \P[2]\Y=400
   \P[3]\X=100 : \P[3]\Y=450
EndWith

Procedure LineStep(n,X1,Y1,X2,Y2,nSteps,*PF.PointF) 
   ;Get position of step n from line x1,y1,x2,y2 divided in nSteps
   *PF\X=X1+(X2-X1)/nSteps*n
   *PF\Y=Y1+(Y2-Y1)/nSteps*n
EndProcedure 

Procedure GetPolygPix(StepX,StepY,nSteps,*VX.PF4,*PF.PointF)
   ; Get position of pixel StepX,StepY from polygon with 4 vertex in *VX  divided in nSteps
   With *VX
      LineStep(StepX,\P[0]\X,\P[0]\Y,\P[1]\X,\P[1]\Y,nSteps,P1.PointF)
      LineStep(StepX,\P[3]\X,\P[3]\Y,\P[2]\X,\P[2]\Y,nSteps,p2.PointF)
      LineStep(StepY,P1\X,P1\Y,p2\X,p2\Y,nSteps,*PF)
   EndWith
EndProcedure 

Procedure GetSubPolyg(StepX,StepY,nSteps,*VX.PF4,*PF.PF4)  
   ;get 4 Vertex of subpolygon starting at StepX,StepY from polygon with 4 vertex in *VX  divided in nSteps
   With *PF
      GetPolygPix(StepX,StepY,nSteps,*VX,\P[0])
      GetPolygPix(StepX+1,StepY,nSteps,*VX,\P[1])
      GetPolygPix(StepX+1,StepY+1,nSteps,*VX,\P[2])
      GetPolygPix(StepX,StepY+1,nSteps,*VX,\P[3])
   EndWith
EndProcedure

Procedure DrawPolyg4(*VX.PF4,RimRGB,BkRGB=-1)  
   With *VX
      LineXY(\P[0]\X,\P[0]\Y,\P[1]\X,\P[1]\Y,RimRGB)
      LineXY(\P[1]\X,\P[1]\Y,\P[2]\X,\P[2]\Y,RimRGB)
      LineXY(\P[2]\X,\P[2]\Y,\P[3]\X,\P[3]\Y,RimRGB)
      LineXY(\P[3]\X,\P[3]\Y,\P[0]\X,\P[0]\Y,RimRGB)
      
      If BkRGB>-1
         GetPolygPix(1,1,2,*VX,Pf.PointF)
         FillArea(Pf\X,Pf\Y, RimRGB, BkRGB)
      EndIf
   EndWith
EndProcedure
 ;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
If OpenWindow(0, 100, 100,700,500 ,"Checkerboard",  #WS_OVERLAPPEDWINDOW |1) 
   AddKeyboardShortcut(0, #PB_Shortcut_Escape, #PB_Shortcut_Escape)
   
   _Img=CreateImage(-1,WindowWidth(0),WindowHeight(0),32)
   _ImGad=ImageGadget(-1,0,0,0,0,ImageID(_Img)) 
   
   StartDrawing(ImageOutput(_Img))
      
      Pf.PF4
      RimRGB=$080598
      Blacks= $003300
      Whites=$99FFFF
      For i=0 To nSteps-1
         For j=0 To nSteps-1
            GetSubPolyg(i,j,nSteps,VX,Pf)
            If (j+i)&1 : RGB=Blacks
            Else       : RGB=Whites
            EndIf
            DrawPolyg4(Pf,RimRGB,RGB)
         Next
      Next
      
      DrawPolyg4(VX ,RimRGB)
   StopDrawing()
   SetGadgetState(_ImGad,ImageID(_Img))
   
   Repeat 
      Ev=WaitWindowEvent() 
      If Ev=#PB_Event_Menu And EventMenu()=#PB_Shortcut_Escape:End:EndIf
   Until Ev=#PB_Event_CloseWindow
EndIf   
Cheers