Compare commits
No commits in common. "Krisp" and "main" have entirely different histories.
Binary file not shown.
@ -1,9 +1,9 @@
|
|||||||
CC = gcc
|
CC = gcc
|
||||||
CFLAGS = -g -Wall -I$(raylibfolder)
|
CFLAGS = -g -Wall
|
||||||
LDFLAGS = -lGL -lX11 -lm
|
LDFLAGS = -lGL -lX11 -lm
|
||||||
BINARIES = ./linux
|
BINARIES = ./linux
|
||||||
|
|
||||||
raylib_folder = ./raylib
|
raylibfolder = ./raylib
|
||||||
unityfolder = ./unity
|
unityfolder = ./unity
|
||||||
|
|
||||||
# --------------------------
|
# --------------------------
|
||||||
@ -28,7 +28,7 @@ game.o: game.c
|
|||||||
$(CC) $(CFLAGS) -c game.c
|
$(CC) $(CFLAGS) -c game.c
|
||||||
|
|
||||||
graphicalGame.o: graphicalGame.c
|
graphicalGame.o: graphicalGame.c
|
||||||
$(CC) $(CFLAGS) -c graphicalGame.c
|
$(CC) $(CFLAGS) -I$(raylibfolder) -c graphicalGame.c
|
||||||
|
|
||||||
# --------------------------
|
# --------------------------
|
||||||
# Unit Tests
|
# Unit Tests
|
||||||
|
|||||||
@ -1,16 +1,15 @@
|
|||||||
#include "unity.h"
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include "unity.h"
|
||||||
|
|
||||||
#include "game.h"
|
|
||||||
#include "input.h"
|
#include "input.h"
|
||||||
|
#include "game.h"
|
||||||
|
|
||||||
// ANSI Escape Codes für Farben
|
// ANSI Escape Codes für Farben
|
||||||
// Hinweis: damit das in CLion mit der integrierten Konsole/Output funktioniert,
|
// Hinweis: damit das in CLion mit der integrierten Konsole/Output funktioniert, muss man auf die Run-Config gehen und
|
||||||
// muss man auf die Run-Config gehen und das Häkchen bei 'emulate terminal in
|
// das Häkchen bei 'emulate terminal in the output console' setzen
|
||||||
// the output console' setzen (s. auch:
|
// (s. auch: https://stackoverflow.com/questions/32742850/how-to-show-colored-console-output-in-clion)
|
||||||
// https://stackoverflow.com/questions/32742850/how-to-show-colored-console-output-in-clion)
|
|
||||||
#define RESET "\033[0m"
|
#define RESET "\033[0m"
|
||||||
#define GREEN "\033[32m"
|
#define GREEN "\033[32m"
|
||||||
#define RED "\033[31m"
|
#define RED "\033[31m"
|
||||||
@ -102,7 +101,37 @@ void test_createWordSalad_too_small(void) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void test_createWordSalad_allWordsPlaced() {
|
||||||
|
char words[3][MAX_WORD_LEN] = {"CAT", "DOG", "MOUSE"};
|
||||||
|
char saladHoriz[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN];
|
||||||
|
char saladVert[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN];
|
||||||
|
|
||||||
|
int placed = createWordSalad(saladHoriz, 20, words, 3);
|
||||||
|
for(int i = 0; i < MAX_SEARCH_FIELD_LEN; i++)
|
||||||
|
{
|
||||||
|
for(int j = 0; j < MAX_SEARCH_FIELD_LEN; j++)
|
||||||
|
{
|
||||||
|
saladVert[j][i] = saladHoriz[i][j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for(int i = 0; i < 3; i++) {
|
||||||
|
const char* word = words[i];
|
||||||
|
int wordFound = 0;
|
||||||
|
for(int j = 0; j < MAX_SEARCH_FIELD_LEN; j++)
|
||||||
|
{
|
||||||
|
const char* row = saladHoriz[j];
|
||||||
|
const char* col = saladVert[j];
|
||||||
|
wordFound |= strstr(row, word) || strstr(col, word);
|
||||||
|
}
|
||||||
|
TEST_ASSERT_TRUE_MESSAGE(wordFound, "Not all words were placed.");
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_ASSERT_EQUAL_INT(3, placed);
|
||||||
|
}
|
||||||
|
|
||||||
// ---------- Test Setup und TearDown Funktionen ----------
|
// ---------- Test Setup und TearDown Funktionen ----------
|
||||||
|
|
||||||
// Hier Setup- und TearDown-Funktionen definieren,
|
// Hier Setup- und TearDown-Funktionen definieren,
|
||||||
// falls Vor- und Nachbereitungen für die Tests benötigt.
|
// falls Vor- und Nachbereitungen für die Tests benötigt.
|
||||||
|
|
||||||
@ -135,6 +164,7 @@ int main(void) {
|
|||||||
RUN_TEST(test_readWords_empty_file);
|
RUN_TEST(test_readWords_empty_file);
|
||||||
RUN_TEST(test_createWordSalad_all_fit);
|
RUN_TEST(test_createWordSalad_all_fit);
|
||||||
RUN_TEST(test_createWordSalad_too_small);
|
RUN_TEST(test_createWordSalad_too_small);
|
||||||
|
RUN_TEST(test_createWordSalad_allWordsPlaced);
|
||||||
|
|
||||||
int result = UNITY_END(); // Test-Ergebnisse
|
int result = UNITY_END(); // Test-Ergebnisse
|
||||||
print_test_result(result);
|
print_test_result(result);
|
||||||
|
|||||||
@ -4,7 +4,7 @@ LDFLAGS = -framework OpenGL -framework CoreFoundation -framework CoreGraphics -f
|
|||||||
ARCH := $(shell uname -m)
|
ARCH := $(shell uname -m)
|
||||||
BINARIES = ./macos-$(ARCH)
|
BINARIES = ./macos-$(ARCH)
|
||||||
|
|
||||||
raylib_folder = ./raylib
|
raylibfolder = ./raylib
|
||||||
unityfolder = ./unity
|
unityfolder = ./unity
|
||||||
|
|
||||||
# --------------------------
|
# --------------------------
|
||||||
@ -43,4 +43,4 @@ test: input.o game.o unit_tests.c $(BINARIES)/libunity.a
|
|||||||
# Clean
|
# Clean
|
||||||
# --------------------------
|
# --------------------------
|
||||||
clean:
|
clean:
|
||||||
rm -f *.o wordsalad
|
rm -f *.o wordsalad $(TEST_BIN)
|
||||||
|
|||||||
@ -101,6 +101,35 @@ void test_createWordSalad_too_small(void) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void test_createWordSalad_allWordsPlaced() {
|
||||||
|
char words[3][MAX_WORD_LEN] = {"CAT", "DOG", "MOUSE"};
|
||||||
|
char saladHoriz[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN];
|
||||||
|
char saladVert[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN];
|
||||||
|
|
||||||
|
int placed = createWordSalad(saladHoriz, 20, words, 3);
|
||||||
|
for(int i = 0; i < MAX_SEARCH_FIELD_LEN; i++)
|
||||||
|
{
|
||||||
|
for(int j = 0; j < MAX_SEARCH_FIELD_LEN; j++)
|
||||||
|
{
|
||||||
|
saladVert[j][i] = saladHoriz[i][j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for(int i = 0; i < 3; i++) {
|
||||||
|
const char* word = words[i];
|
||||||
|
int wordFound = 0;
|
||||||
|
for(int j = 0; j < MAX_SEARCH_FIELD_LEN; j++)
|
||||||
|
{
|
||||||
|
const char* row = saladHoriz[j];
|
||||||
|
const char* col = saladVert[j];
|
||||||
|
wordFound |= strstr(row, word) || strstr(col, word);
|
||||||
|
}
|
||||||
|
TEST_ASSERT_TRUE_MESSAGE(wordFound, "Not all words were placed.");
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_ASSERT_EQUAL_INT(3, placed);
|
||||||
|
}
|
||||||
|
|
||||||
// ---------- Test Setup und TearDown Funktionen ----------
|
// ---------- Test Setup und TearDown Funktionen ----------
|
||||||
|
|
||||||
// Hier Setup- und TearDown-Funktionen definieren,
|
// Hier Setup- und TearDown-Funktionen definieren,
|
||||||
@ -135,6 +164,7 @@ int main(void) {
|
|||||||
RUN_TEST(test_readWords_empty_file);
|
RUN_TEST(test_readWords_empty_file);
|
||||||
RUN_TEST(test_createWordSalad_all_fit);
|
RUN_TEST(test_createWordSalad_all_fit);
|
||||||
RUN_TEST(test_createWordSalad_too_small);
|
RUN_TEST(test_createWordSalad_too_small);
|
||||||
|
RUN_TEST(test_createWordSalad_allWordsPlaced);
|
||||||
|
|
||||||
int result = UNITY_END(); // Test-Ergebnisse
|
int result = UNITY_END(); // Test-Ergebnisse
|
||||||
print_test_result(result);
|
print_test_result(result);
|
||||||
|
|||||||
@ -1,125 +1,23 @@
|
|||||||
#include "game.h"
|
#include "game.h"
|
||||||
|
#include <time.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <time.h>
|
|
||||||
|
|
||||||
#define MAX_RAND_TRIES_PER_WORD 10
|
#define MAX_RAND_TRIES_PER_WORD 10
|
||||||
#define EMPTY_CHAR 0
|
#define EMPTY_CHAR 0
|
||||||
|
|
||||||
int createWordSalad(
|
//TODO: Spiellogik implementieren:
|
||||||
|
/* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren
|
||||||
|
* restliche Felder mit zufälligen Buchstaben füllen */
|
||||||
|
|
||||||
char salad[MAX_SEARCH_FIELD_LEN]
|
// Creates the word salad by placing words randomly and filling empty spaces
|
||||||
[MAX_SEARCH_FIELD_LEN], // salad ist Spielfeld 2D Array mit 20*20
|
int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount)
|
||||||
// Buchstaben
|
{
|
||||||
unsigned int searchFieldLen, const char words[][MAX_WORD_LEN],
|
|
||||||
unsigned int wordCount) {
|
|
||||||
|
|
||||||
enum Richtung { HORIZONTAL, VERTIKAL };
|
|
||||||
|
|
||||||
for (int x = 0; x < searchFieldLen; x++) {
|
|
||||||
|
|
||||||
for (int y = 0; y < searchFieldLen; y++) {
|
|
||||||
|
|
||||||
salad[y][x] = EMPTY_CHAR; // Spielfeld auf EmptyChar setzen
|
|
||||||
}
|
|
||||||
}
|
|
||||||
unsigned int placedWords = 0; // variable platzierte Wörter
|
|
||||||
|
|
||||||
// Wörter platzieren
|
|
||||||
// for schleife mit wortNummer = 0 bis wordCount - 1 (insgesamt wordCount)
|
|
||||||
|
|
||||||
for (int wortNummer = 0; wortNummer < wordCount; wortNummer++) {
|
|
||||||
|
|
||||||
// strlen mit Wortlänge
|
|
||||||
int wortLen = strlen(words[wortNummer]);
|
|
||||||
|
|
||||||
int platziert = 0; // variable, um platziertes Wort zu signalisieren
|
|
||||||
int versuch = 0; // veruche bis max rand tries
|
|
||||||
|
|
||||||
while (versuch < MAX_RAND_TRIES_PER_WORD &&
|
|
||||||
platziert == 0) { // wort platziert -> Schleife wird verlassen oder
|
|
||||||
// konnte nach 10 Versuchen nicht platziert werden
|
|
||||||
|
|
||||||
enum Richtung r =
|
|
||||||
(rand() % 2) ? HORIZONTAL : VERTIKAL; // mit rand Richtung
|
|
||||||
|
|
||||||
int x = rand() % searchFieldLen; // mit rand Koordinaten bestimmen
|
|
||||||
int y = rand() % searchFieldLen;
|
|
||||||
int wortFrei = 1; // Wort passt ins Feld und überschneidet sich nicht
|
|
||||||
|
|
||||||
if (r == VERTIKAL) {
|
|
||||||
|
|
||||||
if ((y + wortLen) > (searchFieldLen)) { // Wortlänge größer als Feld
|
|
||||||
wortFrei = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int i = 0; (i < wortLen) && (wortFrei != 0); i++) {
|
|
||||||
char var = salad[y + i][x];
|
|
||||||
if (var != EMPTY_CHAR) {
|
|
||||||
wortFrei = 0; // Wort überschneidet sich mit bereits platziertem
|
|
||||||
// Wort
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (wortFrei != 0) {
|
|
||||||
|
|
||||||
for (int i = 0; i < wortLen; i++) {
|
|
||||||
|
|
||||||
salad[y + i][x] = words[wortNummer][i]; // Wort platzieren
|
|
||||||
}
|
|
||||||
placedWords++;
|
|
||||||
platziert = 1; // schleife wird verlassen
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
else if (r == HORIZONTAL) {
|
// Prints the word salad to console
|
||||||
|
void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen)
|
||||||
|
{
|
||||||
|
|
||||||
if ((x + wortLen) > searchFieldLen) {
|
|
||||||
wortFrei = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int i = 0; (i < wortLen) && (wortFrei != 0); i++) {
|
|
||||||
char var = salad[y][x + i];
|
|
||||||
if (var != EMPTY_CHAR) {
|
|
||||||
wortFrei = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (wortFrei != 0) {
|
|
||||||
|
|
||||||
for (int i = 0; i < wortLen; i++) {
|
|
||||||
|
|
||||||
salad[y][x + i] = words[wortNummer][i];
|
|
||||||
}
|
|
||||||
placedWords++;
|
|
||||||
platziert = 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
versuch++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int i = 0; i < searchFieldLen; i++) {
|
|
||||||
|
|
||||||
for (int j = 0; j < searchFieldLen; j++) {
|
|
||||||
|
|
||||||
if (salad[i][j] == EMPTY_CHAR) {
|
|
||||||
|
|
||||||
salad[i][j] = rand() % (90 - 65 + 1) + 65;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return placedWords;
|
|
||||||
}
|
|
||||||
|
|
||||||
void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN],
|
|
||||||
unsigned int searchFieldLen) {
|
|
||||||
|
|
||||||
for (int i = 0; i < searchFieldLen; i++) {
|
|
||||||
for (int j = 0; j < searchFieldLen; j++) {
|
|
||||||
printf("%c", salad[i][j]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Binary file not shown.
@ -7,19 +7,22 @@
|
|||||||
#define MAX_MESSAGE_LEN 256
|
#define MAX_MESSAGE_LEN 256
|
||||||
#define MAX_SOLUTION_WORD_LEN 16
|
#define MAX_SOLUTION_WORD_LEN 16
|
||||||
|
|
||||||
typedef struct {
|
typedef struct
|
||||||
|
{
|
||||||
Vector2 startPosition;
|
Vector2 startPosition;
|
||||||
Vector2 endPosition;
|
Vector2 endPosition;
|
||||||
int isSelected;
|
int isSelected;
|
||||||
} MouseSelection;
|
} MouseSelection;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct
|
||||||
|
{
|
||||||
Vector2 position;
|
Vector2 position;
|
||||||
char character[2];
|
char character[2];
|
||||||
int isMarked;
|
int isMarked;
|
||||||
} CharSquare;
|
} CharSquare;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct
|
||||||
|
{
|
||||||
CharSquare *squares;
|
CharSquare *squares;
|
||||||
unsigned int count;
|
unsigned int count;
|
||||||
unsigned int squareSize;
|
unsigned int squareSize;
|
||||||
@ -28,14 +31,16 @@ typedef struct {
|
|||||||
Vector2 size;
|
Vector2 size;
|
||||||
} CharSquarePanel;
|
} CharSquarePanel;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct
|
||||||
|
{
|
||||||
char content[MAX_WORD_LEN];
|
char content[MAX_WORD_LEN];
|
||||||
char *solution;
|
char *solution;
|
||||||
Vector2 position;
|
Vector2 position;
|
||||||
int wasFound;
|
int wasFound;
|
||||||
} SearchWord;
|
} SearchWord;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct
|
||||||
|
{
|
||||||
SearchWord *words;
|
SearchWord *words;
|
||||||
unsigned int count;
|
unsigned int count;
|
||||||
int fontSize;
|
int fontSize;
|
||||||
@ -43,13 +48,15 @@ typedef struct {
|
|||||||
Vector2 size;
|
Vector2 size;
|
||||||
} SearchWordPanel;
|
} SearchWordPanel;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct
|
||||||
|
{
|
||||||
char content[MAX_MESSAGE_LEN];
|
char content[MAX_MESSAGE_LEN];
|
||||||
Vector2 position;
|
Vector2 position;
|
||||||
unsigned int size;
|
unsigned int size;
|
||||||
} WinMessage;
|
} WinMessage;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct
|
||||||
|
{
|
||||||
char text[MAX_MESSAGE_LEN];
|
char text[MAX_MESSAGE_LEN];
|
||||||
Vector2 position;
|
Vector2 position;
|
||||||
Vector2 size;
|
Vector2 size;
|
||||||
@ -57,9 +64,9 @@ typedef struct {
|
|||||||
} HelperMessage;
|
} HelperMessage;
|
||||||
|
|
||||||
// Creates a helper message to guide the user
|
// Creates a helper message to guide the user
|
||||||
static HelperMessage createHelperMessage(unsigned int screenWidth) {
|
static HelperMessage createHelperMessage(unsigned int screenWidth)
|
||||||
const char *text = "Please search below for the words located at the bottom "
|
{
|
||||||
"\nand draw a line exactly on the desired characters ...";
|
const char *text = "Please search below for the words located at the bottom \nand draw a line exactly on the desired characters ...";
|
||||||
HelperMessage msg = {"", {0, 0}, {screenWidth, 0}, 18};
|
HelperMessage msg = {"", {0, 0}, {screenWidth, 0}, 18};
|
||||||
|
|
||||||
// Copy text into msg, ensuring does not exceed max length
|
// Copy text into msg, ensuring does not exceed max length
|
||||||
@ -73,7 +80,8 @@ static HelperMessage createHelperMessage(unsigned int screenWidth) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Creates a winning message when the user wins
|
// Creates a winning message when the user wins
|
||||||
static WinMessage createWinMessage(unsigned int screenSize) {
|
static WinMessage createWinMessage(unsigned int screenSize)
|
||||||
|
{
|
||||||
WinMessage winMsg;
|
WinMessage winMsg;
|
||||||
char *text = "Congratulations! You won!";
|
char *text = "Congratulations! You won!";
|
||||||
|
|
||||||
@ -82,15 +90,15 @@ static WinMessage createWinMessage(unsigned int screenSize) {
|
|||||||
winMsg.size = 30; // Set font size
|
winMsg.size = 30; // Set font size
|
||||||
|
|
||||||
// Calculate x and y positions for centering the message
|
// Calculate x and y positions for centering the message
|
||||||
winMsg.position.x =
|
winMsg.position.x = (screenSize - strlen(winMsg.content)*winMsg.size*0.52) / 2;
|
||||||
(screenSize - strlen(winMsg.content) * winMsg.size * 0.52) / 2;
|
|
||||||
winMsg.position.y = screenSize / 2;
|
winMsg.position.y = screenSize / 2;
|
||||||
|
|
||||||
return winMsg;
|
return winMsg;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Frees memory associated with a search word panel
|
// Frees memory associated with a search word panel
|
||||||
static void freeSearchWordPanel(SearchWordPanel *panel) {
|
static void freeSearchWordPanel(SearchWordPanel *panel)
|
||||||
|
{
|
||||||
for(int i = 0; panel->words != NULL && i < panel->count; i++)
|
for(int i = 0; panel->words != NULL && i < panel->count; i++)
|
||||||
free(panel->words[i].solution); // Free solution strings
|
free(panel->words[i].solution); // Free solution strings
|
||||||
free(panel->words); // Free word array
|
free(panel->words); // Free word array
|
||||||
@ -101,15 +109,13 @@ static void freeSearchWordPanel(SearchWordPanel *panel) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Creates a panel to display a list of search words
|
// Creates a panel to display a list of search words
|
||||||
static SearchWordPanel createSearchWordPanel(const char words[][MAX_WORD_LEN],
|
static SearchWordPanel createSearchWordPanel(const char words[][MAX_WORD_LEN], unsigned int numberOfWords, unsigned int windowOffset)
|
||||||
unsigned int numberOfWords,
|
{
|
||||||
unsigned int windowOffset) {
|
|
||||||
const int maxStringLenInPx = 200; // Max width of each word
|
const int maxStringLenInPx = 200; // Max width of each word
|
||||||
const int fontSize = 18; // Font size for displaying words
|
const int fontSize = 18; // Font size for displaying words
|
||||||
const int rowHeight = fontSize * 1.2 + 5; // Height of each row of words
|
const int rowHeight = fontSize * 1.2 + 5; // Height of each row of words
|
||||||
|
|
||||||
SearchWordPanel panel = {
|
SearchWordPanel panel = {NULL, 0, fontSize, {0, windowOffset}, {windowOffset, 0}};
|
||||||
NULL, 0, fontSize, {0, windowOffset}, {windowOffset, 0}};
|
|
||||||
|
|
||||||
unsigned int xOffset = 5;
|
unsigned int xOffset = 5;
|
||||||
unsigned int yOffset = 15;
|
unsigned int yOffset = 15;
|
||||||
@ -119,9 +125,11 @@ static SearchWordPanel createSearchWordPanel(const char words[][MAX_WORD_LEN],
|
|||||||
panel.words = (SearchWord *)malloc(sizeof(SearchWord) * numberOfWords);
|
panel.words = (SearchWord *)malloc(sizeof(SearchWord) * numberOfWords);
|
||||||
|
|
||||||
// If memory allocation is successful
|
// If memory allocation is successful
|
||||||
if (panel.words != NULL) {
|
if(panel.words != NULL)
|
||||||
|
{
|
||||||
// Loop through and set up the words and their positions
|
// Loop through and set up the words and their positions
|
||||||
for (int i = 0; i < numberOfWords; i++) {
|
for(int i = 0; i < numberOfWords; i++)
|
||||||
|
{
|
||||||
strncpy(panel.words[i].content, words[i], MAX_SOLUTION_WORD_LEN);
|
strncpy(panel.words[i].content, words[i], MAX_SOLUTION_WORD_LEN);
|
||||||
panel.words[i].content[MAX_SOLUTION_WORD_LEN-1] = '\0';
|
panel.words[i].content[MAX_SOLUTION_WORD_LEN-1] = '\0';
|
||||||
|
|
||||||
@ -130,12 +138,12 @@ static SearchWordPanel createSearchWordPanel(const char words[][MAX_WORD_LEN],
|
|||||||
strncpy(panel.words[i].content+MAX_SOLUTION_WORD_LEN-4, "...", 4);
|
strncpy(panel.words[i].content+MAX_SOLUTION_WORD_LEN-4, "...", 4);
|
||||||
|
|
||||||
// Allocate memory for solution word
|
// Allocate memory for solution word
|
||||||
panel.words[i].solution =
|
panel.words[i].solution = (char *)malloc(sizeof(char) * (strlen(words[i]) + 1));
|
||||||
(char *)malloc(sizeof(char) * (strlen(words[i]) + 1));
|
|
||||||
|
|
||||||
if(panel.words[i].solution != NULL)
|
if(panel.words[i].solution != NULL)
|
||||||
strcpy(panel.words[i].solution, words[i]);
|
strcpy(panel.words[i].solution, words[i]);
|
||||||
else {
|
else
|
||||||
|
{
|
||||||
freeSearchWordPanel(&panel); // Free memory in case of failure
|
freeSearchWordPanel(&panel); // Free memory in case of failure
|
||||||
numberOfWords = 0;
|
numberOfWords = 0;
|
||||||
break;
|
break;
|
||||||
@ -149,7 +157,8 @@ static SearchWordPanel createSearchWordPanel(const char words[][MAX_WORD_LEN],
|
|||||||
xOffset += maxStringLenInPx + 5;
|
xOffset += maxStringLenInPx + 5;
|
||||||
|
|
||||||
// Move to next row if needed
|
// Move to next row if needed
|
||||||
if (xOffset > windowOffset) {
|
if(xOffset > windowOffset)
|
||||||
|
{
|
||||||
xOffset = 5;
|
xOffset = 5;
|
||||||
yOffset += rowHeight;
|
yOffset += rowHeight;
|
||||||
}
|
}
|
||||||
@ -165,8 +174,8 @@ static SearchWordPanel createSearchWordPanel(const char words[][MAX_WORD_LEN],
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Creates a square for a character in the search grid
|
// Creates a square for a character in the search grid
|
||||||
static CharSquare createSquare(unsigned int rowIdx, unsigned int colIdx,
|
static CharSquare createSquare(unsigned int rowIdx, unsigned int colIdx, char character, unsigned int squareSize)
|
||||||
char character, unsigned int squareSize) {
|
{
|
||||||
CharSquare square;
|
CharSquare square;
|
||||||
square.position.x = colIdx * squareSize;
|
square.position.x = colIdx * squareSize;
|
||||||
square.position.y = rowIdx * squareSize;
|
square.position.y = rowIdx * squareSize;
|
||||||
@ -178,29 +187,26 @@ static CharSquare createSquare(unsigned int rowIdx, unsigned int colIdx,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Creates a panel of character squares (the search grid)
|
// Creates a panel of character squares (the search grid)
|
||||||
static CharSquarePanel createCharSquarePanel(
|
static CharSquarePanel createCharSquarePanel(const char wordSalad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldSizeInChars, int panelSizeInPx)
|
||||||
const char wordSalad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN],
|
{
|
||||||
unsigned int searchFieldSizeInChars, int panelSizeInPx) {
|
|
||||||
CharSquarePanel squarePanel;
|
CharSquarePanel squarePanel;
|
||||||
squarePanel.squares = (CharSquare *)malloc(
|
squarePanel.squares = (CharSquare *)malloc(sizeof(CharSquare) * searchFieldSizeInChars * searchFieldSizeInChars);
|
||||||
sizeof(CharSquare) * searchFieldSizeInChars * searchFieldSizeInChars);
|
|
||||||
squarePanel.count = 0;
|
squarePanel.count = 0;
|
||||||
squarePanel.squareSize = (double)panelSizeInPx /
|
squarePanel.squareSize = (double)panelSizeInPx / searchFieldSizeInChars; // Calculate the square size
|
||||||
searchFieldSizeInChars; // Calculate the square size
|
squarePanel.fontSize = squarePanel.squareSize * 0.75; // Set font size relative to square size
|
||||||
squarePanel.fontSize =
|
|
||||||
squarePanel.squareSize * 0.75; // Set font size relative to square size
|
|
||||||
squarePanel.position.x = 0;
|
squarePanel.position.x = 0;
|
||||||
squarePanel.position.y = 0;
|
squarePanel.position.y = 0;
|
||||||
squarePanel.size.x = panelSizeInPx;
|
squarePanel.size.x = panelSizeInPx;
|
||||||
squarePanel.size.y = panelSizeInPx;
|
squarePanel.size.y = panelSizeInPx;
|
||||||
|
|
||||||
// If memory for squares is allocated successfully loop through grid and
|
// If memory for squares is allocated successfully loop through grid and create squares for each character
|
||||||
// create squares for each character
|
if(squarePanel.squares != NULL)
|
||||||
if (squarePanel.squares != NULL) {
|
{
|
||||||
for (int i = 0; i < searchFieldSizeInChars; i++) {
|
for(int i = 0; i < searchFieldSizeInChars; i++)
|
||||||
for (int j = 0; j < searchFieldSizeInChars; j++) {
|
{
|
||||||
squarePanel.squares[squarePanel.count] =
|
for(int j = 0; j < searchFieldSizeInChars; j++)
|
||||||
createSquare(i, j, wordSalad[i][j], squarePanel.squareSize);
|
{
|
||||||
|
squarePanel.squares[squarePanel.count] = createSquare(i, j, wordSalad[i][j], squarePanel.squareSize);
|
||||||
squarePanel.count++;
|
squarePanel.count++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -210,49 +216,50 @@ static CharSquarePanel createCharSquarePanel(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Frees memory associated with CharSquarePanel
|
// Frees memory associated with CharSquarePanel
|
||||||
static void freeCharSquarePanel(CharSquarePanel *squarePanel) {
|
static void freeCharSquarePanel(CharSquarePanel *squarePanel)
|
||||||
|
{
|
||||||
free(squarePanel->squares); // Free squares array
|
free(squarePanel->squares); // Free squares array
|
||||||
squarePanel->squares = NULL;
|
squarePanel->squares = NULL;
|
||||||
squarePanel->count = 0; // Reset count
|
squarePanel->count = 0; // Reset count
|
||||||
}
|
}
|
||||||
|
|
||||||
// Draws all squares of CharSquarePanel
|
// Draws all squares of CharSquarePanel
|
||||||
static void drawSquares(const CharSquarePanel squarePanel) {
|
static void drawSquares(const CharSquarePanel squarePanel)
|
||||||
|
{
|
||||||
float fontOffset = squarePanel.squareSize / 4; // Offset for font positioning
|
float fontOffset = squarePanel.squareSize / 4; // Offset for font positioning
|
||||||
|
|
||||||
// Loop through all squares and draw them
|
// Loop through all squares and draw them
|
||||||
for (int i = 0; i < squarePanel.count; i++) {
|
for(int i = 0; i < squarePanel.count; i++)
|
||||||
|
{
|
||||||
CharSquare square = squarePanel.squares[i];
|
CharSquare square = squarePanel.squares[i];
|
||||||
|
|
||||||
Vector2 squareScreenCoord = {squarePanel.position.x + square.position.x,
|
Vector2 squareScreenCoord = {squarePanel.position.x + square.position.x, squarePanel.position.y + square.position.y};
|
||||||
squarePanel.position.y + square.position.y};
|
|
||||||
Color squareColor = DARKBLUE;
|
Color squareColor = DARKBLUE;
|
||||||
Color fontColor = LIGHTGRAY;
|
Color fontColor = LIGHTGRAY;
|
||||||
|
|
||||||
// Change colors if is marked
|
// Change colors if is marked
|
||||||
if (square.isMarked) {
|
if(square.isMarked)
|
||||||
|
{
|
||||||
squareColor = GREEN;
|
squareColor = GREEN;
|
||||||
fontColor = BLACK;
|
fontColor = BLACK;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Draw square and its border
|
// Draw square and its border
|
||||||
DrawRectangle(squareScreenCoord.x, squareScreenCoord.y,
|
DrawRectangle(squareScreenCoord.x, squareScreenCoord.y, squarePanel.squareSize, squarePanel.squareSize, squareColor);
|
||||||
squarePanel.squareSize, squarePanel.squareSize, squareColor);
|
|
||||||
for(int i = 1; i <= 3; i++)
|
for(int i = 1; i <= 3; i++)
|
||||||
DrawRectangleLines(squareScreenCoord.x, squareScreenCoord.y,
|
DrawRectangleLines(squareScreenCoord.x, squareScreenCoord.y, squarePanel.squareSize-i, squarePanel.squareSize-i, LIGHTGRAY);
|
||||||
squarePanel.squareSize - i, squarePanel.squareSize - i,
|
|
||||||
LIGHTGRAY);
|
|
||||||
|
|
||||||
// Draw character inside the square
|
// Draw character inside the square
|
||||||
DrawText(square.character, squareScreenCoord.x + fontOffset,
|
DrawText(square.character, squareScreenCoord.x + fontOffset, squareScreenCoord.y + fontOffset, squarePanel.fontSize, fontColor);
|
||||||
squareScreenCoord.y + fontOffset, squarePanel.fontSize, fontColor);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Checks if selected word is valid solution
|
// Checks if selected word is valid solution
|
||||||
static int isSolution(const char *solution, SearchWordPanel searchWordPanel) {
|
static int isSolution(const char *solution, SearchWordPanel searchWordPanel)
|
||||||
|
{
|
||||||
for(int i = 0; i < searchWordPanel.count; i++)
|
for(int i = 0; i < searchWordPanel.count; i++)
|
||||||
if (strcmp(solution, searchWordPanel.words[i].solution) == 0) {
|
if(strcmp(solution, searchWordPanel.words[i].solution) == 0)
|
||||||
|
{
|
||||||
// Mark word as found and return true if solution matches
|
// Mark word as found and return true if solution matches
|
||||||
searchWordPanel.words[i].wasFound = 1;
|
searchWordPanel.words[i].wasFound = 1;
|
||||||
return 1;
|
return 1;
|
||||||
@ -262,25 +269,23 @@ static int isSolution(const char *solution, SearchWordPanel searchWordPanel) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Updates the marked squares based on user selection
|
// Updates the marked squares based on user selection
|
||||||
static void updateSelectedSquares(const MouseSelection selection,
|
static void updateSelectedSquares(const MouseSelection selection, CharSquarePanel squarePanel, SearchWordPanel searchWordPanel)
|
||||||
CharSquarePanel squarePanel,
|
{
|
||||||
SearchWordPanel searchWordPanel) {
|
|
||||||
unsigned int wordIdx = 0;
|
unsigned int wordIdx = 0;
|
||||||
char selectedWord[MAX_WORD_LEN];
|
char selectedWord[MAX_WORD_LEN];
|
||||||
unsigned int selectedIdx[squarePanel.count];
|
unsigned int selectedIdx[squarePanel.count];
|
||||||
float radius = squarePanel.squareSize / 2;
|
float radius = squarePanel.squareSize / 2;
|
||||||
|
|
||||||
// Loop through all squares and check if selected
|
// Loop through all squares and check if selected
|
||||||
for (int i = 0; i < squarePanel.count && wordIdx < MAX_WORD_LEN - 1; i++) {
|
for(int i = 0; i < squarePanel.count && wordIdx < MAX_WORD_LEN-1; i++)
|
||||||
Vector2 center = {
|
{
|
||||||
squarePanel.squares[i].position.x + squarePanel.position.x,
|
Vector2 center = {squarePanel.squares[i].position.x + squarePanel.position.x, squarePanel.squares[i].position.y + squarePanel.position.y};
|
||||||
squarePanel.squares[i].position.y + squarePanel.position.y};
|
|
||||||
center.x += radius;
|
center.x += radius;
|
||||||
center.y += radius;
|
center.y += radius;
|
||||||
|
|
||||||
// Check if square is selected by mouse
|
// Check if square is selected by mouse
|
||||||
if (CheckCollisionCircleLine(center, radius, selection.startPosition,
|
if(CheckCollisionCircleLine(center, radius, selection.startPosition, selection.endPosition))
|
||||||
selection.endPosition)) {
|
{
|
||||||
selectedWord[wordIdx] = squarePanel.squares[i].character[0];
|
selectedWord[wordIdx] = squarePanel.squares[i].character[0];
|
||||||
selectedIdx[wordIdx] = i;
|
selectedIdx[wordIdx] = i;
|
||||||
wordIdx++;
|
wordIdx++;
|
||||||
@ -289,76 +294,71 @@ static void updateSelectedSquares(const MouseSelection selection,
|
|||||||
selectedWord[wordIdx] = '\0';
|
selectedWord[wordIdx] = '\0';
|
||||||
|
|
||||||
// If selected word is a solution, mark it
|
// If selected word is a solution, mark it
|
||||||
if (isSolution(selectedWord, searchWordPanel)) {
|
if(isSolution(selectedWord, searchWordPanel))
|
||||||
|
{
|
||||||
for(int i = 0; i < wordIdx; i++)
|
for(int i = 0; i < wordIdx; i++)
|
||||||
squarePanel.squares[selectedIdx[i]].isMarked = 1;
|
squarePanel.squares[selectedIdx[i]].isMarked = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handles mouse input for selecting words in grid
|
// Handles mouse input for selecting words in grid
|
||||||
static void handleMouseInput(MouseSelection *selection,
|
static void handleMouseInput(MouseSelection *selection, CharSquarePanel squarePanel, SearchWordPanel searchWordPanel)
|
||||||
CharSquarePanel squarePanel,
|
{
|
||||||
SearchWordPanel searchWordPanel) {
|
|
||||||
if(IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) // Start new selection
|
if(IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) // Start new selection
|
||||||
{
|
{
|
||||||
selection->startPosition = GetMousePosition();
|
selection->startPosition = GetMousePosition();
|
||||||
selection->endPosition = selection->startPosition;
|
selection->endPosition = selection->startPosition;
|
||||||
selection->isSelected = 1;
|
selection->isSelected = 1;
|
||||||
} else if (IsMouseButtonReleased(MOUSE_BUTTON_LEFT)) // End selection
|
}
|
||||||
|
else if(IsMouseButtonReleased(MOUSE_BUTTON_LEFT)) // End selection
|
||||||
{
|
{
|
||||||
updateSelectedSquares(*selection, squarePanel, searchWordPanel);
|
updateSelectedSquares(*selection, squarePanel, searchWordPanel);
|
||||||
|
|
||||||
selection->isSelected = 0;
|
selection->isSelected = 0;
|
||||||
} else if (IsMouseButtonDown(
|
}
|
||||||
MOUSE_BUTTON_LEFT)) // Update end position while selecting
|
else if(IsMouseButtonDown(MOUSE_BUTTON_LEFT)) // Update end position while selecting
|
||||||
{
|
{
|
||||||
selection->endPosition = GetMousePosition();
|
selection->endPosition = GetMousePosition();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Draws selection line on screen if selection is active
|
// Draws selection line on screen if selection is active
|
||||||
static void drawSelection(const MouseSelection selection) {
|
static void drawSelection(const MouseSelection selection)
|
||||||
if (selection.isSelected) {
|
{
|
||||||
DrawLine(selection.startPosition.x, selection.startPosition.y,
|
if(selection.isSelected)
|
||||||
selection.endPosition.x, selection.endPosition.y, WHITE);
|
{
|
||||||
|
DrawLine(selection.startPosition.x, selection.startPosition.y, selection.endPosition.x, selection.endPosition.y, WHITE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Draws search word panel (list of words to be found)
|
// Draws search word panel (list of words to be found)
|
||||||
static void drawSearchWordPanel(SearchWordPanel searchWordPanel) {
|
static void drawSearchWordPanel(SearchWordPanel searchWordPanel)
|
||||||
for (int i = 0; i < searchWordPanel.count; i++) {
|
{
|
||||||
Vector2 wordScreenCoord = {
|
for(int i = 0; i < searchWordPanel.count; i++)
|
||||||
searchWordPanel.words[i].position.x + searchWordPanel.position.x,
|
{
|
||||||
searchWordPanel.words[i].position.y + searchWordPanel.position.y};
|
Vector2 wordScreenCoord = {searchWordPanel.words[i].position.x + searchWordPanel.position.x, searchWordPanel.words[i].position.y + searchWordPanel.position.y};
|
||||||
DrawText(searchWordPanel.words[i].content, wordScreenCoord.x,
|
DrawText(searchWordPanel.words[i].content, wordScreenCoord.x, wordScreenCoord.y, searchWordPanel.fontSize, WHITE);
|
||||||
wordScreenCoord.y, searchWordPanel.fontSize, WHITE);
|
|
||||||
|
|
||||||
// If word has been found, highlight it
|
// If word has been found, highlight it
|
||||||
if (searchWordPanel.words[i].wasFound) {
|
if(searchWordPanel.words[i].wasFound)
|
||||||
int xOffset = MeasureText(searchWordPanel.words[i].content,
|
{
|
||||||
searchWordPanel.fontSize);
|
int xOffset = MeasureText(searchWordPanel.words[i].content, searchWordPanel.fontSize);
|
||||||
for(int i = 0; i <= 3; i++)
|
for(int i = 0; i <= 3; i++)
|
||||||
DrawLine(wordScreenCoord.x - 3, wordScreenCoord.y + 5 + i,
|
DrawLine(wordScreenCoord.x - 3, wordScreenCoord.y + 5 + i, wordScreenCoord.x + xOffset + 3, wordScreenCoord.y + 5 + i, GREEN);
|
||||||
wordScreenCoord.x + xOffset + 3, wordScreenCoord.y + 5 + i,
|
|
||||||
GREEN);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Draws helper message (instructions or tips for user)
|
// Draws helper message (instructions or tips for user)
|
||||||
static void drawHelperMessage(const HelperMessage msg) {
|
static void drawHelperMessage(const HelperMessage msg)
|
||||||
DrawRectangle(msg.position.x, msg.position.y, msg.size.x, msg.size.y,
|
{
|
||||||
BLACK); // Background for message
|
DrawRectangle(msg.position.x, msg.position.y, msg.size.x, msg.size.y, BLACK); // Background for message
|
||||||
DrawText(msg.text, msg.position.x + 5, msg.position.y + 5, msg.fontSize,
|
DrawText(msg.text, msg.position.x + 5, msg.position.y + 5, msg.fontSize, WHITE); // Display message text
|
||||||
WHITE); // Display message text
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Draws the entire game content, including helper message, squares, and search
|
// Draws the entire game content, including helper message, squares, and search words
|
||||||
// words
|
static void drawGameContent(const HelperMessage helperMsg, const CharSquarePanel squarePanel, const MouseSelection selection, const SearchWordPanel searchWordPanel)
|
||||||
static void drawGameContent(const HelperMessage helperMsg,
|
{
|
||||||
const CharSquarePanel squarePanel,
|
|
||||||
const MouseSelection selection,
|
|
||||||
const SearchWordPanel searchWordPanel) {
|
|
||||||
drawHelperMessage(helperMsg);
|
drawHelperMessage(helperMsg);
|
||||||
drawSquares(squarePanel);
|
drawSquares(squarePanel);
|
||||||
drawSearchWordPanel(searchWordPanel);
|
drawSearchWordPanel(searchWordPanel);
|
||||||
@ -366,30 +366,23 @@ static void drawGameContent(const HelperMessage helperMsg,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Draws success message when player wins
|
// Draws success message when player wins
|
||||||
static void drawSuccessContent(WinMessage msg) {
|
static void drawSuccessContent(WinMessage msg)
|
||||||
|
{
|
||||||
unsigned int textWidth = MeasureText(msg.content, msg.size);
|
unsigned int textWidth = MeasureText(msg.content, msg.size);
|
||||||
DrawRectangle(msg.position.x - 20, msg.position.y - 20, textWidth + 40,
|
DrawRectangle(msg.position.x-20, msg.position.y-20, textWidth+40, msg.size+40, GREEN); // Background for success message
|
||||||
msg.size + 40, GREEN); // Background for success message
|
|
||||||
|
|
||||||
for(int i = 0; i < 5; i++) // Draw borders around success message
|
for(int i = 0; i < 5; i++) // Draw borders around success message
|
||||||
DrawRectangleLines(msg.position.x - 20 + i, msg.position.y - 20 + i,
|
DrawRectangleLines(msg.position.x-20+i, msg.position.y-20+i, textWidth+40-i*2, msg.size+40-i*2, WHITE);
|
||||||
textWidth + 40 - i * 2, msg.size + 40 - i * 2, WHITE);
|
|
||||||
|
|
||||||
DrawText(msg.content, msg.position.x, msg.position.y, msg.size,
|
DrawText(msg.content, msg.position.x, msg.position.y, msg.size, WHITE); // Display success text
|
||||||
WHITE); // Display success text
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Draws entire game screen, including game content and success message if
|
// Draws entire game screen, including game content and success message if applicable
|
||||||
// applicable
|
static void drawAll(const CharSquarePanel squarePanel, const MouseSelection selection, const SearchWordPanel searchWordPanel, const HelperMessage helperMsg, const WinMessage msg, int hasWon)
|
||||||
static void drawAll(const CharSquarePanel squarePanel,
|
{
|
||||||
const MouseSelection selection,
|
|
||||||
const SearchWordPanel searchWordPanel,
|
|
||||||
const HelperMessage helperMsg, const WinMessage msg,
|
|
||||||
int hasWon) {
|
|
||||||
BeginDrawing();
|
BeginDrawing();
|
||||||
ClearBackground(BLACK); // Clear screen with a black background
|
ClearBackground(BLACK); // Clear screen with a black background
|
||||||
drawGameContent(helperMsg, squarePanel, selection,
|
drawGameContent(helperMsg, squarePanel, selection, searchWordPanel); // Draw game content
|
||||||
searchWordPanel); // Draw game content
|
|
||||||
|
|
||||||
if(hasWon) // If player has won, draw success message
|
if(hasWon) // If player has won, draw success message
|
||||||
drawSuccessContent(msg);
|
drawSuccessContent(msg);
|
||||||
@ -398,7 +391,8 @@ static void drawAll(const CharSquarePanel squarePanel,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Checks if all words in the search word panel have been found
|
// Checks if all words in the search word panel have been found
|
||||||
static int allWordsFound(SearchWordPanel searchWordPanel) {
|
static int allWordsFound(SearchWordPanel searchWordPanel)
|
||||||
|
{
|
||||||
// Loop through all words and check if any is not found
|
// Loop through all words and check if any is not found
|
||||||
for(int i = 0; i < searchWordPanel.count; i++)
|
for(int i = 0; i < searchWordPanel.count; i++)
|
||||||
if(!searchWordPanel.words[i].wasFound)
|
if(!searchWordPanel.words[i].wasFound)
|
||||||
@ -407,45 +401,34 @@ static int allWordsFound(SearchWordPanel searchWordPanel) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Main game loop where game is run and updated
|
// Main game loop where game is run and updated
|
||||||
static void gameLoop(const Vector2 screenSize, MouseSelection mouseSelection,
|
static void gameLoop(const Vector2 screenSize, MouseSelection mouseSelection, CharSquarePanel squarePanel, SearchWordPanel searchWordPanel, const HelperMessage helperMsg, const WinMessage winMsg)
|
||||||
CharSquarePanel squarePanel,
|
{
|
||||||
SearchWordPanel searchWordPanel,
|
InitWindow(screenSize.x, screenSize.y, "Word Salad"); // Initialize game window
|
||||||
const HelperMessage helperMsg, const WinMessage winMsg) {
|
|
||||||
InitWindow(screenSize.x, screenSize.y,
|
|
||||||
"Word Salad"); // Initialize game window
|
|
||||||
|
|
||||||
SetTargetFPS(60);
|
SetTargetFPS(60);
|
||||||
|
|
||||||
while (!WindowShouldClose()) // Keep running until window is closed
|
while (!WindowShouldClose()) // Keep running until window is closed
|
||||||
{
|
{
|
||||||
handleMouseInput(&mouseSelection, squarePanel,
|
handleMouseInput(&mouseSelection, squarePanel, searchWordPanel); // Handle mouse input (selection)
|
||||||
searchWordPanel); // Handle mouse input (selection)
|
|
||||||
|
|
||||||
// Draw all game content including helper message, squares, and search word
|
// Draw all game content including helper message, squares, and search word panel
|
||||||
// panel
|
drawAll(squarePanel, mouseSelection, searchWordPanel, helperMsg, winMsg, allWordsFound(searchWordPanel));
|
||||||
drawAll(squarePanel, mouseSelection, searchWordPanel, helperMsg, winMsg,
|
|
||||||
allWordsFound(searchWordPanel));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
CloseWindow();
|
CloseWindow();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initializes and starts game, setting up necessary elements and entering game
|
// Initializes and starts game, setting up necessary elements and entering game loop
|
||||||
// loop
|
void startGame(const char wordSalad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldSize, char words[][MAX_WORD_LEN], unsigned int numberOfWords, unsigned int windowSize)
|
||||||
void startGame(const char wordSalad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN],
|
{
|
||||||
unsigned int searchFieldSize, char words[][MAX_WORD_LEN],
|
|
||||||
unsigned int numberOfWords, unsigned int windowSize) {
|
|
||||||
const int windowWidth = windowSize;
|
const int windowWidth = windowSize;
|
||||||
|
|
||||||
Vector2 screenSize;
|
Vector2 screenSize;
|
||||||
|
|
||||||
// Create necessary game elements like helper message, square panel, and
|
// Create necessary game elements like helper message, square panel, and search word panel
|
||||||
// search word panel
|
|
||||||
HelperMessage helperMsg = createHelperMessage(windowWidth);
|
HelperMessage helperMsg = createHelperMessage(windowWidth);
|
||||||
CharSquarePanel squarePanel =
|
CharSquarePanel squarePanel = createCharSquarePanel(wordSalad, searchFieldSize, windowWidth);
|
||||||
createCharSquarePanel(wordSalad, searchFieldSize, windowWidth);
|
SearchWordPanel searchWordPanel = createSearchWordPanel(words, numberOfWords, windowWidth);
|
||||||
SearchWordPanel searchWordPanel =
|
|
||||||
createSearchWordPanel(words, numberOfWords, windowWidth);
|
|
||||||
WinMessage winMsg = createWinMessage(windowWidth);
|
WinMessage winMsg = createWinMessage(windowWidth);
|
||||||
|
|
||||||
MouseSelection mouseSelection;
|
MouseSelection mouseSelection;
|
||||||
@ -461,8 +444,7 @@ void startGame(const char wordSalad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN],
|
|||||||
screenSize.y = helperMsg.size.y + squarePanel.size.y + searchWordPanel.size.y;
|
screenSize.y = helperMsg.size.y + squarePanel.size.y + searchWordPanel.size.y;
|
||||||
|
|
||||||
// Start game loop
|
// Start game loop
|
||||||
gameLoop(screenSize, mouseSelection, squarePanel, searchWordPanel, helperMsg,
|
gameLoop(screenSize, mouseSelection, squarePanel, searchWordPanel, helperMsg, winMsg);
|
||||||
winMsg);
|
|
||||||
|
|
||||||
// Free up allocated memory when game is done
|
// Free up allocated memory when game is done
|
||||||
freeCharSquarePanel(&squarePanel);
|
freeCharSquarePanel(&squarePanel);
|
||||||
@ -470,4 +452,4 @@ void startGame(const char wordSalad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN],
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*gcc -fPIC -c input.c game.c graphicalGame.c main.c
|
/*gcc -fPIC -c input.c game.c graphicalGame.c main.c
|
||||||
gcc -shared -o libwordsalad.a input.o game.o graphicalGame.o main.o*/
|
gcc -shared -o libwortsalat.a input.o game.o graphicalGame.o main.o*/
|
||||||
Binary file not shown.
@ -1,44 +1,12 @@
|
|||||||
#include "input.h"
|
#include "input.h"
|
||||||
#include <ctype.h>
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <ctype.h>
|
||||||
|
|
||||||
|
// TODO:
|
||||||
|
// eine Funktion implementieren, die ein einzelnes Wort aus einer Textdatei (words.txt) einliest und als C-String zurückgibt.
|
||||||
|
|
||||||
// Read words from file and store in 'words' array
|
// Read words from file and store in 'words' array
|
||||||
int readWords(FILE *file, char words[][MAX_WORD_LEN],
|
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
||||||
unsigned int maxWordCount) {
|
{
|
||||||
|
|
||||||
if (file == NULL) {
|
|
||||||
printf("File couldn't be opened...");
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
char line[MAX_LINE_LEN]; // fehlerhaften String zwischenspeichern
|
|
||||||
unsigned int wordCount = 0; // eingelesene Wörter
|
|
||||||
|
|
||||||
while ((fgets(line, sizeof(line), file) != NULL) &&
|
|
||||||
(wordCount < maxWordCount)) { // gesamte Wörterliste einlesen
|
|
||||||
|
|
||||||
// Token initialisieren
|
|
||||||
char *token = strtok(
|
|
||||||
line, " ,;.\n"); // Trennen der Wörter bei den angegebenen Zeichen
|
|
||||||
|
|
||||||
while (token && (wordCount < maxWordCount)) {
|
|
||||||
|
|
||||||
if (*token == '\0') {
|
|
||||||
token = strtok(NULL, " ,;.\n");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int i = 0; token[i] != '\0'; i++) {
|
|
||||||
token[i] = toupper(token[i]); // Alles in Großbuchstaben
|
|
||||||
}
|
|
||||||
|
|
||||||
strcpy(words[wordCount], token); // einzelnes Wort in words array
|
|
||||||
// speichern
|
|
||||||
|
|
||||||
wordCount++; // Nächstes Wort
|
|
||||||
token = strtok(NULL, " ,;.\n"); // Nächster Token
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return wordCount; // Anzahl der eingelesenen Wörter
|
|
||||||
}
|
}
|
||||||
Binary file not shown.
@ -1,32 +1,33 @@
|
|||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include "input.h"
|
||||||
#include "game.h"
|
#include "game.h"
|
||||||
#include "graphicalGame.h"
|
#include "graphicalGame.h"
|
||||||
#include "input.h"
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <time.h>
|
|
||||||
|
|
||||||
#define MAX_NUMBER_OF_WORDS 100
|
#define MAX_NUMBER_OF_WORDS 100
|
||||||
#define SALAD_SIZE 20
|
#define SALAD_SIZE 20
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[])
|
||||||
srand(time(NULL));
|
{
|
||||||
int exitCode = EXIT_SUCCESS;
|
int exitCode = EXIT_SUCCESS;
|
||||||
|
|
||||||
// Check if the correct number of arguments is provided
|
// Check if the correct number of arguments is provided
|
||||||
if (argc != 2) {
|
if(argc != 2)
|
||||||
|
{
|
||||||
fprintf(stderr, "Usage: %s <path to file with search words>\n", argv[0]);
|
fprintf(stderr, "Usage: %s <path to file with search words>\n", argv[0]);
|
||||||
exitCode = EXIT_FAILURE;
|
exitCode = EXIT_FAILURE;
|
||||||
} else {
|
}
|
||||||
char words[MAX_NUMBER_OF_WORDS]
|
else
|
||||||
[MAX_WORD_LEN]; // Array to hold the words to be used in the game
|
{
|
||||||
|
char words[MAX_NUMBER_OF_WORDS][MAX_WORD_LEN]; // Array to hold the words to be used in the game
|
||||||
unsigned int wordCount = 0;
|
unsigned int wordCount = 0;
|
||||||
|
|
||||||
FILE *file = fopen(argv[1], "r");
|
FILE *file = fopen(argv[1], "r");
|
||||||
|
|
||||||
if (file != NULL) {
|
if(file != NULL)
|
||||||
|
{
|
||||||
unsigned int placedWords = 0;
|
unsigned int placedWords = 0;
|
||||||
char wordSalad[MAX_SEARCH_FIELD_LEN]
|
char wordSalad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN]; // 2D array to store the word salad
|
||||||
[MAX_SEARCH_FIELD_LEN]; // 2D array to store the word salad
|
|
||||||
|
|
||||||
// Read words from file and store in 'words' array
|
// Read words from file and store in 'words' array
|
||||||
wordCount = readWords(file, words, MAX_NUMBER_OF_WORDS);
|
wordCount = readWords(file, words, MAX_NUMBER_OF_WORDS);
|
||||||
@ -39,23 +40,10 @@ int main(int argc, char *argv[]) {
|
|||||||
// Check if all words were successfully placed
|
// Check if all words were successfully placed
|
||||||
// Start the game if successful
|
// Start the game if successful
|
||||||
// error message if some words couldn't be placed
|
// error message if some words couldn't be placed
|
||||||
printf("Placed Words: %d\n", placedWords);
|
|
||||||
printf("Word Count:%d\n", wordCount);
|
|
||||||
|
|
||||||
if (placedWords == wordCount) {
|
|
||||||
|
|
||||||
startGame(wordSalad, SALAD_SIZE, words, placedWords, 1024);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
else
|
else
|
||||||
|
|
||||||
{
|
{
|
||||||
// Print error message if words couldn't be placed
|
|
||||||
printf("Placed %d words of %d.", placedWords, wordCount);
|
|
||||||
exitCode = EXIT_FAILURE;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// Print error message if file couldn't be opened
|
// Print error message if file couldn't be opened
|
||||||
fprintf(stderr, "Could not open file %s for reading ...\n", argv[1]);
|
fprintf(stderr, "Could not open file %s for reading ...\n", argv[1]);
|
||||||
exitCode = EXIT_FAILURE;
|
exitCode = EXIT_FAILURE;
|
||||||
|
|||||||
Binary file not shown.
@ -3,7 +3,7 @@ CFLAGS = -g -Wall
|
|||||||
LDFLAGS = -lopengl32 -lgdi32 -lwinmm
|
LDFLAGS = -lopengl32 -lgdi32 -lwinmm
|
||||||
BINARIES = ./windows
|
BINARIES = ./windows
|
||||||
|
|
||||||
raylib_folder = ./raylib
|
raylibfolder = ./raylib
|
||||||
unityfolder = ./unity
|
unityfolder = ./unity
|
||||||
|
|
||||||
# --------------------------
|
# --------------------------
|
||||||
@ -12,21 +12,12 @@ unityfolder = ./unity
|
|||||||
wordsalad_initial:
|
wordsalad_initial:
|
||||||
$(CC) -o wordsalad_initial $(BINARIES)/libwordsalad_complete.a $(BINARIES)/libraylib.a $(LDFLAGS)
|
$(CC) -o wordsalad_initial $(BINARIES)/libwordsalad_complete.a $(BINARIES)/libraylib.a $(LDFLAGS)
|
||||||
|
|
||||||
#---------------------------
|
|
||||||
# eigenes Target bauen
|
|
||||||
#---------------------------
|
|
||||||
wordsalad_myversion: main.o graphicalGame.o input.o game.o
|
|
||||||
$(CC) main.o graphicalGame.o input.o game.o -o wordsalad_myversion $(BINARIES)/libraylib.a $(LDFLAGS)
|
|
||||||
|
|
||||||
# --------------------------
|
# --------------------------
|
||||||
# Normales Spiel bauen
|
# Normales Spiel bauen
|
||||||
# --------------------------
|
# --------------------------
|
||||||
all: $(BINARIES)/libwordsalad.a main.o input.o game.o graphicalGame.o $(BINARIES)/libraylib.a
|
all: main.o input.o game.o graphicalGame.o $(BINARIES)/libraylib.a
|
||||||
$(CC) $(CFLAGS) -o wordsalad main.o input.o game.o graphicalGame.o $(BINARIES)/libraylib.a $(LDFLAGS)
|
$(CC) $(CFLAGS) -o wordsalad main.o input.o game.o graphicalGame.o $(BINARIES)/libraylib.a $(LDFLAGS)
|
||||||
|
|
||||||
$(BINARIES)/libwordsalad.a: main.o input.o game.o graphicalGame.o
|
|
||||||
ar rcs $(BINARIES)/libwordsalad.a main.o input.o game.o graphicalGame.o
|
|
||||||
|
|
||||||
main.o: main.c
|
main.o: main.c
|
||||||
$(CC) -c $(CFLAGS) main.c
|
$(CC) -c $(CFLAGS) main.c
|
||||||
|
|
||||||
@ -37,7 +28,7 @@ game.o: game.c
|
|||||||
$(CC) -c $(CFLAGS) game.c
|
$(CC) -c $(CFLAGS) game.c
|
||||||
|
|
||||||
graphicalGame.o: graphicalGame.c
|
graphicalGame.o: graphicalGame.c
|
||||||
$(CC) -I$(raylib_folder) $(CFLAGS) -c graphicalGame.c
|
$(CC) -I$(raylibfolder) -c $(CFLAGS) graphicalGame.c
|
||||||
|
|
||||||
# --------------------------
|
# --------------------------
|
||||||
# Unit Tests
|
# Unit Tests
|
||||||
@ -51,4 +42,4 @@ test: input.o game.o unit_tests.c
|
|||||||
# Clean
|
# Clean
|
||||||
# --------------------------
|
# --------------------------
|
||||||
clean:
|
clean:
|
||||||
rm -f *.o *.exe $(BINARIES)/libwordsalad.a
|
del /f *.o *.exe
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
@ -101,6 +101,35 @@ void test_createWordSalad_too_small(void) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void test_createWordSalad_allWordsPlaced() {
|
||||||
|
char words[3][MAX_WORD_LEN] = {"CAT", "DOG", "MOUSE"};
|
||||||
|
char saladHoriz[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN];
|
||||||
|
char saladVert[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN];
|
||||||
|
|
||||||
|
int placed = createWordSalad(saladHoriz, 20, words, 3);
|
||||||
|
for(int i = 0; i < MAX_SEARCH_FIELD_LEN; i++)
|
||||||
|
{
|
||||||
|
for(int j = 0; j < MAX_SEARCH_FIELD_LEN; j++)
|
||||||
|
{
|
||||||
|
saladVert[j][i] = saladHoriz[i][j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for(int i = 0; i < 3; i++) {
|
||||||
|
const char* word = words[i];
|
||||||
|
int wordFound = 0;
|
||||||
|
for(int j = 0; j < MAX_SEARCH_FIELD_LEN; j++)
|
||||||
|
{
|
||||||
|
const char* row = saladHoriz[j];
|
||||||
|
const char* col = saladVert[j];
|
||||||
|
wordFound |= strstr(row, word) || strstr(col, word);
|
||||||
|
}
|
||||||
|
TEST_ASSERT_TRUE_MESSAGE(wordFound, "Not all words were placed.");
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_ASSERT_EQUAL_INT(3, placed);
|
||||||
|
}
|
||||||
|
|
||||||
// ---------- Test Setup und TearDown Funktionen ----------
|
// ---------- Test Setup und TearDown Funktionen ----------
|
||||||
|
|
||||||
// Hier Setup- und TearDown-Funktionen definieren,
|
// Hier Setup- und TearDown-Funktionen definieren,
|
||||||
@ -135,6 +164,7 @@ int main(void) {
|
|||||||
RUN_TEST(test_readWords_empty_file);
|
RUN_TEST(test_readWords_empty_file);
|
||||||
RUN_TEST(test_createWordSalad_all_fit);
|
RUN_TEST(test_createWordSalad_all_fit);
|
||||||
RUN_TEST(test_createWordSalad_too_small);
|
RUN_TEST(test_createWordSalad_too_small);
|
||||||
|
RUN_TEST(test_createWordSalad_allWordsPlaced);
|
||||||
|
|
||||||
int result = UNITY_END(); // Test-Ergebnisse
|
int result = UNITY_END(); // Test-Ergebnisse
|
||||||
print_test_result(result);
|
print_test_result(result);
|
||||||
|
|||||||
Binary file not shown.
@ -1,6 +1,5 @@
|
|||||||
Yeti,Nessie Werwolf; Vampir
|
Yeti,Nessie Werwolf; Vampir
|
||||||
Monster
|
Monster
|
||||||
Hydra;Frankenstein
|
Hydra;Frankenstein
|
||||||
Dracula;KingKong,xxxxxxxx
|
Dracula;KingKong;Gremlin;Kobold,Hexe;Poltergeist
|
||||||
Max, Tobi, Kristin
|
Gespenst, Oger
|
||||||
|
|
||||||
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user