B4J Tutorial ✅ Tic-Tac-Toe. Simple XUI cross platform WiFi game example B4J vs B4A vs B4i - Newer developers

Hello fellow B4X developers.
Here is my example of using a broadcast address to automatically find another devices IP address on a network, then to use that IP address to play a game (in this case Tic-Tac-Toe) across that WiFi network (device 1 vs device 2, Desktop, Android or iOS). This tutorial not only shows you how to use UDPSocket (GetBroadcastAddress) to manipulate your routers broadcast address, but I've also used Canvas.DrawLine, SetVisibleAnimated, a Timer, XUI, Images, MediaPlayer (for sound effects), UDPPacket, ServerSocket, B4XSerializator and also B4XView to help create this example which is now based on Erels original example with some changes.

The two modules named TicTacToe.bas and ConnectPlayer.bas are in fact shared modules, thus you should place them in your shared projects modules folder as these two modules work with B4A, B4i and also B4J without any changes necessary. You DO NOT need them in your main projects folder, but if you are not using B4A or B4i then you might as well leave them where they are currently located.

What is a broadcast address (taken from wikipedia): A broadcast address is a network address at which all devices connected to a multiple-access communications network are enabled to receive datagrams, which comprise UDP and TCP/IP packets, for instance. A message sent to a broadcast address may be received by all network-attached hosts

>>> CLICK HERE <<< to download all three Tic-Tac-Toe code examples.

The B4A example can be found below

The B4i example can be found below

Please note: There are multiple ways to achieve what I've done here, I chose this way as I believe that this is one of the easiest for newer B4X developers to follow and to learn from. I would usually only use AsyncStreams (AStream.Write) with ServerSocket (Server.Listen) to send and receive data over a WiFi network, but because I wanted the devices to automatically find and connect to each other without any user interaction I decided to use UDP.

07/06/2020: Released V1.0.0.1
10/06/2020: Remove TTT sub from the main module

Libraries used:-


iOS screenshot


YouTube video



Enjoy...
 
Last edited:
Cookies are required to use this site. You must accept them to continue using the site. Learn more…