Code: Select all
' "WARI" game (this version iPad only)
' This is a version of an old African game, played by two players.
' In this program you play against the computer, each one turn
' at the time.
' The rules are as follows:
' Each of 12 little pits contains a number of little stones or
' marbles.
' A player selects one pit. the stones in that pit are removed from it,
' and added one by one to the pits before the selected pit (counter-
' clockwise). if the pit, receiving the last stone, now contains 2 or
' 3 stones, the pit is emptied and the stones are given to the player.
' then the pit after it (clockwise) is checked for 2 or 3 stones, and
' so on, until a pit is encountered which contains less than 2 or more
' than 3 stones. after this it's the other player's turn.
' The game stops when neither player can gain any more stones.
' The player with the larger amount of stones wins.
'
' This is a very basic version. Further upgrades would be:
' SCALING: a version that suits iPhones and iPods
' GRAPHICS: a wooden tablet with 12 holes/pits and an amount of
' marbles in the pits in stead of numbers.
' DIFFICULTY: a difficulty slider is provided. However, there is
' only one difficultilevel in this version: an medium level,
' where the computerplayer evaluates each pit and selects the pit
' with the biggest gain, without looking for the effects thereafter.
' STOP-CRITERION: not optimal yet (you might find out).
'
dim cx(12),cy(12),cnt(12)
init_prog ! initboard (cx,cy,cnt)
iterate:
if not finished then players_turn (cx,cy,cnt)
if not finished then computers_turn (cnt)
if not finished then iterate
end
def init_prog
option angle degrees ! randomize
graphics ! graphics clear .8,.8,.8
fill color .8,.8,.8 ! draw color 0,0,0
.sw=screen_width() ! .sh=screen_height() ! .ratio=.sh/.sw
.scx=.sw/2 ! .scy=.sh/2
draw size 3
.rad=15*.sw/(4*pi+30) ! .circ=pi*.rad/15 ! .scy=.scx
for i=0 to 11
.cx(i)=.rad*cos(30*i) ! .cy(i)=.rad*sin(-30*i)
.cnt(i)=2+rnd(4)
next i
.c_pot=0 ! .p_pot=0
s_old=-1
slider "d" value 1 at .scx,100 vsize .scx
draw font size 30
draw text "set difficulty level" at 210,20
draw text "low" at .scx+50,450
draw text "high" at .scx+50,100
wait: if not slider_changed("d") then wait
pause .2
while slider_changed("d") ! pause .2 ! end while
.diff=10*(1-slider_value("d")) ! slider "d" delete
fill rect 200,20 to 600,500
end def
def initboard(cx(),cy(),cnt())
graphics lock
draw font size 40
for i=0 to 11
x=.scx+cx(i) ! y=.scy+cy(i) ! v=cnt(i)
draw circle x,y size .circ
if v<10 then xp=x-10 else xp=x-24
draw text v at xp,y-20
next i
draw rect .scx-40,.scy-130 to .scx+40,.scy-80
draw text .c_pot at .scx-24,.scy-120
draw rect .scx-40,.scy+90 to .scx+40,.scy+140
draw text .p_pot at .scx-24,.scy+100
draw font size 20
draw text "computer's score" at .scx-100,.scy-156
draw text "your score" at .scx-60,.scy+64
draw font size 40
graphics unlock
end def
def finished
finished=0 ! n=0
for i=0 to 11
n+=.cnt(i)
if .cnt(i)>1 then return
next i
if n>1 then return
fill rect 220,.scy-20 to 560,.scy+20 ! diff=.p_pot-.c_pot
if diff>0 then
draw text "You win !!" at 270,.scy-20
else
if diff=0 then
draw text "it's a draw!" at 220,.scy-20
else
draw text "You lose !!" at 250,.scy-20
end if
end if
finished=1
end def
def players_turn(cx(),cy(),cnt())
fill rect 220,.scy-20 to 560,.scy+20
draw text "make your move" at 220,.scy-20
loop1: get touch(0) as x,y ! if x=-1 then loop1
for i=0 to 11
xp=.scx+cx(i) ! yp=.scy+cy(i)
if x>xp-.circ and x<xp+.circ and y>yp-.circ and y<yp+.circ then break i
next i
if i=12 then loop1
loop2: while not turn(i,"p") ! goto loop2 ! end while
end def
def computers_turn(cnt())
fill rect 220,.scy-20 to 560,.scy+20
draw text "computer moves" at 220,.scy-20
k=c_move(cnt) ! turn(k,"c")
end def
def pit(ind)
x=.scx+.cx(ind) ! y=.scy+.cy(ind) ! val=.cnt(ind)
fill color .8,.8,0 ! fill circle x,y size .circ-2
if val<10 then xp=x-10 else xp=x-24
draw text val at xp,y-20
pause 1
fill color .8,.8,.8 ! fill circle x,y size .circ-2
draw text val at xp,y-20
end def
def turn(ind,p$)
turn=0 ! ind=redu(ind)
num=.cnt(ind) ! if num=0 then return else turn=1
pit(ind)
for i=ind to ind+num
k=redu(i)
if i=ind then .cnt(k)=0 else .cnt(k)+=1
pit(k)
next i
do
n=.cnt(k)
if n=2 or n=3 then
pit(k) ! pause .5
if p$="c" then
.c_pot+=n
fill rect .scx-38,.scy-128 to .scx+38,.scy-82
draw text .c_pot at .scx-24,.scy-120
else
.p_pot+=n
fill rect .scx-38,.scy+92 to .scx+38,.scy+138
draw text .p_pot at .scx-24,.scy+100
end if
.cnt(k)=0 ! pit(k) ! pause .5
k-=1 ! if k<0 then k+=12
end if
until n<2 or n>3
end def
def c_move(cnt())
res=-1 ! ind=-1
for i=0 to 11
if cnt(i)=0 then continue else w=win(i)
if w>res then ! res=w ! ind=i ! end if
next i
c_move=ind
end def
def win(ind)
dim w(12)
for i=0 to 11 ! w(i)=.cnt(i) ! next i
win=0 ! sum=0 ! ind=redu(ind)
num=w(ind) ! if num=0 then return
for i=ind to ind+num
k=redu(i)
if i=ind then w(k)=0 else w(k)+=1
next i
do
n=w(k)
if n=2 or n=3 then
sum+=n
k-=1 ! if k<0 then k+=12
end if
until n<2 or n>3
win=sum
end def
def redu(x)
while x>11 ! x-=12 ! end while
redu=x
end def
def pi=355/113