Flexible Würfelanzahl

This commit is contained in:
aksaiyan18-design 2025-12-08 16:00:12 +01:00
parent ac117a847b
commit 0e28bbbf41

View File

@ -1,12 +1,21 @@
#include "../includes/gamecube.h" #include "gamecube.h"
#include <algorithm> #include <algorithm>
#include <ctime> #include <ctime>
#include <vector>
#include <iostream>
// ----------------------------------------------------------- // -----------------------------------------------------------
// 3D Memory Game Hauptprogramm // 3D Memory Game Hauptprogramm mit flexibler Paaranzahl
// ----------------------------------------------------------- // -----------------------------------------------------------
int main() int main()
{ {
// ✅ Schritt 1: Anzahl der Paare eingeben
int numPairs;
std::cout << "Geben Sie die Anzahl der Paare ein (2-10): ";
std::cin >> numPairs;
if(numPairs < 2) numPairs = 2;
if(numPairs > 10) numPairs = 10;
// Zufall initialisieren // Zufall initialisieren
srand(time(NULL)); srand(time(NULL));
@ -21,37 +30,42 @@ int main()
camera.fovy = 45.0f; camera.fovy = 45.0f;
camera.projection = CAMERA_PERSPECTIVE; camera.projection = CAMERA_PERSPECTIVE;
// Nur 3 Farben für 3 Paare // Farben generieren (maximal 10 Farben)
Color colors[] = { RED, GREEN, BLUE }; Color baseColors[10] = { RED, GREEN, BLUE, ORANGE, PURPLE, GOLD, PINK, BROWN, SKYBLUE, LIME };
std::vector<Color> colors(baseColors, baseColors + numPairs);
// 6 Karten-Positionen im 3x2 Raster // Positionen dynamisch berechnen (Raster 3 Spalten)
std::vector<Vec3> positions = {{-2, 0, -2}, {0, 0, -2}, {2, 0, -2},{-2, 0, 0}, {0, 0, 0}, {2, 0, 0}}; std::vector<Vec3> positions;
for (int i = 0; i < numPairs*2; ++i)
positions.push_back({ float(i % 3) * 2 - 2, 0, float(i / 3) * 2 - 2 });
// Farben doppelt in einen Pool legen und mischen // Farben doppelt in einen Pool legen und mischen
std::vector<Color> colorPool; std::vector<Color> colorPool;
for (int i = 0; i < 3; i++) for (int i = 0; i < numPairs; i++)
{ {
colorPool.push_back(colors[i]); colorPool.push_back(colors[i]);
colorPool.push_back(colors[i]); colorPool.push_back(colors[i]);
} }
// Fisher-Yates Shuffle mit rand() // Fisher-Yates Shuffle mit rand()
for (int i = colorPool.size() - 1; i > 0; --i) for (int i = colorPool.size() - 1; i > 0; --i)
{ {
int j = rand() % (i + 1); // Zufallsindex von 0 bis i int j = rand() % (i + 1);
std::swap(colorPool[i], colorPool[j]); std::swap(colorPool[i], colorPool[j]);
} }
// Karten/Würfel erstellen // Karten/Würfel erstellen
std::vector<gamecube> cubes; std::vector<gamecube> cubes;
for (int i = 0; i < 6; i++) for (int i = 0; i < numPairs*2; i++)
cubes.emplace_back(positions[i], colorPool[i]); cubes.emplace_back(positions[i], colorPool[i]);
gamecube* first = nullptr; gamecube* first = nullptr;
gamecube* second = nullptr; gamecube* second = nullptr;
float flipSpeed = 5.0f; // Drehgeschwindigkeit float flipSpeed = 5.0f;
bool gameWon = false; bool gameWon = false;
// ----------------------------------------------------------- // -----------------------------------------------------------
// Hauptspielschleife // Hauptspielschleife
// ----------------------------------------------------------- // -----------------------------------------------------------
@ -90,6 +104,8 @@ int main()
// Matching-Logik // Matching-Logik
if (first && second) if (first && second)
{ {
Color col1 = first->GetColor(); Color col1 = first->GetColor();
Color col2 = second->GetColor(); Color col2 = second->GetColor();
@ -104,9 +120,10 @@ int main()
second->FlipBackward(); second->FlipBackward();
} }
first = second = nullptr; first = second = nullptr; // Zurücksetzen für den nächsten Versuch
} }
// Gewinnprüfung // Gewinnprüfung
if (!gameWon) if (!gameWon)
gameWon = std::all_of(cubes.begin(), cubes.end(), [](const gamecube &c){ return c.IsMatched(); }); gameWon = std::all_of(cubes.begin(), cubes.end(), [](const gamecube &c){ return c.IsMatched(); });
@ -128,6 +145,8 @@ int main()
else else
DrawText("Flip 2 cubes - find matching pairs!", 10, 10, 20, DARKGRAY); DrawText("Flip 2 cubes - find matching pairs!", 10, 10, 20, DARKGRAY);
EndDrawing(); EndDrawing();
} }