- 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