29 Commits
Author SHA1 Message Date
dimartinoma102424 f6cee165ee racecondition, fixed bug 2025-12-01 14:53:51 +01:00
fischerth80683 261c5d45eb added menu for game init 2025-12-01 13:40:41 +01:00
fischerth80683 323bd67577 added dynamic cube count 2025-12-01 13:20:18 +01:00
fischerth80683 3c7ae2a9b3 added playtime functionality 2025-11-29 17:03:35 +01:00
fischerth80683 c9dd4958c7 added Turncount functionality 2025-11-29 16:47:11 +01:00
fischerth80683 5738586110 Remove build folder and add to .gitignore 2025-11-29 15:29:51 +01:00
graapka99558 8eda60d498 Merge remote-tracking branch 'origin/main'
# Conflicts:
#	CMakeLists.txt
2025-11-17 15:14:36 +01:00
graapka99558 69dfbdf139 Funktionsfähig gemacht 2025-11-17 15:13:47 +01:00
graapka99558 16e918b180 includes 2025-11-17 14:58:28 +01:00
graapka99558 48903fdf01 hinzugefügt cmake:current 2025-11-17 14:58:17 +01:00
dimartinoma102424 6e2119ab01 Merge branch 'project_tests' 2025-11-17 14:29:53 +01:00
graapka99558 ff5ec85747 Merge branch 'developer' 2025-11-17 14:28:19 +01:00
fischerth80683 4bc2574c03 Merge branch 'project_leader' 2025-11-17 14:26:09 +01:00
graapka99558 7a34810145 gamematrix.h included 2025-11-17 14:20:13 +01:00
dimartinoma102424 5b467f42ae tests.cpp commited 2025-11-17 14:17:38 +01:00
graapka99558 00bb22347f Gamematrix erstellt 2 2025-11-17 14:12:14 +01:00
graapka99558 c3dc997071 Gamematrix erstellt 2025-11-17 14:10:13 +01:00
dimartinoma102424 8023f4e866 CMAKE list is f***ed 2025-11-03 15:31:53 +01:00
dimartinoma102424 a8b2545b6c marco test files 2025-11-03 15:29:22 +01:00
graapka99558 a561b26425 CMake angepasst 2025-11-03 15:24:49 +01:00
graapka99558 96a19a6312 Design.txt Änderungen 2025-11-03 15:21:44 +01:00
graapka99558 0c71e3d2c7 Docs Design 2025-11-03 15:10:58 +01:00
dimartinoma102424 aa7f72820b Test Dokument erstellt 2025-11-03 15:08:56 +01:00
graapka99558 5ff1281385 Docs Design + Gamematrix 2025-11-03 15:04:36 +01:00
fischerth80683 d3ce2aa5f8 added folder docs/requirements.txt 2025-11-03 14:57:55 +01:00
graapka99558 a1e712dcf7 Gamematrix.cpp erstellt 2025-11-03 14:31:45 +01:00
graapka99558 93759ebef1 Gamematrix erstellt 2025-11-03 14:30:03 +01:00
fischerth80683 28717ca4ed Merge branch 'main' of https://git.efi.th-nuernberg.de/gitea/fischerth80683/Prog3 2025-11-03 14:05:44 +01:00
fischerth80683 d2effaa92f Projektstruktur abgeändert; Added src, includes folders; Misc changes: var names in gamecube.cpp; Altered Cmakelists.txt for changed folder structur 2025-11-03 13:27:41 +01:00
14 changed files with 762 additions and 207 deletions

No files matched your search

+18 -5
View File
@@ -12,19 +12,32 @@ if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release CACHE STRING "Build type" FORCE)
endif()
##Tom: /src/ dazu gefügt
set(SRC_FILES
${CMAKE_CURRENT_LIST_DIR}/main.cpp
${CMAKE_CURRENT_LIST_DIR}/gamecube.cpp
${CMAKE_CURRENT_LIST_DIR}/src/main.cpp
${CMAKE_CURRENT_LIST_DIR}/src/gamecube.cpp
src/gamematrix.cpp
)
##Tom: prev: ${CMAKE_CURRENT_LIST_DIR}/linux -> now /include
set(INCLUDE_DIRS
${CMAKE_CURRENT_LIST_DIR}/linux
${CMAKE_CURRENT_LIST_DIR}/include
)
add_executable(${EXECUTABLE_NAME} ${SRC_FILES})
target_include_directories(${EXECUTABLE_NAME} PRIVATE ${INCLUDE_DIRS})
##Tom: changed; old: target_include_directories(${EXECUTABLE_NAME} PRIVATE ${INCLUDE_DIRS})
## also added: line for raylib
target_include_directories(${EXECUTABLE_NAME} PRIVATE
${INCLUDE_DIRS}
${CMAKE_CURRENT_LIST_DIR}/includes
${CMAKE_CURRENT_SOURCE_DIR}/raylib
)
target_link_libraries(${EXECUTABLE_NAME} PRIVATE
${CMAKE_CURRENT_LIST_DIR}/${OS_NAME}/libgamematrix.a
# ${CMAKE_CURRENT_LIST_DIR}/${OS_NAME}/libgamematrix.a
${CMAKE_CURRENT_LIST_DIR}/${OS_NAME}/libraylib.a
)
if (WIN32)
View File
File renamed without changes.
+74
View File
@@ -0,0 +1,74 @@
========================================================
Projekt: gamematrix (C++ Library)
Rolle: Architekt
Datei: design.txt
Datum: 03-November
Team: Thomas&Co
========================================================
# ----------------------------
# 1. Projektstruktur / Namespace
# ----------------------------
Namespace: Matrix3D
Ziel: Saubere Trennung der Bibliothek, Vermeidung von Namenskonflikten.
Beispiel:
namespace Matrix3D {
// Funktionen, ggf Klasse(n)
}
# ----------------------------
# 2. Datenstrukturen / Klassen
# ----------------------------
Listen Sie die Klassen oder Structs auf, die verwendet werden:
| Name | Typ | Beschreibung |
|--------|------------------------------------------|--------------|
| Vec3 | struct Vec3 | 3D-Vektor (x, y, z) |
| Mat4 | std::array<std::array<double,4>,4> | 4x4-Matrix (homogen) |
| ______ | ________ | ___________________ |
| ______ | ________ | ___________________ |
# ----------------------------
# 3. Operatoren / Templates
# ----------------------------
Welche Operatoren oder Templates sollen definiert werden?
- Templates für unterschiedliche Datentypen? ☐ Ja ☐ Nein
- Operatoren:
- Mat4 * Mat4
- Mat4 * Vec3
# ----------------------------
# 4. Funktionen / Schnittstellen
# ----------------------------
Liste der Funktionen mit Eingabe/Ausgabe und kurzer Beschreibung:
| Funktion | Eingabe | Ausgabe | Kurzbeschreibung |
|---------------|------------------------------------|-----------------------|----------------------------------------|
| matmul | Mat4 A, Mat4 B | Mat4 | Matrixmultiplikation 4x4 |
| translate | Vec3 pos | Mat4 | Verschiebungstransformation |
| rot3D | double angle_deg, char axis | Mat4 | Rotation um Achse x/y/z |
| identity (optional)| --- | Mat4 | Identitätsmatrix |
| _____________ | __________________________________ | ____________________ | ______________________________ |
# ----------------------------
# 5. Designentscheidungen / Hinweise
# ----------------------------
- Rückgabe der Matrizen per Wert oder Referenz? ___________
- Verwendung von std::array oder std::vector? ___________
- Homogene Koordinaten für Translation / Rotation (4x4)? ☐ Ja ☐ Nein
- Weitere Designüberlegungen: ___________________________
# ----------------------------
# 6. Deliverables / Milestones
# ----------------------------
- design.txt fertig und im Branch architect committed
- Übergabe an Entwickler für Implementierung
========================================================
Hinweis:
- Dieses Dokument dient als Grundlage für die Implementierung.
- Alle Designentscheidungen sollen klar nachvollziehbar sein.
========================================================
+60
View File
@@ -0,0 +1,60 @@
========================================================
Projekt: gamematrix (C++ Library)
Rolle: Projektleiter
Datei: requirements.txt
Datum: 3.11.2025
Team: Thomas und Co
========================================================
# ----------------------------
# 1. Projektziel
# ----------------------------
Beschreiben Sie hier kurz das Ziel des Projekts:
Ziel: Implementierung der Matrixmulitplikation, -Rotation, -Verschiebung und anschließendes Testen
# ----------------------------
# 2. Funktionale Anforderungen
# ----------------------------
Listen Sie alle Funktionen auf, die die Bibliothek bereitstellen soll.
Tragen Sie ein: Funktion, Eingabe, Ausgabe, kurze Beschreibung
| Funktion | Eingabe | Ausgabe | Kurzbeschreibung |
|---------------|------------------------------------|-----------------------|----------------------------------------|
| matmul | 4x4 Matrix A, 4x4 Matrix B | 4x4 Matrix | Multiplikation zweier Matrizen wie in Mathe gelernt
| translate | 3D Vektor | 4x4 Matrix | Verschiebung
| rot3D | Winkel in °, Rotationsachse (x/y/z)| 4x4 Matrix | Rotation
| identity (optional)| --- | 4x4 Matrix | _____________________________________ |
| _____________ | __________________________________ | ____________________ | ______________________________ |
| _____________ | __________________________________ | ____________________ | ______________________________ |
# ----------------------------
# 3. Nicht-funktionale Anforderungen
# ----------------------------
(z. B. Performance, Lesbarkeit, Wartbarkeit, Python-Kompatibilität via pybind11)
- Lesbarkeit nach Coding Guidelines: https://elearning.ohmportal.de/pluginfile.php/395279/mod_page/content/5/Coding_Guidelines.pdf
# ----------------------------
# 4. Annahmen / Einschränkungen
# ----------------------------
(z. B. alle Matrizen sind 4x4, Winkel in Grad, nur double)
- alle Matrizen sind 4x4
- Winkel in Grad
- nur double
# ----------------------------
# 5. Abnahmekriterien
# ----------------------------
Wie soll geprüft werden, dass die Anforderungen erfüllt sind?
(z. B. Unit-Tests, Beispielrotationen, Matrizenmultiplikation)
- Tests von Marco müssen fehlerfrei verlaufen
========================================================
Hinweis:
- Diese Datei wird vom Projektleiter erstellt und gepflegt.
- Jede Phase des Projekts soll hier dokumentiert werden.
========================================================
+137
View File
@@ -0,0 +1,137 @@
//
// Created by marco on 03.11.2025.
// Changeb by marco on 16.11.2025
#include <iostream>
#include <cmath>
#include "gamematrix.h"
using namespace Matrix3D;
bool nearlyEqual(double a, double b, double eps = 1e-6) {
return std::abs(a - b) < eps;
}
void test_matmul_identity() {
Mat4 A = identity();
Mat4 B = identity();
Mat4 R = matmul(A, B);
std::cout << "Test matmul: Identity * Identity -> Identity: ";
bool ok = true;
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
double expected = (i==j ? 1.0 : 0.0);
if(!nearlyEqual(R[i][j], expected)) ok = false;
}
}
std::cout << (ok ? "OK\n" : "FAIL\n");
}
void test_matmul_example() {
Mat4 A = {{
{{1, 2, 3, 4}},
{{0, 1, 2, 3}},
{{0, 0, 1, 2}},
{{0, 0, 0, 1}}
}};
Mat4 B = {{
{{1, 0, 0, 0}},
{{1, 1, 0, 0}},
{{1, 1, 1, 0}},
{{1, 1, 1, 1}}
}};
Mat4 R = matmul(A, B);
std::cout << "Test matmul: Beispielmatrizen A * B: ";
// Erwartete Matrix mit Handrechnung:
Mat4 Expected = {{
{{1+2+3+4, 2+3+4, 3+4, 4}},
{{0+1+2+3, 1+2+3, 2+3, 3}},
{{0+0+1+2, 0+1+2, 1+2, 2}},
{{1, 1, 1, 1}}
}};
bool ok = true;
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
if(!nearlyEqual(R[i][j], Expected[i][j])) ok = false;
}
}
std::cout << (ok ? "OK\n" : "FAIL\n");
}
void test_translate() {
Vec3 v{1,2,3};
Mat4 T = translate({5,-2,1});
Vec3 out = T * v;
std::cout << "Test translate (Vec3{1,2,3} + {5,-2,1}): ";
if(nearlyEqual(out.x, 6) && nearlyEqual(out.y, 0) && nearlyEqual(out.z, 4))
std::cout << "OK\n";
else
std::cout << "FAIL (" << out.x << "," << out.y << "," << out.z << ")\n";
}
void test_rotZ_90() {
Vec3 v{1,0,0};
Mat4 Rm = rot3D(90, 'z');
Vec3 out = Rm * v;
std::cout << "Test rot3D Z 90° (1,0,0 -> 0,1,0): ";
if(nearlyEqual(out.x, 0) && nearlyEqual(out.y, 1))
std::cout << "OK\n";
else
std::cout << "FAIL (" << out.x << "," << out.y << "," << out.z << ")\n";
}
void test_rotX_180() {
Vec3 v{0,1,0};
Mat4 Rm = rot3D(180, 'x');
Vec3 out = Rm * v;
std::cout << "Test rot3D X 180° (0,1,0 -> 0,-1,0): ";
if(nearlyEqual(out.y, -1))
std::cout << "OK\n";
else
std::cout << "FAIL (" << out.x << "," << out.y << "," << out.z << ")\n";
}
void test_rotY_270() {
Vec3 v{1,0,0};
Mat4 Rm = rot3D(270, 'y');
Vec3 out = Rm * v;
std::cout << "Test rot3D Y 270° (1,0,0 -> 0,0,-1): ";
if(nearlyEqual(out.x, 0) && nearlyEqual(out.z, -1))
std::cout << "OK\n";
else
std::cout << "FAIL (" << out.x << "," << out.y << "," << out.z << ")\n";
}
int main() {
std::cout << "===== Matrix3D Tests =====\n";
test_matmul_identity();
test_matmul_example();
test_translate();
test_rotZ_90();
test_rotX_180();
test_rotY_270();
std::cout << "==========================\n";
return 0;
}
+42
View File
@@ -0,0 +1,42 @@
========================================================
Projekt: gamematrix (C++ Library)
Rolle: Tester
Datei: tests.txt
Datum: ____________________
Team: ____________________
========================================================
# ----------------------------
# 1. Testplan Übersicht
# ----------------------------
Ziel: Überprüfung der Funktionen matmul(), translate(), rot3D().
| Funktion | Testfall | Eingabe | Erwartetes Ergebnis | Bemerkung |
|---------------|---------------------------|------------------------------|-----------------------------------|----------------------------|
| matmul | Identity * Identity | 4x4 Identity Matrizen | Identity | Basisfall |
| matmul | Beispielmatrizen | A=[[...]], B=[[...]] | C=[[...]] | Prüfen mit Handrechnung |
| translate | Verschiebung | Vec3 {1,2,3} | Matrix mit Translation 1,2,3 | Prüfen letzte Spalte |
| rot3D | Rotation Z 90° | angle_deg=90, axis='z' | (1,0,0) -> (0,1,0) | Prüfen Anwendung auf Vektor|
| rot3D | Rotation X 180° | angle_deg=180, axis='x' | (0,1,0) -> (0,-1,0) | Prüfen Anwendung auf Vektor|
| rot3D | Rotation Y 270° | angle_deg=270, axis='y' | (1,0,0) -> (0,0,-1) | Prüfen Anwendung auf Vektor|
# ----------------------------
# 2. Testdaten / Matrizen
# ----------------------------
- Matrizen für matmul: Identity, Beispiel A/B Matrizen
- Vektoren für translate: Vec3 {x, y, z}
- Vektoren für rot3D: Vec3 {1,0,0}, Vec3 {0,1,0}
# ----------------------------
# 3. Abnahmekriterien
# ----------------------------
- Alle Unit-Tests erfolgreich
- Keine Exceptions außer gewollt (z. B. ungültige Achse)
- Testbericht in tests.txt dokumentiert
========================================================
Hinweis:
- Diese Datei wird vom Tester gepflegt.
- Tester dokumentiert Input, Output, erwartetes Ergebnis und Erfolg/Fehler.
========================================================
-66
View File
@@ -1,66 +0,0 @@
#include "gamecube.h"
gamecube::gamecube(const Vec3 &pos, Color col)
: position(pos), color(col) {}
void gamecube::Update(float flipSpeed)
{
if (flippingForward)
{
rotation += flipSpeed;
if (rotation >= 180.0f)
{
rotation = 180.0f;
flippingForward = false;
flipped = true;
}
}
else if (flippingBackward)
{
rotation -= flipSpeed;
if (rotation <= 0.0f)
{
rotation = 0.0f;
flippingBackward = false;
flipped = false;
}
}
}
void gamecube::FlipForward() { flippingForward = true; }
void gamecube::FlipBackward() { flippingBackward = true; }
bool gamecube::IsFlipped() const { return flipped; }
bool gamecube::IsMatched() const { return matched; }
void gamecube::SetMatched(bool m) { matched = m; }
void gamecube::Draw() const
{
rlPushMatrix();
// Matrizen für Rotation und Translation erzeugen
auto matrix_a = gameMatrix::translate({ position.x, position.y, position.z});
auto matrix_b = gameMatrix::rot3D(rotation, 'y');
// Matrizen multiplizieren (Translation * Rotation)
auto model = gameMatrix::matmul(matrix_a, matrix_b);
// transform for raylib matrix
float f[16];
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
f[j * 4 + i] = model[i][j];
rlMultMatrixf(f);
if (rotation < 90.0f)
DrawCube({0,0,0}, 1,1,1, GRAY);
else
DrawCube({0,0,0}, 1,1,1, color);
DrawCubeWires({0,0,0}, 1,1,1, BLACK);
rlPopMatrix();
}
Vec3 gamecube::GetPosition() const { return position; }
float gamecube::GetRotationY() const { return rotation; }
+3
View File
@@ -2,6 +2,9 @@
#include "gamematrix.h"
#include "raylib.h"
#include <rlgl.h>
#include <array>
using Mat4 = std::array<std::array<double, 4>, 4>;
struct Vec3
{
+1
View File
@@ -3,6 +3,7 @@
#include <array>
#include <stdexcept>
#include <cmath>
#include "gamecube.h"
class gameMatrix
{
-136
View File
@@ -1,136 +0,0 @@
#include "gamecube.h"
#include <algorithm>
#include <ctime>
// -----------------------------------------------------------
// 3D Memory Game – Hauptprogramm
// -----------------------------------------------------------
int main()
{
// Zufall initialisieren
srand(time(NULL));
// Fenster und Kamera
InitWindow(800, 600, "3D Memory Game with Matrix3D Library");
SetTargetFPS(60);
Camera3D camera{};
camera.position = {6.0f, 6.0f, 6.0f};
camera.target = {0.0f, 0.0f, 0.0f};
camera.up = {0.0f, 1.0f, 0.0f};
camera.fovy = 45.0f;
camera.projection = CAMERA_PERSPECTIVE;
// Nur 3 Farben für 3 Paare
Color colors[] = { RED, GREEN, BLUE };
// 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}};
// Farben doppelt in einen Pool legen und mischen
std::vector<Color> colorPool;
for (int i = 0; i < 3; i++)
{
colorPool.push_back(colors[i]);
colorPool.push_back(colors[i]);
}
// Fisher-Yates Shuffle mit rand()
for (int i = colorPool.size() - 1; i > 0; --i)
{
int j = rand() % (i + 1); // Zufallsindex von 0 bis i
std::swap(colorPool[i], colorPool[j]);
}
// Karten/Würfel erstellen
std::vector<gamecube> cubes;
for (int i = 0; i < 6; i++)
cubes.emplace_back(positions[i], colorPool[i]);
gamecube* first = nullptr;
gamecube* second = nullptr;
float flipSpeed = 5.0f; // Drehgeschwindigkeit
bool gameWon = false;
// -----------------------------------------------------------
// Hauptspielschleife
// -----------------------------------------------------------
while (!WindowShouldClose())
{
// Klick-Erkennung
if (!gameWon && IsMouseButtonPressed(MOUSE_LEFT_BUTTON))
{
Vector2 mouse = GetMousePosition();
for (auto &c : cubes)
{
if (!c.IsFlipped() && !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();
}
}
}
// Animation aller Würfel
for (auto &c : cubes)
{
c.Update(flipSpeed);
// Sobald ein Würfel vollständig umgedreht ist → merken
if (c.IsFlipped() && !c.IsMatched())
{
if (!first) first = &c;
else if (!second && &c != first) second = &c;
}
}
// Matching-Logik
if (first && second)
{
Color col1 = first->GetColor();
Color col2 = second->GetColor();
if (col1.r == col2.r && col1.g == col2.g && col1.b == col2.b)
{
first->SetMatched(true);
second->SetMatched(true);
}
else
{
first->FlipBackward();
second->FlipBackward();
}
first = second = nullptr;
}
// Gewinnprüfung
if (!gameWon)
gameWon = std::all_of(cubes.begin(), cubes.end(), [](const gamecube &c){ return c.IsMatched(); });
// -----------------------------------------------------------
// Zeichnen
// -----------------------------------------------------------
BeginDrawing();
ClearBackground(RAYWHITE);
BeginMode3D(camera);
for (auto &c : cubes)
c.Draw();
EndMode3D();
if (gameWon)
DrawText("Congrats! You found all pairs!", 150, 260, 30, DARKBLUE);
else
DrawText("Flip 2 cubes - find matching pairs!", 10, 10, 20, DARKGRAY);
EndDrawing();
}
CloseWindow();
return 0;
}
+70
View File
@@ -0,0 +1,70 @@
#include "gamecube.h"
gamecube::gamecube(const Vec3 &pos, Color col)
: position(pos), color(col) {}
void gamecube::Update(float flipSpeed)
{
//Tom: Added vars for clarity; replaced old 180.0f, 0.0f
const float MaxRotationAngle = 180.0f;
const float NoRotationAngle = 0.0f;
if (flippingForward)
{
rotation += flipSpeed;
if (rotation >= MaxRotationAngle)
{
rotation = MaxRotationAngle;
flippingForward = false;
flipped = true;
}
}
else if (flippingBackward)
{
rotation -= flipSpeed;
if (rotation <= NoRotationAngle)
{
rotation = NoRotationAngle;
flippingBackward = false;
flipped = false;
}
}
}
void gamecube::FlipForward() { flippingForward = true; }
void gamecube::FlipBackward() { flippingBackward = true; }
bool gamecube::IsFlipped() const { return flipped; }
bool gamecube::IsMatched() const { return matched; }
void gamecube::SetMatched(bool m) { matched = m; }
void gamecube::Draw() const
{
rlPushMatrix();
// Matrizen für Rotation und Translation erzeugen
auto matrix_a = gameMatrix::translate({ position.x, position.y, position.z});
auto matrix_b = gameMatrix::rot3D(rotation, 'y');
// Matrizen multiplizieren (Translation * Rotation)
auto model = gameMatrix::matmul(matrix_a, matrix_b);
// transform for raylib matrix
float f[16];
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
f[j * 4 + i] = model[i][j];
rlMultMatrixf(f);
if (rotation < 90.0f)
DrawCube({0,0,0}, 1,1,1, GRAY);
else
DrawCube({0,0,0}, 1,1,1, color);
DrawCubeWires({0,0,0}, 1,1,1, BLACK);
rlPopMatrix();
}
Vec3 gamecube::GetPosition() const { return position; }
float gamecube::GetRotationY() const { return rotation; }
+75
View File
@@ -0,0 +1,75 @@
#include "gamematrix.h"
#include <cmath>
// Entfernt: namespace Matrix3D { ... } <-- GEÄNDERT
// Implementierungen jetzt als Klassenmethoden von gameMatrix <-- GEÄNDERT
// Optional: identity() als Hilfsmethode hinzugefügt
static std::array<std::array<double,4>,4> identity() { // <-- NEU
std::array<std::array<double,4>,4> m{};
for(int i = 0; i < 4; i++) {
for(int j = 0; j < 4; j++) {
m[i][j] = (i == j) ? 1.0 : 0.0;
}
}
return m;
}
// -------------------- matmul --------------------
std::array<std::array<double,4>,4> gameMatrix::matmul( // <-- GEÄNDERT
const std::array<std::array<double,4>,4>& A,
const std::array<std::array<double,4>,4>& B)
{
std::array<std::array<double,4>,4> R{};
for(int i = 0; i < 4; i++) {
for(int j = 0; j < 4; j++) {
double sum = 0.0;
for(int k = 0; k < 4; k++) {
sum += A[i][k] * B[k][j];
}
R[i][j] = sum;
}
}
return R;
}
// -------------------- translate --------------------
std::array<std::array<double,4>,4> gameMatrix::translate( // <-- GEÄNDERT
const std::array<double,3>& pos)
{
auto t = identity(); // <-- NEU: nutzt die Hilfsmethode
t[0][3] = pos[0];
t[1][3] = pos[1];
t[2][3] = pos[2];
return t;
}
// -------------------- rot3D --------------------
std::array<std::array<double,4>,4> gameMatrix::rot3D( // <-- GEÄNDERT
double angle_deg, char axis)
{
auto r = identity(); // <-- NEU: nutzt die Hilfsmethode
double rad = angle_deg * M_PI / 180.0;
double c = std::cos(rad);
double s = std::sin(rad);
switch(axis) {
case 'x': case 'X':
r[1][1] = c; r[1][2] = -s;
r[2][1] = s; r[2][2] = c;
break;
case 'y': case 'Y':
r[0][0] = c; r[0][2] = s;
r[2][0] = -s; r[2][2] = c;
break;
case 'z': case 'Z':
r[0][0] = c; r[0][1] = -s;
r[1][0] = s; r[1][1] = c;
break;
}
return r;
}
+282
View File
@@ -0,0 +1,282 @@
#include "gamecube.h"
#include "gamematrix.h"
#include <algorithm>
#include <ctime>
#include <cmath> // Für ceil
// -----------------------------------------------------------
// 3D Memory Game – Hauptprogramm
// -----------------------------------------------------------
int main()
{
// =====================================================================
// 1) KLEINES MENÜFENSTER (UNVERÄNDERT)
// =====================================================================
InitWindow(400, 200, "Wuerfelspiel - Menu");
SetTargetFPS(60);
char input[4] = "";
int len = 0;
int numberOfCubes = 0;
bool error = false;
while (!WindowShouldClose())
{
int key = GetCharPressed();
// Zahlen erfassen
if (key >= '0' && key <= '9' && len < 3)
{
input[len++] = (char)key;
input[len] = '\0';
}
// Backspace
if (IsKeyPressed(KEY_BACKSPACE) && len > 0)
input[--len] = '\0';
// ENTER = übernehmen
if (IsKeyPressed(KEY_ENTER))
{
numberOfCubes = atoi(input);
if (numberOfCubes > 0 && numberOfCubes % 2 == 0)
break; // gültig → Menü beenden
else
error = true;
}
// Zeichnen des Menüs
BeginDrawing();
ClearBackground(RAYWHITE);
DrawText("Anzahl der Wuerfel (gerade Zahl):", 30, 40, 20, DARKGRAY);
DrawRectangle(30, 80, 200, 40, LIGHTGRAY);
DrawText(input, 40, 90, 20, BLACK);
if (error)
DrawText("Ungueltige Eingabe!", 30, 140, 20, RED);
EndDrawing();
}
CloseWindow(); // Menüfenster schließen
// =====================================================================
// 2) HAUPTSPIELFENSTER
// =====================================================================
// Fenster und Kamera
InitWindow(800, 600, "3D Memory Game with Matrix3D Library");
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
int turnCount = 0;
// Startzeit für timer berechnung -> aktueller Zeitpunkt in Sekunden
time_t startTime = time(nullptr);
// gespielte Zeit
int playTime = 0;
// Zufall initialisieren
srand(time(NULL));
Camera3D camera{};
camera.position = {12.0f, 9.0f, 12.0f};
camera.target = {0.0f, 0.0f, 0.0f};
camera.up = {0.0f, 1.0f, 0.0f};
camera.fovy = 45.0f;
camera.projection = CAMERA_PERSPECTIVE;
// Nur 3 Farben für 3 Paare
Color colors[] = { RED, GREEN, BLUE };
// Positionen bestimmen für dynamisches NumberOfCubes
std::vector<Vec3> positions;
int columns = ceil(sqrt(numberOfCubes));
int rows = ceil(numberOfCubes / (float)columns);
float posi_x = 0;
float posi_z = 0;
for (int i = 0; i < columns; i++)
{
for (int j = 0; j < rows; j++)
{
positions.push_back({posi_x, 0, posi_z});
posi_z += 2;
}
posi_z = 0;
posi_x += 2;;
}
// Farben doppelt in einen Pool legen und mischen
std::vector<Color> colorPool;
int numberOfPairs = numberOfCubes / 2;
for (int i = 0; i < numberOfPairs; i++)
{
colorPool.push_back(colors[i % 3]);
colorPool.push_back(colors[i % 3]);
}
// Fisher-Yates Shuffle mit rand()
for (int i = colorPool.size() - 1; i > 0; --i)
{
int j = rand() % (i + 1);
std::swap(colorPool[i], colorPool[j]);
}
// Karten/Würfel erstellen
std::vector<gamecube> cubes;
for (int i = 0; i < numberOfCubes; i++)
cubes.emplace_back(positions[i], colorPool[i]);
gamecube* first = nullptr;
gamecube* second = nullptr;
float flipSpeed = 5.0f; // Drehgeschwindigkeit
bool gameWon = false;
// -----------------------------------------------------------
// Hauptspielschleife
// -----------------------------------------------------------
while (!WindowShouldClose())
{
// die aktuelle Spielzeit berechnen
if (!gameWon)
{
playTime = static_cast<int>(time(nullptr) - startTime);
}
// Klick-Erkennung (ÜBERSCHRIEBEN durch GameState-Logik)
// Steuerung des Spielflusses: Nur im Idle- oder OneFlipped-Zustand wird reagiert!
if (!gameWon
&& state != GameState::LockInput
&& state != GameState::CheckingMatch
&& IsMouseButtonPressed(MOUSE_LEFT_BUTTON))
{
Vector2 mouse = GetMousePosition();
for (auto &c : cubes)
{
if (!c.IsFlipped() && !c.IsMatched()) // Ist verdeckt und nicht gematcht
{
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)
{
// Klick verarbeitet, Würfel umdrehen
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
for (auto &c : cubes)
{
c.Update(flipSpeed);
}
// NEU: Zustandswechsel LockInput -> CheckingMatch
// 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()
{
state = GameState::CheckingMatch;
}
}
// Matching-Logik (ÜBERSCHRIEBEN und an GameState gekoppelt)
if (state == GameState::CheckingMatch && first && second)
{
// Vergleichslogik der Aufgabe
if (first->GetColor().r == second->GetColor().r &&
first->GetColor().g == second->GetColor().g &&
first->GetColor().b == second->GetColor().b)
{
first->SetMatched(true);
second->SetMatched(true);
}
else
{
first->FlipBackward();
second->FlipBackward();
}
// Rücksetzung und Zustandswechsel zu Idle
first = second = nullptr;
state = GameState::Idle;
}
// Gewinnprüfung
if (!gameWon)
gameWon = std::all_of(cubes.begin(), cubes.end(), [](const gamecube &c){ return c.IsMatched(); });
// -----------------------------------------------------------
// Zeichnen (UNVERÄNDERT)
// -----------------------------------------------------------
BeginDrawing();
ClearBackground(RAYWHITE);
BeginMode3D(camera);
for (auto &c : cubes)
c.Draw();
EndMode3D();
if (gameWon)
{
DrawText("Congrats! You found all pairs!", 150, 260, 30, DARKBLUE);
DrawText(("TurnCount: " + std::to_string(turnCount)).c_str(), 150, 290, 20, DARKBLUE);
DrawText(("Seconds played: " + std::to_string(playTime)).c_str(), 150, 310, 20, DARKBLUE);
}
else
{
DrawText("Flip 2 cubes - find matching pairs!", 10, 10, 20, DARKGRAY);
DrawText(("TurnCount: " + std::to_string(turnCount)).c_str(), 10, 30, 20, DARKGRAY); //Zeichne TurnCount
DrawText(("PlayTime: " + std::to_string(playTime)).c_str(), 10, 50, 20, DARKGRAY); //Zeichne Playtime
}
EndDrawing();
}
CloseWindow();
return 0;
}
BIN
View File
Binary file not shown.