setup()
type user_input
   click as integer
   mmx as integer
   mmy as integer
   mx as integer
   my as integer
endtype
global user as user_input
 
`________________
`|              |
`|   Main Loop  |
`________________
 
do
time=timer()/25
update_user()
color backdrop rgb(abs(sin(time+30))*255,abs(sin(time))*255,abs(cos(time))*255)
select user.click
   case 1
      po=pick object(user.mx,user.my,1,1)
      if po=1
         climb=select_limb()
         repeat
            update_user()
         until user.click=0
         repeat
            sync
            update_user()
            cls
            text 0,0,"  |  |"
            text 0,10,"__|__|__"
            text 0,20,"  |  |"
            text 0,30,"__|__|__"
            text 0,40,"  |  |"
            text 0,50,"  |  |"
            for x=1 to 3
               for y=1 to 3
                  if board(climb+1,x,y)=1
                     text (x-1)*23+5,(y-1)*23+5,"X"
                  endif
                  if board(climb+1,x,y)=2
                     text (x-1)*23+5,(y-1)*23+5,"O"
                  endif
               next y
            next x
            if user.mx<60 and user.my<60
               x=user.mx/20+1
               y=user.my/20+1
               if board(climb+1,x,y)=0
                  ink rgb(100,210,250),0
                  text (x-1)*23+5,(y-1)*23+5,"X"
                  ink -1,0
               else
                  x=-1
               endif
            else
               x=-1
            endif
         until user.click=1
         if x>-1
            board(climb+1,x,y)=1
            set current bitmap climb+1
            dec x
            dec y
            ink 0,0
            line x*16/3,y*16/3+1,(x+1)*16/3-1,(y+1)*16/3
            line x*16/3,(y+1)*16/3-1,(x+1)*16/3-1,y*16/3
            get image climb+1,0,0,16,16,1
            texture limb 1,climb,climb+1
            check_win(climb+1,1)
            cbd=artificial_intelligence()
            check_win(cbd,2)
            set current bitmap 0
         endif
      endif
   endcase
   case 2
      rotate object 1,object angle x(1)+user.mmy/4,object angle y(1)-user.mmx/4,0
   endcase
endselect
sync
loop
 
`___________________
`|                 |
`|    Functions    |
`___________________
 
function setup()
sync on
sync rate 60
randomize timer()
autocam off
move camera -150
vectors()
make_cube()
for i=1 to 6
   create_board(i)
next i
global dim board(6,3,3) as integer
endfunction
 
function vectors()
null=make vector3(1)
null=make vector3(2)
null=make vector3(3)
global dim dist(6) as float
endfunction
 
function make_cube()
make object plain 1,100,100
make mesh from object 1,1
offset limb 1,0,0,0,-50
add limb 1,1,1
offset limb 1,1,0,0,50
add limb 1,2,1
rotate limb 1,2,0,90,0
offset limb 1,2,50,0,0
add limb 1,3,1
rotate limb 1,3,0,-90,0
offset limb 1,3,-50,0,0
add limb 1,4,1
rotate limb 1,4,90,0,0
offset limb 1,4,0,-50,0
add limb 1,5,1
rotate limb 1,5,-90,0,0
offset limb 1,5,0,50,0
set object light 1,0
endfunction
 
function create_board(board)
create bitmap board,16,16
cls rgb(rnd(255),rnd(255),rnd(255))
box 1,16/3-1,15,16/3+1,rgb(rnd(200)+55,rnd(200)+55,rnd(200)+55),rgb(rnd(200)+55,rnd(200)+55,rnd(200)+55),rgb(rnd(200)+55,rnd(200)+55,rnd(200)+55),rgb(rnd(200)+55,rnd(200)+55,rnd(200)+55)
box 1,32/3-1,15,32/3+1,rgb(rnd(200)+55,rnd(200)+55,rnd(200)+55),rgb(rnd(200)+55,rnd(200)+55,rnd(200)+55),rgb(rnd(200)+55,rnd(200)+55,rnd(200)+55),rgb(rnd(200)+55,rnd(200)+55,rnd(200)+55)
box 16/3-1,1,16/3+1,15,rgb(rnd(200)+55,rnd(200)+55,rnd(200)+55),rgb(rnd(200)+55,rnd(200)+55,rnd(200)+55),rgb(rnd(200)+55,rnd(200)+55,rnd(200)+55),rgb(rnd(200)+55,rnd(200)+55,rnd(200)+55)
box 32/3-1,1,32/3+1,15,rgb(rnd(200)+55,rnd(200)+55,rnd(200)+55),rgb(rnd(200)+55,rnd(200)+55,rnd(200)+55),rgb(rnd(200)+55,rnd(200)+55,rnd(200)+55),rgb(rnd(200)+55,rnd(200)+55,rnd(200)+55)
get image board,0,0,16,16,1
texture limb 1,board-1,board
set current bitmap 0
endfunction
 
function update_user()
user.click=mouseclick()
user.mmx=mousemovex()
user.mmy=mousemovey()
user.mx=mousex()
user.my=mousey()
endfunction
 
function select_limb()
gx#=get pick vector x()+camera position x()
gy#=get pick vector y()+camera position y()
gz#=get pick vector z()+camera position z()
set vector3 1,gx#,gy#,gz#
for i=1 to 6
   set vector3 2,limb position x(1,i-1),limb position y(1,i-1),limb position z(1,i-1)
   subtract vector3 3,2,1
   dist(i)=length vector3(3)
next i
lowestdist=dist(1)
lowestdistpos=1
for i=2 to 6
   if dist(i)<lowestdist
      lowestdist=dist(i)
      lowestdistpos=i
   endif
next i
climb=lowestdistpos-1
endfunction climb
 
function check_win(bd,player)
total=0
for x=1 to 3
   for y=1 to 3
      if board(bd,x,y)=0
         inc total
      endif
   next y
next x
if total=0
   for x=1 to 3
      for y=1 to 3
         board(bd,x,y)=0
      next y
   next x
   create_board(bd)
   exitfunction
endif
if board(bd,1,1)=player and board(bd,2,1)=player and board(bd,3,1)=player or board(bd,1,2)=player and board(bd,2,2)=player and board(bd,3,2)=player or board(bd,1,3)=player and board(bd,2,3)=player and board(bd,3,3)=player or board(bd,1,1)=player and board(bd,1,2)=player and board(bd,1,3)=player or board(bd,2,1)=player and board(bd,2,2)=player and board(bd,2,3)=player or board(bd,3,1)=player and board(bd,3,2)=player and board(bd,3,3)=player or board(bd,1,1)=player and board(bd,2,2)=player and board(bd,3,3)=player or board(bd,1,3)=player and board(bd,2,2)=player and board(bd,3,1)=player
   select bd
      case 1
         x=0
         y=0
      endcase
      case 2
         x=0
         y=180
      endcase
      case 3
         x=0
         y=90
      endcase
      case 4
         x=0
         y=-90
      endcase
      case 5
         x=90
         y=0
      endcase
      case 6
         x=-90
         y=0
      endcase
   endselect
   set current bitmap 0
   for i=1 to 250
      sync
      rotate object 1,curveangle(x,object angle x(1),80),curveangle(y,object angle y(1),80),0
   next i
   wait 1000
   if player=1
      exit prompt "You Win","Yay..."
      end
   else
      exit prompt "You Lose","Boo..."
      end
   endif
endif
endfunction
 
function artificial_intelligence()
`Checks to see if it can win
for i=1 to 6
   if board(i,1,1)=0
      if board(i,1,2)=2 and board(i,1,3)=2 or board(i,2,2)=2 and board(i,3,3)=2 or board(i,2,1)=2 and board(i,3,1)=2
         ax=1
         ay=1
         exit
      endif
   endif
   if board(i,2,1)=0
      if board(i,1,1)=2 and board(i,3,1)=2 or board(i,2,2)=2 and board(i,2,3)=2
         ax=2
         ay=1
         exit
      endif
   endif
   if board(i,3,1)=0
      if board(i,1,1)=2 and board(i,2,1)=2 or board(i,3,2)=2 and board(i,3,3)=2 or board(i,2,2)=2 and board(i,1,3)=2
         ax=3
         ay=1
         exit
      endif
   endif
   if board(i,1,2)=0
      if board(i,1,1)=2 and board(i,1,3)=2 or board(i,2,2)=2 and board(i,3,2)=2
         ax=1
         ay=2
         exit
      endif
   endif
   if board(i,2,2)=0
      if board(i,1,1)=2 and board(i,3,3)=2 or board(i,2,1)=2 and board(i,2,3)=2 or board(i,3,1)=2 and board(i,1,3)=2 or board(i,1,2)=2 and board(i,3,2)=2
         ax=2
         ay=2
         exit
      endif
   endif
   if board(i,3,2)=0
      if board(i,3,1)=2 and board(i,3,3)=2 or board(i,1,2)=2 and board(i,2,2)=2
         ax=3
         ay=2
         exit
      endif
   endif
   if board(i,1,3)=0
      if board(i,1,1)=2 and board(i,1,2)=2 or board(i,2,3)=2 and board(i,3,3)=2 or board(i,2,2)=2 and board(i,3,1)=2
         ax=1
         ay=3
         exit
      endif
   endif
   if board(i,2,3)=0
      if board(i,1,3)=2 and board(i,3,3)=2 or board(i,2,2)=2 and board(i,2,1)=2
         ax=2
         ay=3
         exit
      endif
   endif
   if board(i,3,3)=0
      if board(i,1,1)=2 and board(i,2,2)=2 or board(i,3,1)=2 and board(i,3,2)=2 or board(i,1,3)=2 and board(i,2,3)=2
         ax=3
         ay=3
         exit
      endif
   endif
next i
if ax=0
   `Checks to see if player can win
   for i=1 to 6
      if board(i,1,1)=0
         if board(i,1,2)=1 and board(i,1,3)=1 or board(i,2,2)=1 and board(i,3,3)=1 or board(i,2,1)=1 and board(i,3,1)=1
            ax=1
            ay=1
            exit
         endif
      endif
      if board(i,2,1)=0
         if board(i,1,1)=1 and board(i,3,1)=1 or board(i,2,2)=1 and board(i,2,3)=1
            ax=2
            ay=1
            exit
         endif
      endif
      if board(i,3,1)=0
         if board(i,1,1)=1 and board(i,2,1)=1 or board(i,3,2)=1 and board(i,3,3)=1 or board(i,2,2)=1 and board(i,1,3)=1
            ax=3
            ay=1
            exit
         endif
      endif
      if board(i,1,2)=0
         if board(i,1,1)=1 and board(i,1,3)=1 or board(i,2,2)=1 and board(i,3,2)=1
            ax=1
            ay=2
            exit
         endif
      endif
      if board(i,2,2)=0
         if board(i,1,1)=1 and board(i,3,3)=1 or board(i,2,1)=1 and board(i,2,3)=1 or board(i,3,1)=1 and board(i,1,3)=1 or board(i,1,2)=1 and board(i,3,2)=1
            ax=2
            ay=2
            exit
         endif
      endif
      if board(i,3,2)=0
         if board(i,3,1)=1 and board(i,3,3)=1 or board(i,1,2)=1 and board(i,2,2)=1
            ax=3
            ay=2
            exit
         endif
      endif
      if board(i,1,3)=0
         if board(i,1,1)=1 and board(i,1,2)=1 or board(i,2,3)=1 and board(i,3,3)=1 or board(i,2,2)=1 and board(i,3,1)=1
            ax=1
            ay=3
            exit
         endif
      endif
      if board(i,2,3)=0
         if board(i,1,3)=1 and board(i,3,3)=1 or board(i,2,2)=1 and board(i,2,1)=1
            ax=2
            ay=3
            exit
         endif
      endif
      if board(i,3,3)=0
         if board(i,1,1)=1 and board(i,2,2)=1 or board(i,3,1)=1 and board(i,3,2)=1 or board(i,1,3)=1 and board(i,2,3)=1
            ax=3
            ay=3
            exit
         endif
      endif
   next i
endif
if ax=0
   repeat
      i=rnd(5)+1
      ax=rnd(2)+1
      ay=rnd(2)+1
   until board(i,ax,ay)=0
endif
board(i,ax,ay)=2
set current bitmap i
circle (ax-0.5)*16/3,(ay-0.5)*16/3,1
ink -1,0
get image i,0,0,16,16,1
texture limb 1,i-1,i
endfunction i