Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cb751b132b | ||
| 19304d3344 |
@ -22,6 +22,7 @@ public:
|
|||||||
Vec3 GetPosition() const;
|
Vec3 GetPosition() const;
|
||||||
float GetRotationY() const;
|
float GetRotationY() const;
|
||||||
Color GetColor() const { return color; }
|
Color GetColor() const { return color; }
|
||||||
|
bool IsAnimating() const;
|
||||||
//zustände klarer abbikden bzw weniger bools = klarare Logik
|
//zustände klarer abbikden bzw weniger bools = klarare Logik
|
||||||
private:
|
private:
|
||||||
Vec3 position;
|
Vec3 position;
|
||||||
|
|||||||
@ -64,3 +64,7 @@ void gamecube::Draw() const
|
|||||||
|
|
||||||
Vec3 gamecube::GetPosition() const { return position; }
|
Vec3 gamecube::GetPosition() const { return position; }
|
||||||
float gamecube::GetRotationY() const { return rotation; }
|
float gamecube::GetRotationY() const { return rotation; }
|
||||||
|
bool gamecube::IsAnimating() const
|
||||||
|
{
|
||||||
|
return flippingForward || flippingBackward;
|
||||||
|
}
|
||||||
98
src/main.cpp
98
src/main.cpp
@ -54,80 +54,124 @@ int main()
|
|||||||
float flipSpeed = 5.0f; // Drehgeschwindigkeit
|
float flipSpeed = 5.0f; // Drehgeschwindigkeit
|
||||||
bool gameWon = false;
|
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()) {
|
while (!WindowShouldClose()) {
|
||||||
|
|
||||||
if (state == MENU) {
|
if (state == GameState::Menu)
|
||||||
|
{
|
||||||
MenuResult res = DrawMenu();
|
MenuResult res = DrawMenu();
|
||||||
|
switch (res)
|
||||||
switch (res) {
|
{
|
||||||
case MENU_SELECT_3: pairCount = 3; state = GAME; break;
|
case MENU_SELECT_3:
|
||||||
case MENU_SELECT_6: pairCount = 6; state = GAME; break;
|
case MENU_SELECT_6:
|
||||||
case MENU_SELECT_9: pairCount = 9; state = GAME; break;
|
case MENU_SELECT_9:
|
||||||
default: break;
|
state = GameState::Idle;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
if (state == GameState::Menu)
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// === GAME ===
|
// === GAME ===
|
||||||
// Klick-Erkennung
|
// Klick-Erkennung
|
||||||
if (!gameWon && IsMouseButtonPressed(MOUSE_LEFT_BUTTON))
|
if (!gameWon
|
||||||
|
&& state != GameState::LockInput
|
||||||
|
&& state != GameState::CheckingMatch
|
||||||
|
&& IsMouseButtonPressed(MOUSE_LEFT_BUTTON))
|
||||||
{
|
{
|
||||||
Vector2 mouse = GetMousePosition();
|
Vector2 mouse = GetMousePosition();
|
||||||
|
|
||||||
for (auto &c : cubes)
|
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);
|
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)
|
||||||
|
{
|
||||||
c.FlipForward();
|
c.FlipForward();
|
||||||
|
if (!first)
|
||||||
|
{
|
||||||
|
first = &c;
|
||||||
|
state = GameState::OneFlipped;
|
||||||
|
}
|
||||||
|
else if (!second)
|
||||||
|
{
|
||||||
|
second = &c;
|
||||||
|
state = GameState::LockInput;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Animation aller Würfel
|
// Animation aller Würfel
|
||||||
|
bool anyAnimating = false;
|
||||||
for (auto &c : cubes)
|
for (auto &c : cubes)
|
||||||
{
|
{
|
||||||
c.Update(flipSpeed);
|
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;
|
// If both pointers are null → this was a failed attempt finishing its backflip
|
||||||
else if (!second && &c != first) second = &c;
|
if (!first && !second)
|
||||||
|
{
|
||||||
|
state = GameState::Idle;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Two cubes fully opened → proceed to matching
|
||||||
|
state = GameState::CheckingMatch;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Matching-Logik
|
// Matching-Logik
|
||||||
if (first && second)
|
if (state == GameState::CheckingMatch && first && second)
|
||||||
{
|
{
|
||||||
Color col1 = first->GetColor();
|
bool match =
|
||||||
Color col2 = second->GetColor();
|
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);
|
first->SetMatched(true);
|
||||||
second->SetMatched(true);
|
second->SetMatched(true);
|
||||||
|
|
||||||
|
first = nullptr;
|
||||||
|
second = nullptr;
|
||||||
|
|
||||||
|
state = GameState::Idle;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
first->FlipBackward();
|
first->FlipBackward();
|
||||||
second->FlipBackward();
|
second->FlipBackward();
|
||||||
}
|
|
||||||
|
|
||||||
first = second = nullptr;
|
first = nullptr;
|
||||||
}
|
second = nullptr;
|
||||||
|
|
||||||
// Gewinnprüfung
|
// WAIT FOR BACKFLIP ANIMATION
|
||||||
if (!gameWon)
|
state = GameState::LockInput;
|
||||||
gameWon = std::all_of(cubes.begin(), cubes.end(), [](const gamecube &c){ return c.IsMatched(); });
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// -----------------------------------------------------------
|
// -----------------------------------------------------------
|
||||||
// Zeichnen
|
// Zeichnen
|
||||||
|
|||||||
125
src/thread.cpp
Normal file
125
src/thread.cpp
Normal 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;
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user