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
alert ( "Computer (me) won")
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
alert ("You won")
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)

a$ = toradix (at+8,2)
b$ = toradix (bt+8,2)
c$ = toradix (ct+8,2)
d$ = toradix (dt+8,2)
e$ = toradix (et+8,2)
f$ = toradix (ft+8,2)
g$ = toradix (gt+8,2)

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
color rgb(255-radius*7,255-radius*7,0)
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 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

Sunday, October 13, 2013

n body simulation

# this is a n-body simulation , use arrow keys to keep it centered
# Number of inital particles


graphsize 600,600
fastgraphics
n=100
dim x(n+1)
dim y(n+1)
dim xspeed(n+1)
dim yspeed(n+1)
dim xac(n+1)
dim yac(n+1)
dim mass(n+1)
dim radius(n+1)
# initializing variables ##############
for p = 1 to n
a=rand*2*pi
r=rand*300
x[p]=r*cos(a)+300
y[p]=r*sin(a)+300
s=rand*2*pi/20
xspeed[p]=cos(a+s)-cos(a)
yspeed[p]=sin(a+s)-sin(a)
radius[p]=3
mass[p]=radius[p]^3
next p

gravity=0
dist=0
colision = 0
dx=0
dy=0
v=0
h=0

loop:
# arrow keys####################
a = key
if 16777234=a then h=h-5
if 16777236 =a then h=h+5
if 16777237=a then v=v-5
if 16777235=a then v=v+5

for u = 1 to n
for t = 1 to n
if u<>t and mass[t]>0 and mass[u]>0 then
dx=x[u]-x[t]
dy=y[u]-y[t]
dist=(dx^2 + dy^2)^(1/2)
collide = false
if dist < radius[u]+radius[t] then collide = true
# gravity code################
if collide = true then
x[u]=(x[u]*mass[u]+x[t]*mass[t])/(mass[u]+mass[t])
y[u]=(y[u]*mass[u]+y[t]*mass[t])/(mass[u]+mass[t])
xspeed[u]=(xspeed[u]*mass[u]+xspeed[t]*mass[t])/(mass[u]+mass[t])
yspeed[u]=(yspeed[u]*mass[u]+yspeed[t]*mass[t])/(mass[u]+mass[t])
mass[u]=mass[u]+mass[t]
radius[u]=mass[u]^(1/3)
mass[t]=0
radius[t]=0
end if

# colision code################
if collide = false then
gravity = mass[t]*mass[u]/dist^2
xac[u]= gravity*dx/(1000*mass[u])
yac[u]= gravity*dy/(1000*mass[u])
xspeed[u]=xspeed[u]-xac[u]
yspeed[u]=yspeed[u]-yac[u]
x[u]=x[u]+xspeed[u]
y[u]=y[u]+yspeed[u]
end if
###############################
end if
next t
color white
circle x[u]+h,y[u]+v,radius[u]
next u
refresh
clg
color black
rect 0,0,600,600

goto loop

Wednesday, October 9, 2013

Solid of revolution



# This program calculates the volume of a revolution solid.  The parameters  are hardcoded but they are highlighted for you to change
clg
font "arial", 15,100
graphsize 500,500
fastgraphics
#Graph Area######################
xmin=-6
xmax=6
ymin=-5
ymax=5
################################
tx=500/(xmax-xmin)
ty=500/(ymax-ymin)
area=0
#x Boudaries######################
bound1=-3
bound2=3
#################################
range=bound2-bound1
dx=.02
Volume_solid=0
for s = 0 to 2*pi step .1
if s <pi then
 for y = ymin to ymax 
 for x = xmin to xmax
  color black
  circle tx*x-xmin*tx, ty*y-ymin*ty,3
  if y=ymax+ymin then text tx*x-xmin*tx-15, ty*y-ymin*ty,x
  if x=0 then text tx*x-xmin*tx-15,ty*y-ymin*ty,-y+ymax+ymin
 next x : next y
line -xmin*tx,0,-xmin*tx,500 : line 0,500+ymin*ty,500,500+ymin*ty
end if
for x = xmin to xmax step dx
xg= tx*x-xmin*tx
color red
y0=y
#Equation###############################
y=sin(x)+2
########################################
yg1= 500-ty*y+ty*ymin
circle xg, yg1,1
#Solid
if x<= bound2 and  x>= bound1 then
fill= 100*(sin(s))+100
color rgb(200-fill,200-fill,fill)
arc xg-.05*y*ty*2, yg1, .1*y*ty*2, y*ty*2,s,.1
if s=0 then Volume_solid=Volume_solid+dx*(y0/2+y/2)^2*pi
#Area
if s <pi then
color green
if int(xg+1)/2 = int(xg/2+1) then line xg,yg1,xg,500+ymin*ty
fill= 255*(x-bound1)/range
end if
end if
next x
refresh
if s=0 then pause .5
pause .02
next s
input "press any key",k
Text 20,20, "Volume="+Volume_solid
refresh

Tuesday, October 8, 2013

Definite integral

# This program calculates the area between two functions the parameters functions, boundaries are hardcoded but they are highlighted for you to change





 font "arial", 15,100
 graphsize 500,500
 fastgraphics
 # Choose the dimentions of the graph#############
 xmin=-5
 xmax=5
 ymin=-5
 ymax=5
 ##########################################
 y1=0:y2=0
 tx=500/(xmax-xmin)
 ty=500/(ymax-ymin)
 for y = ymin to ymax
 for x = xmin to xmax
 circle tx*x-xmin*tx, ty*y-ymin*ty,3
 if y=ymax+ymin then text tx*x-xmin*tx-15, ty*y-ymin*ty,x
 if x=0 then text tx*x-xmin*tx-15, ty*y-ymin*ty,-y+ymax+ymin
 next x : next y
 line -xmin*tx,0,-xmin*tx,500 : line 0,500+ymin*ty,500,500+ymin*ty
 rem generate area
 #Choose the x boundaries#####################
 bound1=-2
 bound2=2
 #########################################
 area=0
 range=bound2-bound1
 dx=.01
 for x = xmin to xmax step dx
 xg= tx*x-xmin*tx
 color red
 p1=y1
 #Equation1 red#############################
 y1=x^2
 #########################################
 yg1= 500-ty*y1+ty*ymin
 circle xg, yg1,1
 color blue
 p2=y2
 #Equation2 blue###########################
 y2=x-2
 ########################################
 yg2= 500-ty*y2+ty*ymin
 circle xg, yg2,1
 if x<= bound2 and x>= bound1 then
 color darkgreen
 area=area+((y1+p1)/2-(y2+p2)/2)*dx
 if int(xg+1)/2 = int(xg/2+1) then line xg,yg1,xg,yg2
 fill= 255*(x-bound1)/range
 end if
 next x
 refresh
 input "press a key",a
 text 20,20,"Area="+area
 refresh