-
Notifications
You must be signed in to change notification settings - Fork 0
/
PiecesContainer.h
180 lines (140 loc) · 3.71 KB
/
PiecesContainer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#ifndef TETRIS_GAME_MANAGER_PIECESCONTAINER
#define TETRIS_GAME_MANAGER_PIECESCONTAINER
//INCLUSIONS
#include "Pieces.h"
#include "SpecialPieces.h"
using namespace std;
/***************************************
ClassName: PiecesContainer
Purpose: Holds all the piecs being used
by the tetris game
Abilities: Allows the game to run through
and handle each tetronimoe and
move it in accordance with other
obstacles (including the wall)
ReWritten: It was re implemented to, instead of
stuffing copies of pieces that are created...to stuff
pointers to the actual copies that are made. This way,
less memory is stolen and when i move a piece in action
i dont have to worry about its counterpart in the container
not moving.
FutureNeeds:Destuctor must be written lest a huge amount of memory be lost
***************************************/
class PiecesContainer
{
private:
//THE CONTAINER
Piece** container_;
//Properties of the container
int capacity_;
int numPieces_;
//Helper Functions
void PiecesContainer::grow()
{
//Alloc
capacity_ = capacity_ * 2;
Piece** temp = new Piece*[capacity_];
//Copy into
for(int i = 0; i < numPieces_; i++)
temp[i] = container_[i];
//Destroy member variables previous stuff and then set it to temp
delete [] container_;
container_ = temp;
}
public:
/*
Description: Gets the container ready to be stuffed
Assumptions: Nothing
Postcondition: Its ready to be stuffed
*/
PiecesContainer(void)
{
capacity_ = 10;
numPieces_ = 0;
container_ = new Piece*[capacity_];
}
/*
Description: Gets the size
Assumptions: Nothing
Postcondition: object is unchanged
Returns: The number of pieces
*/
int getNumPieces()
{
return numPieces_;
}
/*
Description: Deletes the containers contents and
makes it so that it is ready to go.
Assumtions: The container has stuff
Postcond: all container contents are deleted
and ready to go for the next game
*/
void deleteContainer()
{
delete []container_;
capacity_ = 10;
numPieces_ = 0;
container_ = new Piece*[capacity_];
}
/**********************************
Description: Adds a piece to the container
assuming the container exists
Precondition: The object exists
Postcondition: A piece is added, the numPieces is
incremented.
Return: Returns whether we have additional memory to
deal with.
***********************************/
bool addPiece(Piece* p)
{
Piece* newPiece = p;
bool outOfMemory = false;
try
{
if(numPieces_ >= capacity_)
grow();
}
catch(bad_alloc)
{
outOfMemory = true;
}
container_[numPieces_] = newPiece;
numPieces_++;
//Return
return outOfMemory;
}
/*******************************
description: Let's game use operator
[] to call any piece from
container
precondition: the container exists
postcondition: returns a piece and throws
a bad_index if index is given out of
bounds
return: returns the piece aske for by reference
********************************/
Piece& operator [] (int num)
{
if(num < 0 || num >= numPieces_)
throw ("Index is Bad");
//Return
return (*container_[num]);
}
/*
Description: Instead of setting the
piece at a certain point numm, (**)
it sets a single derefrenced point
to its pointer.
Assumptions: Nothing
Postcondition: container_ stores the pointer at num
Returns: nada
*/
void setPointerAt(int num, Piece* p)
{
if(num < 0 || num >= numPieces_)
throw ("Index is Bad");
container_[num] = p;
}
};
#endif