Challenge: 
Winner?: 
No
Code Snippet: 
REM Project: tictactoe
REM Created: 11/5/2006 11:51:49 AM
REM
REM ***** Main Source File *****
REM

sync on
sync rate 60
disable escapekey

randomize timer()

set text font "arial"

set text size 20

global dim squares(10,10)
squares(0,0)=5
global first
global mids
global sw
global sh
global comp
global back
global center

comp=5

mids=screen width()/2

sw=screen width()

sh=screen height()


do
   cls
   repeat

      ink rgb(91,246,181),rgb(91,246,181)

      box 100,100,screen width()-100,screen height()-100

      ink rgb(25,163,255),rgb(25,163,255)

      box 100,100,screen width()-100,110

      box 100,100,110,screen height()-100

      box screen width()-110,110,screen width()-100,screen height()-100

      box 100,screen height()-110,screen width()-100,screen height()-100

      center text mids,150,"Welcome to Tic Tac Toe or naughts and crosses"

      if 1=button("Play",200,0,0,3)
         play=1
      endif

      if 1=button("Options",250,0,0,3)
         gosub options
      endif

      if 1=button("EXIT",300,0,0,3)
         end
      endif

      sync

   until play=1

   play=0

   Cls

   repeat
      again=0
      move=0
      first=0
      sync

      ink rgb(255,255,255),rgb(255,255,255)

      box 0,0,screen width(),screen height()

      ink 0,rgb(255,255,255)

      Line screen width()/3,100,screen width()/3,screen height()-100
      Line (screen width()/3)*2,100,(screen width()/3)*2,screen height()-100
      Line 100,screen height()/3,screen width()-100,screen height()/3
      Line 100,(screen height()/3)*2,screen width()-100,(screen height()/3)*2

      sync



     repeat
        place(1)

        if back=1 then exit

        move=move+1

        if 1=win(1)
           sync
           wait 1000
           cls rgb(255,255,255)
           sync
           text 0,0,"Player 1 wins"
           sync
           suspend for key
           exit
        endif

        if move=9
           cls rgb(255,255,255)
           sync
           text 0,0,"Tie"
           sync
           suspend for key
           exit
        endif

        sync

        if comp=5 then compplaceexpt()
        if comp=4 then compplacehard()
        if comp=3 then compplacemed()
        if comp=2 then compplaceeasy()
        if comp=1 then place(2)

        if back=1 then exit

        move=move+1

        if 1=win(2)
           sync
           wait 1000
           cls rgb(255,255,255)
           sync
           text 0,0,"Player 2 wins"
           sync
           suspend for key
           exit
        endif



        sync

     until again=1 or back=1

     undim squares(10,10)
     global dim squares(10,10)
     squares(0,0)=5


   until back=1

   back=0
loop

`The options menu

options:

ink rgb(91,246,181),rgb(91,246,181)

box 100,100,screen width()-100,screen height()-100

ink rgb(25,163,255),rgb(25,163,255)

box 100,100,screen width()-100,110

box 100,100,110,screen height()-100

box screen width()-110,110,screen width()-100,screen height()-100

box 100,screen height()-110,screen width()-100,screen height()-100

center text mids,150,"Options"

repeat

   if 1=button("Easy",200,0,0,3)
      comp=2
   endif

   if 1=button("Medium",250,0,0,3)
      comp=3
   endif

   if 1=button("Hard",300,0,0,3)
      comp=4
   endif

   if 1=button("Expert",350,0,0,3)
      comp=5
   endif

   if 1=button("Player",400,0,0,3)
      comp=1
   endif

   if 1=button("BACK",450,0,0,3)
      back1=1
   endif

   sync

until back1=1

back1=0

wait 200
cls
sync

return


`Makes buttons and checks if they are being pressed

function button(but$,y,in,bac,size)

   if size=2
      set text size 30
   endif

   if size=3
      set text size 40
   endif

   if in=0
      in=rgb(25,163,255)
   endif

   if bac=0
      bac=rgb(91,246,181)
   endif

   length=text width(but$)/2

   if mids-length<mousex() and mids+length>mousex() and y<mousey() and y+text height(but$)>mousey()
      bac=rgb(91,196,181)
      if mouseclick()=1
         selected=1
      endif
   endif

   ink in,bac
   center text mids,y,but$

   set text size 20

endfunction selected

`Alows the player to pick their own space

function place(play)

repeat
   x=mousex()
   y=mousey()

   click=mouseclick()

   if click=1
      if x<sw/3
         xpos#=.166666
         if y<sh/3
            plac=1
            ypos#=.166666
         endif
         if y>sh/3 and y<2*(sh/3)
            plac=4
            ypos#=.5
         endif
         if y>2*(sh/3)
            plac=7
            ypos#=.83333
         endif
      endif

      if x>sw/3 and x<(sw/3)*2
         xpos#=.5
         if y<sh/3
            plac=2
            ypos#=.166666
         endif
         if y>sh/3 and y<2*(sh/3)
            plac=5
            ypos#=.5
         endif
         if y>2*(sh/3)
            plac=8
            ypos#=.83333
         endif
      endif

      if x>(sw/3)*2
         xpos#=.8333333
         if y<sh/3
            plac=3
            ypos#=.166666
         endif
         if y>sh/3 and y<2*(sh/3)
            plac=6
            ypos#=.5
         endif
         if y>2*(sh/3)
            plac=9
            ypos#=.83333
         endif
      endif

   endif

   row=(plac-0.5)/3+1
   column=((plac-1) mod 3)+1

   if squares(row,column)=0 and play=1

      squares(row,column)=1
      nex=1
      circle screen width()*xpos#,screen height()*ypos#,50
   ENDIF

   if squares(row,column)=0 and play=2

      squares(row,column)=2
      nex=1
      box screen width()*xpos#-20,screen height()*ypos#-20,screen width()*xpos#+20,screen height()*ypos#+20
   ENDIF

   if escapekey()=1
      back=1
   endif

until nex=1 or back=1

endfunction nex

`Easy Chosses only random places

function compplaceeasy()

repeat

   plac=rnd(8)+1

   row=(plac-0.5)/3+1
   column=((plac-1) mod 3)+1

until squares(row,column)=0

squares(row,column)=2

xpos#=column*2-1
xpos#=xpos#/6

ypos#=row*2-1
ypos#=ypos#/6

box screen width()*xpos#-20,screen height()*ypos#-20,screen width()*xpos#+20,screen height()*ypos#+20


endfunction nex

`Pretty easy to beat only checks for a win

function compplacemed()

skip=0

`checks for a win

plac=check(2)

if plac>0

   row=(plac-0.5)/3+1
   column=((plac-1) mod 3)+1
   skip=1

endif

`Chosses a random place

if skip=0
   repeat

      plac=rnd(8)+1

      row=(plac-0.5)/3+1
      column=((plac-1) mod 3)+1

   until squares(row,column)=0
endif

squares(row,column)=2

xpos#=column*2-1
xpos#=xpos#/6

ypos#=row*2-1
ypos#=ypos#/6

box screen width()*xpos#-20,screen height()*ypos#-20,screen width()*xpos#+20,screen height()*ypos#+20


endfunction nex

`Difficult however beatable

function compplacehard()

skip=0


`checks to see if it can make a win

plac=check(1)

if plac>0

   row=(plac-0.5)/3+1
   column=((plac-1) mod 3)+1
   skip=1

endif

`Checks to see if it can make a block

plac=check(2)

if plac>0

   row=(plac-0.5)/3+1
   column=((plac-1) mod 3)+1
   skip=1

endif

`Choses a random spot if there is not a good spot

if skip=0
   repeat

      plac=rnd(8)+1

      row=(plac-0.5)/3+1
      column=((plac-1) mod 3)+1

   until squares(row,column)=0
endif

squares(row,column)=2

xpos#=column*2-1
xpos#=xpos#/6

ypos#=row*2-1
ypos#=ypos#/6

box screen width()*xpos#-20,screen height()*ypos#-20,screen width()*xpos#+20,screen height()*ypos#+20


endfunction nex

`Unbeatable difficulty.... At least I think

function compplaceexpt()

`If it is the first move it does this

if first=0

   plac=firstcompmove()

   if plac>0

      row=(plac-0.5)/3+1
      column=((plac-1) mod 3)+1
      skip=1

   endif

else
   skip=0


   `checks to see if it can make a block

   plac=check(1)

   if plac>0

      row=(plac-0.5)/3+1
      column=((plac-1) mod 3)+1
      skip=1

   endif

   `checks to see if it can make a win

   plac=check(2)

   if plac>0

      row=(plac-0.5)/3+1
      column=((plac-1) mod 3)+1
      skip=1

   endif

   `checks to see if it can make an educated move (if the space is avalible)
   `if it went center first then it goes edges
   `if it went corner first then it goes corners

   if skip=0
      plac=conner()

      if plac>0

         row=(plac-0.5)/3+1
         column=((plac-1) mod 3)+1
         skip=1

      endif
   endif

   `If there isnt a better move then it picks a random spot

   if skip=0
      repeat

         plac=rnd(8)+1

         row=(plac-0.5)/3+1
         column=((plac-1) mod 3)+1

      until squares(row,column)=0
   endif
endif

squares(row,column)=2

xpos#=column*2-1
xpos#=xpos#/6

ypos#=row*2-1
ypos#=ypos#/6

box screen width()*xpos#-20,screen height()*ypos#-20,screen width()*xpos#+20,screen height()*ypos#+20


endfunction nex

`Determines if anyone has won

function win(player)

   for x=1 to 3
      for y=1 to 3
         if squares(x,y)=player then row=row+1
      next y
      if row=3 then yes=1
      if row<3 then row=0
   next x

   for y=1 to 3
      for x=1 to 3
         if squares(x,y)=player then col=col+1
      next x
      if col=3 then yes=1
      if col<3 then col=0
   next y

   for x=1 to 3
      if squares(x,x)=player then dig=dig+1
   next x

   for x=1 to 3
      if squares(x,4-x)=player then dig2=dig2+1
   next x

   if dig=3 or dig2=3 then yes=1

endfunction yes


`Check- decides if there is a block (Wi=1) or a win (wi=2)

function check(wi)

   for i=1 to 3
      for j=1 to 3

         if squares(i,j)=wi
            col=col+1
         endif
         if squares(i,j)=0
            notcheck=j
         endif

      next j

      if col=2 and notcheck>0
         row=i
         column=notcheck
         skip=1
         col=0
         notcheck=0
      else
         col=0
         notcheck=0
      endif

   next i

   for j=1 to 3
      for i=1 to 3

         if squares(i,j)=wi
            ro=ro+1
         endif
         if squares(i,j)=0
            notcheck=i
         endif

      next i

      if ro=2 and notcheck>0
         column=j
         row=notcheck
         skip=1
         ro=0
         notcheck=0
      else
         ro=0
         notcheck=0
      endif

   next j



   for j=1 to 3

         if squares(j,j)=wi
            ro=ro+1
         endif
         if squares(j,j)=0
            notcheck=j
         endif

   next j

   if ro=2 and notcheck>0
      column=notcheck
      row=notcheck
      skip=1
      ro=0
      notcheck=0
   else
      ro=0
      notcheck=0
   endif

   for j=1 to 3

         if squares(j,4-j)=wi
            ro=ro+1
         endif
         if squares(j,4-j)=0
            notcheck=j
         endif

   next j

   if ro=2 and notcheck>0
      column=4-notcheck
      row=notcheck
      skip=1
      ro=0
      notcheck=0
   else
      ro=0
      notcheck=0
   endif

   plac=column+3*(row-1)

endfunction plac

`Makes the first move on expert, if your first move was the center it goes on a corner
`Otherwise it goes in the center

function firstcompmove()

   first=1
   if squares(2,2)=1
      center=0
      choice=rnd(3)
      if choice=1 then plac=1
      if choice=2 then plac=3
      if choice=3 then plac=7
      if choice=4 then plac=9
   else
      center=1
      plac=5
   endif

endfunction plac

`Makes an educated move on expert

function conner()
   if center=0
      if squares(1,1)=0 then plac=1
      if squares(1,3)=0 then plac=3
      if squares(3,1)=0 then plac=7
      if squares(3,3)=0 then plac=9
   else
      if squares(1,2)=0 then plac=2
      if squares(2,1)=0 then plac=4
      if squares(2,3)=0 then plac=6
      if squares(3,2)=0 then plac=8
   endif


endfunction plac