Worms -- OOP Redo  2016-01
Worms-Redo Project as a learning vehicle.
worm.hpp
Go to the documentation of this file.
1 
2 /* worm.cpp deals with a single worm */
3 
4 #include <stdlib.h>
5 #include <string.h>
6 #include <unistd.h>
7 #include <time.h>
8 
9 // temporary cure begin
10 #define random(x) (rand() % x)
11 #define isDigit(x) ('0' <= x && x <= '9')
12 #define min(a, b) ((a) < (b)? (a) : (b))
13 #define ESC '\033' // the ESCape char
14 #define CARROT '.' // a carrot is shown as a dot
15 #define NORTH 00 // TBD
16 #define asog(ptr, row, col) (ptr + row*asogCols + col)
17 #define MAXsegs 100 // max # segments per worm
18 // temporary cure end
19 
20 class Worm {
21 
22 public:
23  typedef enum {
25  } WormKind; // 0, 1, 2
26 
27  void createWorm(WormKind type, const char * sy);
28 
29 private:
30 
31  typedef enum {
33  } LIFE;
34 
35  typedef struct {
36  int x, y; // coordinates of the segment
37  char c; // the letter it carries
38  } SEGMENT;
39 
40 private: // data members
41  WormKind type; // once set, type does not change
42  int direction; // its (head's) direction
43  int stomach; // food value
44  LIFE status; // EATEN, DEAD, or ALIVE; redundant
45  int nsegs; // # of segs in this worm
46  SEGMENT body[MAXsegs]; // body parts
47 
48 private: // methods
49  int isHungry();
50  void gotEaten();
51  void checkStomach();
52  void eatACarrot();
53  int isAt(int row, int col);
54  void setVictimWorm();
55  void shiftBodyDown(Worm * vp, int vsn);
56  Worm * sliceTheVictim(Worm * vp, int vsn);
57  void eat();
58  void live();
59  void showOneWorm();
60 };
61 
62 class WormInfo {
63 public:
64  int color;
65  int capacity; // stomach size
66  int foodValue;
67 };
68 
69 #define COLOR_PAIR(x) (x) // dummy TBD
70 
71 WormInfo worm_info[] = { // indexed by WORM_KIND
72  {COLOR_PAIR(1), 3, 3}, // from <ncurses.h>
73  {COLOR_PAIR(2), 4, 5},
74  {COLOR_PAIR(3), 5, 4}
75 };
76 
77 class ASOG {
78 };
79 
80 
81 extern Worm * findSlot();
82 extern int asogCols, asogRows;
83 extern int xworms[];
84 extern int hxWorms;
85 extern Worm wormArray[];
86 
void gotEaten()
Definition: worm.cpp:40
int color
Definition: worm.hpp:64
void eatACarrot()
Definition: worm.cpp:63
Worm * sliceTheVictim(Worm *vp, int vsn)
Definition: worm.cpp:131
int asogCols
Definition: display.cpp:18
Worm * findSlot()
Definition: worm.hpp:77
SEGMENT body[MAXsegs]
Definition: worm.hpp:46
int nsegs
Definition: worm.hpp:45
Worm wormArray[]
int direction
Definition: worm.hpp:42
LIFE status
Definition: worm.hpp:44
void shiftBodyDown(Worm *vp, int vsn)
Definition: worm.cpp:114
int hxWorms
int asogRows
Definition: display.cpp:18
void eat()
Definition: worm.cpp:155
void setVictimWorm()
Definition: worm.cpp:94
int xworms[]
void checkStomach()
Definition: worm.cpp:51
#define MAXsegs
Definition: worm.hpp:17
int isAt(int row, int col)
Definition: worm.cpp:76
void createWorm(WormKind type, const char *sy)
Definition: worm.cpp:208
void live()
Definition: worm.cpp:184
int stomach
Definition: worm.hpp:43
int isHungry()
Definition: worm.cpp:31
void showOneWorm()
Definition: worm.cpp:15
WormKind
Definition: worm.hpp:23
WormInfo worm_info[]
Definition: worm.hpp:71
char c
Definition: worm.hpp:37
int foodValue
Definition: worm.hpp:66
#define COLOR_PAIR(x)
Definition: worm.hpp:69
int capacity
Definition: worm.hpp:65
Definition: worm.hpp:20
WormKind type
Definition: worm.hpp:41
LIFE
Definition: worm.hpp:31