Platforms to show: All Mac Windows Linux Cross-Platform

/Main/Web Edition Examples/TicTacToe game/TicTacToe game Web


Required plugins for this example:

You find this example project in your Plugins Download as a Xojo project file within the examples folder: /Main/Web Edition Examples/TicTacToe game/TicTacToe game Web

This example is the version from Mon, 16th Jan 2022.

Project "TicTacToe game Web.xojo_binary_project"
Class App Inherits WebApplication
EventHandler Sub Opening(args() as String) Dim leer As New Picture(100,100,32) Dim kreis As Picture = picture.Open(findFile("kreis.png")) Dim kreuz As Picture = picture.Open(findFile("kreuz.png")) If kreis = Nil Then Break If kreuz = Nil Then Break KreisPic = New WebPicture(kreis, picture.FormatPNG) kreuzPic = New WebPicture(kreuz, picture.FormatPNG) leerPic = New WebPicture(leer, picture.FormatPNG) End EventHandler
Function FindFile(name as string) As FolderItem // Look for file in parent folders from executable on dim parent as FolderItem = app.ExecutableFile.Parent while parent<>Nil dim file as FolderItem = parent.Child(name) if file<>Nil and file.Exists then Return file end if parent = parent.Parent wend End Function
Property KreisPic As WebPicture
Property KreuzPic As WebPicture
Property LeerPic As WebPicture
End Class
Class Session Inherits WebSession
Const ErrorDialogCancel = "Do Not Send"
Const ErrorDialogMessage = "This application has encountered an error and cannot continue."
Const ErrorDialogQuestion = "Please describe what you were doing right before the error occurred:"
Const ErrorDialogSubmit = "Send"
Const ErrorThankYou = "Thank You"
Const ErrorThankYouMessage = "Your feedback helps us make improvements."
Const NoJavascriptInstructions = "To turn Javascript on, please refer to your browser settings window."
Const NoJavascriptMessage = "Javascript must be enabled to access this page."
End Class
Class WebPage1 Inherits WebPage
Control ImageView1 Inherits WebImageViewer
ControlInstance ImageView1 Inherits WebImageViewer
EventHandler Sub Pressed(x as Integer, y as Integer) HumanClicked 0 End EventHandler
End Control
Control ImageView2 Inherits WebImageViewer
ControlInstance ImageView2 Inherits WebImageViewer
EventHandler Sub Pressed(x as Integer, y as Integer) HumanClicked 1 End EventHandler
End Control
Control ImageView3 Inherits WebImageViewer
ControlInstance ImageView3 Inherits WebImageViewer
EventHandler Sub Pressed(x as Integer, y as Integer) HumanClicked 2 End EventHandler
End Control
Control ImageView4 Inherits WebImageViewer
ControlInstance ImageView4 Inherits WebImageViewer
EventHandler Sub Pressed(x as Integer, y as Integer) HumanClicked 3 End EventHandler
End Control
Control ImageView5 Inherits WebImageViewer
ControlInstance ImageView5 Inherits WebImageViewer
EventHandler Sub Pressed(x as Integer, y as Integer) HumanClicked 4 End EventHandler
End Control
Control ImageView6 Inherits WebImageViewer
ControlInstance ImageView6 Inherits WebImageViewer
EventHandler Sub Pressed(x as Integer, y as Integer) HumanClicked 5 End EventHandler
End Control
Control ImageView7 Inherits WebImageViewer
ControlInstance ImageView7 Inherits WebImageViewer
EventHandler Sub Pressed(x as Integer, y as Integer) HumanClicked 6 End EventHandler
End Control
Control ImageView8 Inherits WebImageViewer
ControlInstance ImageView8 Inherits WebImageViewer
EventHandler Sub Pressed(x as Integer, y as Integer) HumanClicked 7 End EventHandler
End Control
Control ImageView9 Inherits WebImageViewer
ControlInstance ImageView9 Inherits WebImageViewer
EventHandler Sub Pressed(x as Integer, y as Integer) HumanClicked 8 End EventHandler
End Control
Control Button1 Inherits WebButton
ControlInstance Button1 Inherits WebButton
EventHandler Sub Pressed() neu End EventHandler
End Control
Control Liste Inherits WebListBox
ControlInstance Liste Inherits WebListBox
End Control
EventHandler Sub Opening() Fields.Append ImageView1 Fields.Append ImageView2 Fields.Append ImageView3 Fields.Append ImageView4 Fields.Append ImageView5 Fields.Append ImageView6 Fields.Append ImageView7 Fields.Append ImageView8 Fields.Append ImageView9 Me.Style.BackgroundColor = &c777777 neu End EventHandler
Protected Sub CalculatePoints() dim si(-1) as integer PointsHuman=0 PointsComputer=0 si.append feld(0)+feld(1)+feld(2) si.append feld(3)+feld(4)+feld(5) si.append feld(6)+feld(7)+feld(8) si.append feld(0)+feld(4)+feld(8) si.append feld(2)+feld(4)+feld(6) si.append feld(0)+feld(3)+feld(6) si.append feld(1)+feld(4)+feld(7) si.append feld(2)+feld(5)+feld(8) for n as integer = 0 to 7 Select case si(n) case 0 PointsHuman=PointsHuman+1 PointsComputer=PointsComputer+1 case 1 PointsHuman=PointsHuman+2 case 2 PointsHuman=PointsHuman+4 case 3 PointsHuman=PointsHuman+50 case 5 PointsComputer=PointsComputer+2 case 10 PointsComputer=PointsComputer+4 case 15 PointsComputer=PointsComputer+50 end Select next End Sub
Protected Sub ComputerTurn() dim best as integer = 0 dim bp as integer = -1 dim frei as integer = FreeFieldCount if frei = 9 then // computer starts put 4 Return end if if frei = 1 then // last field putany Return end if if frei = 0 then // ended Return end if // can computer win? for i as integer = 0 to 8 if feld(i)=0 then feld(i)=5 CalculatePoints if PointsComputer>49 then put i Return end if feld(i)=0 end if next // can human win? for i as integer = 0 to 8 if feld(i)=0 then feld(i)=1 CalculatePoints if PointsHuman > 49 then put i Return end if feld(i)=0 end if next // see if human could win after our move for i as integer = 0 to 8 if feld(i) = 0 then for j as integer = 0 to 8 if i<>j and feld(j) = 0 then feld(i)=5 feld(j)=1 CalculatePoints dim w as integer = PointsComputer-PointsHuman if (w>bp) then bp=w best=i end if feld(i)=0 feld(j)=0 end if next end if next if bp>=0 then put(best) else putany end if End Sub
Protected Function FreeFieldCount() As integer dim j as integer for i as integer = 0 to 8 if feld(i)=0 then j = j + 1 end if next Return j End Function
Protected Sub HumanClicked(n as integer) if ende = false and feld(n)=0 then Fields(n).Picture = app.KreuzPic feld(n)=1 message "Human takes field "+str(n mod 3 + 1)+"/"+str(n\3+1) CalculatePoints if PointsHuman >=50 then Message "Human wins!" MessageBox "You win!" ende = true else ComputerTurn CalculatePoints if PointsComputer >= 50 then Message "Computer wins!" MessageBox "You loose!" ende = true end if end if end if End Sub
Protected Sub Message(s as string) Liste.AddRowAt 0, s End Sub
Protected Sub neu() ende = false for i as integer = 0 to 8 feld(i)=0 Fields(i).Picture=app.LeerPic next PointsHuman = 0 PointsComputer = 0 liste.RemoveAllRows If Rnd > 0.5 Then message "Computer starts..." ComputerTurn else message "Human starts..." End If End Sub
Protected Sub put(n as integer) feld(n)=5 Fields(n).picture=app.KreisPic Message "Computer takes field "+str(n mod 3 + 1)+"/"+str(n\3+1) End Sub
Protected Sub putany() static moves(-1) as integer = Array(4, 0, 2, 6, 8, 1, 3, 5, 7) for n as integer = 0 to 8 dim j as integer = moves(n) if feld(j)=0 then put j Return end if next End Sub
Property Protected Fields() As WebImageViewer
Property Protected PointsComputer As Integer
Property Protected PointsHuman As Integer
Property Protected ende As Boolean
Property Protected feld(8) As Integer
End Class
End Project

See also:

The items on this page are in the following plugins: MBS Main Plugin.


The biggest plugin in space...