Chessboard - A simple 2 player chess set (iPad only)

Post Reply
User avatar
Dav
Posts: 279
Joined: Tue Dec 30, 2014 5:12 pm
My devices: iPad Mini, iPod Touch.
Location: North Carolina, USA
Contact:

Chessboard - A simple 2 player chess set (iPad only)

Post by Dav »

A simple chess board you can use to play a quick 2 player game with a friend. No rules, no AI. Just a free style board with moving pieces. Captured pieces are sent to the holding pen at the bottom. You can take back a capture if you need too.

- Dav

New for v1.1 - Holding pen is now working. Added a clear button to clear board.

Code: Select all

'chessboard.txt v1.1
'Simple Chessboard for 2 players (ipad only)
'Coded by Dav, OCT/2017

'Making a simple chess set for 2 players.
'It's just a simple digital chess set you can
'use to play a quick game with a your friend.
'There is no AI game play, no rules, etc...

'Captured pieces are sent to holding pen.
'You can move pieces to and from holding pen.


if lowstr$(device_type$())<>"ipad" then
  print "Sorry, only designed for iPad."
  end
end if


option base 1
set toolbar off
get orientation ort
set orientation vertical

graphics
graphics clear .3,.3,.3
get screen size sw,sh
sz=sw/8

dim btnv(128),btnc(128),btnx(128),btny(128),pen(64)

'white pieces
wk=9812!wq=9813!wc=9814!wb=9815!wh=9816!wp=9817
'black pieces
bk=9818!bq=9819!bc=9820!bb=9821!bh=9822!bp=9823

redraw:

'set white places
btnv(1)=wc!btnv(2)=wh!btnv(3)=wb!btnv(4)=wq
btnv(5)=wk!btnv(6)=wb!btnv(7)=wh!btnv(8)=wc
for t=9 to 16
   btnv(t)=wp
next t

'rest of board is clear
for t=17 to 48
    btnv(t)=0
next t

'set black pieces
for t=49 to 56
   btnv(t)=bp
next t
btnv(57)=bc!btnv(58)=bh!btnv(59)=bb!btnv(60)=bq
btnv(61)=bk!btnv(62)=bb!btnv(63)=bh!btnv(64)=bc

set buttons custom
set buttons font size sz

draw color 0,0,0

'draw board, set x/y button values

b=1
for y= 0 to 7
  for x = 0 to 7
    if odd(b+y) then
      fill color .6,.6,.1
      btnc(b)=0
    else
      fill color 1,1,.8
      btnc(b)=1
    end if
    btnx(b)=x*sz!btny(b)=y*sz
    button str$(b) text chr$(btnv(b)) at btnx(b),btny(b) size sz,sz
    b=b+1
  next x
next y

set buttons font size sz/2
'draw a holding pen for captured pieces
b=65!p=1
for y =0 to 3
   y2=y*(sz/2)+(sz*8)+20
   for x=0 to 7
      x2=x*sz/2
      btnx(b)=x2!btny(b)=y2
      btnc(b)=0
      button str$(b) text chr$(0) at x2,y2 size sz/2,sz/2
      pen(p)=0
      b=b+1!p=p+1
   next x
next y


set buttons font size sz/2
button "clear" text "Clear" at 425,900
button "quit" text "Quit" at 600,900
button "redraw" text "Redraw board" at 425,800
set buttons font size sz


main:

selected=0

second:


do

  for b=1 to 96 '(8*8)

     if button_pressed(str$(b)) then
     
        if b>64 then bs=sz/2 else bs=sz

        if selected=0 then

           'first choice...

           'only if button has a value
           if btnv(b)=0 then goto main

           'highlight this button
           fill color .8,.8,1
           button str$(b) text chr$(btnv(b)) at btnx(b),btny(b) size bs,bs
           selected=1
           'save selected button num
           selb=b  'num of selected button
           'get second choice
           goto second

        else

           'making 2nd choice...

           'if clicked same button...
           if b=selb then
             drawbtn(selb,btnv(selb))
             goto main
           end if

           if b<>selb then

             'clicked another square
             'if its empty...
             if btnv(b)=0 then

                'just move it...
                'set new value
                btnv(b)=btnv(selb)
                'draw new piece moved
                drawbtn(b,btnv(selb))

                'erase old square
                drawbtn(selb,0)
                'wipe out its value
                btnv(selb)=0
                
             else
                 
                 'if white made the move...
                 if side(btnv(selb))=1 then
                   'if selected another white...
                   '... just reset
                   if side(btnv(b))=1 then 
                     'reset 1st selected
                     drawbtn(selb,btnv(selb))
                     'goto main
                   end if

                   if side(btnv(b))=0 then
                     
                     'capture piece
                     
                     'send it to holding pen
                     for h=1 to 64
                       if pen(h)=0 then
                          pen(h)=btnv(b)
                          drawbtn(64+h,btnv(b))
                          btnv(64+h)=btnv(b)
                          break
                       end if
                     next h

                     'set new values
                     btnv(b)=btnv(selb)
                     btnv(selb)=0

                     'draw new place
                     drawbtn(b,btnv(b))
                     'erase old place
                     drawbtn(selb,0)
                     'goto main
                    
                   end if

                 else


                   'black made the move
                   'if selected another black...
                   '... just reset
                   if side(btnv(b))=0 then 
                     'reset 1st selected
                     drawbtn(selb,btnv(selb))
                     'goto main
                   end if

                   if side(btnv(b))=1 then

                     'send it to holding pen
                     for h=1 to 64
                       if pen(h)=0 then
                          pen(h)=btnv(b)
                          drawbtn(64+h,btnv(b))
                          btnv(64+h)=btnv(b)
                          break
                       end if
                     next h

                     'set new values
                     btnv(b)=btnv(selb)
                     btnv(selb)=0

                     'draw new place
                     drawbtn(b,btnv(b))
                     'erase old place
                     drawbtn(selb,0)
                     'goto main
                   end if

                 end if

             end if

             'refresh holding pen info
             for u=1 to 64
               pen(u)=btnv(64+u)
             next u
             
             goto main

           end if
        end if

     end if
  next b

  if button_pressed("quit") then
   set orientation ort ! end
  end if

  if button_pressed("redraw") then goto redraw

  if button_pressed("clear") then gosub clearboard

until 0



end


clearboard:

for t=1 to 64
    btnv(t)=0
next t
btnv(65)=wc!btnv(66)=wh!btnv(67)=wb!btnv(68)=wq
btnv(69)=wk!btnv(70)=wb!btnv(71)=wh!btnv(72)=wc
for t=73 to 80
   btnv(t)=wp
next t
for t=81 to 88
   btnv(t)=bp
next t
btnv(89)=bc!btnv(90)=bh!btnv(91)=bb!btnv(92)=bq
btnv(93)=bk!btnv(94)=bb!btnv(95)=bh!btnv(96)=bc

for d=1 to 96
   if d>64 then bs=sz/2 else bs=sz
   if btnc(d)=1 then
     fill color 1,1,.8
   else
     fill color .6,.6,.1
   end if
   button str$(d) text chr$(btnv(d)) at btnx(d),btny(d) size bs,bs
next d

return



def side(a)
'returns what color piece a is.
'returns 0 if black, 1 if white

if a=.wc then return 1
if a=.wh then return 1
if a=.wb then return 1
if a=.wq then return 1
if a=.wk then return 1
if a=.wp then return 1

if a=.bc then return 0
if a=.bh then return 0
if a=.bb then return 0
if a=.bq then return 0
if a=.bk then return 0
if a=.bp then return 0

end def


def drawbtn(num,val)
if .btnc(num)=1 then
   fill color 1,1,.8
else
   fill color .6,.6,.1
end if

if num <65 then
   set buttons font size .sz
   button str$(num) text chr$(val) at .btnx(num),.btny(num) size .sz,.sz
else
    set buttons font size .sz/2
    button str$(num) text chr$(val) at .btnx(num),.btny(num) size .sz/2,.sz/2
end if

end def

Attachments
ChessBoard.jpg
ChessBoard.jpg (97.08 KiB) Viewed 3157 times
Last edited by Dav on Tue Oct 17, 2017 1:19 pm, edited 3 times in total.

User avatar
Dutchman
Posts: 851
Joined: Mon May 06, 2013 9:21 am
My devices: iMac, iPad Air, iPhone
Location: Netherlands
Flag: Netherlands

Re: Chessboard - A simple 2 player chess set (iPad only)

Post by Dutchman »

beautiful design :D

User avatar
Mr. Kibernetik
Site Admin
Posts: 4786
Joined: Mon Nov 19, 2012 10:16 pm
My devices: iPhone, iPad, MacBook
Location: Russia
Flag: Russia

Re: Chessboard - A simple 2 player chess set (iPad only)

Post by Mr. Kibernetik »

Yes, very cool!

User avatar
rbytes
Posts: 1338
Joined: Sun May 31, 2015 12:11 am
My devices: iPhone 11 Pro Max
iPad Pro 11
MacBook
Dell Inspiron laptop
CHUWI Plus 10 convertible Windows/Android tablet
Location: Calgary, Canada
Flag: Canada
Contact:

Re: Chessboard - A simple 2 player chess set (iPad only)

Post by rbytes »

Well done, Dav!
The only thing that gets me down is gravity...

User avatar
Dav
Posts: 279
Joined: Tue Dec 30, 2014 5:12 pm
My devices: iPad Mini, iPod Touch.
Location: North Carolina, USA
Contact:

Re: Chessboard - A simple 2 player chess set (iPad only)

Post by Dav »

Thanks, everyone.I'll try to work on it more this week.

I came across an interesting buttons behavior that I wasn't aware of while working on the chess holding pen. I couldn't figure out why setting a new buttons font size would not change the font size of the button when redrawing it. It turns out that when you create a button, it's font size is set, and you have to delete it first to redraw the same button name with a new font size. Everything else can be altered and the button redrawn without deleting, but not setting a new font size. The button has to be deleted first for that. Here's a small example showing what I mean.

- Dav

Code: Select all

'Example - Button must be deleted to give it a new font size.
'Code by Dav

'show an interesting behavior of buttons.
'once you set the buttons font size and create the
'button, it's font size is set and can't be changed
'and redrawn unless the button is first deleted.
'All other button settings can be changed/redrawn
'without deleting the button. Only a new font size 
'cannot be set. The button must be deleted first.

graphics
draw color 0,0,0

set buttons custom
set buttons font size 100

button "1" text chr$(9817) at 200,200 size 100,100
pause 1

'now we will change all button 1 settings.
'everything except font size is changable.
'unless you uncomment below....
'button "1" delete

set buttons font size 50 '<<< this won't change font size

button "1" text chr$(9816) at 100,100 size 50,50

User avatar
Dav
Posts: 279
Joined: Tue Dec 30, 2014 5:12 pm
My devices: iPad Mini, iPod Touch.
Location: North Carolina, USA
Contact:

Re: Chessboard - A simple 2 player chess set (iPad only)

Post by Dav »

A new version is posted in first post with a working holding pen. When capturing a players piece it is sent to the holding pen. You can move pieces to and from the holding pen to take back moves. Also added a clear button to clear the board in case you wanted to set up the board in a special way for other games.

I think it's all working correctly. Let me know if you find an error.

- Dav

Post Reply