# peg
# c) 2010 j.m.reneau
cls
print "peg - classic peg jumping game"
print "test how smart you are by jumping"
print "pegs over each other and then"
print "removing the jumpped peg?"
print "Can you have only 1 peg remaining?"
fastgraphics
d = 7 # width of board
cu = 2 # size of cutout from corner
rh = 18 # radius of hole
rp = 15 # radius of hole
# constants used with board array
peg = 1
empty = 0
hidden = -1
# peg selected (-1 meand no peg selected
xsel = -1
ysel = -1
dim board(d,d)
gosub setupboard
gosub drawboard
while true
what = peg
gosub selectspot
if board[x,y] = peg then
xsel = x
ysel = y
gosub drawboard
else
if xsel <> -1 and ysel <> -1 then
if (abs(x-xsel) = 2 xor abs(y-ysel) = 2) then
if board[(x+xsel)/2,(y+ysel)/2] = peg then
board[x,y] = peg
board[(x+xsel)/2,(y+ysel)/2] = empty
board[xsel,ysel] = empty
xsel = -1
ysel = -1
gosub drawboard
end if
end if
end if
end if
end while
end
selectspot: # return x,y if user selects a peg or empty
while true
clickclear
while clickb = 0
pause .01
end while
x = int(clickx / graphwidth * d)
y = int(clicky / graphheight * d)
if board[x,y] = peg or board[x,y] = empty then return
end while
drawboard: #
clg
for x = 0 to d-1
for y = 0 to d-1
xpos = graphwidth / d * (x+.5)
ypos = graphheight / d * (y+.5)
if board[x,y] = peg or board[x,y] = empty then
color black
circle xpos,ypos,rh
endif
if board[x,y] = peg then
if x = xsel and y = ysel then
color red
else
color white
end if
circle xpos,ypos,rp
endif
next y
next x
refresh
return
setupboard: #
# fill entire board with pegs
for x = 0 to d-1
for y = 0 to d-1
board[x,y] = peg
next y
next x
# cut out the corners as hidden
for x = 0 to cu-1
for y = 0 to cu-1
board[x,y] = hidden
board[d-x-1,y] = hidden
board[x,d-y-1] = hidden
board[d-x-1,d-y-1] = hidden
next y
next x
# start with the cemter empty
board[int(d/2), int(d/2)] = empty
return