Worms -- OOP Redo  2016-01
Worms-Redo Project as a learning vehicle.
Classes | Public Types | Public Member Functions | Private Types | Private Member Functions | Private Attributes | List of all members
Worm Class Reference

#include <worm.hpp>

Collaboration diagram for Worm:
Collaboration graph
[legend]

Classes

struct  SEGMENT
 

Public Types

enum  WormKind { VEGETARIAN, CANNIBAL, SCISSORHEAD }
 

Public Member Functions

void createWorm (WormKind type, const char *sy)
 

Private Types

enum  LIFE { EATEN, DEAD, ALIVE }
 

Private Member Functions

void checkStomach ()
 
void eat ()
 
void eatACarrot ()
 
void gotEaten ()
 
int isAt (int row, int col)
 
int isHungry ()
 
void live ()
 
void setVictimWorm ()
 
void shiftBodyDown (Worm *vp, int vsn)
 
void showOneWorm ()
 
WormsliceTheVictim (Worm *vp, int vsn)
 

Private Attributes

SEGMENT body [MAXsegs]
 
int direction
 
int nsegs
 
LIFE status
 
int stomach
 
WormKind type
 

Detailed Description

Definition at line 20 of file worm.hpp.

Member Enumeration Documentation

enum Worm::LIFE
private
Enumerator
EATEN 
DEAD 
ALIVE 

Definition at line 31 of file worm.hpp.

31  {
32  EATEN, DEAD, ALIVE
33  } LIFE;
LIFE
Definition: worm.hpp:31
Enumerator
VEGETARIAN 
CANNIBAL 
SCISSORHEAD 

Definition at line 23 of file worm.hpp.

23  {
25  } WormKind; // 0, 1, 2
WormKind
Definition: worm.hpp:23

Member Function Documentation

void Worm::checkStomach ( )
private

Definition at line 51 of file worm.cpp.

References DEAD, showOneWorm(), status, stomach, type, and xworms.

Referenced by live(), shiftBodyDown(), and sliceTheVictim().

52 {
53  if (this->stomach <= 0) {
54  this->status = DEAD;
55  xworms[this->type] --;
56  this->showOneWorm(); // mv the body to passive
57  }
58 }
LIFE status
Definition: worm.hpp:44
int xworms[]
int stomach
Definition: worm.hpp:43
void showOneWorm()
Definition: worm.cpp:15
WormKind type
Definition: worm.hpp:41
void Worm::createWorm ( WormKind  type,
const char *  sy 
)

Definition at line 208 of file worm.cpp.

References ALIVE, asogCols, asogRows, body, Worm::SEGMENT::c, WormInfo::capacity, direction, findSlot(), headSeg, MAXsegs, min, NORTH, nsegs, random, status, stomach, type, worm_info, Worm::SEGMENT::x, xworms, and Worm::SEGMENT::y.

209 {
210  Worm *wp = findSlot();
211  if (wp == 0)
212  return; // no room for a new worm
213 
214  int n = wp->nsegs = min(strlen(sy), MAXsegs - 1);
215  wp->stomach = n * worm_info[type].capacity;
216  wp->direction = NORTH;
217  wp->status = ALIVE;
218  wp->type = type;
219 
220  SEGMENT * bp = wp->body;
221  int yy = bp->y = random(asogRows); // birth place of this worm
222  int xx = bp->x = random(asogCols);
223  bp->c = ' '; // works as an "eraser"
224 
225  SEGMENT * hp = headSeg(wp);
226  for (bp++; bp <= hp; bp++) {
227  bp->c = *sy++; // store the phrase s[]
228  bp->x = xx; // worm hangs down in the z-axis
229  bp->y = yy;
230  }
231  xworms[type] ++;
232 }
int asogRows
Definition: display.cpp:18
#define headSeg(wp)
Definition: worm.cpp:6
Worm * findSlot()
Definition: worms.h:21
SEGMENT body[MAXsegs]
Definition: worm.hpp:46
int nsegs
Definition: worm.hpp:45
int asogCols
Definition: display.cpp:18
int direction
Definition: worm.hpp:42
LIFE status
Definition: worm.hpp:44
int xworms[]
#define MAXsegs
Definition: worm.hpp:17
int stomach
Definition: worm.hpp:43
#define random(x)
Definition: worm.hpp:10
WormInfo worm_info[]
Definition: worm.hpp:71
int capacity
Definition: worm.hpp:65
#define min(a, b)
Definition: worm.hpp:12
Definition: worm.hpp:20
WormKind type
Definition: worm.hpp:41
void Worm::eat ( )
private

Definition at line 155 of file worm.cpp.

References ALIVE, CANNIBAL, eatACarrot(), WormInfo::foodValue, gotEaten(), nsegs, setVictimWorm(), sliceTheVictim(), status, stomach, type, VEGETARIAN, victimSegNum, and worm_info.

Referenced by live().

156 {
157  if (this->type != VEGETARIAN) {
158  this->setVictimWorm();
159  if (victimWormp != 0) {
162  if (this->type == CANNIBAL && zp->status == ALIVE) {
163  this->stomach += zp->nsegs * worm_info[zp->type].foodValue;
164  zp->gotEaten();
165  }
166  }
167  }
168  this->eatACarrot();
169 }
void gotEaten()
Definition: worm.cpp:40
void eatACarrot()
Definition: worm.cpp:63
Worm * sliceTheVictim(Worm *vp, int vsn)
Definition: worm.cpp:131
int nsegs
Definition: worm.hpp:45
LIFE status
Definition: worm.hpp:44
Worm * victimWormp
Definition: worm.cpp:87
void setVictimWorm()
Definition: worm.cpp:94
int stomach
Definition: worm.hpp:43
int victimSegNum
Definition: worm.cpp:88
WormInfo worm_info[]
Definition: worm.hpp:71
int foodValue
Definition: worm.hpp:66
Definition: worm.hpp:20
WormKind type
Definition: worm.hpp:41
void Worm::eatACarrot ( )
private

Definition at line 63 of file worm.cpp.

References asog, CARROT, ASOG::onec, passive, stomach, xOf, and yOf.

Referenced by eat().

64 {
65  ASOG *sg = asog(passive, yOf(this), xOf(this));
66  if (CARROT == sg->onec) {
67  this->stomach += 2; // food value of one carrot
68  sg->onec = ' ';
69  }
70 }
#define CARROT
Definition: worm.hpp:14
int onec
Definition: worms.h:16
Definition: worm.hpp:77
ASOG passive[MAXrow *MAXcol]
Definition: display.cpp:16
#define yOf(wp)
Definition: worm.cpp:8
int stomach
Definition: worm.hpp:43
#define xOf(wp)
Definition: worm.cpp:7
#define asog(ptr, row, col)
Definition: display.cpp:19
void Worm::gotEaten ( )
private

Definition at line 40 of file worm.cpp.

References ALIVE, EATEN, status, type, and xworms.

Referenced by eat().

41 {
42  if (this->status == ALIVE) {
43  this->status = EATEN;
44  xworms[this->type] --;
45  }
46 }
LIFE status
Definition: worm.hpp:44
int xworms[]
WormKind type
Definition: worm.hpp:41
int Worm::isAt ( int  row,
int  col 
)
private

Definition at line 76 of file worm.cpp.

References headSeg, nsegs, Worm::SEGMENT::x, and Worm::SEGMENT::y.

77 {
78  SEGMENT *bp = headSeg(this);
79  for (int sn = this->nsegs; sn > 0; sn--, bp--)
80  if ((bp->y == row) && (bp->x == col))
81  return sn;
82 }
#define headSeg(wp)
Definition: worm.cpp:6
int nsegs
Definition: worm.hpp:45
int Worm::isHungry ( )
private

Definition at line 31 of file worm.cpp.

References ALIVE, WormInfo::capacity, nsegs, status, stomach, type, and worm_info.

Referenced by live().

32 {
33  int m = this->stomach;
34  int n = this->nsegs * worm_info[this->type].capacity;
35  return (this->status == ALIVE && 4*m < 3*n);
36 }
int nsegs
Definition: worm.hpp:45
LIFE status
Definition: worm.hpp:44
int stomach
Definition: worm.hpp:43
WormInfo worm_info[]
Definition: worm.hpp:71
int capacity
Definition: worm.hpp:65
WormKind type
Definition: worm.hpp:41
void Worm::live ( )
private

Definition at line 184 of file worm.cpp.

References asogCols, asogRows, body, checkStomach(), direction, dxa, dya, eat(), headSeg, isHungry(), nextTurn, random, and stomach.

185 {
186  SEGMENT *hp = headSeg(this);
187  for (SEGMENT * bp = this->body; bp < hp; bp++) // < not <=
188  bp->x = (bp + 1)->x, bp->y = (bp + 1)->y; // crawl
189 
190  int dir = this->direction = (this->direction + nextTurn[random(16)]) % 8;
191  hp->y += dya[dir];
192  hp->x += dxa[dir];
193  if (hp->y < 0) hp->y = asogRows-1;
194  if (hp->x < 0) hp->x = asogCols-1;
195  if (hp->y >= asogRows) hp->y = 0;
196  if (hp->x >= asogCols) hp->x = 0;
197 
198  this->stomach--;
199  if (this->isHungry())
200  this->eat();
201  this->checkStomach();
202 }
const int nextTurn[16]
Definition: worm.cpp:175
int asogRows
Definition: display.cpp:18
#define headSeg(wp)
Definition: worm.cpp:6
SEGMENT body[MAXsegs]
Definition: worm.hpp:46
int asogCols
Definition: display.cpp:18
int direction
Definition: worm.hpp:42
const int dxa[]
Definition: worm.cpp:172
void eat()
Definition: worm.cpp:155
void checkStomach()
Definition: worm.cpp:51
const int dya[]
Definition: worm.cpp:173
int stomach
Definition: worm.hpp:43
int isHungry()
Definition: worm.cpp:31
#define random(x)
Definition: worm.hpp:10
void Worm::setVictimWorm ( )
private

Definition at line 94 of file worm.cpp.

References ALIVE, hxWorms, status, victimSegNum, wormArray, xOf, and yOf.

Referenced by eat().

95 {
96  Worm *headWorm = wormArray + hxWorms;
97  victimWormp = 0;
98  victimSegNum = 0;
99  for (Worm * wp = wormArray; wp < headWorm; wp++) {
100  if (wp != this && this->status == ALIVE) {
101  victimSegNum = wp->isAt(yOf(this), xOf(this));
102  if (victimSegNum > 0) {
103  victimWormp = wp;
104  return; // found the victim
105  }
106  }
107  }
108 }
Worm wormArray[]
LIFE status
Definition: worm.hpp:44
int hxWorms
Worm * victimWormp
Definition: worm.cpp:87
#define yOf(wp)
Definition: worm.cpp:8
int victimSegNum
Definition: worm.cpp:88
#define xOf(wp)
Definition: worm.cpp:7
Definition: worm.hpp:20
void Worm::shiftBodyDown ( Worm vp,
int  vsn 
)
private

Definition at line 114 of file worm.cpp.

References body, checkStomach(), headSeg, nsegs, and stomach.

Referenced by sliceTheVictim().

115 {
116  SEGMENT *dp = vp->body + 1; // dp destination, sp source
117  SEGMENT *hp = headSeg(vp);
118  for (SEGMENT *sp = vp->body + vsn + 1; sp <= hp; )
119  *dp++ = *sp++;
120  int tsegs = vp->nsegs;
121  vp->nsegs -= vsn;
122  vp->stomach = vp->stomach / tsegs * vp->nsegs;
123  vp->checkStomach();
124 }
#define headSeg(wp)
Definition: worm.cpp:6
SEGMENT body[MAXsegs]
Definition: worm.hpp:46
int nsegs
Definition: worm.hpp:45
void checkStomach()
Definition: worm.cpp:51
int stomach
Definition: worm.hpp:43
void Worm::showOneWorm ( )
private

Definition at line 15 of file worm.cpp.

References ALIVE, asog, ASOG::attr, body, Worm::SEGMENT::c, WormInfo::color, EATEN, headSeg, ASOG::onec, passive, screen, status, type, worm_info, Worm::SEGMENT::x, and Worm::SEGMENT::y.

Referenced by checkStomach().

16 {
17  if (this->status != EATEN) {
18  ASOG *f = (this->status == ALIVE ? screen : passive);
19  SEGMENT *bp, *hp = headSeg(this);
20  for (bp = this->body + 1; bp <= hp; bp++) {
21  ASOG *g = asog(f, bp->y, bp->x);
22  g->attr = worm_info[this->type].color;
23  g->onec = bp->c;
24  }
25  }
26 }
int color
Definition: worm.hpp:64
int onec
Definition: worms.h:16
#define headSeg(wp)
Definition: worm.cpp:6
Definition: worm.hpp:77
SEGMENT body[MAXsegs]
Definition: worm.hpp:46
LIFE status
Definition: worm.hpp:44
ASOG passive[MAXrow *MAXcol]
Definition: display.cpp:16
ASOG screen[MAXrow *MAXcol]
Definition: display.cpp:17
WormInfo worm_info[]
Definition: worm.hpp:71
int attr
Definition: worms.h:17
#define asog(ptr, row, col)
Definition: display.cpp:19
WormKind type
Definition: worm.hpp:41
Worm * Worm::sliceTheVictim ( Worm vp,
int  vsn 
)
private

Definition at line 131 of file worm.cpp.

References checkStomach(), findSlot(), nsegs, shiftBodyDown(), stomach, type, and xworms.

Referenced by eat().

132 {
133  int tsegs = vp->nsegs;
134  Worm *wp = findSlot();
135  if (wp == 0) // could not find a slot
136  return vp;
137 
138  xworms[vp->type]++;
139 
140  *wp = *vp; // bottom-half
141  wp->nsegs = vsn - 1;
142  wp->stomach = vp->stomach / tsegs * wp->nsegs;
143  wp->checkStomach();
144 
145  shiftBodyDown(vp, vsn); // the top-half
146  return wp;
147 }
Worm * findSlot()
int nsegs
Definition: worm.hpp:45
void shiftBodyDown(Worm *vp, int vsn)
Definition: worm.cpp:114
int xworms[]
void checkStomach()
Definition: worm.cpp:51
int stomach
Definition: worm.hpp:43
Definition: worm.hpp:20
WormKind type
Definition: worm.hpp:41

Member Data Documentation

SEGMENT Worm::body[MAXsegs]
private

Definition at line 46 of file worm.hpp.

Referenced by createWorm(), live(), shiftBodyDown(), and showOneWorm().

int Worm::direction
private

Definition at line 42 of file worm.hpp.

Referenced by createWorm(), and live().

int Worm::nsegs
private

Definition at line 45 of file worm.hpp.

Referenced by createWorm(), eat(), isAt(), isHungry(), shiftBodyDown(), and sliceTheVictim().

LIFE Worm::status
private

Definition at line 44 of file worm.hpp.

Referenced by checkStomach(), createWorm(), eat(), gotEaten(), isHungry(), setVictimWorm(), and showOneWorm().

int Worm::stomach
private
WormKind Worm::type
private

Definition at line 41 of file worm.hpp.

Referenced by checkStomach(), createWorm(), eat(), gotEaten(), isHungry(), showOneWorm(), and sliceTheVictim().


The documentation for this class was generated from the following files: