Skip to content

Commit

Permalink
added random gen
Browse files Browse the repository at this point in the history
  • Loading branch information
nanda-gopal-sb committed Nov 22, 2024
1 parent e2e17be commit 90a33bd
Showing 1 changed file with 58 additions and 18 deletions.
76 changes: 58 additions & 18 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,39 +15,72 @@ struct Cell {
int y;
bool open;
bool openPerm;
Cell(int x1, int y1, int num) {
Cell(int x1, int y1) {
x = x1;
y = y1;
open = false;
open = true;
openPerm = false;
number = num;
}
};

std::vector<Cell> cells;

bool isWin() {
bool win = true;
for (auto cell : cells) {
if (!cell.openPerm) {
return false;
}
}
return true;
}

sf::Font font;
bool checkInArray(int num) {
bool isInArray(int num, std::vector<int>& arr) {
for (auto nums : arr) {
if (nums == num)
return true;
}
return false;
}

int getCount(int toCheck) {
int count = 0;
for (auto cell : cells) {
if (cell.number == num) {
if (cell.number == toCheck) {
count++;
}
}
if (count >= 32) {
return true;
}
return false;
return count;
}
int getRand() {
void fillNums() {
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> distrib(1, 32);
int rand = distrib(gen);
while (checkInArray(rand)) {
rand = distrib(gen);
std::uniform_int_distribution<> distrib(0, 63);
int rand = -1;
std::vector<int> arr;
for (int i = 1; i <= 32; i++) {
for (int j = 0; j < 2; j++) {
while (isInArray(rand, arr)) {
rand = distrib(gen);
}
arr.push_back(rand);
cells[rand].number = i;
}
}
for (auto cell : cells) {
if (cell.number == 1) {
std::cout << "hit" << "\n";
std::cout << cell.x << "\n";
std::cout << cell.y << "\n";
}
if (cell.number == 0) {
std::cout << "hit" << "\n";
std::cout << cell.x << "\n";
std::cout << cell.y << "\n";
cell.number = 1;
}
}
return rand;
}

sf::Text DrawText() {
Expand All @@ -61,7 +94,7 @@ sf::Text DrawText() {
text.setCharacterSize(64); // in pixels, not points!

// set the color
text.setFillColor(sf::Color::Green);
text.setFillColor(sf::Color(0, 0, 0));
}
return text;
}
Expand All @@ -70,9 +103,11 @@ void fillCell() {
for (int a = 0; a < ROWS; a++) {
for (int b = 0; b < COLUMNS; b++) {
// cells.push_back(Cell(b, a, 4));
cells.push_back(Cell(b, a, getRand()));
cells.push_back(Cell(b, a));
}
}
fillNums();
// std::cout << cells.size() << "\n";
}
void drawRec(sf::RenderWindow& window) {
auto text = DrawText();
Expand All @@ -81,6 +116,10 @@ void drawRec(sf::RenderWindow& window) {
for (int j = 0; j < COLUMNS; j++) {
text.setPosition(CELL_SIZE * i, CELL_SIZE * j);
text.setString(std::to_string(cells[i + COLUMNS * j].number));
// if (cells[i + COLUMNS * j].number == 0) {
// text.setString("1");
// } else
// text.setString(std::to_string(cells[i + COLUMNS * j].number));
cell_shape.setPosition(CELL_SIZE * i, CELL_SIZE * j);
if (!cells[i + COLUMNS * j].open) {
cell_shape.setFillColor(sf::Color(73, 98, 185));
Expand Down Expand Up @@ -111,6 +150,7 @@ bool isCorrect() {
cells[arr[1]].openPerm = true;
cells[arr[0]].open = false;
cells[arr[1]].open = false;
free(arr);
return true;
} else {
cells[arr[0]].open = false;
Expand All @@ -131,7 +171,7 @@ int main() {
switch (event.type) {
case sf::Event::Closed: {
window.close();
break;
continue;
}
case sf::Event::MouseButtonReleased: {
switch (event.mouseButton.button) {
Expand Down

0 comments on commit 90a33bd

Please sign in to comment.