From 71aab498dc014a582d6db27c81ee38d04c207017 Mon Sep 17 00:00:00 2001 From: Marc Di Luzio Date: Tue, 30 Dec 2014 13:24:21 +0000 Subject: [PATCH] extract format functions out into formatters files --- source/client/main.cpp | 5 +- source/gen/gen.cpp | 3 +- source/test/test.cpp | 15 +-- source/ttrts/CMakeLists.txt | 2 +- source/ttrts/formatters.cpp | 196 ++++++++++++++++++++++++++++++++++++ source/ttrts/formatters.h | 29 ++++++ source/ttrts/game.cpp | 113 +-------------------- source/ttrts/game.h | 8 +- source/ttrts/gametypes.h | 7 -- source/ttrts/order.cpp | 33 ------ source/ttrts/order.h | 4 - source/ttrts/unit.cpp | 46 --------- source/ttrts/unit.h | 7 +- source/ttrts/vector2.h | 2 +- 14 files changed, 246 insertions(+), 224 deletions(-) create mode 100644 source/ttrts/formatters.cpp create mode 100644 source/ttrts/formatters.h delete mode 100644 source/ttrts/order.cpp diff --git a/source/client/main.cpp b/source/client/main.cpp index 272abef..27053fb 100644 --- a/source/client/main.cpp +++ b/source/client/main.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include "game.h" #include "version.h" @@ -41,7 +42,7 @@ bool OutputGameStateFile(CTTRTSGame &game, std::string &gameDir) } // Output the turn description - std::string turnDescriptor = game.GetStateAsString(); + std::string turnDescriptor = GetStringFromGame(game); // Append the version number turnDescriptor = std::string("==== ttrts v") @@ -94,7 +95,7 @@ int main(int argc, char* argv[]) } // Create the game - CTTRTSGame game = CTTRTSGame::CreateFromString(gameDescriptor); + CTTRTSGame game = GetGameFromString(gameDescriptor); // Grab the players involved auto players = game.GetPlayers(); diff --git a/source/gen/gen.cpp b/source/gen/gen.cpp index ac7ab2e..ca092d7 100644 --- a/source/gen/gen.cpp +++ b/source/gen/gen.cpp @@ -2,6 +2,7 @@ #include #include +#include "formatters.h" void AddUnitToGame( player_t player, char vis, uvector2 vec, CTTRTSGame& game ) { @@ -15,7 +16,7 @@ void OutputGame( CTTRTSGame&& game ) { std::ofstream output; output.open (game.GetName() + ".txt"); - output << game.GetStateAsString(); + output << GetStringFromGame(game); output.close(); __forceResetCUnitID(); diff --git a/source/test/test.cpp b/source/test/test.cpp index 60d9028..5cbb700 100644 --- a/source/test/test.cpp +++ b/source/test/test.cpp @@ -1,3 +1,4 @@ +#include #include // std::cout #include "order.h" @@ -25,8 +26,8 @@ const char* tests() { CUnit unit1; - std::string unit1Desc = CUnit::GetStringFromUnit(unit1); - CUnit unit2 = CUnit::GetUnitFromString(unit1Desc); + std::string unit1Desc = GetStringFromUnit(unit1); + CUnit unit2 = GetUnitFromString(unit1Desc); if ( unit1 != unit2 ) return "Failed to convert an empty unit to string and back"; @@ -39,8 +40,8 @@ const char* tests() unit1.SetPlayer(player_t::Green); unit1.SetPos(uvector2(5, 10)); - std::string unit1Desc = CUnit::GetStringFromUnit(unit1); - CUnit unit2 = CUnit::GetUnitFromString(unit1Desc); + std::string unit1Desc = GetStringFromUnit(unit1); + CUnit unit2 = GetUnitFromString(unit1Desc); if ( unit1 != unit2 ) return "Failed to convert custom unit to string and back"; @@ -175,10 +176,10 @@ const char* tests() if ( game.GetWinningPlayer() != player_t::Blue ) return "Game failed to recognise a win for the right Player"; - std::string game_string = game.GetStateAsString(); - CTTRTSGame game2 = CTTRTSGame::CreateFromString(game_string); + std::string game_string = GetStringFromGame(game); + CTTRTSGame game2 = GetGameFromString(game_string); - std::string game2_string = game2.GetStateAsString(); + std::string game2_string = GetStringFromGame(game2); // Try matching up the game descriptors if( game_string != game2_string ) diff --git a/source/ttrts/CMakeLists.txt b/source/ttrts/CMakeLists.txt index f77fc63..53f91fb 100644 --- a/source/ttrts/CMakeLists.txt +++ b/source/ttrts/CMakeLists.txt @@ -12,7 +12,7 @@ include_directories( set( SOURCES game.cpp unit.cpp - order.cpp + formatters.cpp ) # Add this library diff --git a/source/ttrts/formatters.cpp b/source/ttrts/formatters.cpp new file mode 100644 index 0000000..25374d7 --- /dev/null +++ b/source/ttrts/formatters.cpp @@ -0,0 +1,196 @@ +#include "formatters.h" + +// Get the game information as a string +std::string GetStringFromGame( const CTTRTSGame& game ) +{ + // Grab the walls + std::string walls; + if( game.GetWalls().size() == 0 ) + { + walls = "NONE"; + } + + for ( auto wall : game.GetWalls() ) + { + char pos[16]; + if( snprintf(pos, 16, GAME_POS_FORMATTER , wall.x, wall.y ) < 0 ) + { + return "BUFFER OVERFLOW"; + } + walls += pos; + } + + + // Print out the header + char header[512]; + if ( snprintf(header, 512, GAME_HEADER_FORMATTER , + game.GetName().c_str(), + game.GetDimensions().x, + game.GetDimensions().y, + game.GetTurn(), + walls.c_str() ) < 0 ) + { + return "BUFFER OVERFLOW"; + } + + // Gather unit information + std::string units; + for ( const SOrderUnitPair & pair : game.GetOrderUnitPairs() ) + { + units += GetStringFromUnit(pair.unit); + units += '\n'; + } + + // Append the header and units + std::string state(header); + state += '\n'; + state += GAME_HEADER_DELIMITER; + state += units; + state += "END"; + + return state; +} + +// Get the game information as a string +CTTRTSGame GetGameFromString( const std::string& input ) +{ + size_t headerEnd = input.find(GAME_HEADER_DELIMITER); + + std::string header = input.substr(0, headerEnd); + std::string units = input.substr(headerEnd + strlen(GAME_HEADER_DELIMITER)); + + // Grab information from the header + char name[64]; + unsigned int turn; + unsigned int sizex; + unsigned int sizey; + char walls[512]; + if( sscanf(header.c_str(), GAME_HEADER_FORMATTER, name, &sizex, &sizey, &turn, walls) != 5 ) + { + return CTTRTSGame(0,0); + } + + std::vector walls_vector; + { + std::string walls_str = walls; + size_t pos; + while ( ( pos = walls_str.find(']') ) != std::string::npos ) + { + std::string pos_string = walls_str.substr(1,pos); + + // Use scanf to extract positions + + unsigned int x; + unsigned int y; + if( sscanf(pos_string.c_str(), GAME_POS_FORMATTER, &x, &y ) != 2 ) + { + return CTTRTSGame(0,0); + } + + // Erase this coordinate + walls_str.erase(0,pos+1); + + // Append our list + walls_vector.push_back({(ucoord_t)x,(ucoord_t)y}); + } + } + + CTTRTSGame game(sizex,sizey); + game.SetName(name); + game.SetTurn(turn); + + // For each line, construct a unit + { + size_t pos; + while ((pos = units.find('\n')) != std::string::npos) { + std::string unit_string = units.substr(0, pos); + units.erase(0, pos + 1); + game.AddUnit(GetUnitFromString(unit_string)); + } + } + + // Add all walls + for ( auto wall : walls_vector) + { + game.AddWall(wall); + } + + return game; +} + +// Get a string descriptor of a unit +std::string GetStringFromUnit(const CUnit& unit ) +{ + static char buff[128]; + memset(buff,0,sizeof(buff)); + + snprintf(buff,128, UNIT_FORMATTER, + unit.GetID(), + (int)unit.GetPlayer(), + unit.GetVisual(), + unit.GetDir(), + unit.GetPos().x, + unit.GetPos().y ); + + return buff; +} + +// Get a unit from a string descriptor +CUnit GetUnitFromString(const std::string& unit ) +{ + CUnit ret; + + unsigned int id; + int player; + char vis; + char dir; + unsigned int posx; + unsigned int posy; + + sscanf(unit.c_str(), UNIT_FORMATTER, + &id, + &player, + &vis, + &dir, + &posx, + &posy ); + + ret.ForceSetID((unit_id_t)id); + ret.SetPlayer((player_t) player); + ret.SetVisual((unitvis_c)vis); + ret.SetDir((dir_c)dir); + ret.SetPos(uvector2(posx,posy)); + + return ret; +} + +// Convert an order to a string +std::string GetStringFromOrder(const SOrder & order ) +{ + static char buff[128]; + memset(buff,0,sizeof(buff)); + + snprintf(buff,128, ORDER_FORMATTER, + order.command, + order.unit); + + return buff; +} + +// Convert a string to an order +SOrder GetOrderFromString( const std::string& order ) +{ + SOrder ret; + + char corder; + unsigned int unit; + + sscanf(order.c_str(), ORDER_FORMATTER, + &corder, + &unit); + + ret.command = (command_c)corder; + ret.unit = unit; + + return ret; +} \ No newline at end of file diff --git a/source/ttrts/formatters.h b/source/ttrts/formatters.h new file mode 100644 index 0000000..96d34fb --- /dev/null +++ b/source/ttrts/formatters.h @@ -0,0 +1,29 @@ +#ifndef _TTRTS_FORMATTERS_H_ +#define _TTRTS_FORMATTERS_H_ + +#include "order.h" +#include "game.h" +#include "unit.h" + +#include "string.h" + +#define GAME_POS_FORMATTER "[%u,%u]" + +#define GAME_HEADER_FORMATTER "NAME:%s\nSIZE:" GAME_POS_FORMATTER "\nTURN:%u\nWALL:%s" +#define GAME_HEADER_DELIMITER "~~~~\n" + +#define UNIT_FORMATTER "UNIT:%u pl:%u vs:%c dr:%c ps:" GAME_POS_FORMATTER + +// order <--> string conversion functions +std::string GetStringFromOrder(const SOrder & order ); +SOrder GetOrderFromString( const std::string& order ); + +// game <--> string conversion functions +CTTRTSGame GetGameFromString( const std::string& input ); +std::string GetStringFromGame( const CTTRTSGame& game ); + +// unit <--> string conversion functions +std::string GetStringFromUnit(const CUnit& unit ); +CUnit GetUnitFromString(const std::string& unit ); + +#endif \ No newline at end of file diff --git a/source/ttrts/game.cpp b/source/ttrts/game.cpp index 05d2b6f..96800d5 100644 --- a/source/ttrts/game.cpp +++ b/source/ttrts/game.cpp @@ -2,6 +2,7 @@ #include #include +#include "formatters.h" CTTRTSGame::CTTRTSGame( ucoord_t c, ucoord_t r ) : dimensions( c,r ) @@ -501,118 +502,6 @@ player_t CTTRTSGame::GetWinningPlayer() const return winningPlayer; } -// Get the game information as a string -std::string CTTRTSGame::GetStateAsString() const -{ - // Grab the walls - std::string walls; - if( m_walls.size() == 0 ) - { - walls = "NONE"; - } - - for ( auto wall : m_walls) - { - char pos[16]; - if( snprintf(pos, 16, GAME_POS_FORMATTER , wall.x, wall.y ) < 0 ) - { - return "BUFFER OVERFLOW"; - } - walls += pos; - } - - - // Print out the header - char header[512]; - if ( snprintf(header, 512, GAME_HEADER_FORMATTER , name.c_str(), dimensions.x, dimensions.y, turn, walls.c_str() ) < 0 ) - { - return "BUFFER OVERFLOW"; - } - - // Gather unit information - std::string units; - for ( const SOrderUnitPair & pair : m_OrderUnitPairs ) - { - units += CUnit::GetStringFromUnit(pair.unit); - units += '\n'; - } - - // Append the header and units - std::string state(header); - state += '\n'; - state += GAME_HEADER_DELIMITER; - state += units; - state += "END"; - - return state; -} -// Get the game information as a string -CTTRTSGame CTTRTSGame::CreateFromString( const std::string& input ) -{ - size_t headerEnd = input.find(GAME_HEADER_DELIMITER); - - std::string header = input.substr(0, headerEnd); - std::string units = input.substr(headerEnd + strlen(GAME_HEADER_DELIMITER)); - - // Grab information from the header - char name[64]; - unsigned int turn; - unsigned int sizex; - unsigned int sizey; - char walls[512]; - if( sscanf(header.c_str(), GAME_HEADER_FORMATTER, name, &sizex, &sizey, &turn, walls) != 5 ) - { - return CTTRTSGame(0,0); - } - - std::vector walls_vector; - { - std::string walls_str = walls; - size_t pos; - while ( ( pos = walls_str.find(']') ) != std::string::npos ) - { - std::string pos_string = walls_str.substr(1,pos); - - // Use scanf to extract positions - - unsigned int x; - unsigned int y; - if( sscanf(pos_string.c_str(), GAME_POS_FORMATTER, &x, &y ) != 2 ) - { - return CTTRTSGame(0,0); - } - - // Erase this coordinate - walls_str.erase(0,pos+1); - - // Append our list - walls_vector.push_back({(ucoord_t)x,(ucoord_t)y}); - } - } - - CTTRTSGame game(sizex,sizey); - game.SetName(name); - game.SetTurn(turn); - - // For each line, construct a unit - { - size_t pos; - while ((pos = units.find('\n')) != std::string::npos) { - std::string unit_string = units.substr(0, pos); - units.erase(0, pos + 1); - game.AddUnit(CUnit::GetUnitFromString(unit_string)); - } - } - - // Add all walls - for ( auto wall : walls_vector) - { - game.AddWall(wall); - } - - return game; -} - // Check if any of the units can move bool CTTRTSGame::UnitsCanMove() const { diff --git a/source/ttrts/game.h b/source/ttrts/game.h index c9c338d..1401b33 100644 --- a/source/ttrts/game.h +++ b/source/ttrts/game.h @@ -13,9 +13,6 @@ class CTTRTSGame { public: - // Get the game information as a string - static CTTRTSGame CreateFromString( const std::string& input ); - // Constructors CTTRTSGame( ucoord_t c, ucoord_t r ); CTTRTSGame(CTTRTSGame&& game); @@ -38,9 +35,6 @@ public: // Check if any of the units can move bool UnitsCanMove() const; - // Get the game information as a string - std::string GetStateAsString() const; - // Issue orders to the game, returns non-zero if orders are incorrect int IssueOrders( player_t player, const std::string& orders ); int IssueOrders( player_t player, const COrderVector& orders ); @@ -61,6 +55,8 @@ public: const CUnit& GetUnitByIDConst( unit_id_t id ) const; const SOrder & GetOrderByIDConst( unit_id_t id ) const; + inline const OrderUnitPairVector& GetOrderUnitPairs() const { return m_OrderUnitPairs; } + // Get dimensions inline const uvector2& GetDimensions() const { return dimensions; } diff --git a/source/ttrts/gametypes.h b/source/ttrts/gametypes.h index d37591b..b01318d 100644 --- a/source/ttrts/gametypes.h +++ b/source/ttrts/gametypes.h @@ -4,13 +4,6 @@ #include // std::numeric_limits #include "stdlib.h" // for size_t -#define GAME_POS_FORMATTER "[%u,%u]" - -#define GAME_HEADER_FORMATTER "NAME:%s\nSIZE:" GAME_POS_FORMATTER "\nTURN:%u\nWALL:%s" -#define GAME_HEADER_DELIMITER "~~~~\n" - -#define UNIT_FORMATTER "UNIT:%u pl:%u vs:%c dr:%c ps:" GAME_POS_FORMATTER - // Type for a Player IDs enum class player_t : char { diff --git a/source/ttrts/order.cpp b/source/ttrts/order.cpp deleted file mode 100644 index 8e0e82f..0000000 --- a/source/ttrts/order.cpp +++ /dev/null @@ -1,33 +0,0 @@ -#include -#include "order.h" - -// Convert an order to a string -std::string GetStringFromOrder(const SOrder & order ) -{ - static char buff[128]; - memset(buff,0,sizeof(buff)); - - snprintf(buff,128, ORDER_FORMATTER, - order.command, - order.unit); - - return buff; -} - -// Convert a string to an order -SOrder GetOrderFromString( const std::string& order ) -{ - SOrder ret; - - char corder; - unsigned int unit; - - sscanf(order.c_str(), ORDER_FORMATTER, - &corder, - &unit); - - ret.command = (command_c)corder; - ret.unit = unit; - - return ret; -} diff --git a/source/ttrts/order.h b/source/ttrts/order.h index 845e27f..1ad995a 100644 --- a/source/ttrts/order.h +++ b/source/ttrts/order.h @@ -47,8 +47,4 @@ inline bool SOrder::operator== ( const SOrder & rhs ) const // Typedef a vector of orders typedef std::vector COrderVector; -// string <--> order conversion functions -std::string GetStringFromOrder(const SOrder & order ); -SOrder GetOrderFromString( const std::string& order ); - #endif //_ORDERS_H_ diff --git a/source/ttrts/unit.cpp b/source/ttrts/unit.cpp index 39d9b48..b6708c5 100644 --- a/source/ttrts/unit.cpp +++ b/source/ttrts/unit.cpp @@ -50,52 +50,6 @@ CUnit CUnit::GetUnitFromVis( unitvis_c vis ) return unit; } -// Get a string descriptor of a unit -std::string CUnit::GetStringFromUnit(const CUnit& unit ) -{ - static char buff[128]; - memset(buff,0,sizeof(buff)); - - snprintf(buff,128, UNIT_FORMATTER, - unit.unit_id, - (int)unit.player_id, - unit.unit_vis, - unit.dir, - unit.pos.x, - unit.pos.y ); - - return buff; -} - -// Get a unit from a string descriptor -CUnit CUnit::GetUnitFromString(const std::string& unit ) -{ - CUnit ret; - - unsigned int id; - int player; - char vis; - char dir; - unsigned int posx; - unsigned int posy; - - sscanf(unit.c_str(), UNIT_FORMATTER, - &id, - &player, - &vis, - &dir, - &posx, - &posy ); - - ret.unit_id = (unit_id_t)id; - ret.player_id = (player_t) player; - ret.unit_vis = (unitvis_c)vis; - ret.dir = (dir_c)dir; - ret.pos = uvector2(posx,posy); - - return ret; -} - // Plain constructor CUnit::CUnit() : unit_id ( get_unique_unit_id() ) diff --git a/source/ttrts/unit.h b/source/ttrts/unit.h index b594836..7f6e77e 100644 --- a/source/ttrts/unit.h +++ b/source/ttrts/unit.h @@ -18,10 +18,6 @@ public: // Factory function for creating units from a visual static CUnit GetUnitFromVis( unitvis_c vis ); - // Unit <--> string conversion functions - static std::string GetStringFromUnit(const CUnit& unit ); - static CUnit GetUnitFromString(const std::string& unit ); - // Constructor CUnit(); @@ -59,6 +55,9 @@ public: dir_c TurnRight(); dir_c TurnAround(); + // Force set an ID + inline void ForceSetID( unit_id_t id ) { unit_id = id; } + private: // Update my visual must be called when setting direction diff --git a/source/ttrts/vector2.h b/source/ttrts/vector2.h index 40ce5ad..1cd2e34 100644 --- a/source/ttrts/vector2.h +++ b/source/ttrts/vector2.h @@ -1,7 +1,7 @@ #ifndef _VECTOR2_H_ #define _VECTOR2_H_ -#include "mathtypes.h" +#include "gametypes.h" struct uvector2;