extract format functions out into formatters files

This commit is contained in:
Marc Di Luzio 2014-12-30 13:24:21 +00:00
parent 3342300324
commit 71aab498dc
14 changed files with 246 additions and 224 deletions

View file

@ -5,6 +5,7 @@
#include <sys/stat.h>
#include <stdio.h>
#include <stdbool.h>
#include <formatters.h>
#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();

View file

@ -2,6 +2,7 @@
#include <iostream>
#include <fstream>
#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();

View file

@ -1,3 +1,4 @@
#include <formatters.h>
#include <iostream> // 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 )

View file

@ -12,7 +12,7 @@ include_directories(
set( SOURCES
game.cpp
unit.cpp
order.cpp
formatters.cpp
)
# Add this library

196
source/ttrts/formatters.cpp Normal file
View file

@ -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<uvector2> 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;
}

29
source/ttrts/formatters.h Normal file
View file

@ -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

View file

@ -2,6 +2,7 @@
#include <algorithm>
#include <string.h>
#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<uvector2> 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
{

View file

@ -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; }

View file

@ -4,13 +4,6 @@
#include <limits> // 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
{

View file

@ -1,33 +0,0 @@
#include <string.h>
#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;
}

View file

@ -47,8 +47,4 @@ inline bool SOrder::operator== ( const SOrder & rhs ) const
// Typedef a vector of orders
typedef std::vector<SOrder> COrderVector;
// string <--> order conversion functions
std::string GetStringFromOrder(const SOrder & order );
SOrder GetOrderFromString( const std::string& order );
#endif //_ORDERS_H_

View file

@ -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() )

View file

@ -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

View file

@ -1,7 +1,7 @@
#ifndef _VECTOR2_H_
#define _VECTOR2_H_
#include "mathtypes.h"
#include "gametypes.h"
struct uvector2;