-
Notifications
You must be signed in to change notification settings - Fork 0
/
fleet.cc
103 lines (64 loc) · 2.74 KB
/
fleet.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Time-stamp: <2016-03-02 16:45:45 dmendyke>
//
// fleet.cc: Defines a fleet of ships
//
// Required header files
//-----------------------------------------------------------------------------
#include <ostream> // std::ostream
#include "fleet.hh" // parsec::fleet_t
#include "chance.hh" // chance::number::upto
// Namespace shorthands
//-----------------------------------------------------------------------------
using namespace std; // standard library
using namespace parsec; // project NS
// Constructor
//-----------------------------------------------------------------------------
fleet_t::fleet_t( const agent_t& agent )
: agent_( agent ), vector_() {
}; // end constructor
// Copy constructor
//-----------------------------------------------------------------------------
fleet_t::fleet_t( const fleet_t& org )
: agent_( org.agent_ ), vector_( org.vector_ ) {
}; // end constructor
// Destructor
//-----------------------------------------------------------------------------
fleet_t::~fleet_t( ) {
}; // end destructor
// Add another ship to this fleet
//-----------------------------------------------------------------------------
void fleet_t::attach( ship_t& ship ) {
vector_.push_back( ship );
}; // end attach
// Attach a number of new ships to this fleet
//-----------------------------------------------------------------------------
void fleet_t::attach( int count, int tech_level ) {
for ( int iter = 0; iter < count; ++iter )
vector_.push_back( ship_t( tech_level ) );
}; // end attach a number of ships
// destroy a ship attached to this fleet
//-----------------------------------------------------------------------------
void fleet_t::destroy( uint64_t id ) {
auto func = [ id ]( const ship_t& S ){ return S.id() == id; };
auto iter = find_if( vector_.begin(), vector_.end(), func );
if ( iter == vector_.end() )
throw runtime_error( "Requesting ship not in this fleet!" );
vector_.erase( iter ); // remove this ship
}; // end destroy
// Randomize the ships in this fleet
//-----------------------------------------------------------------------------
void fleet_t::shuffle( ) {
chance::number::shuffle( vector_.begin(), vector_.end() );
}; // end shuffle
//-----------------------------------------------------------------------------
const ship_t& fleet_t::operator[]( int index ) const {
static ship_t empty;
if ( index >= vector_.size() ) return empty;
return vector_[ index ];
}; // end operator[]
// Dump the contents of this fleet
//-----------------------------------------------------------------------------
ostream& parsec::operator<<( ostream& out, const fleet_t& fleet ) {
out << fleet.agent_ << ": " << fleet.vector_.size() << " ships";
return out;
}; // end operator<<