#DefineSubroutine DoComputerInitialMove()
  if compfirst==1 ; computer
     m=5   ;hard
     if howhard==2 then m=3 ; medium
     if howhard==1 then m=1+Random(8) ; easy
     DialogControlSet(MyDialogHandle,"%m%",4,"O")
     DialogControlState(MyDialogHandle,"%m%"+10,3,1)
     t[m] = -m
     CurrentMoveCount=CurrentMoveCount+1
  endif
#EndSubroutine

#DefineSubroutine ResetGame()
ArrInitialize(T,0)
CurrentMoveCount=0
for xxxx=1 to 9
   DialogControlSet(MyDialogHandle,xxxx,4,"")
   DialogControlState(MyDialogHandle,xxxx+10,4,1)
   DialogControlSet(MyDialogHandle,xxxx,11,"DEFAULT")
next
DoComputerInitialMove()
return
#EndSubroutine

#DefineFunction AnyWins(MDH,T,sum)
     sumflag=0
     if (T[8]+T[3]+T[4]) == sum then sumflag=834
     if (T[1]+T[5]+T[9]) == sum then sumflag=159
     if (T[6]+T[7]+T[2]) == sum then sumflag=672
     if (T[8]+T[1]+T[6]) == sum then sumflag=816
     if (T[3]+T[5]+T[7]) == sum then sumflag=357
     if (T[4]+T[9]+T[2]) == sum then sumflag=492
     if (T[8]+T[5]+T[2]) == sum then sumflag=852
     if (T[6]+T[5]+T[4]) == sum then sumflag=654
     if sumflag!=0
        if sum<0 then color="255|0|0"
                 else color="0|255|0"
        DialogControlSet(MDH,sumflag/100,11,color)
        DialogControlSet(MDH,(sumflag mod 100)/10,11,color)
        DialogControlSet(MDH,sumflag mod 10,11,color)
     endif

     return (sumflag)
#EndFunction

#DEFINEFUNCTION TestWin(T,type,a,b,c)
 if type==1
    if ((t[a]>=0)+(t[b]>=0)+(t[c]>=0))==3
       if ((t[a]>0)+(t[b]>0)+(t[c]>0))==2
          z= 15- (t[a]+t[b]+t[c])
          return (z)
       endif
    endif
 else
    if ((t[a]<=0)+(t[b]<=0)+(t[c]<=0))==3
       if ((t[a]<0)+(t[b]<0)+(t[c]<0))==2
          z= 15+ (t[a]+t[b]+t[c])
          return (z)
       endif
    endif
 endif
 return 0
#EndFunction

#DefineFunction BlockLose(T)
                  m=TestWin(T,1,8,3,4)
     if m==0 then m=TestWin(T,1,1,5,9)
     if m==0 then m=TestWin(T,1,6,7,2)
     if m==0 then m=TestWin(T,1,8,1,6)
     if m==0 then m=TestWin(T,1,3,5,7)
     if m==0 then m=TestWin(T,1,4,9,2)
     if m==0 then m=TestWin(T,1,8,5,2)
     if m==0 then m=TestWin(T,1,6,5,4)
     return(m)
#EndFunction

#DefineFunction FindWin(T)
;DebugTrace(@ON,"debug.txt")

                  m=TestWin(T,0,8,3,4)
     if m==0 then m=TestWin(T,0,1,5,9)
     if m==0 then m=TestWin(T,0,6,7,2)
     if m==0 then m=TestWin(T,0,8,1,6)
     if m==0 then m=TestWin(T,0,3,5,7)
     if m==0 then m=TestWin(T,0,4,9,2)
     if m==0 then m=TestWin(T,0,8,5,2)
     if m==0 then m=TestWin(T,0,6,5,4)
     ;if m<0 then m=-m
     return(m)
#EndFunction

  

#DEFINESUBROUTINE Ticproc(MyDialogHandle,MyDialogMessage,MyDialogControlID,param4,param5)
;DebugTrace(@ON,"debug.txt")
   InitDialog=0 
   DC_ButtonPressed= 2 ; some constant we define laters
   DC_RadioPressed=3
   DC_SetTitle = 3
   switch MyDialogMessage

          case InitDialog   ; Init_Dialog  (case number 0 subject to change on implementation)
               DialogProcOptions(MyDialogHandle,DC_ButtonPressed,1)   ; enable buttonclicked messages
               DialogProcOptions(MyDialogHandle,DC_RadioPressed,1)   ; enable buttonclicked messages
               DoComputerInitialMove()
               return (-1)

          case DC_RadioPressed
               switch MyDialogControlID
                  case whocomputer
                       compfirst=1
                       ResetGame()
                       break
                  case whoplayer
                       compfirst=2
                       ResetGame()
                       break
                  case howhard_easy
                  case howhard_medium
                  case howhard_hard
                      howhard=MyDialogControlId-howhard_easy+1
                       ResetGame()
                      break
               endswitch

               return (-1)
   
          case DC_ButtonPressed
              LocalID=MyDialogControlID-10
              switch LocalID
                  case 10   ;Exit
                      Return(-1) ; default processing

                  case 1
                  case 2 
                  case 3
                  case 4
                  case 5
                  case 6
                  case 7
                  case 8
                  case 9
                    ;Make box with an X
                    DialogControlSet(MyDialogHandle,LocalID,4,"X")
                    DialogControlState(MyDialogHandle,MyDialogControlID,3,1)
                    T[LocalID]=LocalID
                    CurrentMoveCount=CurrentMoveCount+1
                    break


              endswitch   ;MyDialogControlID
              ;Did User Win?
              if AnyWins(MyDialogHandle,T,15)!=0
                    Message("Congratulations!"," You Won")
                    ResetGame()
                    return (-2)
              endif
              if CurrentMoveCount<9
                 CurrentMoveCount=CurrentMoveCount+1

                 ;User did not win.  Pick a move 

                 m=FindWin(T)             ;If we can win, take it 
                
                 if m==0
                    switch howhard
                       case 1; easy
                           while 1
                              m=1+Random(8)
                              if T[m]==0 then break
                           endwhile
                           break
                       case 2 ; medium
                          m=BlockLose(T)        ;if user can win on a move, take it
                          if m==0
                             while 1
                                m=1+Random(8)
                                if T[m]==0 then break
                             endwhile
                           endif
                           break
                       case 3  ;hard 
                          m=BlockLose(T)        ;if user can win on a move, take it
                          if m==0 && T[5]==0   then m=5
                          if (T[6]+T[1]+T[8]+T[3]+T[4]==4 && T[4]==0) then m=8
                          if (T[8]+T[3]+T[4]+T[9]+T[2]==12 && T[4]==0) then m=4
                          if (T[4]+T[9]+T[2]+T[7]+T[6]==16 && T[2]==0) then m=2
                          if (T[2]+T[7]+T[6]+T[1]+T[8]==8 && T[6]==0 && T[8]==0) then m=6
                          if T[5]== -5 && m==0 && compfirst==2
                             if m==0 && T[3]==0   then m=3
                             if m==0 && T[1]==0   then m=1
                             if m==0 && T[9]==0   then m=9
                             if m==0 && T[7]==0   then m=7
                             if m==0 && T[8]==0   then m=8
                             if m==0 && T[4]==0   then m=4
                             if m==0 && T[6]==0   then m=6
                             if m==0 && T[2]==0   then m=2
                          else
                             if m==0 && T[8]==0   then m=8
                             if m==0 && T[4]==0   then m=4
                             if m==0 && T[6]==0   then m=6
                             if m==0 && T[2]==0   then m=2
                             if m==0 && T[3]==0   then m=3
                             if m==0 && T[1]==0   then m=1
                             if m==0 && T[9]==0   then m=9
                             if m==0 && T[7]==0   then m=7
                          endif
                          break

                     endswitch
                 endif
                 DialogControlSet(MyDialogHandle,"%m%",4,"O")
                 DialogControlState(MyDialogHandle,"%m%"+10,3,1)
                 t[m] = -m
   
   
                 ;did we win?
                 if AnyWins(MyDialogHandle,T,-15)!=0
                    Message("Computer","Won")
                    ResetGame()
                    return (-2)
                 endif
                 ;Draw
              endif 
              if CurrentMoveCount==9
                 Message("Ooop","DRAW")
                    ResetGame()
                    return (-2)
              endif
               
              return(-2)  ; do not exit
          endswitch
   endswitch ; DC_ButtonPressed
   Return(-1) ; Do default processing#EndFunction
#ENDSUBROUTINE

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
T=ArrDimension(10) ; not using 0
ArrInitialize(T,0)
CurrentMoveCount=0
compfirst=2
howhard=2


whocomputer=025
whoplayer=026
howhard_easy=028
howhard_medium=029
howhard_hard=030


;TicTacToe
;order
;8 3 4
;1 5 9
;6 7 2

TicTacToeFormat=`WWWDLGED,6.1`

TicTacToeCaption=`TicTacToe`
TicTacToeX=-01
TicTacToeY=-01
TicTacToeWidth=214
TicTacToeHeight=130
TicTacToeNumControls=030
TicTacToeProcedure=`TicProc`
TicTacToeFont=`Microsoft Sans Serif|5325|70|34`
TicTacToeTextColor=`0|0|128`
TicTacToeBackground=`DEFAULT,128|255|255`

TicTacToe001=`009,052,023,024,VARYTEXT,MyVariable1,"",DEFAULT,6,DEFAULT,"Microsoft Sans Serif|24576|40|34","0|0|0",DEFAULT`
TicTacToe002=`094,094,023,023,VARYTEXT,MyVariable2,"",DEFAULT,6,DEFAULT,"Microsoft Sans Serif|24576|40|34","0|0|0",DEFAULT`
TicTacToe003=`052,009,024,023,VARYTEXT,MyVariable3,"",DEFAULT,6,DEFAULT,"Microsoft Sans Serif|24576|40|34","0|0|0",DEFAULT`
TicTacToe004=`094,009,023,023,VARYTEXT,MyVariable4,"",DEFAULT,6,DEFAULT,"Microsoft Sans Serif|24576|40|34","0|0|0",DEFAULT`
TicTacToe005=`052,052,024,024,VARYTEXT,MyVariable5,"",DEFAULT,6,DEFAULT,"Microsoft Sans Serif|24576|40|34","0|0|0",DEFAULT`
TicTacToe006=`009,094,023,023,VARYTEXT,MyVariable6,"",DEFAULT,6,DEFAULT,"Microsoft Sans Serif|24576|40|34","0|0|0",DEFAULT`
TicTacToe007=`052,094,024,023,VARYTEXT,MyVariable7,"",DEFAULT,6,DEFAULT,"Microsoft Sans Serif|24576|40|34","0|0|0",DEFAULT`
TicTacToe008=`009,009,023,023,VARYTEXT,MyVariable8,"",DEFAULT,6,DEFAULT,"Microsoft Sans Serif|24576|40|34","0|0|0",DEFAULT`
TicTacToe009=`094,052,023,024,VARYTEXT,MyVariable9,"",DEFAULT,6,DEFAULT,"Microsoft Sans Serif|24576|40|34","0|0|0",DEFAULT`
TicTacToe010=`001,041,126,003,STATICTEXT,DEFAULT,"Static 1",DEFAULT,1,DEFAULT,DEFAULT,DEFAULT,"0|0|255"`
TicTacToe011=`014,057,014,012,PUSHBUTTON,DEFAULT,"?",1,15,DEFAULT,DEFAULT,DEFAULT,"255|255|0"`
TicTacToe012=`100,100,011,012,PUSHBUTTON,DEFAULT,"?",2,15,DEFAULT,DEFAULT,DEFAULT,"255|255|0"`
TicTacToe013=`057,014,012,014,PUSHBUTTON,DEFAULT,"?",3,15,DEFAULT,DEFAULT,DEFAULT,"255|255|0"`
TicTacToe014=`100,014,011,014,PUSHBUTTON,DEFAULT,"?",4,15,DEFAULT,DEFAULT,DEFAULT,"255|255|0"`
TicTacToe015=`057,057,012,012,PUSHBUTTON,DEFAULT,"?",5,15,DEFAULT,DEFAULT,DEFAULT,"255|255|0"`
TicTacToe016=`014,100,014,012,PUSHBUTTON,DEFAULT,"?",6,15,DEFAULT,DEFAULT,DEFAULT,"255|255|0"`
TicTacToe017=`057,100,012,012,PUSHBUTTON,DEFAULT,"?",7,15,DEFAULT,DEFAULT,DEFAULT,"255|255|0"`
TicTacToe018=`014,014,014,014,PUSHBUTTON,DEFAULT,"?",8,15,DEFAULT,DEFAULT,DEFAULT,"255|255|0"`
TicTacToe019=`100,057,011,012,PUSHBUTTON,DEFAULT,"?",9,15,DEFAULT,DEFAULT,DEFAULT,"255|255|0"`
TicTacToe020=`190,119,020,008,PUSHBUTTON,DEFAULT,"Exit",0,2,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
TicTacToe021=`001,084,126,003,STATICTEXT,DEFAULT,"Static 1",DEFAULT,1,DEFAULT,DEFAULT,DEFAULT,"0|0|255"`
TicTacToe022=`041,001,003,126,STATICTEXT,DEFAULT,"Static 1",DEFAULT,1,DEFAULT,DEFAULT,DEFAULT,"0|0|255"`
TicTacToe023=`084,001,003,126,STATICTEXT,DEFAULT,"Static 1",DEFAULT,1,DEFAULT,DEFAULT,DEFAULT,"0|0|255"`
TicTacToe024=`156,017,048,038,GROUPBOX,DEFAULT,"Who First",DEFAULT,24,DEFAULT,DEFAULT,DEFAULT,128|255|255`
TicTacToe025=`162,030,042,010,RADIOBUTTON,compfirst,"Computer",1,25,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
TicTacToe026=`162,041,042,011,RADIOBUTTON,compfirst,"Player",2,26,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
TicTacToe027=`156,060,048,049,GROUPBOX,DEFAULT,"How Hard",DEFAULT,27,DEFAULT,DEFAULT,DEFAULT,128|255|255`
TicTacToe028=`162,071,042,011,RADIOBUTTON,howhard,"Easy",1,28,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
TicTacToe029=`162,082,042,011,RADIOBUTTON,howhard,"Medium",2,29,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
TicTacToe030=`162,094,042,010,RADIOBUTTON,howhard,"Hard",3,30,DEFAULT,DEFAULT,DEFAULT,DEFAULT`

ButtonPushed=Dialog("TicTacToe")



