-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPieces.cpp
169 lines (145 loc) · 3.29 KB
/
Pieces.cpp
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
#include "Pieces.h"
#include <cstdlib>
#include <ctime>
/*
description: This deletes one of the bits
from the tetronimoes
precondition: A bit exists
postcondition: Deletes bit, throws bad_index if
n is out of bounds
return: If the bit is already deleted
*/
Piece::Piece()
{
//INITIALIZE TYPE
type_ = 'P';
color_ = 0;
srand( time(NULL) );
int randomInteger = rand() % 4;
//PICK WHICH DIRECTION VECTOR TO FEED CONSTRUCTION MECHANISM
Bit constructDirection;
switch(randomInteger)
{
case 1:
constructDirection = rightUnitVector;
break;
case 2:
constructDirection = downUnitVector;
break;
case 3:
constructDirection = leftUnitVector;
break;
default:
constructDirection = upUnitVector;
//note that default case covers the zero integer case that I want to occur
}
//Set the pieces stored directon
direction_ = constructDirection;
}
/*
Description: changes the color of the pieced
Assumptions: a piece exists
Postcondition: the color is changed
*/
void Piece::changeColor(const int amount)
{
//Adds color but wraps back to zero when it hits 200
color_ += amount;
if(color_ > 199)
color_ -= 200;
}
/*
Description: CopyConstructor
Assumptions: A piece exits
Postcondition: This piece is a perfect copy
of the other piece.
*/
Piece::Piece(const Piece& p)
{
//Copy Guts
for(int i = 0; i < 4; i++)
{
bits_[i] = p.bits_[i];
}
type_ = p.type_;
}
/*
Description: defines the [] operator
to give users access to
the individual bits of the
tetronimoe.
Assumption : a piece (tetronimoe) exists
Postcondition: SEE Return: , else it
throws a bad_index for a
bad n value
Return: retuns a reference to
the bit specified by n
*/
Bit& Piece::operator [] (int n)
{
if(n < 0 || n > 3)
throw ("Index is Bad");
return bits_[n];
}
/*
Description: lets two pieces use the equals
operator to set themselves equal
to eachother
Assumption : a piece to be immatated is not itself
Postcondition: a piece becomes a perfect immatation
of another piece.
Return: retuns itself
*/
Piece& Piece::operator = (const Piece& p)
{
//Copy Guts
for(int i = 0; i < 4; i++)
{
bits_[i] = p.bits_[i];
}
type_ = p.type_;
color_ = p.color_;
//Return
return *this;
}
/*
Description: returns a pieces color
Assumption : a piece exists
Postcondition: the object is unchanged
Return: retuns its color
*/
int Piece::getColor() const
{
return color_;
}
/*
Description: deletes a bit
Assumption : a bit is pointed to and not
already deleted
Postcondition: deletes bits_'s bit at n
Return: a bool whether its already
been deleted or not
*/
bool Piece::deleteBit(int n)
{
bool alreadyDeleted = false;
if(n < 0 || n > 3)
throw ("Index is Bad");
if(bits_[n] == 0)
alreadyDeleted = true;
bits_[n] = 0;
return alreadyDeleted;
}
/*
Description: Serves a requirement for
all pieces who inherit from
the Piece class. it is what single
handedly builds the tetronimoe with
unit vectors and a location
Assumption : a piece exists
Postcondition: the Piece is contructed at startLocation
with direction_ direction.
*/
void Piece::pieceBuilder(Bit direction, Bit startLocation)
{
}