// // Created by jovian on 20/08/17. // #include "Stock.h" Stock::Stock() { setZero(); } int Stock::set(Resource res, int val) { int prev(m_tab[res]); m_tab[res] = val; return prev; } int Stock::add(Resource res, int val) { m_tab[res] += val; return m_tab[res]; } int Stock::get(Resource res) const { return m_tab[res]; } void Stock::setZero() { for (int& res : m_tab) { res = 0; } } void Stock::setPositive() { for (int& res : m_tab) { if (res < 0) res = 0; } } Stock &Stock::operator+=(Stock const &stock) { for (int id(0); id < RESOURCE_NUMBER; ++id) { m_tab[id] += stock.m_tab[id]; } return *this; } Stock &Stock::operator-=(Stock const &stock) { for (int id(0); id < RESOURCE_NUMBER; ++id) { m_tab[id] -= stock.m_tab[id]; } return *this; } Stock &Stock::operator*=(int const& multi) { for (int id(0); id < RESOURCE_NUMBER; ++id) { m_tab[id] *= multi; } return *this; } Stock &Stock::operator/=(int const& multi) { for (int id(0); id < RESOURCE_NUMBER; ++id) { m_tab[id] /= multi; } return *this; } Resource Stock::mostResource() const { int highest(0); Resource most(ENERGY); for (int res(0); res < RESOURCE_NUMBER; ++res) { if (m_tab[res] > highest) { highest = m_tab[res]; most = (Resource)res; } } return most; } Stock operator+(Stock const &a, Stock const &b) { Stock copy(a); copy += b; return copy; } Stock operator-(Stock const &a, Stock const &b) { Stock copy(a); copy -= b; return copy; } Stock operator*(Stock const &a, int const &b) { Stock copy(a); copy *= b; return copy; } Stock operator/(Stock const &a, int const &b) { Stock copy(a); copy /= b; return copy; }