-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmealschedule.cpp
97 lines (82 loc) · 2.38 KB
/
mealschedule.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
89
90
91
92
93
94
95
96
#include "mealschedule.hpp"
#include <cmath>
#include <algorithm>
using namespace std;
const string MealSchedule::days[] = {"Sat", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri"};
MealSchedule::MealSchedule() {
meals = vector<Meal>(NUM_MEALS);
numLeftToFill = NUM_MEALS;
}
bool MealSchedule::canBeAdded(Recipe& recipe, short addToDay)
{
//If it can be added to any meal, return true; otherwise false
short i = 0;
for ( vector<Meal>::iterator meal = meals.begin();
meal < meals.end(); ++meal, ++i)
{
if (addToDay != -1 && addToDay != i) continue; //if it's not a day that the meal
//can be added to
if (meal->canBeAdded(recipe)) return true;
}
return false;
}
void MealSchedule::add(Recipe& recipe, short addToDay) {
assert(!isDone());
unsigned mealsLeftToEatIt = recipe.yield;
if (mealsLeftToEatIt > MAX_MEALS_PER_RECIPE)
mealsLeftToEatIt = MAX_MEALS_PER_RECIPE;
if (mealsLeftToEatIt == 0) //unknown yield according to the recipe
//(I leave it blank if I don't know how many it yields)
mealsLeftToEatIt = MAX_MEALS_PER_RECIPE;
//We want to add them in a random order, so I don't have the same thing many days in a row
int numLeft = NUM_MEALS; //Number of meals left to try
vector<int> mealsDone;
while (numLeft-- > 0)
{
int i;
do {
i = ranInt(NUM_MEALS) - 1;
} while (find(mealsDone.begin(), mealsDone.end(), i) != mealsDone.end());
mealsDone.push_back(i);
if (addToDay != -1 && addToDay != i) continue;
if (!meals[i].isFull()) //Without this, things like rice
//would keep getting added to the first meal.
{
if (meals[i].add(recipe))
{
if (meals[i].isFull()) --numLeftToFill;
if (--mealsLeftToEatIt == 0)
return;
}
}
}
}
void MealSchedule::out(short day)
{
short i = 0;
for (vector<Meal>::iterator meal = meals.begin();
meal < meals.end(); ++meal, ++i)
{
if (i == day)
{
meal->out();
--numLeftToFill;
//Take all the recipes already in the meal and put them elsewhere in the schedule
vector<Recipe>& r = meal->recipes;
for (vector<Recipe>::iterator recipe = r.begin(); recipe < r.end(); ++recipe)
{
add(*recipe);
}
return;
}
}
}
ostream& operator<<(ostream& os, MealSchedule& schedule)
{
for (unsigned i = 0; i < MealSchedule::NUM_MEALS; ++i)
{
//output the day of the week, with 0 -> Sat and 6 -> Fri
os << MealSchedule::days[i] << ": " << schedule.meals[i] << "\n";
}
return os;
}