From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

Example Code

Battleships Networked Game in LaVIEW

Code and Documents

Attachment

Battleships!

Classic battleship game programmed entirely in LabVIEW - playable over your local network.

Utilising LabVIEW's networking features, OOP, sub-panels and XControls.

Instructions:

  1. Open up the project in the attached zip, open 'main.vi' and hit run.
  2. Enter your name and whether you want to find other games broadcasting on the network (Join) or whether you want to broadcast yourself (Create).
  3. Next, either wait for a challenge or select a game and click Connect.
  4. Provided your challenge is accepted, place your ships on your board and hit Ready. (Right click to rotate ships)
  5. Follow the in-game instructions until either you've sunk all their ships or your sunk yourself! (To fire select a square on your opponents board and click Fire.

Ships1.png

How it works:

The separate stages of the game are in separate VIs which are loaded dynamically into a sub-panel.

sub-panel.png

The broadcast of the game is done using UDP Multicast. The Host is continuously broadcasting their availability whilst maintaining one end of 2 network streams (one for sending messages, one for receiving).

udp.png

Once  a client hits connect, this connects their network streams to the hosts and messages are sent across this link.

connect.PNG

Messages themselves are LabVIEW classes. All of which inherit from the parent 'Message' Class. Messages are then queued from an event based control loop to be sent over the network stream.

Before sending the objects themselves are flattened to XML.

flatten.PNG

In the receive loop the XML messages are de-serialised by inspecting the XML tags for the class type to load them from disk. At this point the type is a generic LabVIEW object - it is then cast to the Message Class. From here we can Dynamically dispatch an execute method for each message.

execute.PNG

In the execute method we fire a user event to communicate the message data back to the event based control loop!

Hopefully if you look at the code this will become clear!

Many Thanks to Mike Bailey for this architecture which I think works quite neatly!

The game boards themselves are composed of an XControl which accept data of type Board Class.  The control loop maintains the state of the game in this object (one for local - one for opponent).

After each loop iteration the XControls are updated with latest new game state.

xcontrol.PNG

The code for placing the ships, highlighting cells etc is within the XControl. There is a state property for the control to decide how it behaves at a particular time.

I was pretty pleased with the code in the end - the majority quite neat and self documenting (the XControl is quite a mess though!) I'd love to get some feedback for this app - certainly when it comes to architecture and coding style. The game logic itself has a few flaws I know - not announcing sunk ships for example but if you have ideas how I could have better implement the game to make this easier, I'd love to hear.

Also, if you have any questions about the implementation let me know!

Nick

Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.

Contributors