-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhalfmeal.cpp
95 lines (75 loc) · 2.26 KB
/
halfmeal.cpp
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
// halfmeal.cpp
#include <string>
#include <vector>
#include <iostream>
#include "halfmeal.h"
#include "fooddatabase.h"
#include "ingredient.h"
using std::vector;
using std::string;
using std::pair;
using std::cout;
using std::endl;
typedef vector< pair< string,size_t > >::const_iterator vpiter;
typedef vector<string>::const_iterator vsiter;
Halfmeal::Halfmeal(size_t freq) : m_freq(freq), m_foodNames( vector<string>(0) )
{
}
const vector<string>& Halfmeal::getFoodNames() const
{
return m_foodNames;
}
const Ingredient& Halfmeal::getIngred(string type, const FoodDatabase& fdb, const vector< pair< string, size_t > >& used, const vector< pair< string, size_t > >& haveLeft)
{
// Look for an ingredient that we already have in the amount needed.
vector<string> haveEnough(0);
for (vpiter it = haveLeft.begin(); it != haveLeft.end(); ++it)
{
if (it->second >= m_freq && !findIfItemInHalfmeal(it->first) )
{
haveEnough.push_back( it->first );
}
}
if ( !haveEnough.empty() )
{
std::random_shuffle(haveEnough.begin(), haveEnough.end());
for (vsiter it2 = haveEnough.begin(); it2 != haveEnough.end(); ++it2)
{
if ( fdb.checkIngredType(type,*it2) )
{
return fdb.getSpecificIngred(type,*it2);
}
}
}
// If we don't have enough of anything, see if we have just some of something.
vector<string> haveSome(0);
for (vpiter it3 = haveLeft.begin(); it3 != haveLeft.end(); ++it3)
{
if (it3->second < m_freq && !findIfItemInHalfmeal(it3->first))
{
haveSome.push_back( it3->first );
}
}
if ( !haveSome.empty() )
{
std::random_shuffle(haveSome.begin(), haveSome.end());
for (vsiter it4 = haveSome.begin(); it4 != haveSome.end(); ++it4)
{
if ( fdb.checkIngredType(type,*it4) )
{
return fdb.getSpecificIngred(type,*it4);
}
}
}
// If we don't have enough or even some of anything, pick a random ingredient not used in this meal.
return fdb.getRandomIngred(type,m_foodNames);
}
bool Halfmeal::findIfItemInHalfmeal( const string& item ) const
{
for (vsiter it = m_foodNames.begin(); it != m_foodNames.end(); ++it)
{
if (*it == item)
return true;
}
return false;
}