-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFactory.cpp
136 lines (120 loc) · 3.54 KB
/
Factory.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
/*Factory Class Description
*Author:Sara Imamura Sanjay Sajeesh Krishnan Kee Chew Huang
*The Factory Class is resposible for the creation of factories for players to choose from.
*It also runs multiple checks to determine the validity of the move and manipulates the TileSet class
*to move the tiles into the Mosaic.
*/
//Header Files
#include "Factory.h"
#include "LinkedList.h"
#include <string>
Factory::Factory() {
this->firstPlayer = true;
}
Factory::~Factory()
{
}
// this returns a vector of tiles to be "moved" to player's mosaic
std::vector<Tile> Factory::getTile(int index, Tile tile_type){
// this is a vector tiles that will contains tiles of same tile colour
std::vector<Tile> retTiles;
// if tiles are chosen from centre table
if(index == CENTRETABLE){
// check if center of table was chosen for very first time in
// the round by the player
if(firstPlayer == true){
firstPlayer = false;
retTiles.push_back(FIRST_PLAYER);
}
// pick tiles of specified tile colour from center of tables
std::vector<Tile>* copy = nullptr;
copy = ¢reTable[tile_type];
for (Tile t : (*copy)){
//std::cout << t << std::endl;
retTiles.push_back(t);
}
(*copy).clear();
}
// if tiles are chosen from factory plates
else {
for(Tile t : factoryPlate[index-1]){
if(t == tile_type){
retTiles.push_back(t);
}else{
centreTable[t].push_back(t);
}
}
factoryPlate[index-1].clear();
}
return retTiles;
}
// intialise factory plates
void Factory::createFactory(Tile* tiles){
int size = FACTORY_NUM*FACTORY_PLATE;
for(int i =0; i< size; i++){
factoryPlate[i/FACTORY_PLATE].push_back(tiles[i]);
}
firstPlayer = true;
}
// check if the factory is empty
bool Factory::isEmpty(){
bool empty = true;
for(int i = 0; i< TILE_LEN; i++){
if(!centreTable[i].empty()){
empty = false;
}
}
for(int i = 0; i<FACTORY_NUM; i++){
if(!factoryPlate[i].empty()){
empty = false;
}
}
return empty;
}
// string formatted for printing out factory
std::string Factory::printTiles()
{
std::string result;
// represents centre of table
result += "0: ";
if(firstPlayer){
result += SYMBOL_FIRST_PLAYER;
result += " ";
}
for(int i = 0; i < TILE_LEN; i++){
for(Tile tile : centreTable[i]){
result = result + enumToChar(tile) + " ";
}
}
// represents factory plate
for(int i = 0; i < FACTORY_NUM; i++){
// int index = i+1;
result += "\n" + std::to_string(i+1)+ ": ";
for(Tile tile : factoryPlate[i]){
result = result + enumToChar(tile) + " ";
}
}
return result;
}
// this method checks whether the "turn" command parameter is
// valid or not
bool Factory::checkValid(Tile tile, int factoryIndex){
bool valid = false;
if(tile != NO_TILE && tile != FIRST_PLAYER){
// checks the validity in the case of centre table
if(factoryIndex == CENTRETABLE){
if(this->centreTable[tile].size() > 0){
valid = true;
}
}
// checks the validity in the case of factory plate
else{
for(Tile t: this->factoryPlate[factoryIndex-1]){
if(t == tile){
valid = true;
}
}
}
}
return valid;
}