Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

CSC112 Spring 2016 Examples

2369 views
1
#include <iostream>
2
#include <unistd.h>
3
#include "fracas.h"
4
#include "alpaca.h"
5
#include "alpacaFracasPack.h"
6
#include "termmanip.h"
7
8
#define DELAY 500000
9
#define FRACAS_LINE 13
10
11
using namespace std;
12
13
Fracas::Fracas(AlpacaFracasPack *p1, AlpacaFracasPack *p2)
14
{
15
//load the packs!
16
this->p1 = p1;
17
this->p2 = p2;
18
}
19
20
21
//run the fracas and return the winner!
22
AlpacaFracasPack *
23
Fracas::go()
24
{
25
Alpaca *a1;
26
Alpaca *a2;
27
AlpacaFracasPack *winner;
28
29
//unload the first two combatiants
30
a1=p1->unload();
31
a2=p2->unload();
32
cout << cursorPosition(1, FRACAS_LINE+1);
33
34
//let the fracas commence!
35
while(a1 != NULL and a2 != NULL) {
36
clearStatusArea();
37
printAlpaca(1, p1, a1);
38
printAlpaca(41, p2, a2);
39
cout.flush();
40
usleep(DELAY);
41
42
//a1 goes first
43
a1->performRound(a2);
44
//update the display
45
clearStatusArea();
46
printAlpaca(1, p1, a1);
47
printAlpaca(41, p2, a2);
48
cout.flush();
49
if(a2->getHp() <= 0 || a2->getPower() == 0) {
50
cout << a2->name() << " dies." << endl;
51
a2 = p2->unload();
52
if(a2==NULL) continue;
53
cout << a2->name() << " comes out." << endl;
54
}
55
56
57
//a2 goes second
58
a2->performRound(a1);
59
//update the display
60
clearStatusArea();
61
printAlpaca(1, p1, a1);
62
printAlpaca(41, p2, a2);
63
cout.flush();
64
if(a1->getHp() <= 0 || a1->getPower()==0) {
65
cout << a1->name() << " dies." << endl;
66
a1 = p1->unload();
67
if(a1==NULL) continue;
68
cout << a1->name() << " comes out." << endl;
69
}
70
}
71
72
//detect the winner
73
if(a1!=NULL) {
74
winner = p1;
75
} else {
76
winner = p2;
77
}
78
79
//print the winner
80
cout << cursorPosition(30, FRACAS_LINE-3) << "** " << winner->name() << " wins! **";
81
cout.flush();
82
sleep(5);
83
84
return winner;
85
}
86
87
88
//display the alpaca at the given x coordinate
89
void
90
Fracas::printAlpaca(int x, AlpacaFracasPack *p, Alpaca *a)
91
{
92
int y=1;
93
94
cout << saveCursor;
95
96
//print the pack information
97
cout << cursorPosition(x,y) << p->name() << " " << p->packSize() << " remaining alpacas";
98
y+=2;
99
100
101
//print the alpaca information in a nice pack-like way
102
cout << cursorPosition(x, y) << a->name();
103
y++;
104
cout << cursorPosition(x+2,y) << "HP: " << cursorPosition(x+12, y) << a->getHp();
105
y++;
106
cout << cursorPosition(x+2,y) << "Power: " << cursorPosition(x+12, y) << a->getPower();
107
y++;
108
cout << cursorPosition(x+2,y) << "Attack: " << cursorPosition(x+12, y) << a->getAttack();
109
y++;
110
cout << cursorPosition(x+2, y) << "Defense: " << cursorPosition(x+12, y) << a->getDefense();
111
y++;
112
cout << cursorPosition(x+2, y) << "Status: " << cursorPosition(x+12, y);
113
switch(a->getStatus()) {
114
case NONE:
115
cout << "Normal";
116
break;
117
case ASLEEP:
118
cout << "Asleep";
119
break;
120
case STUNNED:
121
cout << "Stunned";
122
break;
123
}
124
125
cout << restoreCursor;
126
}
127
128
129
void
130
Fracas::clearStatusArea()
131
{
132
cout << saveCursor;
133
//clear the top FRACAS_LINE lines
134
for(int i=1; i<=FRACAS_LINE; i++) {
135
cout << cursorPosition(1,i) << clearLine;
136
}
137
cout << restoreCursor;
138
}
139