2 Commits
Author SHA1 Message Date
aksaiyan18-design ece490a1b5 Gamematrix.cpp 2025-11-10 16:52:39 +01:00
aksaiyan18-design 9e9bb92034 gamematrix hinzufügen 2025-11-10 16:48:33 +01:00
10 changed files with 44 additions and 369 deletions

No files matched your search

-2
View File
@@ -1,2 +0,0 @@
.idea
cmake-build-debug
+17 -34
View File
@@ -1,52 +1,35 @@
cmake_minimum_required(VERSION 3.28) cmake_minimum_required(VERSION 3.28)
project(Prog3B) project(Prog3B)
set(EXECUTABLE_NAME Prog3B) set(EXECUTABLE_NAME Prog3B)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
# Default build type # Generate compile_commands.json
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_CXX_STANDARD 20)
# Set the default build type if not specified
if(NOT CMAKE_BUILD_TYPE) if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release CACHE STRING "Build type" FORCE) set(CMAKE_BUILD_TYPE Release CACHE STRING "Build type" FORCE)
endif() endif()
# Quell- und Header-Dateien
set(SRC_FILES set(SRC_FILES
${CMAKE_CURRENT_LIST_DIR}/src/main.cpp ${CMAKE_CURRENT_LIST_DIR}/main.cpp
${CMAKE_CURRENT_LIST_DIR}/src/gamecube.cpp ${CMAKE_CURRENT_LIST_DIR}/gamecube.cpp
${CMAKE_CURRENT_LIST_DIR}/src/gamematrix.cpp
includes/gamestate.h
src/seriell.cpp
) )
#automatisch hinzufügen
file(GLOB SRC_FILES "${CMAKE_CURRENT_LIST_DIR}/src/*.cpp")
set(INCLUDE_DIRS set(INCLUDE_DIRS
${CMAKE_CURRENT_LIST_DIR}/includes ${CMAKE_CURRENT_LIST_DIR}/linux
${CMAKE_CURRENT_LIST_DIR}/raylib
) )
# Executable erstellen
add_executable(${EXECUTABLE_NAME} ${SRC_FILES}) add_executable(${EXECUTABLE_NAME} ${SRC_FILES})
# Include-Verzeichnisse hinzufügen
target_include_directories(${EXECUTABLE_NAME} PRIVATE ${INCLUDE_DIRS}) target_include_directories(${EXECUTABLE_NAME} PRIVATE ${INCLUDE_DIRS})
target_link_libraries(${EXECUTABLE_NAME} PRIVATE
${CMAKE_CURRENT_LIST_DIR}/linux/libgamematrix.a
${CMAKE_CURRENT_LIST_DIR}/linux/libraylib.a
)
# Checks if OSX and links appropriate frameworks (Only required on MacOS)
# Nur noch raylib und systemabhängige Libraries linken if (APPLE)
if(WIN32) target_link_libraries(Prog3B "-framework IOKit")
target_link_libraries(${EXECUTABLE_NAME} PRIVATE target_link_libraries(Prog3B "-framework Cocoa")
${CMAKE_CURRENT_LIST_DIR}/windows/libraylib.a target_link_libraries(Prog3B "-framework OpenGL")
winmm
)
endif()
# macOS
if(APPLE)
target_link_libraries(${EXECUTABLE_NAME} PRIVATE
${CMAKE_CURRENT_LIST_DIR}/mac_arm/libraylib.a
"-framework Cocoa"
"-framework IOKit"
"-framework OpenGL"
)
endif() endif()
-5
View File
@@ -16,9 +16,4 @@ public:
// Verschiebung // Verschiebung
static std::array<std::array<double,4>,4> translate(const std::array<double, 3>& pos); static std::array<std::array<double,4>,4> translate(const std::array<double, 3>& pos);
// 4x4 Einheitsmatrix
static std::array<std::array<double,4>,4> identity(); // <-- add this
}; };
-17
View File
@@ -1,17 +0,0 @@
//
// Created by Anke Bidlingmaier on 08.12.25.
//
#pragma once
#ifndef PROG3B_GAMESTATE_H
#define PROG3B_GAMESTATE_H
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
};
#endif //PROG3B_GAMESTATE_H
-94
View File
@@ -1,94 +0,0 @@
#include <vector>
#include <chrono>
#include <iostream>
#include <omp.h>
#include <cstdlib>
#include <ctime>
// Zufallsmatrizen erzeugen
void generateRandomMatrices(int n,
std::vector<std::vector<double>>& A,
std::vector<std::vector<double>>& B)
{
std::srand(static_cast<unsigned int>(std::time(nullptr)));
A.resize(n, std::vector<double>(n));
B.resize(n, std::vector<double>(n));
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
{
A[i][j] = static_cast<double>(rand()) / RAND_MAX * 10.0;
B[i][j] = static_cast<double>(rand()) / RAND_MAX * 10.0;
}
}
// Serielle Matrizenmultiplikation
std::vector<std::vector<double>> matmul_serial(
const std::vector<std::vector<double>>& A,
const std::vector<std::vector<double>>& B)
{
int n = A.size();
int m = B[0].size();
int p = B.size();
std::vector<std::vector<double>> C(n, std::vector<double>(m, 0.0));
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j)
for (int k = 0; k < p; ++k)
C[i][j] += A[i][k] * B[k][j];
return C;
}
// Spaltenparallel (Column-parallel)
std::vector<std::vector<double>> matmul_column_parallel(
const std::vector<std::vector<double>>& A,
const std::vector<std::vector<double>>& B)
{
int n = A.size();
int m = B[0].size();
int p = B.size();
std::vector<std::vector<double>> C(n, std::vector<double>(m, 0.0));
#pragma omp parallel for
for (int j = 0; j < m; ++j) // Spalten parallel
for (int i = 0; i < n; ++i)
for (int k = 0; k < p; ++k)
C[i][j] += A[i][k] * B[k][j];
return C;
}
// -----------------------------
int main()
{
int N = 500;
std::vector<std::vector<double>> A, B;
generateRandomMatrices(N, A, B);
// Serielle Version
auto start = std::chrono::steady_clock::now();
auto C_serial = matmul_serial(A, B);
auto end = std::chrono::steady_clock::now();
std::cout << "Serial multiplication took: "
<< std::chrono::duration<double>(end - start).count() << "s\n";
// Spaltenparallel
start = std::chrono::steady_clock::now();
auto C_col = matmul_column_parallel(A, B);
end = std::chrono::steady_clock::now();
std::cout << "Column-parallel multiplication took: "
<< std::chrono::duration<double>(end - start).count() << "s\n";
// Vergleich
for (int i = 0; i < N; ++i)
for (int j = 0; j < N; ++j)
if (C_serial[i][j] != C_col[i][j])
{
std::cout << "Mismatch at " << i << "," << j << "\n";
return 1;
}
std::cout << "All results match!\n";
return 0;
}
-94
View File
@@ -1,94 +0,0 @@
#include <vector>
#include <chrono>
#include <iostream>
#include <omp.h>
#include <cstdlib>
#include <ctime>
// Zufallsmatrizen erzeugen
void generateRandomMatrices(int n,
std::vector<std::vector<double>>& A,
std::vector<std::vector<double>>& B)
{
std::srand(static_cast<unsigned int>(std::time(nullptr)));
A.resize(n, std::vector<double>(n));
B.resize(n, std::vector<double>(n));
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
{
A[i][j] = static_cast<double>(rand()) / RAND_MAX * 10.0;
B[i][j] = static_cast<double>(rand()) / RAND_MAX * 10.0;
}
}
// Serielle Matrizenmultiplikation
std::vector<std::vector<double>> matmul_serial(
const std::vector<std::vector<double>>& A,
const std::vector<std::vector<double>>& B)
{
int n = A.size();
int m = B[0].size();
int p = B.size();
std::vector<std::vector<double>> C(n, std::vector<double>(m, 0.0));
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j)
for (int k = 0; k < p; ++k)
C[i][j] += A[i][k] * B[k][j];
return C;
}
// Parallele Zeilenzerlegung
std::vector<std::vector<double>> matmul_row_parallel(
const std::vector<std::vector<double>>& A,
const std::vector<std::vector<double>>& B)
{
int n = A.size();
int m = B[0].size();
int p = B.size();
std::vector<std::vector<double>> C(n, std::vector<double>(m, 0.0));
#pragma omp parallel for
for (int i = 0; i < n; ++i) // Zeilen parallel
for (int j = 0; j < m; ++j)
for (int k = 0; k < p; ++k)
C[i][j] += A[i][k] * B[k][j];
return C;
}
// -----------------------------
int main()
{
int N = 500;
std::vector<std::vector<double>> A, B;
generateRandomMatrices(N, A, B);
// Serielle Version
auto start = std::chrono::steady_clock::now();
auto C_serial = matmul_serial(A, B);
auto end = std::chrono::steady_clock::now();
std::cout << "Serial multiplication took: "
<< std::chrono::duration<double>(end - start).count() << "s\n";
// Zeilenparallel
start = std::chrono::steady_clock::now();
auto C_row = matmul_row_parallel(A, B);
end = std::chrono::steady_clock::now();
std::cout << "Row-parallel multiplication took: "
<< std::chrono::duration<double>(end - start).count() << "s\n";
// Vergleich
for (int i = 0; i < N; ++i)
for (int j = 0; j < N; ++j)
if (C_serial[i][j] != C_row[i][j])
{
std::cout << "Mismatch at " << i << "," << j << "\n";
return 1;
}
std::cout << "All results match!\n";
return 0;
}
Binary file not shown.

Before

Width:  |  Height:  |  Size: 560 KiB

-67
View File
@@ -1,67 +0,0 @@
#include <vector>
#include <chrono>
#include <iostream>
// Funktion erzeugt zwei Matrizen A und B der Größe n x n mit Zufallswerten
void generateRandomMatrices(int n,
std::vector<std::vector<double>>& A,
std::vector<std::vector<double>>& B,
double min_val = 0.0,
double max_val = 10.0)
{
// Zufallsgenerator initialisieren
std::srand(static_cast<unsigned int>(std::time(nullptr)));
A.resize(n, std::vector<double>(n));
B.resize(n, std::vector<double>(n));
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
{
double r1 = static_cast<double>(std::rand()) / RAND_MAX; // 0..1
double r2 = static_cast<double>(std::rand()) / RAND_MAX; // 0..1
A[i][j] = min_val + r1 * (max_val - min_val);
B[i][j] = min_val + r2 * (max_val - min_val);
}
}
// Matrizenmultiplikation
std::vector<std::vector<double>> matmul(const std::vector<std::vector<double>>& A,
const std::vector<std::vector<double>>& B)
{
int n = A.size();
int m = B[0].size();
int p = B.size();
if (A[0].size() != B.size())
throw std::runtime_error("Matrixgrößen passen nicht.");
std::vector<std::vector<double>> C(n, std::vector<double>(m, 0.0));
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j)
for (int k = 0; k < p; ++k)
C[i][j] += A[i][k] * B[k][j];
return C;
}
int main()
{
int N = 500; // z.B. 500x500
std::vector<std::vector<double>> A, B;
auto start = std::chrono::steady_clock::now();
generateRandomMatrices(N, A, B);
auto C_serial = matmul(A, B);
auto end = std::chrono::steady_clock::now();
std::chrono::duration<double> diff = end - start;
std::cout << "Took: " << diff.count() << "s\n";
return 0;
}
//
// Created by Anke Bidlingmaier on 15.12.25.
+8 -10
View File
@@ -53,16 +53,6 @@ std::array<std::array<double,4>,4> gameMatrix::rot3D(double angle_deg, char axis
return R; return R;
} }
// Einheitsmatrix-Matrix
std::array<std::array<double,4>,4> gameMatrix::identity()
{
std::array<std::array<double,4>,4> I = {0};
for (int i = 0; i < 4; ++i)
I[i][i] = 1.0;
return I;
}
// Translation // Translation
std::array<std::array<double,4>,4> gameMatrix::translate(const std::array<double,3>& pos) std::array<std::array<double,4>,4> gameMatrix::translate(const std::array<double,3>& pos)
{ {
@@ -75,3 +65,11 @@ std::array<std::array<double,4>,4> gameMatrix::translate(const std::array<double
return T; return T;
} }
// Einheitsmatrix-Matrix
std::array<std::array<double,4>,4> gameMatrix::identity()
{
std::array<std::array<double,4>,4> I = {0};
for (int i = 0; i < 4; ++i)
I[i][i] = 1.0;
return I;
}
+19 -46
View File
@@ -1,22 +1,12 @@
#include "gamecube.h" #include "../includes/gamecube.h"
#include <algorithm> #include <algorithm>
#include <ctime> #include <ctime>
#include <vector>
#include <iostream>
#include "gamestate.h"
// ----------------------------------------------------------- // -----------------------------------------------------------
// 3D Memory Game – Hauptprogramm mit flexibler Paaranzahl // 3D Memory Game – Hauptprogramm
// ----------------------------------------------------------- // -----------------------------------------------------------
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));
@@ -31,50 +21,45 @@ int main()
camera.fovy = 45.0f; camera.fovy = 45.0f;
camera.projection = CAMERA_PERSPECTIVE; camera.projection = CAMERA_PERSPECTIVE;
// Farben generieren (maximal 10 Farben) // Nur 3 Farben für 3 Paare
Color baseColors[10] = { RED, GREEN, BLUE, ORANGE, PURPLE, GOLD, PINK, BROWN, SKYBLUE, LIME }; Color colors[] = { RED, GREEN, BLUE };
std::vector<Color> colors(baseColors, baseColors + numPairs);
// Positionen dynamisch berechnen (Raster 3 Spalten) // 6 Karten-Positionen im 3x2 Raster
std::vector<Vec3> positions; std::vector<Vec3> positions = {{-2, 0, -2}, {0, 0, -2}, {2, 0, -2},{-2, 0, 0}, {0, 0, 0}, {2, 0, 0}};
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 < numPairs; i++) for (int i = 0; i < 3; 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); int j = rand() % (i + 1); // Zufallsindex von 0 bis i
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 < numPairs*2; i++) for (int i = 0; i < 6; 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; float flipSpeed = 5.0f; // Drehgeschwindigkeit
bool gameWon = false; bool gameWon = false;
GameState state = GameState::Idle;
// ----------------------------------------------------------- // -----------------------------------------------------------
// Hauptspielschleife // Hauptspielschleife
// ----------------------------------------------------------- // -----------------------------------------------------------
while (!WindowShouldClose()) while (!WindowShouldClose())
{ {
//Klick-Erkennung // Klick-Erkennung
if (!gameWon && state != GameState::LockInput && state != GameState::CheckingMatch && IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) if (!gameWon && IsMouseButtonPressed(MOUSE_LEFT_BUTTON))
{ {
Vector2 mouse = GetMousePosition(); Vector2 mouse = GetMousePosition();
for (auto &c : cubes) for (auto &c : cubes)
@@ -83,12 +68,8 @@ int main()
{ {
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();
state = GameState::OneFlipped;
}
} }
} }
} }
@@ -104,20 +85,15 @@ int main()
if (!first) first = &c; if (!first) first = &c;
else if (!second && &c != first) second = &c; else if (!second && &c != first) second = &c;
} }
if (first && second && first->IsFlipped() && second->IsFlipped()) {
state = GameState::LockInput;
}
if (state == GameState::LockInput) {
state = GameState::CheckingMatch;
}
} }
// Matching-Logik // Matching-Logik
if (state == GameState::CheckingMatch && first && second) if (first && second)
{ {
if (first->GetColor().r == second->GetColor().r && Color col1 = first->GetColor();
first->GetColor().g == second->GetColor().g && Color col2 = second->GetColor();
first->GetColor().b == second->GetColor().b)
if (col1.r == col2.r && col1.g == col2.g && col1.b == col2.b)
{ {
first->SetMatched(true); first->SetMatched(true);
second->SetMatched(true); second->SetMatched(true);
@@ -129,7 +105,6 @@ int main()
} }
first = second = nullptr; first = second = nullptr;
state = GameState::Idle;
} }
// Gewinnprüfung // Gewinnprüfung
@@ -153,8 +128,6 @@ 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();
} }