-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPuzzlePiecesMap.cpp
46 lines (40 loc) · 1.4 KB
/
PuzzlePiecesMap.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
//
// PuzzlePieces.cpp
// AdvProgEX1
//
// Created by Alexander Shugaley on 09/12/2017.
// Copyright © 2017 Alexander Shugaley. All rights reserved.
//
#include "PuzzlePiecesMap.h"
PuzzlePiecesMap::PuzzlePiecesMap() {
this->typesMap = std::map<PuzzleType, vector<PuzzlePiece> >();
}
PuzzlePiecesMap::PuzzlePiecesMap(vector<PuzzlePiece>& pieces){
this->typesMap = std::map<PuzzleType, vector<PuzzlePiece> >();
toBuckets(pieces);
}
map<PuzzleType, vector<PuzzlePiece>>& PuzzlePiecesMap::getTypesMap(){
return this->typesMap;
}
void PuzzlePiecesMap::toBuckets(vector<PuzzlePiece>& pieces){
for(auto& p : pieces){
PuzzleType type = PuzzleType(p.getLeftEdge(),p.getTopEdge(),p.getRightEdge(), p.getBottomEdge());
typesMap[type].push_back(p);
}
}
PuzzlePiece* PuzzlePiecesMap::nextPiece(PuzzleRequirement& req){
for(auto& type : typesMap){
// first check if the type itself satisfies the requirement
if(req.typeSatisfiesReq(type.first)){
// then check if we have an unused vector from that type
for(auto& puzzlePiece : type.second){
if(!puzzlePiece.isUsed()){
puzzlePiece.setUsed(true);
return &puzzlePiece;
}
}
}
}
// if we cannot find any puzzle piece that satisfies the requirement (and wan't used yet) - return nullptr
return nullptr;
}