You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
62 lines
2.1 KiB
C++
62 lines
2.1 KiB
C++
#include "samegame.h"
|
|
|
|
// ist es nicht cool?
|
|
extern std::unique_ptr<QSettings> settings;
|
|
|
|
void SameGame::initialiseGameMatrix(int rows, int columns) {
|
|
gameMatrix = std::vector<std::vector<QString>>();
|
|
|
|
srand (time(NULL));
|
|
std::vector<QString> t_row;
|
|
for (int i = 0; i < rows; i++) {
|
|
t_row.clear();
|
|
for (int j = 0; j < columns; j++) {
|
|
// wähle zufällige Farbe aus den verfügbaren Farben
|
|
t_row.push_back( colors[rand() % colors.size()] );
|
|
}
|
|
gameMatrix.push_back(std::move(t_row));
|
|
}
|
|
}
|
|
|
|
SameGame::SameGame(int rows, int columns)
|
|
: colors(settings->value("colors/colors", QList<QVariant>() << "#590d22" << "#a4133c" << "#c9184a" << "#ff4d6d" << "#ff758f").toStringList())
|
|
// also ich finds echt cool!
|
|
{
|
|
initialiseGameMatrix(rows, columns);
|
|
}
|
|
|
|
QString SameGame::getCell(int row, int column) {
|
|
return gameMatrix[row][column];
|
|
}
|
|
|
|
void SameGame::setCell(int row, int column, QString color) {
|
|
gameMatrix[row][column] = color;
|
|
}
|
|
|
|
void SameGame::checkNeighbour(std::set<std::vector<int>> *connectedSet, QString color, int row, int column) {
|
|
// check for grid boundaries
|
|
if (row > -1 && column > -1 && (unsigned long) row < gameMatrix.size() && (unsigned long) column < gameMatrix[0].size()) {
|
|
// check if already visited
|
|
if ( !(connectedSet->contains(std::vector<int>{row, column})) ) {
|
|
// check color
|
|
if (gameMatrix[row][column] == color)
|
|
getConnected(connectedSet, color, row, column);
|
|
}
|
|
}
|
|
}
|
|
|
|
void SameGame::deleteCell(int row, int column) {
|
|
gameMatrix[row][column] = "none";
|
|
}
|
|
|
|
void SameGame::getConnected(std::set<std::vector<int>> *connectedSet, QString color, int row, int column) {
|
|
std::vector<int> cellCoords;
|
|
cellCoords.push_back(row);
|
|
cellCoords.push_back(column);
|
|
connectedSet->insert(cellCoords);
|
|
checkNeighbour(connectedSet, color, row+1, column);
|
|
checkNeighbour(connectedSet, color, row-1, column);
|
|
checkNeighbour(connectedSet, color, row, column+1);
|
|
checkNeighbour(connectedSet, color, row, column-1);
|
|
}
|