The baseResults class defines a packet of information that is sent back to you after you make a move. Many games really don't require any returned information. For example in the Tic-Tac-Toe game, because the board is open you already know the results of your move. However in the Battleship game you need the other player to tell you if it was a hit or a miss. Even if you don't have hit and miss information that needs to be transmitted back, you still need to send a Results packet because it is the receiving player who determines if your move resulted in a win. So in the Tic-Tac-Toe game we have to send a results packet to acknowledge when the game is over.

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 baseResults.

The code for the baseResults object is in the file TwoPlayerGame_base_packet.h and TwoPlayerGame_base_packet.cpp. The baseResults class is derived from the basePacket which is described in the previous section. The baseResults class inherits all of the data and methods of basePacket. Here is the definition of baseResults.

class baseResults : public basePacket {
  public:
    uint16_t resultsNum;
    baseResults(void) {type=RESULTS_PACKET; subType=NORMAL_RESULTS;};
    virtual size_t my_size() { return sizeof( *this ); };
    virtual void require(void) {requireType(RESULTS_PACKET);};
    virtual bool generateResults(baseMove* Move)=0;
    virtual bool processResults(void)=0;
    #if(TPG_DEBUG)
      virtual void print(void);//Prints debug information about the results. Calls basePacket::print
    #endif  
};

The following items are inherited from basePacket...

  • baseRadio* Radio; -- The game engine initializes the Radio pointer automatically.
  • packetType_t type; -- Is always RESULTS_PACKET.
  • packetSubType_t subType; -- Default is NORMAL_RESULTS but can be any of the following: HIT_RESULTS, MISS_RESULTSWIN_RESULTS, or LOSE_RESULTS.

The following items are specific to a results packet...

  • uint16_t resultsNum; -- The move number to which these results refer.
  • baseResults(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 on basePacket.
  • virtual void require(void); -- Waits forever for a RESULTS_PACKET.
  • virtual bool generateResults(baseMove* Move); -- You MUST implement a derived method in your game implementation to produce a packet of data to send back to your opponent telling them the results of their efforts. Here you will implement the rules that determine what is a WIN_RESULTS, LOSE_RESULTS, or TIE_RESULTS in addition to any other results information you may need to provide. Returns true if the results ended the game.
  • virtual bool processResults(void); -- You MUST implement a derived method in your game implementation to react to the results packet you will receive after you make a move. Returns true if the results ended the game.
  • virtual void print(void); -- Prints debug messages on the serial monitor. 

Creating a Derived Results Class

Let's take a look at the TTT_Results and BShip_Results classes for our two demo games.

//Ways to win
enum win_t {NO_WIN, TOP_ROW, MIDDLE_ROW, BOTTOM_ROW, LEFT_COLUMN, MIDDLE_COLUMN, RIGHT_COLUMN,
            DESCENDING_DIAGONAL, ASCENDING_DIAGONAL, TIE};
class TTT_Results : public baseResults {
  public:
    win_t Win;
    size_t my_size() override { return sizeof( *this ); };
    bool processResults(void)override;
    bool generateResults(baseMove* Move)override;
};
class BShip_Results : public baseResults {
  public:
    int8_t shipDestroyed;
    uint8_t shot;
    size_t my_size() override { return sizeof( *this ); };
    bool processResults(void)override;
    bool generateResults(baseMove* Move)override;
};

For the Tic-Tac-Toe game we pass by a value that tells the type of winning move if Results.subType is WIN_RESULTS. We designed the game engine so that the results are the only determining factor of a win or lose game. That way we don't have to implement the rules in 2 different places.

For the Battleship game we have an signed integer that tells which if any ships were destroyed. If none were destroyed it is -1 and if one was destroyed it's 0 through 4. We also echo back to the player the location that he shot at. It saves us some extra work in the processResults() method.

We won't go through the details of the processResults() and generateResults(Move) implementations but you can look at the source code and see what we did.

This guide was first published on Jul 07, 2020. It was last updated on Jun 28, 2020.

This page (The baseResults object class) was last updated on Jun 29, 2020.

Text editor powered by tinymce.