generated from freudenreichan/Programmieren_3b
Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3e4d49fe26 | ||
|
|
0e28bbbf41 | ||
|
|
ac117a847b | ||
|
|
fb5673287a | ||
|
|
5e544891b4 | ||
|
|
43d06f786f | ||
|
|
ebc86d529a | ||
|
|
199e862239 |
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
.idea
|
||||
cmake-build-debug
|
||||
@ -1,35 +1,51 @@
|
||||
cmake_minimum_required(VERSION 3.28)
|
||||
project(Prog3B)
|
||||
|
||||
set(EXECUTABLE_NAME Prog3B)
|
||||
|
||||
# Generate compile_commands.json
|
||||
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
||||
set(CMAKE_CXX_STANDARD 20)
|
||||
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
||||
|
||||
# Set the default build type if not specified
|
||||
# Default build type
|
||||
if(NOT CMAKE_BUILD_TYPE)
|
||||
set(CMAKE_BUILD_TYPE Release CACHE STRING "Build type" FORCE)
|
||||
endif()
|
||||
|
||||
# Quell- und Header-Dateien
|
||||
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
|
||||
${CMAKE_CURRENT_LIST_DIR}/src/gamematrix.cpp
|
||||
includes/gamestate.h
|
||||
)
|
||||
#automatisch hinzufügen
|
||||
file(GLOB SRC_FILES "${CMAKE_CURRENT_LIST_DIR}/src/*.cpp")
|
||||
|
||||
set(INCLUDE_DIRS
|
||||
${CMAKE_CURRENT_LIST_DIR}/linux
|
||||
${CMAKE_CURRENT_LIST_DIR}/includes
|
||||
${CMAKE_CURRENT_LIST_DIR}/raylib
|
||||
)
|
||||
|
||||
# Executable erstellen
|
||||
add_executable(${EXECUTABLE_NAME} ${SRC_FILES})
|
||||
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)
|
||||
if (APPLE)
|
||||
target_link_libraries(Prog3B "-framework IOKit")
|
||||
target_link_libraries(Prog3B "-framework Cocoa")
|
||||
target_link_libraries(Prog3B "-framework OpenGL")
|
||||
# Include-Verzeichnisse hinzufügen
|
||||
target_include_directories(${EXECUTABLE_NAME} PRIVATE ${INCLUDE_DIRS})
|
||||
|
||||
|
||||
# Nur noch raylib und systemabhängige Libraries linken
|
||||
if(WIN32)
|
||||
target_link_libraries(${EXECUTABLE_NAME} PRIVATE
|
||||
${CMAKE_CURRENT_LIST_DIR}/windows/libraylib.a
|
||||
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()
|
||||
|
||||
@ -16,4 +16,9 @@ public:
|
||||
|
||||
// Verschiebung
|
||||
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
includes/gamestate.h
Normal file
17
includes/gamestate.h
Normal file
@ -0,0 +1,17 @@
|
||||
//
|
||||
// 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
|
||||
@ -1,16 +1,27 @@
|
||||
#include "gamecube.h"
|
||||
|
||||
// Konstanten für Rotation und Matrixgröße
|
||||
constexpr float ROTATION_HALF = 90.0f;
|
||||
constexpr float ROTATION_FULL = 180.0f;
|
||||
|
||||
|
||||
// Standardfarben
|
||||
const Color DEFAULT_COLOR = GRAY;
|
||||
const Color WIRES_COLOR = BLACK;
|
||||
|
||||
|
||||
gamecube::gamecube(const Vec3 &pos, Color col)
|
||||
: position(pos), color(col) {}
|
||||
|
||||
// ✅ Klar strukturierte Funktion
|
||||
void gamecube::Update(float flipSpeed)
|
||||
{
|
||||
if (flippingForward)
|
||||
{
|
||||
rotation += flipSpeed;
|
||||
if (rotation >= 180.0f)
|
||||
if (rotation >= ROTATION_FULL) // ⚠️ Magic Number
|
||||
{
|
||||
rotation = 180.0f;
|
||||
rotation = ROTATION_FULL; // ⚠️ Magic Number
|
||||
flippingForward = false;
|
||||
flipped = true;
|
||||
}
|
||||
@ -30,7 +41,7 @@ void gamecube::Update(float flipSpeed)
|
||||
void gamecube::FlipForward() { flippingForward = true; }
|
||||
void gamecube::FlipBackward() { flippingBackward = true; }
|
||||
|
||||
bool gamecube::IsFlipped() const { return flipped; }
|
||||
bool gamecube::IsFlipped() const { return flipped; } // ✅ Getter sauber, const korrekt
|
||||
bool gamecube::IsMatched() const { return matched; }
|
||||
void gamecube::SetMatched(bool m) { matched = m; }
|
||||
|
||||
@ -52,12 +63,12 @@ void gamecube::Draw() const
|
||||
f[j * 4 + i] = model[i][j];
|
||||
rlMultMatrixf(f);
|
||||
|
||||
if (rotation < 90.0f)
|
||||
DrawCube({0,0,0}, 1,1,1, GRAY);
|
||||
if (rotation < ROTATION_HALF) // ⚠️ Magic Number
|
||||
DrawCube({0,0,0}, 1,1,1, DEFAULT_COLOR);
|
||||
else
|
||||
DrawCube({0,0,0}, 1,1,1, color);
|
||||
|
||||
DrawCubeWires({0,0,0}, 1,1,1, BLACK);
|
||||
DrawCubeWires({0,0,0}, 1,1,1,WIRES_COLOR);
|
||||
|
||||
rlPopMatrix();
|
||||
}
|
||||
77
src/gamematrix.cpp
Normal file
77
src/gamematrix.cpp
Normal file
@ -0,0 +1,77 @@
|
||||
#include "gamematrix.h"
|
||||
#include <cmath>
|
||||
#include <stdexcept>
|
||||
|
||||
// Matrix Multiplikation
|
||||
std::array<std::array<double,4>,4> gameMatrix::matmul(
|
||||
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> result = {0};
|
||||
|
||||
for (int i = 0; i < 4; ++i)
|
||||
for (int j = 0; j < 4; ++j)
|
||||
{
|
||||
result[i][j] = 0.0;
|
||||
for (int k = 0; k < 4; ++k)
|
||||
result[i][j] += A[i][k] * B[k][j];
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// Rotationsmatrix
|
||||
std::array<std::array<double,4>,4> gameMatrix::rot3D(double angle_deg, char axis)
|
||||
{
|
||||
double angle_rad = angle_deg * M_PI / 180.0;
|
||||
double c = std::cos(angle_rad);
|
||||
double s = std::sin(angle_rad);
|
||||
|
||||
std::array<std::array<double,4>,4> R = identity();
|
||||
|
||||
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;
|
||||
|
||||
default:
|
||||
throw std::invalid_argument("Invalid axis for rotation (use x, y, or z)");
|
||||
}
|
||||
|
||||
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
|
||||
std::array<std::array<double,4>,4> gameMatrix::translate(const std::array<double,3>& pos)
|
||||
{
|
||||
std::array<std::array<double,4>,4> T = identity();
|
||||
|
||||
T[0][3] = pos[0];
|
||||
T[1][3] = pos[1];
|
||||
T[2][3] = pos[2];
|
||||
|
||||
return T;
|
||||
}
|
||||
|
||||
@ -1,12 +1,22 @@
|
||||
#include "gamecube.h"
|
||||
#include <algorithm>
|
||||
#include <ctime>
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
#include "gamestate.h"
|
||||
|
||||
// -----------------------------------------------------------
|
||||
// 3D Memory Game – Hauptprogramm
|
||||
// 3D Memory Game – Hauptprogramm mit flexibler Paaranzahl
|
||||
// -----------------------------------------------------------
|
||||
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
|
||||
srand(time(NULL));
|
||||
|
||||
@ -21,36 +31,40 @@ int main()
|
||||
camera.fovy = 45.0f;
|
||||
camera.projection = CAMERA_PERSPECTIVE;
|
||||
|
||||
// Nur 3 Farben für 3 Paare
|
||||
Color colors[] = { RED, GREEN, BLUE };
|
||||
// Farben generieren (maximal 10 Farben)
|
||||
Color baseColors[10] = { RED, GREEN, BLUE, ORANGE, PURPLE, GOLD, PINK, BROWN, SKYBLUE, LIME };
|
||||
std::vector<Color> colors(baseColors, baseColors + numPairs);
|
||||
|
||||
// 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 dynamisch berechnen (Raster 3 Spalten)
|
||||
std::vector<Vec3> positions;
|
||||
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
|
||||
std::vector<Color> colorPool;
|
||||
for (int i = 0; i < 3; i++)
|
||||
for (int i = 0; i < numPairs; 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
|
||||
int j = rand() % (i + 1);
|
||||
std::swap(colorPool[i], colorPool[j]);
|
||||
}
|
||||
|
||||
// Karten/Würfel erstellen
|
||||
std::vector<gamecube> cubes;
|
||||
for (int i = 0; i < 6; i++)
|
||||
for (int i = 0; i < numPairs*2; i++)
|
||||
cubes.emplace_back(positions[i], colorPool[i]);
|
||||
|
||||
gamecube* first = nullptr;
|
||||
gamecube* second = nullptr;
|
||||
float flipSpeed = 5.0f; // Drehgeschwindigkeit
|
||||
float flipSpeed = 5.0f;
|
||||
bool gameWon = false;
|
||||
GameState state = GameState::Idle;
|
||||
|
||||
|
||||
// -----------------------------------------------------------
|
||||
// Hauptspielschleife
|
||||
@ -58,8 +72,9 @@ int main()
|
||||
while (!WindowShouldClose())
|
||||
{
|
||||
//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)
|
||||
@ -68,8 +83,12 @@ int main()
|
||||
{
|
||||
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();
|
||||
state = GameState::OneFlipped;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -85,15 +104,20 @@ int main()
|
||||
if (!first) first = &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
|
||||
if (first && second)
|
||||
if (state == GameState::CheckingMatch && first && second)
|
||||
{
|
||||
Color col1 = first->GetColor();
|
||||
Color col2 = second->GetColor();
|
||||
|
||||
if (col1.r == col2.r && col1.g == col2.g && col1.b == col2.b)
|
||||
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);
|
||||
@ -105,6 +129,7 @@ int main()
|
||||
}
|
||||
|
||||
first = second = nullptr;
|
||||
state = GameState::Idle;
|
||||
}
|
||||
|
||||
// Gewinnprüfung
|
||||
@ -128,6 +153,8 @@ int main()
|
||||
else
|
||||
DrawText("Flip 2 cubes - find matching pairs!", 10, 10, 20, DARKGRAY);
|
||||
|
||||
|
||||
|
||||
EndDrawing();
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user