Files
info2Praktikum-WortsalatMI/Wordsalad/game.c
T
2026-10-26 08:07:43 +01:00

90 lines
2.4 KiB
C

#include "game.h"
#include "input.h"
#include <time.h>
#include <stdlib.h>
#include <string.h>
#define MAX_RAND_TRIES_PER_WORD 10
#define EMPTY_CHAR 0
//TODO: Spiellogik implementieren:
/* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren
* restliche Felder mit zufälligen Buchstaben füllen */
// Creates the word salad by placing words randomly and filling empty spaces
int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount) {
/*srand(time(NULL));
char bustaben[] = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z";
char zufall = bustaben[rand() % 26];
printf("Zufaelliger Bustabe: %c\n", zufall);
const char words[15][MAX_WORD_LEN] = "Yeti,Nessie,Werwolf,Vampir,Monster,Hydra,Frankenstein,Dracula,KingKong,Gremlin,Kobold,Hexe,Poltergeist,Gespenst,Oge";
return 0; */
srand(time(NULL));
for (int r=0; r < ROWS; r++) {
for (int c =0; c < COLS; c++) {
grid[r][c] = '.';
}
}
//Wörter plazieren
for (unsigned int w = 0; w< wordCount; w++) {
int wordLen = strlen(words[w]);
int placed = 0;
while (!placed) {
int direction = rand() % 2;
int row;
int col;
int fits = 1;
if (direction == 0) {
row = rand() % ROWS;
col = rand() % (COLS - wordLen +1);
for (int i = 0; i< wordLen; i++) {
if (grid[row][col +i] != '.') {
fits = 0;
}
}
if (fits) {
for (int i=0; i < wordLen; i++) {
grid[row][col +i] = words[w][i];
}
placed = 1;
}
}
else {
row = rand() % (ROWS - wordLen +1);
col = rand() % COLS;
for (int i = 0; i< wordLen; i++) {
if (grid[row+i][col] != '.') {
fits = 0;
}
}
if (fits) {
for (int i = 0; i<worldLen; i++) {
grid[row+i][col] = words[w][i];
}
placed =1;
}
}
}
//Rest mit Zufallszahlen füllen
for (int r=0; r < ROWS; r++) {
for (int c=0; c < COLS; c++) {
if (grid[r][c] == '.') {
grid[r][c]='A' + rand() % 26;
}
}
}
}
return 0;
}