2014-12-16 13:12:54 +00:00
|
|
|
#include <iostream> // std::cout
|
|
|
|
|
2014-12-16 13:13:02 +00:00
|
|
|
#include "order.h"
|
2014-12-16 13:13:00 +00:00
|
|
|
#include "game.h"
|
2014-12-16 13:12:57 +00:00
|
|
|
|
2014-12-16 13:13:00 +00:00
|
|
|
const char* tests()
|
2014-12-16 13:12:54 +00:00
|
|
|
{
|
2014-12-16 13:13:02 +00:00
|
|
|
// Test if we can properly set a unit's visual
|
2014-12-16 13:12:56 +00:00
|
|
|
{
|
2014-12-16 13:13:01 +00:00
|
|
|
CUnit unit;
|
2014-12-21 10:44:08 +00:00
|
|
|
unit.SetFromVisual('v');
|
|
|
|
if(unit.GetVisual() != 'v' )
|
2014-12-16 13:13:00 +00:00
|
|
|
return "failed to properly create V unit";
|
2014-12-16 13:12:56 +00:00
|
|
|
}
|
2014-12-16 13:12:55 +00:00
|
|
|
|
2014-12-16 13:13:02 +00:00
|
|
|
// Test unique unit IDs
|
2014-12-16 13:13:00 +00:00
|
|
|
{
|
2014-12-16 13:13:01 +00:00
|
|
|
CUnit unit;
|
|
|
|
CUnit unit2;
|
2014-12-21 10:44:08 +00:00
|
|
|
if(unit.GetID() == unit2.GetID() )
|
2014-12-16 13:13:00 +00:00
|
|
|
return "Unit IDs the same";
|
|
|
|
}
|
|
|
|
|
2014-12-16 13:13:02 +00:00
|
|
|
// Test basic invalid unit conversion
|
|
|
|
{
|
|
|
|
CUnit unit1;
|
|
|
|
|
2014-12-16 13:13:02 +00:00
|
|
|
std::string unit1Desc = CUnit::GetStringFromUnit(unit1);
|
|
|
|
CUnit unit2 = CUnit::GetUnitFromString(unit1Desc);
|
2014-12-16 13:13:02 +00:00
|
|
|
|
|
|
|
if ( unit1 != unit2 )
|
|
|
|
return "Failed to convert an empty unit to string and back";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test custom unit conversion
|
|
|
|
{
|
|
|
|
CUnit unit1;
|
2014-12-21 10:44:08 +00:00
|
|
|
unit1.SetFromVisual('v');
|
2014-12-21 11:04:26 +00:00
|
|
|
unit1.SetPlayer(Player::Green);
|
2014-12-21 10:44:08 +00:00
|
|
|
unit1.SetPos(uvector2(5, 10));
|
2014-12-16 13:13:02 +00:00
|
|
|
|
2014-12-16 13:13:02 +00:00
|
|
|
std::string unit1Desc = CUnit::GetStringFromUnit(unit1);
|
|
|
|
CUnit unit2 = CUnit::GetUnitFromString(unit1Desc);
|
2014-12-16 13:13:02 +00:00
|
|
|
|
|
|
|
if ( unit1 != unit2 )
|
|
|
|
return "Failed to convert custom unit to string and back";
|
|
|
|
}
|
|
|
|
|
2014-12-16 13:13:02 +00:00
|
|
|
// Test if we can successfully create a unit from a visual
|
2014-12-16 13:12:56 +00:00
|
|
|
{
|
2014-12-16 13:13:02 +00:00
|
|
|
CUnit unit = CUnit::GetUnitFromVis('v');
|
2014-12-21 10:44:08 +00:00
|
|
|
if(unit.GetVisual() != 'v' )
|
2014-12-16 13:13:01 +00:00
|
|
|
return "failed to properly create V unit with factory";
|
2014-12-16 13:12:57 +00:00
|
|
|
}
|
|
|
|
|
2014-12-16 13:13:02 +00:00
|
|
|
// Test if we can successfully convert orders back and forth
|
2014-12-16 13:12:57 +00:00
|
|
|
{
|
2014-12-21 10:39:39 +00:00
|
|
|
SOrder order;
|
2014-12-16 13:13:02 +00:00
|
|
|
order.command = command_c::F;
|
2014-12-16 13:12:57 +00:00
|
|
|
order.unit = 10;
|
|
|
|
std::string order_string = GetStringFromOrder(order);
|
2014-12-21 10:39:39 +00:00
|
|
|
SOrder order2 = GetOrderFromString(order_string);
|
2014-12-16 13:12:57 +00:00
|
|
|
|
|
|
|
if ( order2 != order )
|
2014-12-16 13:13:00 +00:00
|
|
|
return "failed order string conversion test";
|
|
|
|
}
|
|
|
|
|
2014-12-16 13:13:02 +00:00
|
|
|
// Test of the game can logically handle a blank game
|
2014-12-16 13:13:00 +00:00
|
|
|
{
|
|
|
|
CTTRTSGame game( 15, 10 );
|
|
|
|
if( game.SimulateToNextTurn() )
|
|
|
|
return "Failed to simulate a blank game";
|
|
|
|
|
2014-12-16 13:13:01 +00:00
|
|
|
if( game.GetNumUnits() )
|
|
|
|
return "Game started with non-zero unit number";
|
2014-12-16 13:13:02 +00:00
|
|
|
}
|
|
|
|
|
2014-12-16 13:13:02 +00:00
|
|
|
// Test if the game correctly rejects units placed ontop of others
|
2014-12-16 13:13:02 +00:00
|
|
|
{
|
|
|
|
CTTRTSGame game( 5, 5 );
|
|
|
|
|
|
|
|
{
|
2014-12-16 13:13:02 +00:00
|
|
|
CUnit unit = CUnit::GetUnitFromVis('^');
|
2014-12-21 10:44:08 +00:00
|
|
|
unit.SetPos({2, 2});
|
2014-12-21 11:04:26 +00:00
|
|
|
unit.SetPlayer(Player::Red);
|
2014-12-16 13:13:02 +00:00
|
|
|
|
|
|
|
game.AddUnit(std::move(unit));
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2014-12-16 13:13:02 +00:00
|
|
|
CUnit unit = CUnit::GetUnitFromVis('^');
|
2014-12-21 10:44:08 +00:00
|
|
|
unit.SetPos({2, 2});
|
2014-12-21 11:04:26 +00:00
|
|
|
unit.SetPlayer(Player::Red);
|
2014-12-16 13:13:02 +00:00
|
|
|
|
|
|
|
if( !game.AddUnit(std::move(unit)) )
|
|
|
|
return "Game should have rejected unit placed on the same spot";
|
|
|
|
|
|
|
|
if( game.GetNumUnits() != 1 )
|
|
|
|
return "Game ended up with too many units";
|
|
|
|
}
|
|
|
|
}
|
2014-12-16 13:13:00 +00:00
|
|
|
|
2014-12-16 13:13:02 +00:00
|
|
|
// Test on a small board if a movement command succeeds correctly
|
2014-12-16 13:13:01 +00:00
|
|
|
{
|
|
|
|
CTTRTSGame game( 5, 5 );
|
|
|
|
|
2014-12-16 13:13:02 +00:00
|
|
|
CUnit unit = CUnit::GetUnitFromVis('>');
|
2014-12-21 10:44:08 +00:00
|
|
|
const unit_id_t id = unit.GetID();
|
2014-12-21 10:39:39 +00:00
|
|
|
SOrder order;
|
2014-12-16 13:13:01 +00:00
|
|
|
|
2014-12-21 10:44:08 +00:00
|
|
|
unit.SetPos({2, 2});
|
2014-12-21 11:04:26 +00:00
|
|
|
unit.SetPlayer(Player::Red);
|
2014-12-16 13:13:01 +00:00
|
|
|
|
2014-12-16 13:13:01 +00:00
|
|
|
if ( game.AddUnit(std::move(unit)) )
|
|
|
|
return "Game failed to add valid unit";
|
2014-12-16 13:13:01 +00:00
|
|
|
|
|
|
|
order.unit = id;
|
2014-12-16 13:13:02 +00:00
|
|
|
order.command = command_c::F;
|
2014-12-16 13:13:01 +00:00
|
|
|
|
2014-12-21 11:04:26 +00:00
|
|
|
if( game.IssueOrder(Player::Red,order) )
|
2014-12-16 13:13:01 +00:00
|
|
|
return "Game failed to issue valid order";
|
2014-12-16 13:13:01 +00:00
|
|
|
|
2014-12-16 13:13:01 +00:00
|
|
|
if (game.SimulateToNextTurn() )
|
|
|
|
return "Game failed to simulate valid turn";
|
2014-12-16 13:13:01 +00:00
|
|
|
|
2014-12-21 10:44:08 +00:00
|
|
|
if(game.GetUnitByIDConst(id).GetPos() != uvector2{3,2} )
|
2014-12-16 13:13:01 +00:00
|
|
|
return "Simple movement order failed";
|
|
|
|
|
2014-12-16 13:13:01 +00:00
|
|
|
}
|
|
|
|
|
2014-12-16 13:13:02 +00:00
|
|
|
// Test on a tiny board, whether a unit can correctly attack another
|
2014-12-16 13:13:01 +00:00
|
|
|
{
|
2014-12-16 13:13:02 +00:00
|
|
|
CTTRTSGame game( 2, 1 );
|
2014-12-16 13:13:02 +00:00
|
|
|
game.SetName("Test_578");
|
2014-12-16 13:13:01 +00:00
|
|
|
|
2014-12-16 13:13:02 +00:00
|
|
|
unit_id_t id;
|
2014-12-16 13:13:01 +00:00
|
|
|
{
|
2014-12-16 13:13:02 +00:00
|
|
|
CUnit unit = CUnit::GetUnitFromVis('>');
|
2014-12-21 10:44:08 +00:00
|
|
|
id = unit.GetID();
|
2014-12-21 10:39:39 +00:00
|
|
|
SOrder order;
|
2014-12-16 13:13:02 +00:00
|
|
|
|
2014-12-21 10:44:08 +00:00
|
|
|
unit.SetPos({0, 0});
|
2014-12-21 11:04:26 +00:00
|
|
|
unit.SetPlayer(Player::Blue);
|
2014-12-16 13:13:01 +00:00
|
|
|
|
2014-12-16 13:13:02 +00:00
|
|
|
if ( game.AddUnit(std::move(unit)) )
|
|
|
|
return "Game failed to add valid unit";
|
|
|
|
|
|
|
|
order.unit = id;
|
2014-12-16 13:13:02 +00:00
|
|
|
order.command = command_c::A;
|
2014-12-16 13:13:01 +00:00
|
|
|
|
2014-12-21 11:04:26 +00:00
|
|
|
if( game.IssueOrder(Player::Blue,order) )
|
2014-12-16 13:13:02 +00:00
|
|
|
return "Game failed to issue valid order";
|
|
|
|
}
|
2014-12-16 13:13:01 +00:00
|
|
|
{
|
2014-12-16 13:13:02 +00:00
|
|
|
CUnit unit = CUnit::GetUnitFromVis('<');
|
2014-12-16 13:13:01 +00:00
|
|
|
|
2014-12-21 10:44:08 +00:00
|
|
|
unit.SetPos({1, 0});
|
2014-12-21 11:04:26 +00:00
|
|
|
unit.SetPlayer(Player::Red);
|
2014-12-16 13:13:01 +00:00
|
|
|
|
2014-12-16 13:13:02 +00:00
|
|
|
if ( game.AddUnit(std::move(unit)) )
|
|
|
|
return "Game failed to add valid unit";
|
2014-12-16 13:13:01 +00:00
|
|
|
}
|
|
|
|
|
2014-12-16 13:13:02 +00:00
|
|
|
game.SimulateToNextTurn();
|
|
|
|
|
|
|
|
if ( game.GetNumUnits() != 1 )
|
|
|
|
return "Game failed to kill a unit when it logically should have";
|
2014-12-16 13:13:01 +00:00
|
|
|
|
2014-12-21 10:44:08 +00:00
|
|
|
if (game.GetUnitByIndex(0).GetDir() != dir_t::E )
|
2014-12-16 13:13:02 +00:00
|
|
|
return "Game killed the wrong unit";
|
|
|
|
|
2014-12-21 10:44:08 +00:00
|
|
|
if (game.GetUnitByIndex(0).GetID() != id )
|
2014-12-16 13:13:02 +00:00
|
|
|
return "Game killed the wrong unit";
|
2014-12-16 13:13:02 +00:00
|
|
|
|
2014-12-21 11:04:26 +00:00
|
|
|
if ( game.CheckForWin() != Player::Blue )
|
|
|
|
return "Game failed to recognise a win for the right Player";
|
2014-12-16 13:13:02 +00:00
|
|
|
|
2014-12-16 13:13:02 +00:00
|
|
|
std::string game_string = game.GetStateAsString();
|
|
|
|
CTTRTSGame game2 = CTTRTSGame::CreateFromString(game_string);
|
|
|
|
|
|
|
|
std::string game2_string = game2.GetStateAsString();
|
|
|
|
|
|
|
|
// Try matching up the game descriptors
|
|
|
|
if( game_string != game2_string )
|
|
|
|
return "Generating new game from string failed";
|
|
|
|
|
2014-12-16 13:13:01 +00:00
|
|
|
}
|
|
|
|
|
2014-12-16 13:13:02 +00:00
|
|
|
return 0;
|
2014-12-16 13:13:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Main program entry point
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
const char* res = tests();
|
|
|
|
|
|
|
|
if( res )
|
|
|
|
{
|
2014-12-16 22:35:56 +00:00
|
|
|
std::cout<<"ERROR: "<<res<<std::endl;
|
2014-12-16 13:13:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2014-12-16 13:13:01 +00:00
|
|
|
};
|