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?
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
BoardGamecontrols the flow of the game. It is the host for a variety of controller objects like
InputController, and more.
Board: You need to implement
Boardand handle moves that the user makes.
Player Rotator: This object will control when a player receives his turn. We will be using the
CircularPlayerRotatorwhich 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
BoardLayoutand add 9 text-views to it. It will also register
View.OnTouchListenerobjects 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
Contact SilcosTech@outlook.com to report any bugs.
Be the first one to comment