Skip-Bo game (iPad only)

Post Reply
Henko
Posts: 814
Joined: Tue Apr 09, 2013 12:23 pm
My devices: iPhone,iPad
Windows
Location: Groningen, Netherlands
Flag: Netherlands

Skip-Bo game (iPad only)

Post by Henko »

Please read the Skip-Bo manual on internet for the playing rules.
To play a card, first touch the card, then touch the target stack where to place the card.
A selected card may be returne to its original stack by touching that stack again.
IMG_1252.png
IMG_1252.png (348.92 KiB) Viewed 1167 times

Code: Select all

' Skip-bo game
'
dim stock(163),op(5,13),p_op(5),pos(6,5)
pnum=15    '*** initial number of cards/player ***
paus=0.9   '*** length of pause after beep ***
dim player(4,pnum+1),p_player(4),af(4,5,21),p_af(4,5)
dim hand(4,5),xy(5,3),xyi(5)
prog_init()
p=rnd(4)-1    ' player p starts the game
do slowdown
p+=1 ! if p=4 then p=0  '  next player's turn
if p=0 then human_turn() else comp_turn(p)
until forever
end

def prog_init()
    '*** prepare user interface ***
' input "Enter your name": speler$
graphics ! graphics clear .8,.8,.8
get screen size .sw,.sh ! .xc=.sw/2 ! .yc=332
dim .dxdy(4,2)
for i=0 to 3 ! read .dxdy(i,0),.dxdy(i,1) ! next i
data 0,235, -216,0, 0,-185, 216,0
def_pos()   ' prepare locations of the buttons
    '*** prepare the card decks ***
build_stock()   
for i=0 to 3    ' deal cards to the players
  for j=1 to .pnum ! push_player(i,pull_stock()) ! next j
  next i
    '*** display the scene ***
set buttons custom
set buttons font size 29
but_pos(.xc,.yc)      ' display the cental group
disp_card("c0",14,.pos(4,0))
for i=1 to 4 ! disp_card("c"&i,0,.pos(4,i)) ! next i
for p=0 to 3          ' display the 4 player groups
  x=.xc+.dxdy(p,0) ! y=.yc+.dxdy(p,1)
  n$="p"&p&"-c" ! card=.player(p,.p_player(k))
  disp_card(n$,card,.pos(p,0))
  for j=1 to 4
    n$="p"&p&"-"&j ! card=.af(p,j,.p_af(p,j))
    disp_card(n$,card,.pos(p,j))
    next j
  draw color 0,0,0 ! draw size 2
  draw rect x-100,y-100 to x+100,y+80
  if p=0 then ! draw text "You !" at x-30,y-95
    else !  draw text "Player"&p at x-40,y-95
    end if
  draw text "("&.p_player(p)&")" at x-23,y+45
  draw color 1,1,1
  pause 1
  fill color 1,0,0
  button "s"&p text "" at sw/2+x-10,y-70 size 20,20
  button "s"&p hide
  replenish(p)
  next p
end def

def build_stock()   ' build central stock of 162 cards
dim res(163)
p=0          
for i=1 to 12
for j=1 to 12 ! p+=1 ! res(p)=j ! next j
next i
for i=1 to 18 ! p+=1 ! res(p)=13 ! next i
randomize
for i=1 to 162
do ! r=1+rnd(162) ! until res(r)>0
.stock(i)=res(r) ! res(r)=0
next i
.p_stock=162
end def

def human_turn()
button "s"&0 show
replenish(0) ! from=0 ! end_turn=0
do slowdown
if bp("p0-c") then  '*** player button ***
  if from=0 then ! from=1 ! card=pull_player(0)
    else
    if from=1 then ! from=0 ! push_player(0,card) ! end if
    end if
  draw_number(0)
  end if
k=-1                '*** hand button ***
for i=0 to 4
  if bp("h0-"&i) then ! k=i ! break ! end if
  next i
if k>=0 then
  if from=0 and .hand(0,k)>0 then
    from=2 ! card=get_hand(0,k)
    else
    if from=2 and .hand(0,k)=0 then 
      from=0 ! put_hand(0,k,card) ! card=0
      end if
    end if
  end if
k=0              '*** set aside buttons ***
for i=1 to 4
  if bp("p0-"&i) then ! k=i ! break ! end if
  next i
if k>0 then
  if from=0 then
    if .p_af(0,k)=0 then continue
    from=k+3 ! card=pull_af(0,k)
    else
    if from=2 or from=k+3 then
      push_af(0,k,card)
      if from=2 then end_turn=1
      from=0 ! card=0
      end if
    end if
  end if
k=0            '*** build up buttons
for i=1 to 4
  if bp("c"&i) then ! k=i ! break ! end if
  next i
if k=0 or from=0 then continue
topcard=.op(k,.p_op(k))
if card=topcard+1 or card=13 then
  card=topcard+1 ! push_op(k,card)
  win(0)
  from=0 ! card=0
  nh=0
  for i=0 to 4 ! if .hand(0,i)>0 then nh+=1 ! next i
  if nh=0 then replenish(0)
  end if
until end_turn
button "s"&0 hide
end def

def comp_turn(p)
button "s"&p show
replenish(p) ! from=0 ! end_turn=0 ! pause .paus
do
win(p)
card=.player(p,.p_player(p))  ' target card to get rid off 
k=check_op(card)
if k=0 and card=13 then
  k=1+rnd(4) ! .player(p,.p_player(p))=.op(k,.p_op(k))+1
  end if
if k then
  push_op(k,pull_player(p)) ! draw_number(p)
  continue
  end if
for i=0 to 4                 ' check cards in hand
  card=.hand(p,i)
  k=check_op(card)
  if k=0 and card=13 then
    k=1+rnd(4) ! .hand(p,i)=.op(k,.p_op(k))+1
    end if
  if k then ! push_op(k,get_hand(p,i)) ! break ! end if
  next i
nh=0
for i=0 to 4 ! if .hand(p,i)>0 then nh+=1 ! next i
if nh=0 then replenish(p)
if k then ! k=0 ! continue ! end if
for i=1 to 4                 ' check aside cards
  card=.af(p,i,.p_af(p,i))
  k=check_op(card)
  if k=0 and card=13 then
    k=1+rnd(4) ! .af(p,i,.p_af(p,i))=.op(k,.p_op(k))+1
    end if
  if k then ! push_op(k,pull_af(p,i)) ! break ! end if
  next i
if k then ! k=0 ! continue ! end if
maxc=0 ! end_turn=1
do ! i=rnd(5) ! until .hand(p,i)>0
k=1+rnd(4) ! push_af(p,k,get_hand(p,i))
until end_turn
button "s"&p hide
draw_number(p) ! pause .paus
end def

def check_op(card)
k=0
for i=1 to 4
if card=.op(i,.p_op(i))+1 then ! k=i ! break ! end if
next i
return k
end def

def replenish(p)   ' replenish hand of player p to 5 cards
for i=0 to 4
if .hand(p,i)=0 then put_hand(p,i,pull_stock())
next i
end def

def pull_stock()
if .p_stock=0 then build_stock()
.p_stock-=1 ! return .stock(.p_stock+1)
end def

def push_op(n,card)
beep
.p_op(n)+=1 ! .op(n,.p_op(n))=card
if .p_op(n)=12 then .p_op(n)=0
disp_card("c"&n,.op(n,.p_op(n)),.pos(4,n))
pause .paus
end def

def pull_player(p)
if .p_player(p)=0 then return 0
.p_player(p)-=1
disp_card("p"&p&"-c",.player(p,.p_player(p)),.pos(p,0))
return .player(p,.p_player(p)+1)
end def

def push_player(p,card)
.p_player(p)+=1 ! .player(p,.p_player(p))=card
disp_card("p"&p&"-c",card,.pos(p,0))
end def

def pull_af(p,i)
if .p_af(p,i)=0 then return
.p_af(p,i)-=1 ! pp=.p_af(p,i)
disp_card("p"&p&"-"&i,.af(p,i,pp),.pos(p,i))
return .af(p,i,pp+1)
end def

def push_af(p,i,card)
.p_af(p,i)+=1 ! pp=.p_af(p,i) ! .af(p,i,pp)=card
disp_card("p"&p&"-"&i,card,.pos(p,i))
end def

def get_hand(p,i)
card=.hand(p,i) ! .hand(p,i)=0
if p=0 then disp_card("h0-"&i,0,.pos(5,i))
return card
end def

def put_hand(p,i,card)
.hand(p,i)=card
if p=0 then disp_card("h0-"&i,card,.pos(5,i))
end def


def but_pos(xc,yc)
dx=25 ! dy=15
xo=xc-.5*.xcard ! yo=yc-.5*.ycard ! .xyi(0)=xo+1i*yo
x1=xc-1.5*.xcard-dx ! y1=yc-.ycard-.5*dy ! .xyi(1)=x1+1i*y1
x2=xc+.5*.xcard+dx  ! y2=y1 ! .xyi(2)=x2+1i*y2
x3=x2 ! y3=yc+.5*dy ! .xyi(3)=x3+1i*y3
x4=x1 ! y4=y3 ! .xyi(4)=x4+1i*y4
end def

def disp_card(name$,type,xy)
x=real(xy) ! y=imag(xy)
if type<13 then       '*** number card ***
  if type=0 then fill color 1,1,1 
  if type>0 and type<5 then fill color 0,0,1
  if type>4 and i<9 then fill color 0,.7,0
  if type>8 then fill color 1,0,0
  button name$ text type at x,y size .xcard,.ycard
  end if
if type=13 then
fill color .64,.16,.16    '*** define joker card ***
button name$ text chr$(58654) at x,y size .xcard,.ycard
end if
if type=14 then
fill color .4,.4,.4    '*** define back side of cards ***
button name$ text "" at x,y size .xcard,.ycard
end if
end def

def create_5cards()
x=175 ! y=640
for i=0 to 4
name$="h"&0&i ! card=.hand(0,i)
disp_card(name$,card,.pos(5,i))
next i
end def

def def_pos()
xb=5.6 ! yb=8.7/5.6*xb ! scal=7
.xcard=int(scal*xb) ! .ycard=int(scal*yb)
but_pos(.xc,.yc)      ' central group
for j=0 to 4 ! .pos(4,j)=.xyi(j) ! next j
for p=0 to 3          ' 4 player groups
 x=.xc ! y=.yc
 if p=0 then y+=235 ! if p=1 then x-=216
 if p=2 then y-=185 ! if p=3 then x+=216
 but_pos(x,y)
 for j=0 to 4 ! .pos(p,j)=.xyi(j) ! next j
 next p
x=240 ! y=690
for j=0 to 4 ! .pos(5,j)=x+j*65+1i*y ! next j
end def

def draw_number(p)
x=.xc+.dxdy(p,0)-21 ! y=.yc+.dxdy(p,1)+45
fill color .8,.8,.8 ! draw color 0,0,0
fill rect x,y to x+43,y+23
draw text "("&.p_player(p)&")" at x-2,y
draw color 1,1,1
end def

def win(p)
if .p_player(p) then return
draw_number(p)
draw font size 40 ! draw color 0,0,1
if p=0 then ! draw text "You have won !!" at 220,850
  else ! draw text "Player "&p&" has won !!" at 180,850
  end if
stop
end def

def tus(i)
draw color 0,0,0
fill color 1,1,1 
button "but" text "tus("&i&")" at 500,800 size 80,50
button "debug" text "debug" at 500,870 size 80,50
do slowdown
if bp("debug") then db
until bp("but")
button "but" delete ! button "debug" delete ! draw color 1,1,1
end def

def fcol(f$,c)
r=0 ! if c>3 then ! r=1 ! c-=4 ! end if
g=0 ! if c>2 then ! g=1 ! c-=2 ! end if
if f$="d" then draw color r,g,c else fill color r,g,c
end def

def db ! debug pause ! end def
def bp(a$) = button_pressed(a$)


Post Reply