## checkers try

Game development specific discussions.
bluatigro
Posts: 508
Joined: Apr 25, 2012 10:35
Location: netherlands

### checkers try

my pc don't eat this

Code: Select all

`dim shared as integer p(14,14) , i , j dim shared as ulong z(1000)const as integer empty = 0const as integer white = 1const as integer dwhite = 16 or 1const as integer black = 2const as integer dblack = 16 or 2const as integer wal = 7sub drawbord  for i = 3 to 12    for j = 3 to 12      select case p(i,j)        case white          circle(i*10+5,j*10+5),rgb(255,255,0),3,,,,f        case dwhite          circle(i*10+5,j*10+5),rgb(255,255,0),5,,,,f        case black          circle(i*10+5,j*10+5),rgb(0,0,255),3,,,,f        case dblack          circle(i*10+5,j*10+5),rgb(0,0,255),5,,,,f        case empty           line(i*10,j*10)-step(9,9),rgb(0,255,0),bf        case else      end select    next j  next iend subfunction random_move( q as integer ) as ulong  ''looks for al posible moves  ''returns random move   ''if no move then 0  dim as integer ztel = 0 , e=1  for i = 3 to 12    for j = 3 to 12      if p(i,j)<>wal andalso ( p(i,j)and q ) then        if q = black then e=-1        if p(i+e,j+e) = empty then          z(ztel)=rgba(i,j,i+e,j+e)          ztel+=1        end if        if p(i-e,j+e) = empty then          z(ztel)=rgba(i,j,i-e,j+e)          ztel+=1        end if      end if    next j  next i  if ztel <> 0 then     return z( int( rnd * ztel ) )  end if  return 0end functionsub newgame  restore  dim as string a  for i = 0 to 14    read a    for j = 1 to len( a )    p(j,i) = val( mid( a , j , 1 ) )     next j  next iend sub  data "77777777777777"  data "77777777777777"  data "77272727272777"  data "77727272727277"  data "77272727272777"  data "77707070707077"  data "77070707070777"  data "77717171717177"  data "77171717171777"  data "77717171717177"  data "77777777777777"  data "77777777777777" screen 20 , 32randomize timernewgamedrawbordsleep `
SARG
Posts: 824
Joined: May 27, 2005 7:15
Location: FRANCE

### Re: checkers try

Hi,

Exchange color and radius in circle........ ;-)

And when loading data there is only 12 lines.
BasicCoder2
Posts: 3040
Joined: Jan 01, 2009 7:03

### Re: checkers try

Just a suggestion as to another way of doing it.
I use variables such as BS (BlockSize) so you can vary the size of the squares and pieces.
Usually I use i as the columns and j as the rows.
I think you have your circle radius and colors parameters out of order in the CIRCLE statement?
Also I think you want your board to be 12x12?

Code: Select all

`dim shared as integer p(1 to 12,1 to 12)dim shared as ulong   z(1000)const BS = 32const as integer empty = 0const as integer white = 1const as integer dwhite = 16 or 1const as integer black  = 2const as integer dblack = 16 or 2const as integer wal = 7type CMOVE  as integer x  as integer yend typesub drawBord  for j as integer = 3 to 10    for i as integer = 3 to 10      select case p(i,j)        case white          circle(i*BS+BS/2,j*BS+BS/2),3,rgb(255,255,0),,,,f        case dwhite          circle(i*BS+BS/2,j*BS+BS/2),5,rgb(255,255,0),,,,f        case black          circle(i*BS+BS/2,j*BS+BS/2),3,rgb(0,0,255),,,,f        case dblack          circle(i*BS+BS/2,j*BS+BS/2),5,rgb(0,0,255),,,,f        case else      end select      line(i*BS,j*BS)-(i*BS+BS-1,j*BS+BS-1),rgb(0,255,0),b    next i  next jend subsub newgame  restore  dim as string a  for j as integer = 1 to 12    read a    for i as integer = 1 to len( a )      p(i,j) = val( mid( a , i , 1 ) )     next i  next jend sub  data "777777777777"  data "777777777777"  data "772727272777"  data "777272727277"  data "772727272777"  data "777070707077"  data "770707070777"  data "777171717177"  data "771717171777"  data "777171717177"  data "777777777777"  data "777777777777" screen 20 , 32randomize timernewgamedrawbordsleep `
bluatigro
Posts: 508
Joined: Apr 25, 2012 10:35
Location: netherlands

### Re: checkers try

the bord is 10x10
the reson for 14x14
is that i don't have to detect the edges

error :
fbidetemp.exe stops

Code: Select all

`dim shared as integer p(14,14) , i , j dim shared as ulong z(1000)const as integer empty = 0const as integer white = 1const as integer dwhite = 16 or 1const as integer black = 2const as integer dblack = 16 or 2const as integer wal = 7const as integer size = 30sub drawbord  for i = 3 to 12    for j = 3 to 12      select case p(i,j)        case white          circle(i*size+size/2,j*size+size/2) _          ,size/3,rgb(255,255,0),,,,f        case dwhite          circle(i*size+size/2,j*size+size/2) _          ,size/2,rgb(255,255,0),,,,f        case black          circle(i*size+size/2,j*size+size/2) _          ,size/3,rgb(0,0,255),,,,f        case dblack          circle(i*size+size/2,j*size+size/2) _          ,size/2,rgb(0,0,255),,,,f        case empty          line(i*size,j*size)-step(size,size),rgb(0,255,0),bf        case else      end select    next j  next iend subfunction random_move( q as integer ) as ulong  ''looks for al posible moves  ''returns random move   ''if no move then 0  dim as integer ztel = 0 , e=1  for i = 3 to 12    for j = 3 to 12      if p(i,j)<>wal andalso ( p(i,j)and q ) then        if q = black then e=-1        if p(i+e,j+e) = empty then          z(ztel)=rgba(i,j,i+e,j+e)          ztel+=1        end if        if p(i-e,j+e) = empty then          z(ztel)=rgba(i,j,i-e,j+e)          ztel+=1        end if      end if    next j  next i  if ztel <> 0 then     return z( int( rnd * ztel ) )  end if  return 0end functionsub newgame  restore  dim as string a  for i = 0 to 14    read a    for j = 1 to len( a )    p(j-1,i) = val( mid( a , j , 1 ) )     next j  next iend sub  data "77777777777777"  data "77777777777777"  data "77272727272777"  data "77727272727277"  data "77272727272777"  data "77727272727277"  data "77070707070777"  data "77707070707077"  data "77171717171777"  data "77717171717177"  data "77171717171777"  data "77717171717177"  data "77777777777777"  data "77777777777777" screen 20 , 32randomize timernewgamedrawbordsleep `
MrSwiss
Posts: 2291
Joined: Jun 02, 2013 9:27
Location: Switzerland

### Re: checkers try

Well, the Array definition, currently says different: (15 x 15)
dim shared as integer p(14,14)

Explanation (lets write it BASIC style, to show the error):
dim shared as integer p(0 to 14, 0 to 14)
declares actually a 15 x 15 elements Array, therefore, its either:

dim shared as integer p(13,13) ' NULL based, or one based (below)
dim shared as integer p(1 to 14, 1 to 14)

Btw: a Checker Board is: 8 x 8 (Standard).
8 x 8 Board (with borders), alternative drawing method (no Array!)
The code is proof of concept for a re-coloring method, using Alpha-Channel value,
but the Board drawing remains the same.
BasicCoder2
Posts: 3040
Joined: Jan 01, 2009 7:03

### Re: checkers try

Yes I understood the use of a border for not needing to check for out of bounds.
I assumed eight squares not nine squares for a checkers board.
The pieces are drawn on top of the board so the board or at least each square has to be drawn before the piece.
You could also insert code to draw a two color board shown below in drawbord().
Personally I find using the step word confusing.
Note I think you only need 3 to 11 (9 squares?)
Also I think there is a problem with double pieces being 16 and 17? How to put that in the data strings?

Code: Select all

`sub drawbord  for i = 3 to 11    for j = 3 to 11      'assume a blank square      line (i*size,j*size)-(i*size+size,j*size+size),rgb(200,100,40),bf      select case p(i,j)      case white        'draw back color of square first        line(i*size,j*size)-step(size,size),rgb(0,255,0),bf        'then draw the piece on top          circle(i*size+size/2,j*size+size/2) _          ,size/3,rgb(255,255,0),,,,f        case dwhite          line(i*size,j*size)-step(size,size),rgb(0,255,0),bf          circle(i*size+size/2,j*size+size/2) _          ,size/2,rgb(255,255,0),,,,f        case black          line(i*size,j*size)-step(size,size),rgb(0,255,0),bf          circle(i*size+size/2,j*size+size/2) _          ,size/3,rgb(0,0,255),,,,f        case dblack          line(i*size,j*size)-step(size,size),rgb(0,255,0),bf          circle(i*size+size/2,j*size+size/2) _          ,size/2,rgb(0,0,255),,,,f        case empty          line(i*size,j*size)-step(size,size),rgb(0,255,0),bf        case else      end select    next j  next iend sub`
Last edited by BasicCoder2 on Jan 17, 2018 15:52, edited 2 times in total.
MrSwiss
Posts: 2291
Joined: Jun 02, 2013 9:27
Location: Switzerland

### Re: checkers try

BasicCoder2 wrote:Personally I find using the step word confusing.
Not at all, it simply states:
the second set of coordinates, is relative (to the start coordinates) instead of absolute.

Thus, eliminating some calculations required, otherwise. (Speed, is the name of the game!)
BasicCoder2
Posts: 3040
Joined: Jan 01, 2009 7:03

### Re: checkers try

For me I would ask: Is it fast enough? Usually I would put readability (by a novice) before speed.
Ok. STEP is "readable" once you get it and perhaps are cognisant of the hidden draw variables for position.

Code: Select all

`screenres 640,480,32pset (100,100)   'set draw coordinates at 100,100line -(40,40)    'draw relative to 140,140sleep`

Back in 2008 I played around with checker programs. We all have our own way of coding stuff.
This is one way I drew a checker board for comparison which may give bluatigro some alternate ideas.

Code: Select all

`dim shared as integer b(12,12)      'board arrayconst BLANK = 0const WHITE = 1const BLACK = 2const WKING = 3 'white Kingconst BKING = 4 'black Kingconst MAG = 40  'magnification of game boardscreen 12'initialize board valuesfor j as integer = 0 to 11  for i as integer = 0 to 11    read b(i,j)  next inext jsub DisplayBoard()  screenlock  cls  dim as integer shade  shade = 0  'shade flag  line (0,0)-(639,479),15,bf  for j as integer = 2 to 9    if shade=0 then shade=1 else shade=0    'toggle shade flag    for i as integer = 2 to 9      if shade=0 then shade=1 else shade=0  'toggle shade flag      if shade=0 then        line(i*MAG,j*MAG)-(i*MAG+MAG-2,j*MAG+MAG-2),7,b    'unshaded      else        line(i*MAG,j*MAG)-(i*MAG+MAG-2,j*MAG+MAG-2),7,bf   'shaded square        'draw any player pieces in square        if b(i,j)=WHITE or b(i,j)=WKING then          circle(i*MAG+MAG/2,j*MAG+MAG/2),MAG/4,15,,,,f 'a white piece          circle(i*MAG+MAG/2,j*MAG+MAG/2),MAG/4,0        end if        if b(i,j)=BLACK or b(i,j)=BKING then          circle(i*MAG+MAG/2,j*MAG+MAG/2),MAG/4,0,,,,f 'a black piece          circle(i*MAG+MAG/2,j*MAG+MAG/2),MAG/4,0        end if        if b(i,j)=WKING then          circle(i*MAG+MAG/2,j*MAG+MAG/2),MAG/8,1,,,,f  'a white KING        end if        if b(i,j)=BKING then          circle(i*MAG+MAG/2,j*MAG+MAG/2),MAG/8,4,,,,f  'a black KING        end if              end if    next i  next j    screenunlockend subdo  DisplayBoard()  sleep 2loop until multikey(&H01)data 9,9,9,9,9,9,9,9,9,9,9,9data 9,9,9,9,9,9,9,9,9,9,9,9data 9,9,9,1,9,1,9,1,9,1,9,9data 9,9,1,9,1,9,1,9,1,9,9,9data 9,9,9,1,9,1,9,1,9,1,9,9data 9,9,0,9,0,9,0,9,0,9,9,9data 9,9,9,0,9,0,9,0,9,0,9,9data 9,9,2,9,2,9,2,9,2,9,9,9data 9,9,9,2,9,2,9,2,9,2,9,9data 9,9,2,9,2,9,2,9,2,9,9,9data 9,9,9,9,9,9,9,9,9,9,9,9data 9,9,9,9,9,9,9,9,9,9,9,9`
SARG
Posts: 824
Joined: May 27, 2005 7:15
Location: FRANCE

### Re: checkers try

BasicCoder2 wrote:Ok. STEP is "readable" once you get it and perhaps are cognisant of the hidden draw variables for position.

Code: Select all

`screenres 640,480,32pset (100,100)   'set draw coordinates at 100,100line -(40,40)    'draw relative to 140,140sleep`

No, no, no --> draw 100,100 to 40,40. It only keeps the last position.

For testing try with

Code: Select all

`screenres 640,480,32pset (100,100) line -(0,0)sleep`
Last edited by SARG on Jan 17, 2018 20:55, edited 1 time in total.
MrSwiss
Posts: 2291
Joined: Jun 02, 2013 9:27
Location: Switzerland

### Re: checkers try

BasicCoder2 wrote:Usually I would put readability (by a novice) before speed.
Your codes (all of them) are examples, of non-readability, IMHO.
(all the clumsy calculations, in Sub/Function parameters, to be precise,
apart from the fact, that it is also: sloooooooooow)