The baseMove
class contains all the data for making and receiving moves. You MUST create a derived move class from it because it contains pure virtual functions that you will have to create. Your derived move class will contain whatever data and methods you need for your particular game. Here are details on the baseMove
.
The code for the baseMove
object is in the file TwoPlayerGame_base_packet.h and TwoPlayerGame_base_packet.cpp. The baseMove
class is derived from the basePacket
which is described in the previous section. The baseMove
class inherits all of the data and methods of basePacket
. Here is the definition of baseMove
.
class baseMove : public basePacket { public: uint16_t moveNum; baseMove(void) {type=MOVE_PACKET;subType=NORMAL_MOVE;}; virtual size_t my_size() { return sizeof( *this ); }; virtual void decideMyMove(void)=0; void require(void) {requireType(MOVE_PACKET);}; #if(TPG_DEBUG) virtual void print(void); #endif };
The following items are inherited from basePacket...
-
baseRadio* Radio;
-- The game engine initializes the Radio pointer automatically. -
packetType_t type;
-- Is alwaysMOVE_PACKET
. -
packetSubType_t subType;
-- Default isNORMAL_MOVE
but can also bePASS_MOVE
orQUIT_MOVE
.
The following items are specific to a move packet
-
uint16_t moveNum;
-- The number of this move. -
baseMove(void);
-- Constructor. -
virtual size_t my_size() { return sizeof( *this ); };
-- Returns the size of the actual object as instantiated. See the discussion in the section onbasePacket
. -
virtual void decideMyMove(void);
-- You MUST implement and override this pure virtual function. It will prompt the user for some input so he can specify his move using any method you want such as buttons, joysticks, or serial monitor input. -
void require(void);
-- Wait forever for aMOVE_PACKET
from the other device. -
virtual void print(void);
-- Prints debug messages on the serial monitor.
Creating a Derived Move Class
Let's take a brief look at the TTT_Move
class for the Tic-Tac-Toe game as well as the BShip_Move
class for the Battleship game.
class TTT_Move : public baseMove { public: uint8_t square; size_t my_size() override { return sizeof( *this ); }; void decideMyMove(void)override; }; class BShip_Move : public baseMove { public: uint8_t shot; size_t my_size() override { return sizeof( *this ); }; void decideMyMove(void)override; };
The only data we need to transmit for either of these games is the location that we selected during our move. In the Tic-Tac-Toe game we called it square
and in the Battleship game we called it shot
but it's just a single integer that is an index into the board denoting a location.
The real action goes on in the decideMyMove()
method. We won't go through all the details of the decideMyMove
implementations. You can look at the source code yourself which is well commented. The basic procedure is to place a cursor on the screen and then take input from the joystick using the Adafruit_Arcada_Library.
When you make a selection, it first checks to make sure you didn't overwrite a previous selection. If it's in the clear then it copies the index into square
or shot
and then exits. You do not need to do anything to send your move. The game engine takes care of that for you. All you have to do is fill out the one data item and then exit.
Page last edited March 08, 2024
Text editor powered by tinymce.