## Tuesday, October 22, 2013

### Nim (with levels)

#After complaints I added some levels to the game

#Take as many pieces you want from one of the piles

#The one who takes the last piece wins

Print "Click to select the collumn and the number of pieces to remove "
Print "Who takes the last wins "
fastgraphics
graphsize 400,420
font("arial",17,100)
a=0
b=0
c=0
d=0
level\$ = prompt("What level 3-7")
level = int(level\$)
if level >6 then a=7
if level >5 then b=6
if level >4 then c=5
if level >3 then d=4
e=3
f=2
g=1

for turn = 1 to 1000
gosub draw
if a+b+c+d+e+f+g=0 then
color white
refresh
end
end if

refresh

if turn=1 then
ans = confirm("Do you want to play first y/n? ")
if ans then Print "Start"

if not (ans) then
gosub Computerplay
gosub draw
refresh
end if
end if

gosub humanplay
gosub draw

if a+b+c+d+e+f+g=0 then
refresh
end
end if

refresh
gosub Computerplay
next turn

Computerplay:
thought =0
wining=false
do
thought=thought+1

do
at=a
bt=b
ct=c
dt=d
et=e
ft=f
gt=g
pile = int(rand*7)
If pile = 0 then at=at-int(rand*at)-1
If pile = 1 then bt=bt-int(rand*bt)-1
If pile = 2 then ct=ct-int(rand*ct)-1
If pile = 3 then dt=dt-int(rand*dt)-1
If pile = 4 then et=et-int(rand*et)-1
If pile = 5 then ft=ft-int(rand*ft)-1
If pile = 6 then gt=gt-int(rand*gt)-1
until (at>-1) and (bt>-1) and (ct>-1)and(dt>-1) and (et>-1) and (ft>-1)and (gt>-1)

units = int (mid(a\$,4,1))+int (mid(b\$,4,1))+int(mid(c\$,4,1))+int (mid(d\$,4,1))+int (mid(e\$,4,1))+int(mid(f\$,4,1))+int(mid(g\$,4,1))
twos = int (mid(a\$,3,1))+int (mid(b\$,3,1))+int(mid(c\$,3,1))+int (mid(d\$,3,1))+int (mid(e\$,3,1))+int(mid(f\$,3,1))+int(mid(g\$,3,1))
fours = int (mid(a\$,2,1))+int (mid(b\$,2,1))+int(mid(c\$,2,1))+int (mid(d\$,2,1))+int (mid(e\$,2,1))+int(mid(f\$,2,1))+int(mid(g\$,2,1))

if units/2=int(units/2) and twos/2=int(twos/2) and fours/2=int(fours/2) then wining=true
until wining=true or thought =1000
a=at
b=bt
c=ct
d=dt
e=et
f=ft
g=gt
pause 1
return

draw:
clg
color darkblue
rect 0,0,400,420
color grey
text 33,20," A .....B.....C .....D.....E .....F .....G"
for n = 1 to 7
for radius = 20 to 0 step -1
if a>= n then circle 50,n*50+30,radius
if b>= n then circle 100,n*50+30,radius
if c>= n then circle 150,n*50+30,radius
if d>= n then circle 200,n*50+30,radius
if e>= n then circle 250,n*50+30,radius
if f>= n then circle 300,n*50+30,radius
if g>= n then circle 350,n*50+30,radius
next n
for n = 1 to 7
color black
if a>= n then text 43,n*50+20,a+1-n
if b>= n then text 93,n*50+20,b+1-n
if c>= n then text 143,n*50+20,c+1-n
if d>= n then text 193,n*50+20,d+1-n
if e>= n then text 243,n*50+20,e+1-n
if f>= n then text 293,n*50+20,f+1-n
if g>= n then text 343,n*50+20,g+1-n
next n
return

humanplay:
p=0
p\$=""
clickclear
do
if clickx >30 and clicky >60 and (clickx +clicky)<470 then
if clickx<370 then p\$="g"
if clickx<320 then p\$="f"
if clickx<270 then p\$="e"
if clickx<220 then p\$="d"
if clickx<170 then p\$="c"
if clickx<120 then p\$="b"
if clickx<70 then p\$="a"

row=floor((clicky-55)/50)

If p\$ = "a" then p=a-row
If p\$ = "b" then p=b-row
If p\$ = "c" then p=c-row
If p\$ = "d" then p=d-row
If p\$ = "e" then p=e-row
If p\$ = "f" then p=f-row
If p\$ = "g" then p=g-row

If p\$ = "a" then a=a-p
If p\$ = "b" then b=b-p
If p\$ = "c" then c=c-p
If p\$ = "d" then d=d-p
If p\$ = "e" then e=e-p
If p\$ = "f" then f=f-p
If p\$ = "g" then g=g-p
end if
pause .1

until p >0

return