forked from schroederen/info2Praktikum-Wortsalat
kopieren von mac in windows
This commit is contained in:
parent
560fee08c8
commit
323eeb7735
Binary file not shown.
@ -2,6 +2,7 @@
|
|||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
#define MAX_RAND_TRIES_PER_WORD 10
|
#define MAX_RAND_TRIES_PER_WORD 10
|
||||||
#define EMPTY_CHAR 0
|
#define EMPTY_CHAR 0
|
||||||
@ -11,82 +12,96 @@
|
|||||||
* restliche Felder mit zufälligen Buchstaben füllen */
|
* restliche Felder mit zufälligen Buchstaben füllen */
|
||||||
|
|
||||||
// Creates the word salad by placing words randomly and filling empty spaces
|
// 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)
|
int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN],
|
||||||
|
unsigned int searchFieldLen,
|
||||||
|
const char words[][MAX_WORD_LEN],
|
||||||
|
unsigned int wordCount)
|
||||||
{
|
{
|
||||||
//Spielfeld vorbereiten
|
// Spielfeld vorbereiten
|
||||||
for(unsigned int i = 0; i < searchFieldLen; i++ ){
|
for(unsigned int i = 0; i < searchFieldLen; i++) {
|
||||||
for(unsigned int j = 0; j < searchFieldLen; j++ ){
|
for(unsigned int j = 0; j < searchFieldLen; j++) {
|
||||||
salad[i][j] = EMPTY_CHAR;
|
salad[i][j] = EMPTY_CHAR;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (unsigned int w = 0; w < wordCount; w++){
|
|
||||||
size_t len = strlen(words[w]);
|
|
||||||
int placed = 0;
|
|
||||||
for(int attempt = 0; attempt < MAX_RAND_TRIES_PER_WORD && !placed; attempt++){
|
|
||||||
int horizontal = rand() % 2;
|
|
||||||
int row = rand() % searchFieldLen;
|
|
||||||
int col = rand() % searchFieldLen;
|
|
||||||
if(horizontal){
|
|
||||||
if(col + len > searchFieldLen) continue;
|
|
||||||
|
|
||||||
int conflict = 0;
|
int placedWordsCount = 0; // Zähler für platzierte Wörter
|
||||||
for(size_t i = 0; i < len; i++){
|
|
||||||
if(salad[row][col+i] != EMPTY_CHAR && salad[row][col+i] != words[w][i]){
|
// Jedes Wort versuchen zu platzieren
|
||||||
conflict = 1;
|
for (unsigned int w = 0; w < wordCount; w++) {
|
||||||
break;
|
size_t len = strlen(words[w]);
|
||||||
}
|
int placed = 0;
|
||||||
|
|
||||||
|
for(int attempt = 0; attempt < MAX_RAND_TRIES_PER_WORD && !placed; attempt++) {
|
||||||
|
int horizontal = rand() % 2;
|
||||||
|
int row = rand() % searchFieldLen;
|
||||||
|
int col = rand() % searchFieldLen;
|
||||||
|
|
||||||
|
if(horizontal) {
|
||||||
|
// Horizontal platzieren
|
||||||
|
if(col + len > searchFieldLen) continue;
|
||||||
|
|
||||||
|
int conflict = 0;
|
||||||
|
for(size_t i = 0; i < len; i++) {
|
||||||
|
if(salad[row][col+i] != EMPTY_CHAR && salad[row][col+i] != words[w][i]) {
|
||||||
|
conflict = 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(conflict) continue;
|
||||||
|
|
||||||
|
for(size_t i = 0; i < len; i++) {
|
||||||
|
salad[row][col+i] = words[w][i];
|
||||||
|
}
|
||||||
|
placed = 1;
|
||||||
}
|
}
|
||||||
if(conflict) continue;
|
else {
|
||||||
for(size_t i = 0; i < len; i++){
|
// Vertikal platzieren
|
||||||
salad[row][col+i] = words[w][i];
|
if(row + len > searchFieldLen) continue;
|
||||||
|
|
||||||
|
int conflict = 0;
|
||||||
|
for(size_t i = 0; i < len; i++) {
|
||||||
|
if(salad[row+i][col] != EMPTY_CHAR && salad[row+i][col] != words[w][i]) {
|
||||||
|
conflict = 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(conflict) continue;
|
||||||
|
|
||||||
|
for(size_t i = 0; i < len; i++) {
|
||||||
|
salad[row+i][col] = words[w][i];
|
||||||
|
}
|
||||||
|
placed = 1;
|
||||||
}
|
}
|
||||||
placed = 1;
|
}
|
||||||
}
|
|
||||||
else{
|
|
||||||
if(row + len > searchFieldLen) continue;
|
|
||||||
|
|
||||||
int conflict = 0;
|
if(placed) {
|
||||||
for(size_t i = 0; i < len; i++){
|
placedWordsCount++;
|
||||||
if(salad[row+1][col] != EMPTY_CHAR && salad[row+1][col] != words[w][i]){
|
}
|
||||||
conflict = 1;
|
}
|
||||||
break;
|
|
||||||
}
|
// Leere Felder mit zufälligen Buchstaben befüllen
|
||||||
|
for(unsigned int i = 0; i < searchFieldLen; i++) {
|
||||||
|
for(unsigned int j = 0; j < searchFieldLen; j++) {
|
||||||
|
if (salad[i][j] == EMPTY_CHAR) {
|
||||||
|
salad[i][j] = 'A' + rand() % 26;
|
||||||
}
|
}
|
||||||
if(conflict) continue;
|
}
|
||||||
for(size_t i = 0; i < len; i++){
|
}
|
||||||
salad[row+1][col] = words[w][i];
|
|
||||||
}
|
|
||||||
placed = 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
return placedWordsCount; // Rückgabewert wie viele Wörter eingefügt wurden
|
||||||
/* code */
|
|
||||||
|
|
||||||
|
|
||||||
//leere Felder mit zufälligen Buchstaben befüllen
|
|
||||||
for(unsigned int i = 0; i < searchFieldLen; i++ ){
|
|
||||||
for(unsigned int j = 0; j < searchFieldLen; j++ ){
|
|
||||||
if (salad[i][j] == EMPTY_CHAR) {
|
|
||||||
salad[i][j] = 'A' + rand() % 26;
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prints the word salad to console
|
// Prints the word salad to console
|
||||||
void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen)
|
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 i = 0; i < searchFieldLen; i++) {
|
||||||
for (int j=0; j < searchFieldLen; j++) {
|
for (int j = 0; j < searchFieldLen; j++) {
|
||||||
printf("$c",salad[i][j]);
|
printf("%c", salad[i][j]);
|
||||||
printf(" ");
|
printf(" ");
|
||||||
}
|
}
|
||||||
printf ("\n");
|
printf("\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -42,7 +42,7 @@ int main(int argc, char *argv[])
|
|||||||
if(placedWords == wordCount)
|
if(placedWords == wordCount)
|
||||||
{
|
{
|
||||||
// Start the game if successful
|
// Start the game if successful
|
||||||
startGame(wordSalad,SALAD_SIZE,words,wordCount,SALAD_SIZE);
|
startGame(wordSalad,SALAD_SIZE,words,wordCount,800);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user