Drawing a chessboard

Advanced game related topics
Kaisen2100
Enthusiast
Enthusiast
Posts: 121
Joined: Fri May 28, 2004 4:16 pm
Location: Madeira Island, Portugal
Contact:

Drawing a chessboard

Post by Kaisen2100 »

Hello i am going crazy trying to draw a chessboard using loop ...

can anyone help please!!!!

thanks
PureBasic File Relocator [NEW VERSION 1.1] ==> http://pbfilerelocator.atspace.com/
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post 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
oh... and have a nice day.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8452
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post 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.
BERESHEIT
User avatar
Michael Vogel
Addict
Addict
Posts: 2818
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Post 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
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5498
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Post 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
ImageThe happiness is a road...
Not a destination
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post 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
oh... and have a nice day.
Kaisen2100
Enthusiast
Enthusiast
Posts: 121
Joined: Fri May 28, 2004 4:16 pm
Location: Madeira Island, Portugal
Contact:

Post 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
PureBasic File Relocator [NEW VERSION 1.1] ==> http://pbfilerelocator.atspace.com/
Prod
User
User
Posts: 14
Joined: Thu Mar 31, 2005 3:47 pm
Location: Denmark

Post 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:
User avatar
einander
Enthusiast
Enthusiast
Posts: 744
Joined: Thu Jun 26, 2003 2:09 am
Location: Spain (Galicia)

Post 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
Post Reply