Compare commits

...

7 Commits

Author SHA1 Message Date
Niklumm
58ff9f2e63 Merge branch 'main' into bug_fix_branch 2025-12-15 10:48:42 +01:00
Niklumm
a4b18d1207 bug fixed 2025-12-15 10:47:32 +01:00
pumcookie
ef779381a6 HighScore Final 2025-12-01 14:37:35 +01:00
marcelbls
c6cdebfc84 Zähler eingebaut und versucht highscore zu fixen
Zähler eingebaut und versucht highscore zu fixen
2025-12-01 13:11:11 +01:00
pumcookie
aa5ca11829 ScoreManager Score Part 2025-11-30 20:36:17 +01:00
Legaeli
c0c25360cb Added user interface 2025-11-25 11:24:28 +01:00
Niklumm
9b3e642475 small max cube_count fix 2025-11-23 13:35:52 +01:00
12 changed files with 6325 additions and 45 deletions

65
.idea/workspace.xml generated
View File

@ -30,11 +30,8 @@
</component>
<component name="ChangeListManager">
<list default="true" id="db070755-9708-4a46-a9cb-e7c4dc1bfb77" name="Changes" comment="">
<change afterPath="$PROJECT_DIR$/createCube.cpp" afterDir="false" />
<change afterPath="$PROJECT_DIR$/createCube.h" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/editor.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/editor.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/CMakeLists.txt" beforeDir="false" afterPath="$PROJECT_DIR$/CMakeLists.txt" afterDir="false" />
<change beforePath="$PROJECT_DIR$/gamecube.h" beforeDir="false" afterPath="$PROJECT_DIR$/gamecube.h" afterDir="false" />
<change beforePath="$PROJECT_DIR$/main.cpp" beforeDir="false" afterPath="$PROJECT_DIR$/main.cpp" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
@ -49,6 +46,18 @@
<component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
</component>
<component name="HighlightingSettingsPerFile">
<setting file="mock:///dummy.cpp" root0="SKIP_HIGHLIGHTING" />
<setting file="mock:///dummy.cpp" root0="SKIP_HIGHLIGHTING" />
<setting file="mock:///dummy.cpp" root0="SKIP_HIGHLIGHTING" />
<setting file="mock:///dummy.cpp" root0="SKIP_HIGHLIGHTING" />
<setting file="mock:///dummy.cpp" root0="SKIP_HIGHLIGHTING" />
<setting file="mock:///dummy.cpp" root0="SKIP_HIGHLIGHTING" />
<setting file="mock:///dummy.cpp" root0="SKIP_HIGHLIGHTING" />
<setting file="mock:///dummy.cpp" root0="SKIP_HIGHLIGHTING" />
<setting file="mock:///dummy.cpp" root0="SKIP_HIGHLIGHTING" />
<setting file="mock:///dummy.cpp" root0="SKIP_HIGHLIGHTING" />
</component>
<component name="ProjectApplicationVersion">
<option name="ide" value="CLion" />
<option name="majorVersion" value="2024" />
@ -64,33 +73,28 @@
<option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" />
</component>
<component name="PropertiesComponent"><![CDATA[{
"keyToString": {
"CMake Application.Prog3B.executor": "Run",
"RunOnceActivity.RadMigrateCodeStyle": "true",
"RunOnceActivity.ShowReadmeOnStart": "true",
"RunOnceActivity.cidr.known.project.marker": "true",
"RunOnceActivity.git.unshallow": "true",
"RunOnceActivity.readMode.enableVisualFormatting": "true",
"RunOnceActivity.west.config.association.type.startup.service": "true",
"cf.first.check.clang-format": "false",
"cidr.known.project.marker": "true",
"git-widget-placeholder": "Flexible-cube-count",
"last_opened_file_path": "C:/Desktop/StudiumME/3.Sem/Prog3/B/MatrixPybind",
"node.js.detected.package.eslint": "true",
"node.js.detected.package.tslint": "true",
"node.js.selected.package.eslint": "(autodetect)",
"node.js.selected.package.tslint": "(autodetect)",
"nodejs_package_manager_path": "npm",
"vue.rearranger.settings.migration": "true"
<component name="PropertiesComponent">{
&quot;keyToString&quot;: {
&quot;CMake Application.Prog3B.executor&quot;: &quot;Run&quot;,
&quot;RunOnceActivity.RadMigrateCodeStyle&quot;: &quot;true&quot;,
&quot;RunOnceActivity.ShowReadmeOnStart&quot;: &quot;true&quot;,
&quot;RunOnceActivity.cidr.known.project.marker&quot;: &quot;true&quot;,
&quot;RunOnceActivity.git.unshallow&quot;: &quot;true&quot;,
&quot;RunOnceActivity.readMode.enableVisualFormatting&quot;: &quot;true&quot;,
&quot;RunOnceActivity.west.config.association.type.startup.service&quot;: &quot;true&quot;,
&quot;cf.first.check.clang-format&quot;: &quot;false&quot;,
&quot;cidr.known.project.marker&quot;: &quot;true&quot;,
&quot;git-widget-placeholder&quot;: &quot;bug__fix__branch&quot;,
&quot;last_opened_file_path&quot;: &quot;C:/Desktop/StudiumME/3.Sem/Prog3/B/MatrixPybind&quot;,
&quot;node.js.detected.package.eslint&quot;: &quot;true&quot;,
&quot;node.js.detected.package.tslint&quot;: &quot;true&quot;,
&quot;node.js.selected.package.eslint&quot;: &quot;(autodetect)&quot;,
&quot;node.js.selected.package.tslint&quot;: &quot;(autodetect)&quot;,
&quot;nodejs_package_manager_path&quot;: &quot;npm&quot;,
&quot;vue.rearranger.settings.migration&quot;: &quot;true&quot;
}
}]]></component>
}</component>
<component name="RunManager" selected="CMake Application.Prog3B">
<configuration default="true" type="CLionExternalRunConfiguration" factoryName="Application" REDIRECT_INPUT="false" ELEVATE="false" USE_EXTERNAL_CONSOLE="false" EMULATE_TERMINAL="false" PASS_PARENT_ENVS_2="true">
<method v="2">
<option name="CLION.EXTERNAL.BUILD" enabled="true" />
</method>
</configuration>
<configuration name="Prog3B" type="CMakeRunConfiguration" factoryName="Application" REDIRECT_INPUT="false" ELEVATE="false" USE_EXTERNAL_CONSOLE="false" EMULATE_TERMINAL="false" PASS_PARENT_ENVS_2="true" PROJECT_NAME="Prog3B" TARGET_NAME="Prog3B" CONFIG_NAME="Debug" RUN_TARGET_PROJECT_NAME="Prog3B" RUN_TARGET_NAME="Prog3B">
<method v="2">
<option name="com.jetbrains.cidr.execution.CidrBuildBeforeRunTaskProvider$BuildBeforeRunTask" enabled="true" />
@ -118,6 +122,9 @@
<workItem from="1763652636624" duration="1226000" />
<workItem from="1763653877709" duration="28000" />
<workItem from="1763895078693" duration="5675000" />
<workItem from="1764526482502" duration="8000" />
<workItem from="1764526493477" duration="4519000" />
<workItem from="1765791349412" duration="635000" />
</task>
<servers />
</component>

View File

@ -17,6 +17,9 @@ set(SRC_FILES
${CMAKE_CURRENT_LIST_DIR}/gamecube.cpp
${CMAKE_CURRENT_LIST_DIR}/gamematrix.cpp
${CMAKE_CURRENT_LIST_DIR}/createCube.cpp
${CMAKE_CURRENT_LIST_DIR}/userinterface.cpp
${CMAKE_CURRENT_LIST_DIR}/raygui_impl.cpp
${CMAKE_CURRENT_LIST_DIR}/ScoreManager.cpp
)
set(INCLUDE_DIRS

2
Highscores.txt Normal file
View File

@ -0,0 +1,2 @@
score 99
time 99999.99

72
ScoreManager.cpp Normal file
View File

@ -0,0 +1,72 @@
#include "ScoreManager.h"
#include <fstream>
#include <iostream>
ScoreManager::ScoreManager(const std::string& filename) : highscoreFile(filename) {
loadHighscore();
}
void ScoreManager::loadHighscore() {
std::ifstream file(highscoreFile);
if (!file.is_open()) {
std::cout << "No highscore file found, creating new file.\n";
highScore = 9999;
bestTime = 99999.99;
return;
}
std::string key;
while (file >> key) {
if (key == "score") file >> highScore;
else if (key == "time") file >> bestTime;
}
file.close();
// If file contains useless values (like old format), reset
if (highScore <= 0 || highScore > 9999) highScore = 9999;
if (bestTime <= 0 || bestTime > 99999.99) bestTime = 99999.99;
}
void ScoreManager::incrementScore() {
currentScore++;
}
void ScoreManager::resetScore() {
currentScore = 0;
}
void ScoreManager::saveHighScore(int finalScore, double finalTime) {
bool updated = false;
if (finalScore < highScore) {
highScore = finalScore;
updated = true;
}
if (finalTime < bestTime) {
bestTime = finalTime;
updated = true;
}
// For safety: If file was default → always write
if (highScore == 9999 && bestTime == 99999.99)
updated = true;
if (!updated) return;
std::ofstream file(highscoreFile);
if (!file.is_open()) {
std::cerr << "❌ ERROR: Can't write highscore file\n";
return;
}
file << "score " << highScore << "\n";
file << "time " << bestTime << "\n";
file.close();
std::cout << "✅ Highscore updated!\n";
}

25
ScoreManager.h Normal file
View File

@ -0,0 +1,25 @@
#pragma once
#include <string>
#include <limits>
class ScoreManager {
public:
ScoreManager(const std::string& filename);
void incrementScore();
void resetScore();
void saveHighScore(int finalScore, double finalTime);
int getCurrentScore() const { return currentScore; }
int getHighScore() const { return highScore; }
double getBestTime() const { return bestTime; }
private:
int currentScore = 0;
int highScore = std::numeric_limits<int>::max();
double bestTime = std::numeric_limits<double>::max();
std::string highscoreFile;
void loadHighscore();
};

View File

@ -3,12 +3,6 @@
std::vector<Vec3> createCubes(int pairs)
{
//zwei bis zehn paare
if (pairs >10 || pairs <= 1)
{
// 6 Karten-Positionen im 3x2 Raster
return {{-2, 0, -2}, {0, 0, -2}, {2, 0, -2},{-2, 0, 0}, {0, 0, 0}, {2, 0, 0}}; // Default field
}
std::vector<int> fieldSize = calculateFieldSize(pairs);
std::vector<Vec3> cubePositions;
//std::cout<<(fieldSize[0])<<std::endl;
@ -48,7 +42,6 @@ std::vector<int> calculateFieldSize(int pairs)
std::vector<Color> getColors(int pairs)
{
if (pairs >10) pairs = 10;
std::vector<Color> returnColors;
Color colors[] = {
{230, 25, 75, 255}, // Red

View File

@ -32,3 +32,11 @@ private:
bool flippingBackward = false;
float rotation = 0.0f;
};
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
};

View File

@ -1,13 +1,20 @@
#include "gamecube.h"
#include "createCube.h"
#include "userinterface.h"
#include <algorithm>
#include <ctime>
#include <iostream>
#include "ScoreManager.h"
// -----------------------------------------------------------
// 3D Memory Game Hauptprogramm
// -----------------------------------------------------------
int main()
{
UserInterface userInterface;
ScoreManager score("Highscores.txt");
//CHRIS WAR HIER
//AXIOM WAR HIER
// Zufall initialisieren
@ -24,10 +31,32 @@ int main()
camera.fovy = 45.0f;
camera.projection = CAMERA_PERSPECTIVE;
int cubePairs = 3; //Cube count => 2*cubePairs
GameState state = GameState::Idle;
//TODO: Hier die Zeit abrufen.
double currentScoreTime = 0.0;
double startTime = 0.0;
double highScoreTime = score.getBestTime();
//TODO: Hier die Anzahl der Züge abrufen.
int currentScoreTurns = 0;
int highScoreTurns = score.getHighScore();
//if (currentScoreTime <= highScoreTime) highScoreTime = currentScoreTime;
//if (currentScoreTime <= highScoreTurns) highScoreTurns = currentScoreTurns;
int cubePairs = userInterface.getCubeCount();
if (cubePairs <= 0) {
userInterface.showMenu(currentScoreTime, highScoreTime, currentScoreTurns, highScoreTurns);
cubePairs = userInterface.getCubeCount();
// Startzeit speichern nach Enter
startTime = currentScoreTime;
}
std::vector<Vec3> positions =createCubes(cubePairs);
// Nur 3 Farben für 3 Paare
// Nur x Farben für x Paare
std::vector<Color> colors= getColors(cubePairs);
@ -62,7 +91,11 @@ int main()
while (!WindowShouldClose())
{
// Klick-Erkennung
if (!gameWon && IsMouseButtonPressed(MOUSE_LEFT_BUTTON))
//if (!gameWon && IsMouseButtonPressed(MOUSE_LEFT_BUTTON))
if (!gameWon
&& state != GameState::LockInput
&& state != GameState::CheckingMatch
&& IsMouseButtonPressed(MOUSE_LEFT_BUTTON))
{
Vector2 mouse = GetMousePosition();
@ -73,7 +106,27 @@ 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 (state == GameState::OneFlipped && first == &c)
break;
c.FlipForward();
if (state == GameState::Idle)
{
first = &c;
state = GameState::OneFlipped;
}else if (state == GameState::OneFlipped && first != &c)
{
second = &c;
state = GameState::LockInput;
}
score.incrementScore();
currentScoreTurns = score.getCurrentScore() / 2;
break;
}
}
}
}
@ -82,18 +135,21 @@ int main()
for (auto &c : cubes)
{
c.Update(flipSpeed);
}
// Sobald ein Würfel vollständig umgedreht ist → merken
if (c.IsFlipped() && !c.IsMatched())
if (state == GameState::LockInput && first && second)
{
if (first->GetRotationY() == 180.0f && second->GetRotationY() == 180.0f)
{
if (!first) first = &c;
else if (!second && &c != first) second = &c;
state = GameState::CheckingMatch;
}
}
// Matching-Logik
if (first && second)
if (state == GameState::CheckingMatch && first && second)
{
//currentScoreTurns = score.getCurrentScore();
Color col1 = first->GetColor();
Color col2 = second->GetColor();
@ -109,15 +165,25 @@ int main()
}
first = second = nullptr;
state = GameState::Idle;
std::cout <<"unlock";
}
// Gewinnprüfung
if (!gameWon)
gameWon = std::all_of(cubes.begin(), cubes.end(), [](const gamecube &c){ return c.IsMatched(); });
//Zeit Berechnung
if (!gameWon)
{
currentScoreTime = GetTime() - startTime;
DrawText(TextFormat("%.2f", currentScoreTime), 10, 30, 20, DARKGRAY);
}
// -----------------------------------------------------------
// Zeichnen
// -----------------------------------------------------------
BeginDrawing();
ClearBackground(RAYWHITE);
BeginMode3D(camera);
@ -127,11 +193,22 @@ int main()
EndMode3D();
if (gameWon)
if (gameWon) {
DrawText("Congrats! You found all pairs!", 150, 260, 30, DARKBLUE);
//TODO: Neue Scores zuweisen, current genügen.
//currentScoreTime = 17.3;
//currentScoreTurns = 10;
if (currentScoreTime <= highScoreTime) highScoreTime = currentScoreTime;
if (currentScoreTurns <= highScoreTurns) highScoreTurns = currentScoreTurns;
score.saveHighScore(currentScoreTurns, currentScoreTime);
userInterface.showScore(currentScoreTime, highScoreTime, currentScoreTurns, highScoreTurns);
}
else
DrawText("Flip 2 cubes - find matching pairs!", 10, 10, 20, DARKGRAY);
EndDrawing();
}

2
raygui_impl.cpp Normal file
View File

@ -0,0 +1,2 @@
#define RAYGUI_IMPLEMENTATION
#include "raygui.h"

5994
raylib/raygui.h Normal file

File diff suppressed because it is too large Load Diff

77
userinterface.cpp Normal file
View File

@ -0,0 +1,77 @@
#include "userinterface.h"
#include "raylib.h"
#include "raygui.h"
#include <iostream>
#include <cstring>
#include <format>
int UserInterface::getCubeCount() {
return cubeCount;
};
void UserInterface::showMenu(double &currentScoreTime, double &highScoreTime, int &currentScoreTurns, int &highScoreTurns) {
char textInput[3] = "3";
menuOpen = true;
Rectangle inputBox = {300, 250, 200, 50};
while (menuOpen && !WindowShouldClose()) {
BeginDrawing();
ClearBackground(RAYWHITE);
showScore(currentScoreTime, highScoreTime, currentScoreTurns, highScoreTurns);
if (wrongInput) {
DrawText("Falsche Eingabe.\nWie viele Würfelpaare? (2-10)", 220, 180, 20, DARKGRAY);
} else {
DrawText("Wie viele Würfelpaare? (2-10)", 220, 180, 20, DARKGRAY);
}
GuiTextBox(inputBox, textInput, 3, true);
if (IsKeyPressed(KEY_ENTER)) {
if (!(std::atoi(textInput) > 10 || std::atoi(textInput) <= 1)) {
menuOpen = false;
cubeCount = std::atoi(textInput);
currentScoreTime = GetTime();
} else {
wrongInput = true;
}
}
EndDrawing();
}
}
void UserInterface::showScore(double &currentScoreTime, double &highScoreTime, int &currentScoreTurns, int &highScoreTurns) {
Color currentScoreTimeColor = RED;
Color highScoreTimeColor = GREEN;
Color currentScoreTurnsColor = RED;
Color highScoreTurnsColor = GREEN;
if (currentScoreTime <= highScoreTime) {
currentScoreTimeColor = GREEN;
} else if (currentScoreTime < highScoreTime) {
currentScoreTimeColor = RED;
}
if (currentScoreTurns <= highScoreTurns) {
currentScoreTurnsColor = GREEN;
} else if (currentScoreTurns > highScoreTurns) {
currentScoreTurnsColor = RED;
}
if (!menuOpen) {
std::string currentTimeOutput = std::format("Deine Zeit: {:.2f}", currentScoreTime);
DrawText(currentTimeOutput.c_str(), 220, 100, 20, currentScoreTimeColor);
std::string currentTurnsOutput = "Deine Züge: " + std::to_string(currentScoreTurns);
DrawText(currentTurnsOutput.c_str(), 440, 100, 20, currentScoreTurnsColor);
}
std::string highTimeOutput = std::format("Beste Zeit: {:.2f}", highScoreTime);
DrawText(highTimeOutput.c_str(), 220, 120, 20, highScoreTimeColor);
std::string highTurnsOutput = "Wenigste Züge: " + std::to_string(highScoreTurns);
DrawText(highTurnsOutput.c_str(), 440, 120, 20, highScoreTurnsColor);
}

20
userinterface.h Normal file
View File

@ -0,0 +1,20 @@
#pragma once
class UserInterface {
public:
int getCubeCount();
void showMenu(double &currentScoreTime, double &highScoreTime, int &currentScoreTurns, int &highScoreTurns);
void showScore(double &currentScoreTime, double &highScoreTime, int &currentScoreTurns, int &highScoreTurns);
bool menuOpen = false;
private:
int cubeCount = 0;
bool wrongInput = false;
};
#ifndef USERINTERFACE_H
#define USERINTERFACE_H
#endif //USERINTERFACE_H