Microsoft Small Basic

Program Listing:
' October 2011 Advanced Challenge 1 "Tic-Tac-Toe" game by NaoChanON
' position
' 1 2 3
' 4 5 6
' 7 8 9
'

init_Data()

GraphicsWindow.MouseDown=onmousedown
Controls.ButtonClicked = OnButtonClicked

Sub PC_Choice      ' rather strong mode
NN=NN+1
if NN=10 then        ' If you did last choice
Try_Again()
elseIf NN=1 Then     ' 1 , 3 , 5 , 7 , 9
NN_1st()
Elseif NN=2 then     ' 5 or 1 , 3 , 7 , 9
NN_2nd()
ElseIf NN=3 then     ' ( 1 , 3 ) , 7 , 9
NN_3rd()
Elseif 3<NN then     ' residue
YP_Check()
Line3_Check()      ' check PC's win lose
result()
If NN=9 then
Try_Again()
EndIf
Endif
EndSub

Sub NN_1st
RND=9
Rnd_Odd_Number()  ' set X-mark at odd number position
SX_Set()
EndSub

Sub NN_2nd  '
if block[5]="."  then
Shapes.Animate(sX[NN],(2-1)*100+45,(2-1)*100+45,600)
block[5]="P"      ' set X-mark at 5 :center
Else
RND=3
Rnd_Odd_Number()  ' set X-mark at 1 or 3 corner
SX_Set()
Endif
endsub

Sub NN_3rd
Loop3:
RND=9
Rnd_Odd_Number()     ' set X-mark at odd number position
If block[RN]<>"." Then
Goto Loop3
endif
SX_Set()
EndSub

Sub YP_Check
For L=1 To 9
if block[L]="." then
block[L]="P"
Line3_Check()
result()
if YN=-1 then    ' if chk[k]="PPP" to win
RN=L
SX_Set()
Check_Next()
Goto YP_end
Else
block[L]="."
Endif
EndIf
EndFor
'
For L=1 To 9
if block[L]="." then
block[L]="Y"
Line3_Check()
result()
if YN=1 then   ' if chk[k]="YYY" to protect
RN=L
SX_Set()
Goto YP_end
Else
block[L]="."
Endif
EndIf
EndFor
'
Loop4:
RN=math.GetRandomNumber(9)   ' draw case
If block[RN]<>"." Then
Goto Loop4
EndIf
SX_Set()
YP_end:
EndSub

Sub SX_Set     ' X- mark setting
MY=math.Floor((RN-1)/3)+1
MX=RN-3*(MY-1)
Shapes.Animate(sX[NN],(MX-1)*100+45,(MY-1)*100+45,600)
block[RN]="P"
EndSub

Sub Line3_Check
For i=1 To 8
chk[i]=""
EndFor
For j=1 To 3   ' Horizontal line
For i=1 To 3
ij=3*(j-1)+i
chk[j]=chk[j]+block[ij]
EndFor
EndFor
For i=1 To 3     ' Vertical line
For j=1 To 3
ij=3*(j-1)+i
chk[i+3]=chk[i+3]+block[ij]
EndFor
EndFor
chk[7]=block[1]+ block[5]+block[9]
chk[8]=block[3]+ block[5]+block[7]
EndSub

Sub result
For k=1 To 8
If chk[k]="YYY" Then ' you win
YN=1
Goto R_end
ElseIf chk[k]="PPP"  then ' PC win
YN=-1
Goto R_end
EndIf
EndFor
YN=0   ' Draw
R_end:
VN=K
EndSub

NN=NN+1
MX= 1+math.floor(GraphicsWindow.MouseX/100)
MY= 1+math.floor(GraphicsWindow.MouseY/100)
RN=MX+3*(MY-1)
Shapes.animate(sO[NN],(MX-1)*100+45,(MY-1)*100+45,400) ' set O-mark
block[RN]="Y"
'
Line3_Check()
Result()
Check_Next()
EndSub

Sub Check_Next
if YN=0 then    ' Draw case
PC_Choice()
ElseIf YN=1 or YN=-1 then
Try_Again()  ' Win or Lose case
Endif
EndSub

Sub Rnd_Odd_Number     ' get odd number
Loop1:
RN=math.GetRandomNumber(RND)
if Math.remainder(RN,2)=0  then
goto Loop1
endif
EndSub

Sub OnButtonClicked
name=controls.LastClickedButton     'Last button name
nm=controls.GetButtonCaption(name)  ' Button's caption
if  nm="Try__Again" then
Try_Again()
Restart()
elseif nm="You= O" then
btn_rmv()   '
You_Start()
elseif  nm="PC = X" then
btn_rmv()
PC_Choice()
endif
EndSub

Sub btn_rmv   ' delete control button and message
Controls.HideControl(btn[1])
Controls.HideControl(btn[2])
Shapes.move(smsg1,100,-100)
Shapes.move(smsg2,100,-100)
EndSub

Sub next_msg
Shapes.move(msg,50,-100)
Shapes.Animate(msg,100,350,500)
EndSub

Sub you_start
next_msg()
EndSub

Sub Try_Again  ' try again button and win / lose message and line
GraphicsWindow.BrushColor="Navy"
Controls.SetSize(btn[3] ,250,40)
If YN=1 Then
Shapes.Animate(Swin,100,400,800)
Shapes.Zoom(Swin,2,3)
DrawLine()
ElseIf YN=-1 then
Shapes.Animate(Slose,100,400,800)
Shapes.Zoom(Slose,2,3)
DrawLine()
Else
Shapes.Move(swin,50,-100)
Shapes.Animate(Sdraw,100,400,800)
Shapes.Zoom(Sdraw,2,3)
EndIf
EndSub

Sub Restart
GraphicsWindow.Clear()
init_Data()
Endsub

Sub Btn_set   ' message and button set
GraphicsWindow.BrushColor="Yellow"
Shapes.Animate(smsg1,25,305,1000)
GraphicsWindow.BrushColor="Red"
Shapes.Animate(smsg2,185,305,1000)
GraphicsWindow.BrushColor="Yellow"
'
GraphicsWindow.BrushColor="Navy"
Controls.SetSize(btn[1] ,100,40)
GraphicsWindow.BrushColor="Red"
Controls.SetSize(btn[2] ,100,40)
GraphicsWindow.BrushColor="Yellow"
EndSub

Sub DrawLine   ' draw line if win /lose
GraphicsWindow.PenColor="Yellow"
GraphicsWindow.PenWidth=3
If VN<4 Then
GraphicsWindow.DrawLine(0,50+100*(VN-1),300,50+100*(VN-1))
ElseIf 3<VN and VN<7 then
GraphicsWindow.DrawLine(50+100*(VN-4),0,50+100*(VN-4),300)
elseif VN=7 then
GraphicsWindow.DrawLine(0,0,300,300)
Elseif VN=8 then
GraphicsWindow.DrawLine(300,0,0,300)
EndIf
EndSub

Sub Init_Data
BW=100
BH=100
GW=3*BW+2
GH=4.5*BH
NN=0
GraphicsWindow.clear()
GraphicsWindow.top=0
GraphicsWindow.Left=300
GraphicsWindow.Width = GW
GraphicsWindow.Height = GH
GraphicsWindow.Title = "Tic Tac Toe"
GraphicsWindow.Show()
GraphicsWindow.BackgroundColor = "Teal"
GraphicsWindow.BrushColor ="Yellow"
GraphicsWindow.FontSize = 20
GraphicsWindow.FontName = "Coorie new"
GraphicsWindow.PenColor="Yellow"
GraphicsWindow.PenWidth=3
' init
For j=1 To 3
For i=1 To 3
GraphicsWindow.DrawRectangle(2+100*(i-1),2+100*(j-1),98,98)
EndFor
endfor
'
for i=1 To 9
block[i]="."
chk[i]=""
EndFor
'
GraphicsWindow.BrushColor="Yellow"
For i=1 To 9
Shapes.Zoom(sO[i],5,5)
Shapes.Move(sO[i],45,-150)
EndFor
GraphicsWindow.BrushColor="Red"
For i=1 To 9
Shapes.Zoom(sX[i],5,5)
Shapes.Move(sX[i],45,-50)
EndFor
'