REM Project: TicTacToe
REM Created: 11/13/2006 7:51:11 PM
REM
REM ***** Main Source File *****
REM
 
set display mode 640,480,32
Hide Mouse
Sync On
 
REM Horizontal and Vertical Dividers
Vertical = 559/3
Horizontal = 439/3
 
REM Mark 1 = 'X' and 2 = 'O'
Mark = 1
 
REM ttt values are 0 by default
dim ttt (3,3)
 
grow = 0 ; c = 0
Randomize (3)
 
REM //////// Initial Selection Process ///////////
   while ReturnKey() = 0
      box 60,60,screen width() - 60 ,screen height() - 60
      ink RGB(0,0,75),1
      box 65,65,screen width() - 65, screen height() - 65
      ink RGB(255,255,255),1
      Set Text Size 50
      set cursor 70,70
      ink RGB(200,0,0),1
      print "     Tic Tac Toe"
      ink RGB(255,255,255),1
      set cursor 80,150
      Set Text Size 35
      print "      Select Game Option"
      Set Text Size 50
      set cursor 70,230
      print "  Play Versus Human"
      set cursor 60,300
      Print " Play Versus Computer"
      if c => 0 and grow = 0
         c = c + 10
         if c = 150 Then grow = 1
      endif
      if c <= 200 and grow = 1
         c = c - 10
         if c = 0 Then grow = 0
      endif
      if UpKey() = 1 Then Selection = 0
      if DownKey() = 1 Then Selection = 2
      if Selection = 0
         ink RGB(255,255,100),1
         box 65,230,screen width() - 65,280
         ink RGB(c,c/2,50),1
         box 67,232,screen width() - 67,278
      endif
      if Selection = 2
         ink RGB(255,255,100),1
         box 65,370 - 73,screen width() - 65,426 - 73
         ink RGB(c,c/2,50),1
         box 67,372 - 73,screen width() - 67,424 - 73
      endif
      ink RGB(255,255,255),1
 
      set cursor 70,230
      print "  Play Versus Human"
      set cursor 60,300
      Print " Play Versus Computer"
      if ReturnKey() = 1
         if Selection = 0
            for i = 1 to 3
               for d = 1 to 3
                  ttt(i,d) = 0
               next d
            next i
            Winner = 0
         endif
      endif
      sync
      cls
   EndWhile
 
REM ############## MAIN LOOP ######################
 
do
   REM Draws the Tic Tac Toe Board on the screen
   ink RGB(255,255,255),1
   box 30,30,screen width() - 30,screen height() - 30
   ink RGB(0,0,0),1
   box 40,40,screen width() - 40,screen height() - 40
   ink RGB(255,255,255),1
   box 40,Horizontal*2,screen width() - 40, Horizontal*2 + 10
   box 40,Horizontal,screen width() - 40, Horizontal + 10
   box Vertical + 40,40,Vertical + 50,screen height() - 40
   box Vertical*2 + 40,40,Vertical*2 + 50,screen height() - 40
 
 
 
   REM Draw Mouse Position for the 'O' Mark
   if Mark = 2 and Winner = 0 and selection = 0
      for i = 35 to 45
         Circle MouseX(), MouseY(),i
      next i
   endif
 
   REM Artifical Intelligence ############### ARTIFICIAL INTELLIGENCE
   if Mark = 2 and Winner = 0 and selection = 2
      if ttt(1,1) = 0 and ttt(1,2) = 0 and ttt(1,3) = 0
         if ttt(2,1) = 0 and ttt(2,2) = 0 and ttt(2,3) = 0
            if ttt(3,1) = 0 and ttt(3,2) = 0 and ttt(3,3) = 0
               x = RND(3) ; y = RND(3)
               if x = 0 Then x = x + 1
               if y = 0 Then y = y + 1
               if Mark = 2 and ttt(x,y) = 0
               ttt(x,y) = 2 ; Mark = 1
               endif
            endif
         endif
      endif
      if ttt(1,1) = 1 and ttt(1,3) = 1 and Mark = 2
         ttt(1,2) = 2 ; Mark = 1
      endif
      if ttt(2,1) = 1 and ttt(2,3) = 1 and Mark = 2
         ttt(2,2) = 2 ; Mark = 1
      endif
      if ttt(3,1) = 1 and ttt(3,3) = 1 and Mark = 2
         ttt(3,2) = 2 ; Mark = 1
      endif
      if ttt(1,1) = 1 and ttt(3,1) = 1 and Mark = 2
         ttt(2,1) = 2 ; Mark = 1
      endif
      if ttt(2,1) = 1 and ttt(3,2) = 1 and Mark = 2
         ttt(2,2) = 2 ; Mark = 1
      endif
      if ttt(3,1) = 1 and ttt(3,3) = 1 and Mark = 2
         ttt(3,2) = 2 ; Mark = 1
      endif
      if ttt(1,1) = 1 and ttt(3,3) = 1 and Mark = 2
         ttt(2,2) = 2 ; Mark = 1
      endif
      if ttt(3,1) = 1 and ttt(1,3) = 1 and Mark = 2
         ttt(2,2) = 2 ; Mark = 1
      endif
      for i = 1 to 3
         for b = 1 to 3
            if Mark = 2 and ttt(i,b) = 0
               ttt(i,b) = 2 ; Mark = 1
            endif
         next b
      next i
      while Mark = 2
         x = RND(3) ; y = RND(3)
         if x = 0 Then x = x + 1
         if y = 0 Then y = y + 1
         if ttt(x,y) = 0 and Mark = 2
            ttt(x,y) = 2 ; Mark = 1
         endif
      endwhile
   endif
 
   REM Draw Mouse Position for the 'X' mark
   if Mark = 1 and Winner = 0
      for i = 1 to 10
         line MouseX() - 40 - i, MouseY() - 40,MouseX() + 40 - i, MouseY() + 40
         line MouseX() - 40 - i, MouseY() + 40,MouseX() + 40 - i, MouseY() - 40
      next i
   endif
 
   REM Check where the mouse was clicked and record marking data
   if mouseclick() = 1
      if mouseX() > 40 and mouseY() > 40 and MouseX() < 225 and MouseY() < 145 and ttt(1,1) = 0
         ttt(1,1) = Mark
         REM To make sure the players take turns
         if ttt(1,1) = 1 Then Mark = 2
         if ttt(1,1) = 2 Then Mark = 1
      endif
      if mouseX() > 236 and mouseY() > 40 and MouseX() < 412 and MouseY() < 145 and ttt(1,2) = 0
         ttt(1,2) = Mark
         if ttt(1,2) = 1 Then Mark = 2
         if ttt(1,2) = 2 Then Mark = 1
      endif
      if mouseX() > 424 and mouseY() > 41 and MouseX() < 599 and MouseY() < 145 and ttt(1,3) = 0
         ttt(1,3) = Mark
         if ttt(1,3) = 1 Then Mark = 2
         if ttt(1,3) = 2 Then Mark = 1
      endif
      if mouseX() > 42 and mouseY() > 158 and MouseX() < 224 and MouseY() < 290 and ttt(2,1) = 0
         ttt(2,1) = Mark
         if ttt(2,1) = 1 Then Mark = 2
         if ttt(2,1) = 2 Then Mark = 1
      endif
      if mouseX() > 236 and mouseY() > 156 and mouseX() < 409 and mouseY() < 290 and ttt(2,2) = 0
         ttt(2,2) = Mark
         if ttt(2,2) = 1 Then Mark = 2
         if ttt(2,2) = 2 Then Mark = 1
      endif
      if mouseX() > 424 and mouseY() > 157 and mouseX() < 598 and mouseY() < 289 and ttt(2,3) = 0
         ttt(2,3) = Mark
         if ttt(2,3) = 1 Then Mark = 2
         if ttt(2,3) = 2 Then Mark = 1
      endif
      if mouseX() > 41 and mouseY() > 304 and mouseX() < 223 and mouseY() < 438 and ttt(3,1) = 0
         ttt(3,1) = Mark
         if ttt(3,1) = 1 Then Mark = 2
         if ttt(3,1) = 2 Then Mark = 1
      endif
      if mouseX() > 238 and mouseY() > 303 and mouseX() < 411 and mouseY() < 438 and ttt(3,2) = 0
         ttt(3,2) = Mark
         if ttt(3,2) = 1 Then Mark = 2
         if ttt(3,2) = 2 Then Mark = 1
      endif
      if mouseX() > 422 and mouseY() > 303 and mouseX() < 598 and mouseY() < 438 and ttt(3,3) = 0
         ttt(3,3) = Mark
         if ttt(3,3) = 1 Then Mark = 2
         if ttt(3,3) = 2 Then Mark = 1
      endif
   endif
 
   REM Draw all recorded marks
      for b = 1 to 3
         for a = 1 to 3
            REM Will Check Cell Blocks to see if an 'X' was marked
            if ttt(b,a) = 1
               for i = 1 to 10
                  if b = 1 and a = 1
                     line 130 - 40 - i, 93 - 40,130 + 40 - i, 93 + 40
                     line 130 - 40 - i, 93 + 40,130 + 40 - i, 93 - 40
                  endif
                  if b = 1 and a = 2
                     line 328 - 40 - i, 93 - 40,328 + 40 - i, 93 + 40
                     line 328 - 40 - i, 93 + 40,328 + 40 - i, 93 - 40
                  endif
                  if b = 1 and a = 3
                     line 517 - 40 - i, 93 - 40,517 + 40 - i, 93 + 40
                     line 517 - 40 - i, 93 + 40,517 + 40 - i, 93 - 40
                  endif
                  if b = 2 and a = 1
                     line 130 - 40 - i, 223 - 40,130 + 40 - i, 223 + 40
                     line 130 - 40 - i, 223 + 40,130 + 40 - i, 223 - 40
                  endif
                  if b = 2 and a = 2
                     line 328 - 40 - i, 223 - 40,328 + 40 - i, 223 + 40
                     line 328 - 40 - i, 223 + 40,328 + 40 - i, 223 - 40
                  endif
                  if b = 2 and a = 3
                     line 517 - 40 - i, 223 - 40,517 + 40 - i, 223 + 40
                     line 517 - 40 - i, 223 + 40,517 + 40 - i, 223 - 40
                  endif
                  if b = 3 and a = 1
                     line 130 - 40 - i, 370 - 40,130 + 40 - i, 370 + 40
                     line 130 - 40 - i, 370 + 40,130 + 40 - i, 370 - 40
                  endif
                  if b = 3 and a = 2
                     line 328 - 40 - i, 370 - 40,328 + 40 - i, 370 + 40
                     line 328 - 40 - i, 370 + 40,328 + 40 - i, 370 - 40
                  endif
                  if b = 3 and a = 3
                     line 517 - 40 - i, 370 - 40,517 + 40 - i, 370 + 40
                     line 517 - 40 - i, 370 + 40,517 + 40 - i, 370 - 40
                  endif
 
               next i
            endif
         next a
      next b
 
      for b = 1 to 3
         for a = 1 to 3
            REM Will Check Cell Blocks to see if an 'O' was marked
            if ttt(b,a) = 2
               for i = 35 to 45
                  if b = 1 and a = 1
                     Circle 130,93,i
                  endif
                  if b = 1 and a = 2
                     Circle 328,93,i
                  endif
                  if b = 1 and a = 3
                     Circle 517,93,i
                  endif
                  if b = 2 and a = 1
                     Circle 130,223,i
                  endif
                  if b = 2 and a = 2
                     Circle 328,223,i
                  endif
                  if b = 2 and a = 3
                     Circle 517,223,i
                  endif
                  if b = 3 and a = 1
                     Circle 130,370,i
                  endif
                  if b = 3 and a = 2
                     Circle 328,370,i
                  endif
                  if b = 3 and a = 3
                     Circle 517,370,i
                  endif
 
               next i
            endif
         next a
      next b
   REM Check To see if Player 1 Won : These are all possible combinations
   if ttt(1,1) = 1 and ttt(1,2) = 1 and ttt(1,3) = 1 Then Winner = 1
   if ttt(2,1) = 1 and ttt(2,2) = 1 and ttt(2,3) = 1 Then Winner = 1
   if ttt(3,1) = 1 and ttt(3,2) = 1 and ttt(3,3) = 1 Then Winner = 1
   if ttt(1,1) = 1 and ttt(2,1) = 1 and ttt(3,1) = 1 Then Winner = 1
   if ttt(1,2) = 1 and ttt(2,2) = 1 and ttt(3,2) = 1 Then Winner = 1
   if ttt(1,3) = 1 and ttt(2,3) = 1 and ttt(3,3) = 1 Then Winner = 1
   if ttt(1,1) = 1 and ttt(2,2) = 1 and ttt(3,3) = 1 Then Winner = 1
   if ttt(1,3) = 1 and ttt(2,2) = 1 and ttt(3,1) = 1 Then Winner = 1
 
   Rem Check to see if Player 2 Won : These are all possible combinations
   if ttt(1,1) = 2 and ttt(1,2) = 2 and ttt(1,3) = 2 Then Winner = 2
   if ttt(2,1) = 2 and ttt(2,2) = 2 and ttt(2,3) = 2 Then Winner = 2
   if ttt(3,1) = 2 and ttt(3,2) = 2 and ttt(3,3) = 2 Then Winner = 2
   if ttt(1,1) = 2 and ttt(2,1) = 2 and ttt(3,1) = 2 Then Winner = 2
   if ttt(1,2) = 2 and ttt(2,2) = 2 and ttt(3,2) = 2 Then Winner = 2
   if ttt(1,3) = 2 and ttt(2,3) = 2 and ttt(3,3) = 2 Then Winner = 2
   if ttt(1,1) = 2 and ttt(2,2) = 2 and ttt(3,3) = 2 Then Winner = 2
   if ttt(1,3) = 2 and ttt(2,2) = 2 and ttt(3,1) = 2 Then Winner = 2
 
   REM Display who the winner is
   if ttt(1,1) > 0 and ttt(1,2) > 0 and ttt(1,3) > 0
      if ttt(2,1) > 0 and ttt(2,2) > 0 and ttt(2,3) > 0
         if ttt(3,1) > 0 and ttt(3,2) > 0 and ttt(3,3) > 0
         Tie = 1
      box 60,60,screen width() - 60 ,screen height() - 60
      ink RGB(0,0,75),1
      box 65,65,screen width() - 65, screen height() - 65
      ink RGB(255,255,255),1
      Set Text Size 50
      set cursor 70,70
      ink RGB(200,0,0),1
      print "      Tie Game"
      ink RGB(255,255,255),1
      set cursor 80,150
      Set Text Size 35
      print "      Select Game Option"
      Set Text Size 50
      set cursor 70,230
      print "  Play Versus Human"
      set cursor 60,300
      Print " Play Versus Computer"
         endif
      endif
   endif
   if Winner = 1
      box 60,60,screen width() - 60 ,screen height() - 60
      ink RGB(0,0,75),1
      box 65,65,screen width() - 65, screen height() - 65
      ink RGB(255,255,255),1
      Set Text Size 50
      set cursor 70,70
      ink RGB(200,0,0),1
      print "   PLAYER ONE WINS"
      ink RGB(255,255,255),1
      set cursor 80,150
      Set Text Size 35
      print "      Select Game Option"
      Set Text Size 50
      set cursor 70,230
      print "  Play Versus Human"
      set cursor 60,300
      Print " Play Versus Computer"
   endif
   if Winner = 2
      box 60,60,screen width() - 60 ,screen height() - 60
      ink RGB(0,0,75),1
      box 65,65,screen width() - 65, screen height() - 65
      ink RGB(255,255,255),1
      Set Text Size 50
      set cursor 70,70
      ink RGB(200,0,0),1
      print "   PLAYER TWO WINS"
      ink RGB(255,255,255),1
      set cursor 80,150
      Set Text Size 35
      print "      Select Game Option"
      Set Text Size 50
   endif
   REM Display Selection If there has been a winner
   if Winner = 1 or Winner = 2 or Tie = 1
      if c => 0 and grow = 0
         c = c + 10
         if c = 150 Then grow = 1
      endif
      if c <= 200 and grow = 1
         c = c - 10
         if c = 0 Then grow = 0
      endif
      if UpKey() = 1 Then Selection = 0
      if DownKey() = 1 Then Selection = 2
      if Selection = 0
         ink RGB(255,255,100),1
         box 65,230,screen width() - 65,280
         ink RGB(c,c/2,50),1
         box 67,232,screen width() - 67,278
      endif
      if Selection = 2
         ink RGB(255,255,100),1
         box 65,370 - 73,screen width() - 65,426 - 73
         ink RGB(c,c/2,50),1
         box 67,372 - 73,screen width() - 67,424 - 73
      endif
      ink RGB(255,255,255),1
 
      set cursor 70,230
      print "  Play Versus Human"
      set cursor 60,300
      Print " Play Versus Computer"
      if ReturnKey() = 1
         if Selection = 0
            for i = 1 to 3
               for d = 1 to 3
                  ttt(i,d) = 0
               next d
            next i
            Winner = 0 ; Tie = 0
         endif
         if Selection = 2
            for i = 1 to 3
               for d = 1 to 3
                  ttt(i,d) = 0
               next d
            next i
            Winner = 0
            Tie = 0
         endif
      endif
   endif
   Sync
   cls
loop