1 #include "IosVector.h"
2 
3 #ifdef _WIN32
4 #define srandom srand
5 #define random rand
6 #endif
7 
8 #ifndef PUYOGAME_H
9 #define PUYOGAME_H
10 
11 #define PUYODIMX 6
12 #define PUYODIMY 14
13 
14 #define PUYO_STILL PUYO_BLUE-PUYO_FALLINGBLUE
15 
16 enum PuyoState {
17 	PUYO_FALLINGBLUE = 0,
18 	PUYO_FALLINGRED = 1,
19 	PUYO_FALLINGGREEN = 2,
20 	PUYO_FALLINGVIOLET = 3,
21 	PUYO_FALLINGYELLOW = 4,
22 	PUYO_EMPTY = 5,
23 	PUYO_BLUE = 6,
24 	PUYO_RED = 7,
25 	PUYO_GREEN = 8,
26 	PUYO_VIOLET = 9,
27 	PUYO_YELLOW = 10,
28 	PUYO_NEUTRAL = 11,
29 	PUYO_UNMOVEABLE = 12,
30 	PUYO_MARKED = 13,
31 	PUYO_REMOVED = 14
32 };
33 
34 class PuyoRandomSystem {
35 public:
36 	PuyoRandomSystem();
37 	PuyoState getPuyoForSequence(int sequence);
38 private:
39 	IosVector sequenceItems;
40 };
41 
42 // A PuyoPuyo is an entity of the game
43 class PuyoPuyo {
44 public:
45     PuyoPuyo(PuyoState state);
~PuyoPuyo()46     virtual ~PuyoPuyo() {};
47     PuyoState getPuyoState();
48     void setPuyoState(PuyoState state);
49     bool isFalling();
50     int getPuyoX() const;
51     int getPuyoY() const;
52     void setPuyoXY(int X, int Y);
setFlag()53     void setFlag() { flag = true; }
unsetFlag()54     void unsetFlag() { flag = false; }
getFlag()55     bool getFlag() const { return flag; }
56 private:
57     PuyoState state;
58     int X, Y;
59     bool flag;
60 };
61 
62 // The puyos must be created by a factory to ensure custom puyo creation
63 class PuyoFactory {
64  public:
65   virtual PuyoPuyo *createPuyo(PuyoState state) = 0;
deletePuyo(PuyoPuyo * target)66   virtual void deletePuyo(PuyoPuyo *target) { delete target; }
67 };
68 
69 class PuyoDefaultFactory : public PuyoFactory {
70  public:
createPuyo(PuyoState state)71   PuyoPuyo *createPuyo(PuyoState state) {
72     return new PuyoPuyo(state);
73   }
74 };
75 
76 class PuyoDelegate {
77 public:
78   virtual void gameDidAddNeutral(PuyoPuyo *neutralPuyo, int neutralIndex) = 0;
79   virtual void companionDidTurn(PuyoPuyo *companionPuyo,
80 				int companionVector,
81 				bool counterclockwise) = 0;
82   virtual void puyoDidFall(PuyoPuyo *puyo, int originX, int originY) = 0;
83   virtual void puyoWillVanish(IosVector &puyoGroup, int groupNum, int phase) = 0;
84   virtual void gameDidEndCycle() = 0;
85   virtual void gameLost() = 0;
86 };
87 
88 class PuyoGame {
89 public:
90   PuyoGame(PuyoRandomSystem *attachedRandom, PuyoFactory *attachedFactory);
91   PuyoGame(PuyoRandomSystem *attachedRandom);
92   virtual ~PuyoGame();
93   void setDelegate(PuyoDelegate *delegate);
94   void cycle();
95 
96   // Get the state of the puyo at the indicated coordinates
97   PuyoState getPuyoCellAt(int X, int Y) const;
98   // Get the puyo at the indicated coordinates
99   PuyoPuyo *getPuyoAt(int X, int Y) const;
100 
101   // List access to the PuyoPuyo objects
102   int getPuyoCount() const;
103   PuyoPuyo *getPuyoAtIndex(int index) const;
104 
105   void moveLeft();
106   void moveRight();
107   void rotate(bool left);
108   void rotateLeft();
109   void rotateRight();
110   PuyoState getNextFalling();
111   PuyoState getNextCompanion();
getCompanionState()112   PuyoState getCompanionState() const { return companionPuyo->getPuyoState(); }
getFallingState()113   PuyoState getFallingState() const { return fallingPuyo->getPuyoState(); }
114 
getFallingX()115   int getFallingX() const { return fallingPuyo->getPuyoX(); }
getFallingY()116   int getFallingY() const { return fallingPuyo->getPuyoY(); }
getCompanionX()117   int getCompanionX() const { return companionPuyo->getPuyoX(); }
getCompanionY()118   int getCompanionY() const { return companionPuyo->getPuyoY(); }
119   int getFallingCompanionX() const;
120   int getFallingCompanionY() const;
getFallingCompanionDir()121   int getFallingCompanionDir() const { return fallingCompanion; }
getFallingPuyo()122   PuyoPuyo *getFallingPuyo() const { return fallingPuyo; }
123 
124   void increaseNeutralPuyos(int incr);
125   int getNeutralPuyos() const;
126   void dropNeutrals();
isGameRunning()127   bool isGameRunning() const { return gameRunning; }
isEndOfCycle()128   bool isEndOfCycle() const { return endOfCycle; }
129   int getColumnHeigth(int colNum) const;
130   int getMaxColumnHeight() const;
131   int getSamePuyoAround(int X, int Y, PuyoState color);
132 
getSemiMove()133   int getSemiMove() const { return semiMove; }
getPoints()134   int getPoints() const { return points; }
135   int points;
136 
137  private:
138   void InitGame(PuyoRandomSystem *attachedRandom);
139   // Set the state of the puyo at the indicated coordinates (not recommanded)
140   void setPuyoCellAt(int X, int Y, PuyoState value);
141   // Set the puyo at the indicated coordinates
142   void setPuyoAt(int X, int Y, PuyoPuyo *newPuyo);
143 
144   void setFallingAtTop(bool gameConstruction = false);
145   int getFallY(int X, int Y) const;
146   void cycleEnding();
147   void markPuyoAt(int X, int Y, PuyoState color, bool includeNeutral);
148   void deleteMarkedPuyosAt(int X, int Y);
149   int removePuyos();
150   void notifyReductions();
151 
152   bool gameRunning;
153   bool endOfCycle;
154 
155   // The falling is the puyo you couldn't control,
156   // whereas you can make the companion turn around the falling puyo
157   PuyoPuyo *fallingPuyo, *companionPuyo;
158   int fallingX, fallingY;
159 
160   // Position of the companion is relative of the falling puyo
161   // 0 = up 1 = left 2 = down 3 = up
162   unsigned char fallingCompanion;
163 
164   PuyoPuyo *puyoCells[PUYODIMX * (PUYODIMY+1)];
165   PuyoRandomSystem *attachedRandom;
166   int sequenceNr;
167   int neutralPuyos;
168   PuyoDelegate *delegate;
169   PuyoFactory *attachedFactory;
170   int phase;
171   int semiMove;
172 
173   // This is not really a puyo, it is instead an indicator for the edges of the game
174   PuyoPuyo *unmoveablePuyo;
175 
176   // We are keeping a list of current puyos
177   IosVector puyoVector;
178   int nbFalled;
179 };
180 
181 #endif // PUYOGAME_H
182