-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplayerpieces.cpp
More file actions
263 lines (251 loc) · 10.9 KB
/
Copy pathplayerpieces.cpp
File metadata and controls
263 lines (251 loc) · 10.9 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#include <iomanip>
#include <cstdlib>
#include <iostream>
#include "player.h"
void Player::createPieces() {
char o = (id == 0 ? '#' : 'O'), i = '-';
char allspieces[21][5][5] = {{
{o, i, i, i, i}, // Piece 1
{i, i, i, i, i},
{i, i, i, i, i},
{i, i, i, i, i},
{i, i, i, i, i}
}, {
{o, i, i, i, i}, // Piece 2
{o, i, i, i, i},
{i, i, i, i, i},
{i, i, i, i, i},
{i, i, i, i, i}
}, {
{o, i, i, i, i}, // Piece 3
{o, i, i, i, i},
{o, i, i, i, i},
{i, i, i, i, i},
{i, i, i, i, i}
}, {
{o, i, i, i, i}, // Piece 4
{o, o, i, i, i},
{i, i, i, i, i},
{i, i, i, i, i},
{i, i, i, i, i}
}, {
{o, i, i, i, i}, // Piece 5
{o, i, i, i, i},
{o, i, i, i, i},
{o, i, i, i, i},
{i, i, i, i, i}
}, {
{i, o, i, i, i}, // Piece 6
{i, o, i, i, i},
{o, o, i, i, i},
{i, i, i, i, i},
{i, i, i, i, i}
}, {
{o, i, i, i, i}, // Piece 7
{o, o, i, i, i},
{o, i, i, i, i},
{i, i, i, i, i},
{i, i, i, i, i}
}, {
{o, o, i, i, i}, // Piece 8
{o, o, i, i, i},
{i, i, i, i, i},
{i, i, i, i, i},
{i, i, i, i, i}
}, {
{o, o, i, i, i}, // Piece 9
{i, o, o, i, i},
{i, i, i, i, i},
{i, i, i, i, i},
{i, i, i, i, i}
}, {
{o, i, i, i, i}, // Piece 10
{o, i, i, i, i},
{o, i, i, i, i},
{o, i, i, i, i},
{o, i, i, i, i}
}, {
{i, o, i, i, i}, // Piece 11
{i, o, i, i, i},
{i, o, i, i, i},
{o, o, i, i, i},
{i, i, i, i, i}
}, {
{i, o, i, i, i}, // Piece 12
{i, o, i, i, i},
{o, o, i, i, i},
{o, i, i, i, i},
{i, i, i, i, i}
}, {
{i, o, i, i, i}, // Piece 13
{o, o, i, i, i},
{o, o, i, i, i},
{i, i, i, i, i},
{i, i, i, i, i}
}, {
{o, o, i, i, i}, // Piece 14
{i, o, i, i, i},
{o, o, i, i, i},
{i, i, i, i, i},
{i, i, i, i, i}
}, {
{o, i, i, i, i}, // Piece 15
{o, o, i, i, i},
{o, i, i, i, i},
{o, i, i, i, i},
{i, i, i, i, i}
}, {
{i, o, i, i, i}, // Piece 16
{i, o, i, i, i},
{o, o, o, i, i},
{i, i, i, i, i},
{i, i, i, i, i}
}, {
{o, i, i, i, i}, // Piece 17
{o, i, i, i, i},
{o, o, o, i, i},
{i, i, i, i, i},
{i, i, i, i, i}
}, {
{o, o, i, i, i}, // Piece 18
{i, o, o, i, i},
{i, i, o, i, i},
{i, i, i, i, i},
{i, i, i, i, i}
}, {
{o, i, i, i, i}, // Piece 19
{o, o, o, i, i},
{i, i, o, i, i},
{i, i, i, i, i},
{i, i, i, i, i}
}, {
{o, i, i, i, i}, // Piece 20
{o, o, o, i, i},
{i, o, i, i, i},
{i, i, i, i, i},
{i, i, i, i, i}
}, {
{i, o, i, i, i}, // Piece 21
{o, o, o, i, i},
{i, o, i, i, i},
{i, i, i, i, i},
{i, i, i, i, i}
}};
for (int i = 0; i < 21; i++){
// Compute the size of each piece
int sizeX = 0;
int sizeY = 0;
for(int x = 0; x < 5; x++){
for(int y = 0; y < 5; y++) {
if (allspieces[i][x][y] != '-'){
sizeX = (x + 1 > sizeX) ? (x + 1) : sizeX;
sizeY = (y + 1 > sizeY) ? (y + 1) : sizeY;
}
}
}
// Create the squares of each piece
Square*** squares = new Square**[sizeX];
for(int x = 0; x < sizeX; x++) {
squares[x] = new Square*[sizeY];
for(int y = 0; y < sizeY; y++) {
squares[x][y] = new Square(x, y, allspieces[i][x][y]);
}
}
// Create the piece
pieces[i] = new Piece(i + 1, id == 0 ? '#' : 'O', squares, sizeX, sizeY);
}
};
void Player::printPiecesSideBySide(Piece** pieces, int pstart, int pend){
// Compute the number of pieces to be print
int numPieces = pend - pstart;
// Print the ids of the pieces and concatenate the pieces using a stream
stringstream ss[numPieces];
cout << " ";
for (int i = pstart; i < pend; i++){
cout << (pieces[i]->getId() > 9 ? "" : " ") << pieces[i]->getId() << " ";
ss[i - pstart] << pieces[i]->toString();
}
cout << endl;
// Print the pieces line by line
string to[numPieces];
for (int j = 0; j < 5; j++){
for (int i = pstart; i < pend; i++){
getline(ss[i - pstart], to[i - pstart], '\n');
cout << to[i - pstart] << " ";
}
cout << endl;
}
}
void Player::printAvailablePieces(){
// Create an array with the available pieces of the player
int numAvailablePieces = getNumberOfAvailablePieces();
Piece** availablePieces = new Piece*[numAvailablePieces];
int j = 0;
for (int i = 0; i < numPieces; i++){
if (!pieces[i]->isPlaced())
availablePieces[j++] = pieces[i];
}
// Print the name of the player, the number of available pieces, and the available pieces
cout << "\n\nPlayer " << (id + 1) << " has " << numAvailablePieces << " available pieces:" << endl;
if (numAvailablePieces > 7){
printPiecesSideBySide(availablePieces, 0, 7);
if (numAvailablePieces > 14){
printPiecesSideBySide(availablePieces, 7, 14);
printPiecesSideBySide(availablePieces, 14, numAvailablePieces);
}
else
printPiecesSideBySide(availablePieces, 7, numAvailablePieces);
}
else
printPiecesSideBySide(availablePieces, 0, numAvailablePieces);
delete[] availablePieces;
}
bool Player::canPlacePiece(Board* board, Piece* piece) {
Orientation orientations[] = {UP, RIGHT, DOWN, LEFT};
Flip flips[] = {NO, YES};
if (!piece->isPlaced()) {
for (int x = 0; x < board->getSizeX(); x++) {
for (int y = 0; y < board->getSizeY(); y++) {
for (int o = 0; o < 4; o++) {
for (int f = 0; f < 2; f++) {
if (board->pieceCanBePlaced(piece, x, y, orientations[o], flips[f]))
return true;
}
}
}
}
}
return false;
}
bool Player::canPlaceAnyPiece(Board* board) {
for (int i = 0; i < numPieces; i++) {
if (canPlacePiece(board, pieces[i]))
return true;
}
return false;
}
Move* HumanPlayer::placePiece(Board* board) {
int pieceId, x, y;
int orientation, flip;
cout << "Select piece (1-21), orientation (0 for UP, 1 for RIGHT, 2 for DOWN, 3 for LEFT), " <<
"flip (0 for NO, 1 for YES), position (x, y), (e.g. 3 0 0 3 4): " << endl;
if (getNumberOfPlacedPieces() == 0)
cout << "First piece must touch square " << (getId() == 0 ? "4, 4" : "9, 9") << endl;
cin >> pieceId >> orientation >> flip >> x >> y;
return new Move(pieces[pieceId-1], x, y, (Orientation)orientation, (Flip)flip);
}
Move* ComputerPlayer::placePiece(Board* board) {
int pieceId, x, y;
Orientation orientation;
Flip flip;
Piece* piece;
do {
pieceId = getRandomPieceId();
piece = pieces[pieceId-1];
x = rand() % board->getSizeX();
y = rand() % board->getSizeY();
orientation = getRandomOrientation();
flip = getRandomFlip();
} while(piece->isPlaced() || !board->pieceCanBePlaced(piece, x, y, orientation, flip));
return new Move(piece, x, y, orientation, flip);
}