Rename all usages of Team to Player
This was confusingly inconsistent in the codebase. fixes #7
This commit is contained in:
parent
d4f9470704
commit
9e44d5144b
9 changed files with 98 additions and 100 deletions
|
@ -30,7 +30,7 @@ CTTRTSGame& CTTRTSGame::operator=(CTTRTSGame&& game)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Interpret a string of orders
|
// Interpret a string of orders
|
||||||
int CTTRTSGame::IssueOrders( Team team, const std::string& _orders )
|
int CTTRTSGame::IssueOrders( Player player, const std::string& _orders )
|
||||||
{
|
{
|
||||||
COrderVector orderVector;
|
COrderVector orderVector;
|
||||||
|
|
||||||
|
@ -53,17 +53,17 @@ int CTTRTSGame::IssueOrders( Team team, const std::string& _orders )
|
||||||
}
|
}
|
||||||
|
|
||||||
// Call our add order by vector method
|
// Call our add order by vector method
|
||||||
return IssueOrders(team,orderVector);
|
return IssueOrders(player,orderVector);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Issue orders by vector to the game
|
// Issue orders by vector to the game
|
||||||
int CTTRTSGame::IssueOrders( Team team, const COrderVector& orders )
|
int CTTRTSGame::IssueOrders( Player player, const COrderVector& orders )
|
||||||
{
|
{
|
||||||
// verify all the orders
|
// verify all the orders
|
||||||
for ( auto order : orders )
|
for ( auto order : orders )
|
||||||
{
|
{
|
||||||
// If any order returns non-zero, back out
|
// If any order returns non-zero, back out
|
||||||
if ( IssueOrder(team,order) )
|
if ( IssueOrder(player,order) )
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -71,10 +71,10 @@ int CTTRTSGame::IssueOrders( Team team, const COrderVector& orders )
|
||||||
}
|
}
|
||||||
|
|
||||||
// Issue a single order
|
// Issue a single order
|
||||||
int CTTRTSGame::IssueOrder( Team team, const SOrder & order )
|
int CTTRTSGame::IssueOrder( Player player, const SOrder & order )
|
||||||
{
|
{
|
||||||
// Verify the order
|
// Verify the order
|
||||||
if ( VerifyOrder(team,order) )
|
if ( VerifyOrder(player,order) )
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
// Get the right unit for the order
|
// Get the right unit for the order
|
||||||
|
@ -366,7 +366,7 @@ int CTTRTSGame::AddUnits( CUnitVector&& units )
|
||||||
}
|
}
|
||||||
|
|
||||||
// Verify any order
|
// Verify any order
|
||||||
int CTTRTSGame::VerifyOrder( Team team, const SOrder & order ) const
|
int CTTRTSGame::VerifyOrder( Player player, const SOrder & order ) const
|
||||||
{
|
{
|
||||||
int ret = 1;
|
int ret = 1;
|
||||||
|
|
||||||
|
@ -378,7 +378,7 @@ int CTTRTSGame::VerifyOrder( Team team, const SOrder & order ) const
|
||||||
{
|
{
|
||||||
// Accept if we have the unit
|
// Accept if we have the unit
|
||||||
if (pair.unit.GetID() == unitID
|
if (pair.unit.GetID() == unitID
|
||||||
&& pair.unit.GetTeam() == team )
|
&& pair.unit.GetPlayer() == player)
|
||||||
{
|
{
|
||||||
ret = 0;
|
ret = 0;
|
||||||
break;
|
break;
|
||||||
|
@ -434,60 +434,60 @@ CUnit& CTTRTSGame::GetUnitByID( unit_id_t id )
|
||||||
return invalid_unit;
|
return invalid_unit;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get a vector of the teams in the current game
|
// Get a vector of the players in the current game
|
||||||
std::vector<Team> CTTRTSGame::GetTeams() const
|
std::vector<Player> CTTRTSGame::GetPlayers() const
|
||||||
{
|
{
|
||||||
std::vector<Team> teams;
|
std::vector<Player> players;
|
||||||
teams.reserve(GetNumUnits());
|
players.reserve(GetNumUnits());
|
||||||
|
|
||||||
// Grab all teams
|
// Grab all players
|
||||||
for ( const SOrderUnitPair & pair : m_OrderUnitPairs )
|
for ( const SOrderUnitPair & pair : m_OrderUnitPairs )
|
||||||
{
|
{
|
||||||
teams.push_back(pair.unit.GetTeam());
|
players.push_back(pair.unit.GetPlayer());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove dupes
|
// Remove dupes
|
||||||
std::sort( teams.begin(), teams.end() );
|
std::sort( players.begin(), players.end() );
|
||||||
teams.erase( std::unique( teams.begin(), teams.end() ), teams.end() );
|
players.erase( std::unique( players.begin(), players.end() ), players.end() );
|
||||||
|
|
||||||
return teams;
|
return players;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if we have a win state
|
// Check if we have a win state
|
||||||
Team CTTRTSGame::CheckForWin() const
|
Player CTTRTSGame::CheckForWin() const
|
||||||
{
|
{
|
||||||
// Array of units for each Team
|
// Array of units for each Player
|
||||||
unsigned int units[(int) Team::NUM_INVALID];
|
unsigned int units[(int) Player::NUM_INVALID];
|
||||||
memset(units,0,sizeof(units));
|
memset(units,0,sizeof(units));
|
||||||
|
|
||||||
// Count up all the units for each Team
|
// Count up all the units for each Player
|
||||||
for ( const SOrderUnitPair & pair : m_OrderUnitPairs )
|
for ( const SOrderUnitPair & pair : m_OrderUnitPairs )
|
||||||
{
|
{
|
||||||
const int team = (int) pair.unit.GetTeam();
|
const int player = (int) pair.unit.GetPlayer();
|
||||||
units[team] += 1;
|
units[player] += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Default winning Team to invalid (no win)
|
// Default winning Player to invalid (no win)
|
||||||
Team winningTeam = Team::NUM_INVALID;
|
Player winningPlayer = Player::NUM_INVALID;
|
||||||
|
|
||||||
// For each of the teams
|
// For each of the players
|
||||||
for ( unsigned int i = 0; i < _countof(units); i++ )
|
for ( unsigned int i = 0; i < _countof(units); i++ )
|
||||||
{
|
{
|
||||||
// if there are still units in this Team, and the winning Team hasn't been set
|
// if there are still units in this Player, and the winning Player hasn't been set
|
||||||
if( units[i] > 0 && winningTeam == Team::NUM_INVALID )
|
if( units[i] > 0 && winningPlayer == Player::NUM_INVALID )
|
||||||
{
|
{
|
||||||
winningTeam = (Team)i;
|
winningPlayer = (Player)i;
|
||||||
}
|
}
|
||||||
// Otherwise, if there are units in this Team and the winning Team HAS been set
|
// Otherwise, if there are units in this Player and the winning Player HAS been set
|
||||||
else if ( units[i] > 0 )
|
else if ( units[i] > 0 )
|
||||||
{
|
{
|
||||||
// Set back to invalid and break out of the loop
|
// Set back to invalid and break out of the loop
|
||||||
winningTeam = Team::NUM_INVALID;
|
winningPlayer = Player::NUM_INVALID;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return winningTeam;
|
return winningPlayer;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the game information as a string
|
// Get the game information as a string
|
||||||
|
|
|
@ -33,15 +33,15 @@ public:
|
||||||
// Check for a win, returns invalid for no win state reached
|
// Check for a win, returns invalid for no win state reached
|
||||||
// Note: this function will return invalid a draw was reached
|
// Note: this function will return invalid a draw was reached
|
||||||
// best practice would be to call with GetNumUnits() == 0
|
// best practice would be to call with GetNumUnits() == 0
|
||||||
Team CheckForWin() const;
|
Player CheckForWin() const;
|
||||||
|
|
||||||
// Get the game information as a string
|
// Get the game information as a string
|
||||||
std::string GetStateAsString() const;
|
std::string GetStateAsString() const;
|
||||||
|
|
||||||
// Issue orders to the game, returns non-zero if orders are incorrect
|
// Issue orders to the game, returns non-zero if orders are incorrect
|
||||||
int IssueOrders( Team team, const std::string& orders );
|
int IssueOrders( Player player, const std::string& orders );
|
||||||
int IssueOrders( Team team, const COrderVector& orders );
|
int IssueOrders( Player player, const COrderVector& orders );
|
||||||
int IssueOrder( Team team, const SOrder & order );
|
int IssueOrder( Player player, const SOrder & order );
|
||||||
|
|
||||||
// Add a units to the game, nonzero return value indicates error
|
// Add a units to the game, nonzero return value indicates error
|
||||||
int AddUnit( CUnit&& unit );
|
int AddUnit( CUnit&& unit );
|
||||||
|
@ -70,8 +70,8 @@ public:
|
||||||
inline int SetTurn( int in ) { return (turn = in); }
|
inline int SetTurn( int in ) { return (turn = in); }
|
||||||
inline int GetTurn() const { return turn; }
|
inline int GetTurn() const { return turn; }
|
||||||
|
|
||||||
// Get a vector of the teams in the current game
|
// Get a vector of the players in the current game
|
||||||
std::vector<Team> GetTeams() const;
|
std::vector<Player> GetPlayers() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
|
@ -79,7 +79,7 @@ private:
|
||||||
static bool CheckForPassThrough( const CUnit& one, const CUnit& two );
|
static bool CheckForPassThrough( const CUnit& one, const CUnit& two );
|
||||||
|
|
||||||
// Verify any order or position - non-zero is error
|
// Verify any order or position - non-zero is error
|
||||||
int VerifyOrder( Team team, const SOrder & order ) const;
|
int VerifyOrder( Player player, const SOrder & order ) const;
|
||||||
int VerifyPos( uvector2 vec ) const;
|
int VerifyPos( uvector2 vec ) const;
|
||||||
|
|
||||||
// Get a units new position after an order
|
// Get a units new position after an order
|
||||||
|
|
|
@ -3,8 +3,8 @@
|
||||||
|
|
||||||
#include <limits> // std::numeric_limits
|
#include <limits> // std::numeric_limits
|
||||||
|
|
||||||
// Type for a team IDs
|
// Type for a Player IDs
|
||||||
enum class Team : char
|
enum class Player : char
|
||||||
{
|
{
|
||||||
Red = 0,
|
Red = 0,
|
||||||
Green,
|
Green,
|
||||||
|
|
|
@ -51,7 +51,6 @@ CUnit CUnit::GetUnitFromVis( unitvis_c vis )
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get a string descriptor of a unit
|
// Get a string descriptor of a unit
|
||||||
// "U id:[unit_id] team:[team_id] player:[player_id] vis:[unit_vis] dir:[dir] pos:[pos.x],[pos.y]"
|
|
||||||
std::string CUnit::GetStringFromUnit(const CUnit& unit )
|
std::string CUnit::GetStringFromUnit(const CUnit& unit )
|
||||||
{
|
{
|
||||||
static char buff[128];
|
static char buff[128];
|
||||||
|
@ -59,7 +58,7 @@ std::string CUnit::GetStringFromUnit(const CUnit& unit )
|
||||||
|
|
||||||
snprintf(buff,128, UNIT_FORMATTER,
|
snprintf(buff,128, UNIT_FORMATTER,
|
||||||
unit.unit_id,
|
unit.unit_id,
|
||||||
(int)unit.team_id,
|
(int)unit.player_id,
|
||||||
unit.unit_vis,
|
unit.unit_vis,
|
||||||
unit.dir,
|
unit.dir,
|
||||||
unit.pos.x,
|
unit.pos.x,
|
||||||
|
@ -69,13 +68,12 @@ std::string CUnit::GetStringFromUnit(const CUnit& unit )
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get a unit from a string descriptor
|
// Get a unit from a string descriptor
|
||||||
// "U id:[unit_id] team:[team_id] player:[player_id] vis:[unit_vis] dir:[dir] pos:[pos.x],[pos.y]"
|
|
||||||
CUnit CUnit::GetUnitFromString(const std::string& unit )
|
CUnit CUnit::GetUnitFromString(const std::string& unit )
|
||||||
{
|
{
|
||||||
CUnit ret;
|
CUnit ret;
|
||||||
|
|
||||||
unsigned int id;
|
unsigned int id;
|
||||||
int team;
|
int player;
|
||||||
char vis;
|
char vis;
|
||||||
char dir;
|
char dir;
|
||||||
unsigned int posx;
|
unsigned int posx;
|
||||||
|
@ -83,14 +81,14 @@ CUnit CUnit::GetUnitFromString(const std::string& unit )
|
||||||
|
|
||||||
sscanf(unit.c_str(), UNIT_FORMATTER,
|
sscanf(unit.c_str(), UNIT_FORMATTER,
|
||||||
&id,
|
&id,
|
||||||
&team,
|
&player,
|
||||||
&vis,
|
&vis,
|
||||||
&dir,
|
&dir,
|
||||||
&posx,
|
&posx,
|
||||||
&posy );
|
&posy );
|
||||||
|
|
||||||
ret.unit_id = (unit_id_t)id;
|
ret.unit_id = (unit_id_t)id;
|
||||||
ret.team_id = (Team)team;
|
ret.player_id = (Player) player;
|
||||||
ret.unit_vis = (unitvis_c)vis;
|
ret.unit_vis = (unitvis_c)vis;
|
||||||
ret.dir = (dir_t)dir;
|
ret.dir = (dir_t)dir;
|
||||||
ret.pos = uvector2(posx,posy);
|
ret.pos = uvector2(posx,posy);
|
||||||
|
@ -101,7 +99,7 @@ CUnit CUnit::GetUnitFromString(const std::string& unit )
|
||||||
// Plain constructor
|
// Plain constructor
|
||||||
CUnit::CUnit()
|
CUnit::CUnit()
|
||||||
: unit_id ( get_unique_unit_id() )
|
: unit_id ( get_unique_unit_id() )
|
||||||
, team_id ( Team::NUM_INVALID )
|
, player_id ( Player::NUM_INVALID )
|
||||||
, unit_vis (unitvis_invalid)
|
, unit_vis (unitvis_invalid)
|
||||||
, dir ( dir_t::S )
|
, dir ( dir_t::S )
|
||||||
, pos ( { ucoord_invalid, ucoord_invalid } )
|
, pos ( { ucoord_invalid, ucoord_invalid } )
|
||||||
|
@ -112,7 +110,7 @@ CUnit::CUnit()
|
||||||
// Move constructor
|
// Move constructor
|
||||||
CUnit::CUnit(CUnit&& unit)
|
CUnit::CUnit(CUnit&& unit)
|
||||||
: unit_id ( std::move(unit.unit_id) )
|
: unit_id ( std::move(unit.unit_id) )
|
||||||
, team_id ( std::move(unit.team_id) )
|
, player_id ( std::move(unit.player_id) )
|
||||||
, unit_vis ( std::move(unit.unit_vis) )
|
, unit_vis ( std::move(unit.unit_vis) )
|
||||||
, dir ( std::move(unit.dir) )
|
, dir ( std::move(unit.dir) )
|
||||||
, pos ( std::move(unit.pos) )
|
, pos ( std::move(unit.pos) )
|
||||||
|
@ -125,7 +123,7 @@ CUnit::CUnit(CUnit&& unit)
|
||||||
CUnit& CUnit::operator=(CUnit&& unit)
|
CUnit& CUnit::operator=(CUnit&& unit)
|
||||||
{
|
{
|
||||||
unit_id = std::move(unit.unit_id) ;
|
unit_id = std::move(unit.unit_id) ;
|
||||||
team_id = std::move(unit.team_id) ;
|
player_id = std::move(unit.player_id) ;
|
||||||
unit_vis = std::move(unit.unit_vis) ;
|
unit_vis = std::move(unit.unit_vis) ;
|
||||||
dir = std::move(unit.dir) ;
|
dir = std::move(unit.dir) ;
|
||||||
pos = std::move(unit.pos) ;
|
pos = std::move(unit.pos) ;
|
||||||
|
@ -136,7 +134,7 @@ CUnit& CUnit::operator=(CUnit&& unit)
|
||||||
bool CUnit::operator==(const CUnit& rhs)
|
bool CUnit::operator==(const CUnit& rhs)
|
||||||
{
|
{
|
||||||
return (unit_id == rhs.unit_id)
|
return (unit_id == rhs.unit_id)
|
||||||
&& (team_id == rhs.team_id)
|
&& (player_id == rhs.player_id)
|
||||||
&& (unit_vis == rhs.unit_vis)
|
&& (unit_vis == rhs.unit_vis)
|
||||||
&& (dir == rhs.dir)
|
&& (dir == rhs.dir)
|
||||||
&& (pos == rhs.pos);
|
&& (pos == rhs.pos);
|
||||||
|
|
|
@ -7,7 +7,7 @@
|
||||||
#include "gametypes.h"
|
#include "gametypes.h"
|
||||||
#include "vector2.h"
|
#include "vector2.h"
|
||||||
|
|
||||||
#define UNIT_FORMATTER "UNIT:%u tm:%u vs:%c dr:%c ps:[%u,%u]"
|
#define UNIT_FORMATTER "UNIT:%u pl:%u vs:%c dr:%c ps:[%u,%u]"
|
||||||
|
|
||||||
// force a reset of the unit ID value
|
// force a reset of the unit ID value
|
||||||
void __forceResetCUnitID();
|
void __forceResetCUnitID();
|
||||||
|
@ -36,13 +36,13 @@ public:
|
||||||
|
|
||||||
// Getters for all the members
|
// Getters for all the members
|
||||||
inline const unit_id_t& GetID() const { return unit_id; }
|
inline const unit_id_t& GetID() const { return unit_id; }
|
||||||
inline const Team & GetTeam() const { return team_id; }
|
inline const Player & GetPlayer() const { return player_id; }
|
||||||
inline const unitvis_c & GetVisual() const { return unit_vis; }
|
inline const unitvis_c & GetVisual() const { return unit_vis; }
|
||||||
inline const dir_t& GetDir() const { return dir; }
|
inline const dir_t& GetDir() const { return dir; }
|
||||||
inline const uvector2& GetPos() const { return pos; }
|
inline const uvector2& GetPos() const { return pos; }
|
||||||
|
|
||||||
// Set
|
// Set
|
||||||
inline Team SetTeam(const Team &v) { return (team_id = v); }
|
inline Player SetPlayer(const Player &v) { return (player_id = v); }
|
||||||
inline unitvis_c SetVisual(const unitvis_c &v) { return (unit_vis = v); }
|
inline unitvis_c SetVisual(const unitvis_c &v) { return (unit_vis = v); }
|
||||||
inline dir_t SetDir(const dir_t &v) { return (dir = v); }
|
inline dir_t SetDir(const dir_t &v) { return (dir = v); }
|
||||||
inline void SetPos(const uvector2 &v) { pos = v; }
|
inline void SetPos(const uvector2 &v) { pos = v; }
|
||||||
|
@ -72,8 +72,8 @@ private:
|
||||||
// Visual
|
// Visual
|
||||||
unitvis_c unit_vis;
|
unitvis_c unit_vis;
|
||||||
|
|
||||||
// Team ID
|
// Player ID
|
||||||
Team team_id;
|
Player player_id;
|
||||||
|
|
||||||
// Direction
|
// Direction
|
||||||
dir_t dir;
|
dir_t dir;
|
||||||
|
@ -90,7 +90,7 @@ typedef std::vector< unit_id_t > CUnitIDVector;
|
||||||
inline bool CUnit::Valid() const
|
inline bool CUnit::Valid() const
|
||||||
{
|
{
|
||||||
return (unit_id != unit_id_invalid )
|
return (unit_id != unit_id_invalid )
|
||||||
&& (team_id != Team::NUM_INVALID )
|
&& (player_id != Player::NUM_INVALID )
|
||||||
&& (unit_vis != unitvis_invalid);
|
&& (unit_vis != unitvis_invalid);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,11 +3,11 @@
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
|
||||||
void AddUnitToGame( Team team, char vis, uvector2 vec, CTTRTSGame& game )
|
void AddUnitToGame( Player player, char vis, uvector2 vec, CTTRTSGame& game )
|
||||||
{
|
{
|
||||||
CUnit unit = CUnit::GetUnitFromVis(vis);
|
CUnit unit = CUnit::GetUnitFromVis(vis);
|
||||||
unit.SetPos(vec);
|
unit.SetPos(vec);
|
||||||
unit.SetTeam(team);
|
unit.SetPlayer(player);
|
||||||
game.AddUnit(std::move(unit));
|
game.AddUnit(std::move(unit));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -28,10 +28,10 @@ int main()
|
||||||
CTTRTSGame game(6, 6);
|
CTTRTSGame game(6, 6);
|
||||||
game.SetName("Tiny2v2");
|
game.SetName("Tiny2v2");
|
||||||
|
|
||||||
AddUnitToGame( Team::Red, '<', uvector2(4, 2), game);
|
AddUnitToGame( Player::Red, '<', uvector2(4, 2), game);
|
||||||
AddUnitToGame( Team::Red, '<', uvector2(4, 4), game);
|
AddUnitToGame( Player::Red, '<', uvector2(4, 4), game);
|
||||||
AddUnitToGame( Team::Green, '>', uvector2(1, 1), game);
|
AddUnitToGame( Player::Green, '>', uvector2(1, 1), game);
|
||||||
AddUnitToGame( Team::Green, '>', uvector2(1, 3), game);
|
AddUnitToGame( Player::Green, '>', uvector2(1, 3), game);
|
||||||
|
|
||||||
OutputGame(std::move(game));
|
OutputGame(std::move(game));
|
||||||
}
|
}
|
||||||
|
@ -42,9 +42,9 @@ int main()
|
||||||
game.SetName("Big2v2");
|
game.SetName("Big2v2");
|
||||||
|
|
||||||
for ( ucoord_t y : { 2,4,6,8,10 } )
|
for ( ucoord_t y : { 2,4,6,8,10 } )
|
||||||
AddUnitToGame( Team::Red, '<', uvector2(18, y), game);
|
AddUnitToGame( Player::Red, '<', uvector2(18, y), game);
|
||||||
for ( ucoord_t y : { 1,3,5,7,9 } )
|
for ( ucoord_t y : { 1,3,5,7,9 } )
|
||||||
AddUnitToGame( Team::Green, '>', uvector2(1, y), game);
|
AddUnitToGame( Player::Green, '>', uvector2(1, y), game);
|
||||||
|
|
||||||
|
|
||||||
OutputGame(std::move(game));
|
OutputGame(std::move(game));
|
||||||
|
@ -56,13 +56,13 @@ int main()
|
||||||
game.SetName("Chess");
|
game.SetName("Chess");
|
||||||
|
|
||||||
for ( ucoord_t y : { 1,3,5,7 } ) {
|
for ( ucoord_t y : { 1,3,5,7 } ) {
|
||||||
AddUnitToGame(Team::Red, '<', uvector2(6, y), game);
|
AddUnitToGame(Player::Red, '<', uvector2(6, y), game);
|
||||||
AddUnitToGame(Team::Red, '<', uvector2(7, y), game);
|
AddUnitToGame(Player::Red, '<', uvector2(7, y), game);
|
||||||
}
|
}
|
||||||
|
|
||||||
for ( ucoord_t y : { 0,2,4,6 } ) {
|
for ( ucoord_t y : { 0,2,4,6 } ) {
|
||||||
AddUnitToGame(Team::Green, '>', uvector2(0, y), game);
|
AddUnitToGame(Player::Green, '>', uvector2(0, y), game);
|
||||||
AddUnitToGame(Team::Green, '>', uvector2(1, y), game);
|
AddUnitToGame(Player::Green, '>', uvector2(1, y), game);
|
||||||
}
|
}
|
||||||
|
|
||||||
OutputGame(std::move(game));
|
OutputGame(std::move(game));
|
||||||
|
|
|
@ -36,7 +36,7 @@ const char* tests()
|
||||||
{
|
{
|
||||||
CUnit unit1;
|
CUnit unit1;
|
||||||
unit1.SetFromVisual('v');
|
unit1.SetFromVisual('v');
|
||||||
unit1.SetTeam(Team::Green);
|
unit1.SetPlayer(Player::Green);
|
||||||
unit1.SetPos(uvector2(5, 10));
|
unit1.SetPos(uvector2(5, 10));
|
||||||
|
|
||||||
std::string unit1Desc = CUnit::GetStringFromUnit(unit1);
|
std::string unit1Desc = CUnit::GetStringFromUnit(unit1);
|
||||||
|
@ -82,7 +82,7 @@ const char* tests()
|
||||||
{
|
{
|
||||||
CUnit unit = CUnit::GetUnitFromVis('^');
|
CUnit unit = CUnit::GetUnitFromVis('^');
|
||||||
unit.SetPos({2, 2});
|
unit.SetPos({2, 2});
|
||||||
unit.SetTeam(Team::Red);
|
unit.SetPlayer(Player::Red);
|
||||||
|
|
||||||
game.AddUnit(std::move(unit));
|
game.AddUnit(std::move(unit));
|
||||||
}
|
}
|
||||||
|
@ -90,7 +90,7 @@ const char* tests()
|
||||||
{
|
{
|
||||||
CUnit unit = CUnit::GetUnitFromVis('^');
|
CUnit unit = CUnit::GetUnitFromVis('^');
|
||||||
unit.SetPos({2, 2});
|
unit.SetPos({2, 2});
|
||||||
unit.SetTeam(Team::Red);
|
unit.SetPlayer(Player::Red);
|
||||||
|
|
||||||
if( !game.AddUnit(std::move(unit)) )
|
if( !game.AddUnit(std::move(unit)) )
|
||||||
return "Game should have rejected unit placed on the same spot";
|
return "Game should have rejected unit placed on the same spot";
|
||||||
|
@ -109,7 +109,7 @@ const char* tests()
|
||||||
SOrder order;
|
SOrder order;
|
||||||
|
|
||||||
unit.SetPos({2, 2});
|
unit.SetPos({2, 2});
|
||||||
unit.SetTeam(Team::Red);
|
unit.SetPlayer(Player::Red);
|
||||||
|
|
||||||
if ( game.AddUnit(std::move(unit)) )
|
if ( game.AddUnit(std::move(unit)) )
|
||||||
return "Game failed to add valid unit";
|
return "Game failed to add valid unit";
|
||||||
|
@ -117,7 +117,7 @@ const char* tests()
|
||||||
order.unit = id;
|
order.unit = id;
|
||||||
order.command = command_c::F;
|
order.command = command_c::F;
|
||||||
|
|
||||||
if( game.IssueOrder(Team::Red,order) )
|
if( game.IssueOrder(Player::Red,order) )
|
||||||
return "Game failed to issue valid order";
|
return "Game failed to issue valid order";
|
||||||
|
|
||||||
if (game.SimulateToNextTurn() )
|
if (game.SimulateToNextTurn() )
|
||||||
|
@ -140,7 +140,7 @@ const char* tests()
|
||||||
SOrder order;
|
SOrder order;
|
||||||
|
|
||||||
unit.SetPos({0, 0});
|
unit.SetPos({0, 0});
|
||||||
unit.SetTeam(Team::Blue);
|
unit.SetPlayer(Player::Blue);
|
||||||
|
|
||||||
if ( game.AddUnit(std::move(unit)) )
|
if ( game.AddUnit(std::move(unit)) )
|
||||||
return "Game failed to add valid unit";
|
return "Game failed to add valid unit";
|
||||||
|
@ -148,14 +148,14 @@ const char* tests()
|
||||||
order.unit = id;
|
order.unit = id;
|
||||||
order.command = command_c::A;
|
order.command = command_c::A;
|
||||||
|
|
||||||
if( game.IssueOrder(Team::Blue,order) )
|
if( game.IssueOrder(Player::Blue,order) )
|
||||||
return "Game failed to issue valid order";
|
return "Game failed to issue valid order";
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
CUnit unit = CUnit::GetUnitFromVis('<');
|
CUnit unit = CUnit::GetUnitFromVis('<');
|
||||||
|
|
||||||
unit.SetPos({1, 0});
|
unit.SetPos({1, 0});
|
||||||
unit.SetTeam(Team::Red);
|
unit.SetPlayer(Player::Red);
|
||||||
|
|
||||||
if ( game.AddUnit(std::move(unit)) )
|
if ( game.AddUnit(std::move(unit)) )
|
||||||
return "Game failed to add valid unit";
|
return "Game failed to add valid unit";
|
||||||
|
@ -172,8 +172,8 @@ const char* tests()
|
||||||
if (game.GetUnitByIndex(0).GetID() != id )
|
if (game.GetUnitByIndex(0).GetID() != id )
|
||||||
return "Game killed the wrong unit";
|
return "Game killed the wrong unit";
|
||||||
|
|
||||||
if ( game.CheckForWin() != Team::Blue )
|
if ( game.CheckForWin() != Player::Blue )
|
||||||
return "Game failed to recognise a win for the right Team";
|
return "Game failed to recognise a win for the right Player";
|
||||||
|
|
||||||
std::string game_string = game.GetStateAsString();
|
std::string game_string = game.GetStateAsString();
|
||||||
CTTRTSGame game2 = CTTRTSGame::CreateFromString(game_string);
|
CTTRTSGame game2 = CTTRTSGame::CreateFromString(game_string);
|
||||||
|
|
|
@ -47,7 +47,7 @@
|
||||||
|
|
||||||
## ORDER FILE FORMAT
|
## ORDER FILE FORMAT
|
||||||
### Name
|
### Name
|
||||||
Turn_{TURN_NUMBER}_Team_{TEAM_NUMBER}.txt
|
Player_{TEAM_NUMBER}_Turn_{TURN_NUMBER}.txt
|
||||||
### Contents
|
### Contents
|
||||||
ORDER:{ORDER_CHAR} id:{UNIT_ID}
|
ORDER:{ORDER_CHAR} id:{UNIT_ID}
|
||||||
... {continue for all orders}
|
... {continue for all orders}
|
||||||
|
|
|
@ -96,8 +96,8 @@ int main(int argc, char* argv[])
|
||||||
// Create the game
|
// Create the game
|
||||||
CTTRTSGame game = CTTRTSGame::CreateFromString(gameDescriptor);
|
CTTRTSGame game = CTTRTSGame::CreateFromString(gameDescriptor);
|
||||||
|
|
||||||
// Grab the teams involved
|
// Grab the players involved
|
||||||
auto teams = game.GetTeams();
|
auto players = game.GetPlayers();
|
||||||
|
|
||||||
// Current game directory
|
// Current game directory
|
||||||
std::string gameDir = "ttrts_" + game.GetName();
|
std::string gameDir = "ttrts_" + game.GetName();
|
||||||
|
@ -134,8 +134,8 @@ int main(int argc, char* argv[])
|
||||||
system(cmd1);
|
system(cmd1);
|
||||||
|
|
||||||
// While the game hasn't been won
|
// While the game hasn't been won
|
||||||
Team winningTeam;
|
Player winningPlayer;
|
||||||
while ( ((winningTeam = game.CheckForWin()) == Team::NUM_INVALID) // We have a winning team
|
while ( ((winningPlayer = game.CheckForWin()) == Player::NUM_INVALID) // We have a winning player
|
||||||
&& game.GetNumUnits() > 0 ) // We have no units left
|
&& game.GetNumUnits() > 0 ) // We have no units left
|
||||||
{
|
{
|
||||||
std::cout<<"Starting turn "<<game.GetTurn()<<std::endl;
|
std::cout<<"Starting turn "<<game.GetTurn()<<std::endl;
|
||||||
|
@ -148,22 +148,22 @@ int main(int argc, char* argv[])
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wait for order files
|
// Wait for order files
|
||||||
for( Team team : teams )
|
for( Player player : players)
|
||||||
{
|
{
|
||||||
// Construct the team order filename
|
// Construct the player order filename
|
||||||
char teamOrderFileName[128];
|
char playerOrderFileName[128];
|
||||||
snprintf(teamOrderFileName, 128, "%s/Turn_%i_Team_%i.txt", gameDir.c_str(), game.GetTurn(), (int) team);
|
snprintf(playerOrderFileName, 128, "%s/Player_%i_Turn_%i.txt", gameDir.c_str(), game.GetTurn(), (int) player);
|
||||||
|
|
||||||
// Wait for the team order file to be created
|
// Wait for the player order file to be created
|
||||||
std::cout<<"Waiting for "<<teamOrderFileName<<std::endl;
|
std::cout<<"Waiting for "<< playerOrderFileName <<std::endl;
|
||||||
bool hasOrderFile = false;
|
bool hasOrderFile = false;
|
||||||
while(!hasOrderFile)
|
while(!hasOrderFile)
|
||||||
{
|
{
|
||||||
WaitForFile(teamOrderFileName,sk_waitTime); // Wait for the file
|
WaitForFile(playerOrderFileName,sk_waitTime); // Wait for the file
|
||||||
|
|
||||||
// File must have END
|
// File must have END
|
||||||
// Method taken from http://stackoverflow.com/questions/11876290/c-fastest-way-to-read-only-last-line-of-text-file
|
// Method taken from http://stackoverflow.com/questions/11876290/c-fastest-way-to-read-only-last-line-of-text-file
|
||||||
std::ifstream turnFile(teamOrderFileName);
|
std::ifstream turnFile(playerOrderFileName);
|
||||||
turnFile.seekg(-1,std::ios_base::end);
|
turnFile.seekg(-1,std::ios_base::end);
|
||||||
|
|
||||||
// Loop back from the end of file
|
// Loop back from the end of file
|
||||||
|
@ -191,7 +191,7 @@ int main(int argc, char* argv[])
|
||||||
hasOrderFile = true;
|
hasOrderFile = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::ifstream turnFile(teamOrderFileName);
|
std::ifstream turnFile(playerOrderFileName);
|
||||||
|
|
||||||
// Reserve the full order string
|
// Reserve the full order string
|
||||||
std::string orders;
|
std::string orders;
|
||||||
|
@ -203,8 +203,8 @@ int main(int argc, char* argv[])
|
||||||
orders.assign((std::istreambuf_iterator<char>(turnFile)),std::istreambuf_iterator<char>());
|
orders.assign((std::istreambuf_iterator<char>(turnFile)),std::istreambuf_iterator<char>());
|
||||||
|
|
||||||
// Issue the orders to the game
|
// Issue the orders to the game
|
||||||
if( game.IssueOrders(team, orders) )
|
if( game.IssueOrders(player, orders) )
|
||||||
std::cerr<<"Warning: Orders for team "<<(int)team<<" failed to correctly parse"<<std::endl;
|
std::cerr<<"Warning: Orders for player "<<(int) player <<" failed to correctly parse"<<std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Simulate turn
|
// Simulate turn
|
||||||
|
@ -221,9 +221,9 @@ int main(int argc, char* argv[])
|
||||||
OutputGameStateFile(game, gameDir);
|
OutputGameStateFile(game, gameDir);
|
||||||
|
|
||||||
// Print the winner!
|
// Print the winner!
|
||||||
if ( winningTeam != Team::NUM_INVALID )
|
if ( winningPlayer != Player::NUM_INVALID )
|
||||||
{
|
{
|
||||||
std::cout<<"Game over! Winner:"<<(int)winningTeam<<std::endl;
|
std::cout<<"Game over! Winner:"<<(int) winningPlayer <<std::endl;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
Loading…
Add table
Reference in a new issue