Compare commits

...

2 Commits

Author SHA1 Message Date
BrSh31
cb751b132b to be ignored, task 4b 2025-12-22 17:13:47 +01:00
19304d3344 gamestate update 2025-12-08 17:00:36 +01:00
4 changed files with 203 additions and 29 deletions

View File

@ -22,6 +22,7 @@ public:
Vec3 GetPosition() const;
float GetRotationY() const;
Color GetColor() const { return color; }
bool IsAnimating() const;
//zustände klarer abbikden bzw weniger bools = klarare Logik
private:
Vec3 position;

View File

@ -64,3 +64,7 @@ void gamecube::Draw() const
Vec3 gamecube::GetPosition() const { return position; }
float gamecube::GetRotationY() const { return rotation; }
bool gamecube::IsAnimating() const
{
return flippingForward || flippingBackward;
}

View File

@ -54,80 +54,124 @@ int main()
float flipSpeed = 5.0f; // Drehgeschwindigkeit
bool gameWon = false;
enum GameState { MENU, GAME };
GameState state = MENU;
int pairCount = 3;
enum class GameState
{
Menu,
Idle, // kein Würfel offen, Eingabe erlaubt
OneFlipped, // ein Würfel offen
LockInput, // Würfel drehen gerade Eingabe kurz blockiert
CheckingMatch // zwei Würfel vollständig aufgeklappt, Vergleich läuft
};
GameState state = GameState::Menu;
while (!WindowShouldClose()) {
if (state == MENU) {
if (state == GameState::Menu)
{
MenuResult res = DrawMenu();
switch (res) {
case MENU_SELECT_3: pairCount = 3; state = GAME; break;
case MENU_SELECT_6: pairCount = 6; state = GAME; break;
case MENU_SELECT_9: pairCount = 9; state = GAME; break;
default: break;
switch (res)
{
case MENU_SELECT_3:
case MENU_SELECT_6:
case MENU_SELECT_9:
state = GameState::Idle;
break;
default:
break;
}
if (state == GameState::Menu)
continue;
}
// === GAME ===
// Klick-Erkennung
if (!gameWon && IsMouseButtonPressed(MOUSE_LEFT_BUTTON))
if (!gameWon
&& state != GameState::LockInput
&& state != GameState::CheckingMatch
&& IsMouseButtonPressed(MOUSE_LEFT_BUTTON))
{
Vector2 mouse = GetMousePosition();
for (auto &c : cubes)
{
if (!c.IsFlipped() && !c.IsMatched())
if (!c.IsAnimating() && !c.IsMatched())
{
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)
{
c.FlipForward();
if (!first)
{
first = &c;
state = GameState::OneFlipped;
}
else if (!second)
{
second = &c;
state = GameState::LockInput;
}
break;
}
}
}
}
// Animation aller Würfel
bool anyAnimating = false;
for (auto &c : cubes)
{
c.Update(flipSpeed);
if (c.IsAnimating()) anyAnimating = true;
}
// Sobald ein Würfel vollständig umgedreht ist → merken
if (c.IsFlipped() && !c.IsMatched())
if (state == GameState::LockInput && !anyAnimating)
{
if (!first) first = &c;
else if (!second && &c != first) second = &c;
// If both pointers are null → this was a failed attempt finishing its backflip
if (!first && !second)
{
state = GameState::Idle;
}
else
{
// Two cubes fully opened → proceed to matching
state = GameState::CheckingMatch;
}
}
// Matching-Logik
if (first && second)
if (state == GameState::CheckingMatch && first && second)
{
Color col1 = first->GetColor();
Color col2 = second->GetColor();
bool match =
first->GetColor().r == second->GetColor().r &&
first->GetColor().g == second->GetColor().g &&
first->GetColor().b == second->GetColor().b;
if (col1.r == col2.r && col1.g == col2.g && col1.b == col2.b)
if (match)
{
first->SetMatched(true);
second->SetMatched(true);
first = nullptr;
second = nullptr;
state = GameState::Idle;
}
else
{
first->FlipBackward();
second->FlipBackward();
}
first = second = nullptr;
}
first = nullptr;
second = nullptr;
// Gewinnprüfung
if (!gameWon)
gameWon = std::all_of(cubes.begin(), cubes.end(), [](const gamecube &c){ return c.IsMatched(); });
// WAIT FOR BACKFLIP ANIMATION
state = GameState::LockInput;
}
}
// -----------------------------------------------------------
// Zeichnen

125
src/thread.cpp Normal file
View File

@ -0,0 +1,125 @@
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <thread>
#include <chrono>
#include <numeric>
bool readFile(const std::string& filename, std::vector<int>& data) {
std::ifstream file(filename);
if (!file.is_open()) {
std::cerr << "FEHLER: Konnte Datei nicht oeffnen!\n";
std::cerr << "Pfad: " << filename << "\n";
return false;
}
int value;
std::cout << "Lese Datei ein... (Pfad: " << filename << ")\n";
while (file >> value) {
data.push_back(value);
}
file.close();
return true;
}
int main() {
std::vector<int> messungen;
std::string pfad = "C:\\Users\\Bruno\\Desktop\\BME\\BME 3\\PROG B\\praktikum\\thread\\measurements.txt";
if (!readFile(pfad, messungen)) {
std::cout << "\nDruecke ENTER zum Beenden...";
std::cin.get();
return 1;
}
if (messungen.size() < 2) {
std::cout << "Datei ist leer oder hat zu wenig Werte!\n";
std::cin.get();
return 0;
}
std::cout << "Erfolg! " << messungen.size() << " Messwerte geladen.\n";
std::cout << "-----------------------------------\n";
{
std::cout << "Starte serielle Berechnung...\n";
auto start = std::chrono::steady_clock::now();
long long increases = 0;
for (size_t i = 1; i < messungen.size(); i++) {
if (messungen[i] > messungen[i - 1]) {
increases++;
}
}
auto end = std::chrono::steady_clock::now();
std::chrono::duration<double> diff = end - start;
std::cout << "[SERIELL] Anstiege: " << increases << "\n";
std::cout << " Zeit: " << diff.count() << " s\n\n";
}
{
std::cout << "Starte parallele Berechnung...\n";
auto start = std::chrono::steady_clock::now();
unsigned int numThreads = std::thread::hardware_concurrency();
if (numThreads == 0) numThreads = 4;
std::vector<std::thread> threads;
std::vector<long long> results(numThreads, 0);
size_t totalComparisons = messungen.size() - 1;
size_t base = totalComparisons / numThreads;
size_t remainder = totalComparisons % numThreads;
size_t idx = 1;
for (unsigned int t = 0; t < numThreads; t++)
{
size_t startIdx = idx;
size_t count = base + (t < remainder ? 1 : 0);
size_t endIdx = idx + count - 1;
idx = endIdx + 1;
threads.emplace_back([&, t, startIdx, endIdx]() {
long long localCount = 0;
if (startIdx < messungen.size()) {
for (size_t i = startIdx; i <= endIdx; ++i) {
if (messungen[i] > messungen[i - 1]) {
++localCount;
}
}
}
results[t] = localCount;
});
}
for (auto& th : threads) {
if (th.joinable())
th.join();
}
long long totalParallel = 0;
for (long long r : results) {
totalParallel += r;
}
auto end = std::chrono::steady_clock::now();
std::chrono::duration<double> diff = end - start;
std::cout << "[PARALLEL] Anstiege: " << totalParallel << " (Threads: " << numThreads << ")\n";
std::cout << " Zeit: " << diff.count() << " s\n";
}
std::cout << "\n-----------------------------------\n";
std::cout << "Druecke ENTER zum Beenden...";
std::cin.get();
return 0;
}