racecondition, fixed bug
This commit is contained in:
parent
261c5d45eb
commit
f6cee165ee
99
src/main.cpp
99
src/main.cpp
@ -2,6 +2,7 @@
|
|||||||
#include "gamematrix.h"
|
#include "gamematrix.h"
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <ctime>
|
#include <ctime>
|
||||||
|
#include <cmath> // Für ceil
|
||||||
|
|
||||||
// -----------------------------------------------------------
|
// -----------------------------------------------------------
|
||||||
// 3D Memory Game – Hauptprogramm
|
// 3D Memory Game – Hauptprogramm
|
||||||
@ -9,7 +10,7 @@
|
|||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
// =====================================================================
|
// =====================================================================
|
||||||
// 1) KLEINES MENÜFENSTER
|
// 1) KLEINES MENÜFENSTER (UNVERÄNDERT)
|
||||||
// =====================================================================
|
// =====================================================================
|
||||||
|
|
||||||
InitWindow(400, 200, "Wuerfelspiel - Menu");
|
InitWindow(400, 200, "Wuerfelspiel - Menu");
|
||||||
@ -64,11 +65,6 @@ int main()
|
|||||||
CloseWindow(); // Menüfenster schließen
|
CloseWindow(); // Menüfenster schließen
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// =====================================================================
|
// =====================================================================
|
||||||
// 2) HAUPTSPIELFENSTER
|
// 2) HAUPTSPIELFENSTER
|
||||||
// =====================================================================
|
// =====================================================================
|
||||||
@ -77,6 +73,18 @@ int main()
|
|||||||
InitWindow(800, 600, "3D Memory Game with Matrix3D Library");
|
InitWindow(800, 600, "3D Memory Game with Matrix3D Library");
|
||||||
SetTargetFPS(60);
|
SetTargetFPS(60);
|
||||||
|
|
||||||
|
// NEU: GameState Definition
|
||||||
|
enum class GameState
|
||||||
|
{
|
||||||
|
Idle, // kein Würfel offen, Eingabe erlaubt
|
||||||
|
OneFlipped, // ein Würfel offen
|
||||||
|
CheckingMatch, // zwei Würfel vollständig aufgeklappt, Vergleich läuft
|
||||||
|
LockInput // Würfel drehen gerade – Eingabe kurz blockiert
|
||||||
|
};
|
||||||
|
|
||||||
|
// NEU: GameState Initialisierung
|
||||||
|
GameState state = GameState::Idle;
|
||||||
|
|
||||||
// Anzahl Spielzüge -> wie oft wurde ein Wuerfel geklickt
|
// Anzahl Spielzüge -> wie oft wurde ein Wuerfel geklickt
|
||||||
int turnCount = 0;
|
int turnCount = 0;
|
||||||
|
|
||||||
@ -86,14 +94,10 @@ int main()
|
|||||||
// gespielte Zeit
|
// gespielte Zeit
|
||||||
int playTime = 0;
|
int playTime = 0;
|
||||||
|
|
||||||
// anzahl an wuerfeln -> immer gerade anzahl und groeßer null -> jetzt nutzlos, weil menu window
|
|
||||||
//int numberOfCubes = 12;
|
|
||||||
|
|
||||||
// Zufall initialisieren
|
// Zufall initialisieren
|
||||||
srand(time(NULL));
|
srand(time(NULL));
|
||||||
|
|
||||||
Camera3D camera{};
|
Camera3D camera{};
|
||||||
//camera.position = {6.0f, 6.0f, 6.0f}; // changed camera position to show all cubes if number very high
|
|
||||||
camera.position = {12.0f, 9.0f, 12.0f};
|
camera.position = {12.0f, 9.0f, 12.0f};
|
||||||
camera.target = {0.0f, 0.0f, 0.0f};
|
camera.target = {0.0f, 0.0f, 0.0f};
|
||||||
camera.up = {0.0f, 1.0f, 0.0f};
|
camera.up = {0.0f, 1.0f, 0.0f};
|
||||||
@ -103,14 +107,8 @@ int main()
|
|||||||
// Nur 3 Farben für 3 Paare
|
// Nur 3 Farben für 3 Paare
|
||||||
Color colors[] = { RED, GREEN, BLUE };
|
Color colors[] = { RED, GREEN, BLUE };
|
||||||
|
|
||||||
//==================================================================================== (alter comment)
|
|
||||||
// 6 Karten-Positionen im 3x2 Raster
|
|
||||||
//std::vector<Vec3> positions = {{-2, 0, -2}, {0, 0, -2}, {2, 0, -2},
|
|
||||||
// {-2, 0, 0}, {0, 0, 0}, {2, 0, 0}};
|
|
||||||
//====================================================================================
|
|
||||||
|
|
||||||
// Positionen bestimmen für dynamisches NumberOfCubes
|
// Positionen bestimmen für dynamisches NumberOfCubes
|
||||||
std::vector<Vec3> positions; //noch leer, wird später befüllt in der schleife
|
std::vector<Vec3> positions;
|
||||||
int columns = ceil(sqrt(numberOfCubes));
|
int columns = ceil(sqrt(numberOfCubes));
|
||||||
int rows = ceil(numberOfCubes / (float)columns);
|
int rows = ceil(numberOfCubes / (float)columns);
|
||||||
float posi_x = 0;
|
float posi_x = 0;
|
||||||
@ -128,20 +126,19 @@ int main()
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Farben doppelt in einen Pool legen und mischen
|
// Farben doppelt in einen Pool legen und mischen
|
||||||
std::vector<Color> colorPool;
|
std::vector<Color> colorPool;
|
||||||
int numberOfPairs = numberOfCubes / 2;
|
int numberOfPairs = numberOfCubes / 2;
|
||||||
for (int i = 0; i < numberOfPairs; i++)
|
for (int i = 0; i < numberOfPairs; i++)
|
||||||
{
|
{
|
||||||
colorPool.push_back(colors[i % 3]); // i % 3, weil nur 3 Farben zur Zeit im Pool sein können -> keine out of range exception
|
colorPool.push_back(colors[i % 3]);
|
||||||
colorPool.push_back(colors[i % 3]);
|
colorPool.push_back(colors[i % 3]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -166,44 +163,74 @@ int main()
|
|||||||
playTime = static_cast<int>(time(nullptr) - startTime);
|
playTime = static_cast<int>(time(nullptr) - startTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Klick-Erkennung
|
// Klick-Erkennung (ÜBERSCHRIEBEN durch GameState-Logik)
|
||||||
if (!gameWon && IsMouseButtonPressed(MOUSE_LEFT_BUTTON))
|
// Steuerung des Spielflusses: Nur im Idle- oder OneFlipped-Zustand wird reagiert!
|
||||||
|
if (!gameWon
|
||||||
|
&& state != GameState::LockInput
|
||||||
|
&& state != GameState::CheckingMatch
|
||||||
|
&& IsMouseButtonPressed(MOUSE_LEFT_BUTTON))
|
||||||
{
|
{
|
||||||
turnCount++;
|
|
||||||
Vector2 mouse = GetMousePosition();
|
Vector2 mouse = GetMousePosition();
|
||||||
|
|
||||||
for (auto &c : cubes)
|
for (auto &c : cubes)
|
||||||
{
|
{
|
||||||
if (!c.IsFlipped() && !c.IsMatched())
|
if (!c.IsFlipped() && !c.IsMatched()) // Ist verdeckt und nicht gematcht
|
||||||
{
|
{
|
||||||
Vector2 screenPos = GetWorldToScreen({c.GetPosition().x, c.GetPosition().y, c.GetPosition().z}, camera);
|
Vector2 screenPos = GetWorldToScreen({c.GetPosition().x, c.GetPosition().y, c.GetPosition().z}, camera);
|
||||||
|
|
||||||
if (fabs(mouse.x - screenPos.x) < 40 && fabs(mouse.y - screenPos.y) < 40)
|
if (fabs(mouse.x - screenPos.x) < 40 && fabs(mouse.y - screenPos.y) < 40)
|
||||||
|
{
|
||||||
|
// Klick verarbeitet, Würfel umdrehen
|
||||||
c.FlipForward();
|
c.FlipForward();
|
||||||
|
turnCount++;
|
||||||
|
|
||||||
|
// Zustandswechsel:
|
||||||
|
if (state == GameState::Idle)
|
||||||
|
{
|
||||||
|
state = GameState::OneFlipped; // 1. Klick → OneFlipped
|
||||||
|
first = &c;
|
||||||
|
break; // Verlasse die Schleife, da Klick verarbeitet
|
||||||
|
}
|
||||||
|
else if (state == GameState::OneFlipped)
|
||||||
|
{
|
||||||
|
// Sicherstellen, dass nicht derselbe Würfel geklickt wird
|
||||||
|
if (&c != first)
|
||||||
|
{
|
||||||
|
state = GameState::LockInput; // 2. Klick → Animation läuft, Eingabe sperren
|
||||||
|
second = &c;
|
||||||
|
break; // Verlasse die Schleife
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// Ende Klick-Erkennung
|
||||||
|
|
||||||
// Animation aller Würfel
|
// Animation aller Würfel
|
||||||
for (auto &c : cubes)
|
for (auto &c : cubes)
|
||||||
{
|
{
|
||||||
c.Update(flipSpeed);
|
c.Update(flipSpeed);
|
||||||
|
}
|
||||||
|
|
||||||
// Sobald ein Würfel vollständig umgedreht ist → merken
|
// NEU: Zustandswechsel LockInput -> CheckingMatch
|
||||||
if (c.IsFlipped() && !c.IsMatched())
|
// Prüft, ob die Flip-Animationen abgeschlossen sind
|
||||||
|
if (state == GameState::LockInput && first && second)
|
||||||
|
{
|
||||||
|
// Warten auf das Ende der Umdreh-Animation. IsFlipped() prüft, ob der Würfel vollständig aufgedeckt ist.
|
||||||
|
if (first->IsFlipped() && second->IsFlipped()) // Änderung: !IsFlipping() ersetzt durch IsFlipped()
|
||||||
{
|
{
|
||||||
if (!first) first = &c;
|
state = GameState::CheckingMatch;
|
||||||
else if (!second && &c != first) second = &c;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Matching-Logik
|
// Matching-Logik (ÜBERSCHRIEBEN und an GameState gekoppelt)
|
||||||
if (first && second)
|
if (state == GameState::CheckingMatch && first && second)
|
||||||
{
|
{
|
||||||
Color col1 = first->GetColor();
|
// Vergleichslogik der Aufgabe
|
||||||
Color col2 = second->GetColor();
|
if (first->GetColor().r == second->GetColor().r &&
|
||||||
|
first->GetColor().g == second->GetColor().g &&
|
||||||
if (col1.r == col2.r && col1.g == col2.g && col1.b == col2.b)
|
first->GetColor().b == second->GetColor().b)
|
||||||
{
|
{
|
||||||
first->SetMatched(true);
|
first->SetMatched(true);
|
||||||
second->SetMatched(true);
|
second->SetMatched(true);
|
||||||
@ -214,7 +241,9 @@ int main()
|
|||||||
second->FlipBackward();
|
second->FlipBackward();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Rücksetzung und Zustandswechsel zu Idle
|
||||||
first = second = nullptr;
|
first = second = nullptr;
|
||||||
|
state = GameState::Idle;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Gewinnprüfung
|
// Gewinnprüfung
|
||||||
@ -222,7 +251,7 @@ int main()
|
|||||||
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(); });
|
||||||
|
|
||||||
// -----------------------------------------------------------
|
// -----------------------------------------------------------
|
||||||
// Zeichnen
|
// Zeichnen (UNVERÄNDERT)
|
||||||
// -----------------------------------------------------------
|
// -----------------------------------------------------------
|
||||||
BeginDrawing();
|
BeginDrawing();
|
||||||
ClearBackground(RAYWHITE);
|
ClearBackground(RAYWHITE);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user