Sign in
Log inSign up
Writing a board game for Android

Writing a board game for Android

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

I started taking Android programming seriously in January, 2019 because I wanted to build an app for playing Surakarta. I had built a prototype for that thing in JavaFX and was amused with the idea of converting that to Android. So I started the journey of learning the Android platform and building my own app simultaneously. I began from the very very basics and went to even building my own custom layout manager called BoardLayout.

I started my journey from the Android developer tutorials.

I created my first activity and started populating it with cool text-views and other little stuff, little did I realize I was going to start from scratch after a while.

I had the vague design of the board in my mind:

GamePlayer.png

Building the layout

Do you see that grid with curves coming out of it? I had to actually design it myself (with help from Wikipedia), using LibreOffic. I then added it as a vector drawable to my Android project. However, I struggled with positioning those red and black pebbles (they're ImageViews) using a TableView, on top of the designed background. The problem with a TableView was that you had to use TableRows with it, and then place the pebbles inside them. But it just didn't work out, and the output just wasn't in shape. So I decided to get my hands dirty, and build my own custom layout manager - the BoardLayout.

A BoardLayout is essentially a "dirty" solution to placing views on a 2D table. The rectangular space it gets, on the screen, is divided into the specified number of rows and columns. Each child view gets positioned into the right box at run-time. What I just told you is a really really rough sketch of BoardLayout. I also added a few adjustments (to place the view at a percentage offset from the top-left corner).

Programming the game

Since the UI prototype was working, I had to build the gaming logic. I searched the Internet for a board game library, that just provides stuff like Board, Piece, BoardGame, etc. I couldn't find one and decided to build my own one: Board.

This library allows you to store the state of each piece on a Board, notifies Players when it's their turn, issues events whenever a change occurs, and also provides a timer API.

This library isn't specific to Surakarta. You can use it to implement any board game. Actually, I created a separate library that depended on Board called Board.Permainan for Surakarta.

Adding more features

At the end, I added additional features like playing online using Firebase. I will plan to write another story for that one. I also added a SettingsActivity to change the board design and pebbles used.

Thanks for reading... Shukant Pal