My FeedDiscussionsHeadless CMS
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more

My secret to building a board game for Android in 5 minutes

Shukant Pal's photo
Shukant Pal
·Feb 10, 2019

I almost always use two special libraries that can help you fire up a board game in under 5 minutes - Board and Board.Android.UI.

What is Board?

Board is a set of event-based, multi-threaded, open-source board game libraries that will help you build the core logic and UI for your own game. To use the Board library, we need to extend the BoardPlatformProvider class and setup our own threads of execution.

How do I use Board?

Use the links Board and Board.Android.UI and download the sources and import both projects as separate modules into your Android app.

Main components of the Board library

Board mainly is based around these four things:

  • Platform Provider: Each application that uses the Board library must provide its own implementation of a platform provider. These objects allow the library to execute CPU-intensive tasks on separate threads.

  • Board Game: The BoardGame controls the flow of the game. It is the host for a variety of controller objects like Board, InputController, and more.

  • Board: You need to implement Board and handle moves that the user makes.

  • Player Rotator: This object will control when a player receives his turn. We will be using the CircularPlayerRotator which is useful for most cases.

From the Board.Android.UI library, we will use the BoardLayout custom view to display our pieces.

Building our app

In Android Studio, create a project "BoardDemo" with a blank activity.

Then, we add a square FrameLayout in the parent ConstraintLayout. That will hold space for the BoardLayout which we will add programmatically.

Now, before creating the UI, we will need to create our board-game logic. In these three files - we write our tic-tac-toe board game logic:

We created three components for our game: the TTTBoardGame, TTTBoard, and TTTHumanPlayer. Now we need to finish our platform-provider and MainActivity.

Our platform-provider needs to provide two threads - one for a possible computer player (if you make an AI player) and for game-events that may need to be executed on a separate thread.

Finally, let's build our MainActivity. Our MainActivity also controls the game in a way. It needs to listen to all the events that the game will issue:

  • Player-Wiring Event: This event is issued before all other events. This signals the players and other listeners to initialize their component for the game. Our MainActivity will create a new BoardLayout and add 9 text-views to it. It will also register View.OnTouchListener objects to the text-views so whenever they are touched - a move will be delivered on behalf of the current player.

  • Move Event: This event is issued whenever a move occurs (legal move). We update our UI in this to show that the move happened.

  • We aren't going to handle other events like timer-tick, elimination, and more.

At last, in the onStart method we initialize the game.

Consider adding a background to the BoardLayout by downloading an image from the Internet, that will show the grid for the tic-tac-toe game.

Thanks for reading

Shukant Pal

Contact to report any bugs.