Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

CSC112 Spring 2016 Examples

2370 views
1
#include <iostream>
2
#include <cstdlib>
3
#include <time.h>
4
#include <vector>
5
#include <unistd.h>
6
#include "termmanip.h"
7
#include "cat.h"
8
9
using namespace std;
10
11
int main() {
12
vector <animal *> world; //the stuff that will struggle
13
int n;
14
15
srand(time(0)); //start the random numbers
16
17
//get the number of living things
18
cout << "Welcome to life, where none escape alive!" << endl;
19
cout << "How many living things? ";
20
cin >> n;
21
22
//make that number of living things
23
while(n--) {
24
animal *t;
25
26
//make a new cat
27
t = new cat();
28
29
world.push_back(t);
30
}
31
32
while(true) {
33
animal *eater, *eaten;
34
35
//select to random living things
36
eater = world[rand()%world.size()];
37
eaten = world[rand()%world.size()];
38
39
//they eat!
40
cout << clearScreen << cursorPosition(1,1);
41
cout << eater << " eats " << eaten << endl;
42
eater->eat(eaten);
43
44
for(auto itr = world.begin(); itr != world.end(); itr++) {
45
cout << *itr;
46
(*itr)->print();
47
}
48
49
sleep(1);
50
}
51
}
52
53