implemented input.c (fails unit test, wegen Groß/Kleinschreibung) und game.c tests sind jetzt ok
This commit is contained in:
1 parent
ce4be9f902
commit
029eb426ab
2 files changed
+32
-15
No files matched your search
+7
-15
@@ -31,17 +31,11 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi
|
|||||||
//Iterate over words
|
//Iterate over words
|
||||||
unsigned int counter = 0;
|
unsigned int counter = 0;
|
||||||
while(counter < wordCount){
|
while(counter < wordCount){
|
||||||
//load current word
|
|
||||||
char currentWord[MAX_WORD_LEN];
|
|
||||||
if(strncpy(currentWord, words[counter], MAX_WORD_LEN) == NULL){
|
|
||||||
//strncpy failed
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t len = strlen(currentWord);
|
size_t len = strlen(words[counter]);
|
||||||
//try to place a word
|
//try to place a word
|
||||||
int try = 0;
|
|
||||||
while(try < 10){
|
for(int try = 0; try < 10; try++){
|
||||||
int x = rand() % MAX_SEARCH_FIELD_LEN;
|
int x = rand() % MAX_SEARCH_FIELD_LEN;
|
||||||
int y = rand() % MAX_SEARCH_FIELD_LEN;
|
int y = rand() % MAX_SEARCH_FIELD_LEN;
|
||||||
|
|
||||||
@@ -50,7 +44,6 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi
|
|||||||
if ((MAX_SEARCH_FIELD_LEN - x) < len){
|
if ((MAX_SEARCH_FIELD_LEN - x) < len){
|
||||||
if((MAX_SEARCH_FIELD_LEN - y) < len){
|
if((MAX_SEARCH_FIELD_LEN - y) < len){
|
||||||
// cant place word here (too far in the bottom right corner)
|
// cant place word here (too far in the bottom right corner)
|
||||||
try++;
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -62,7 +55,6 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi
|
|||||||
for (int i = 0; i < (int)len; i++){
|
for (int i = 0; i < (int)len; i++){
|
||||||
if(salad[x][y+i] != '0'){
|
if(salad[x][y+i] != '0'){
|
||||||
cont = true;
|
cont = true;
|
||||||
try++;
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -70,12 +62,12 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi
|
|||||||
if(cont) continue;
|
if(cont) continue;
|
||||||
//insert currentWord into playing field
|
//insert currentWord into playing field
|
||||||
for (int i = 0; i < (int)len; i++){
|
for (int i = 0; i < (int)len; i++){
|
||||||
salad[x][y+i] = currentWord[i];
|
salad[x][y+i] = words[counter][i];
|
||||||
}
|
}
|
||||||
placedWords++;
|
placedWords++;
|
||||||
|
counter++;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
//place currentWord horizontally
|
//place currentWord horizontally
|
||||||
|
|
||||||
//see if all neccessary spaces are emtry
|
//see if all neccessary spaces are emtry
|
||||||
@@ -83,7 +75,6 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi
|
|||||||
for (int i = 0; i < (int)len; i++){
|
for (int i = 0; i < (int)len; i++){
|
||||||
if(salad[x+1][y] != '0'){
|
if(salad[x+1][y] != '0'){
|
||||||
cont = true;
|
cont = true;
|
||||||
try++;
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -93,9 +84,10 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi
|
|||||||
|
|
||||||
//place currentWord
|
//place currentWord
|
||||||
for (int i = 0; i < (int)len; i++){
|
for (int i = 0; i < (int)len; i++){
|
||||||
salad[x+1][y] = currentWord[i];
|
salad[x+1][y] = words[counter][i];
|
||||||
}
|
}
|
||||||
placedWords++;
|
placedWords++;
|
||||||
|
counter++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (int i = 0; i < MAX_SEARCH_FIELD_LEN; i++){
|
for (int i = 0; i < MAX_SEARCH_FIELD_LEN; i++){
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
#include "input.h"
|
#include "input.h"
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
// TODO:
|
// TODO:
|
||||||
// eine Funktion implementieren, die ein einzelnes Wort aus einer Textdatei (words.txt) einliest und als C-String zurückgibt.
|
// eine Funktion implementieren, die ein einzelnes Wort aus einer Textdatei (words.txt) einliest und als C-String zurückgibt.
|
||||||
@@ -8,5 +9,29 @@
|
|||||||
// 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], unsigned int maxWordCount)
|
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
||||||
{
|
{
|
||||||
|
int count = 0;
|
||||||
|
char *line = calloc(MAX_LINE_LEN, sizeof(char));
|
||||||
|
|
||||||
|
char delimit[] = ",.;:!? \n";
|
||||||
|
|
||||||
|
while(fgets(line, MAX_LINE_LEN, file) != NULL && (count < maxWordCount)){
|
||||||
|
|
||||||
|
char *token = strtok(line, delimit);
|
||||||
|
|
||||||
|
while(token != NULL){
|
||||||
|
size_t len = strlen(token);
|
||||||
|
token[len] = '\0';
|
||||||
|
char copy[MAX_LINE_LEN];
|
||||||
|
strncpy(copy, token, len);
|
||||||
|
copy[len] = '\0';
|
||||||
|
|
||||||
|
strncpy(words[count], copy, len);
|
||||||
|
words[count][len] = '\0';
|
||||||
|
count++;
|
||||||
|
|
||||||
|
token = strtok(NULL, delimit);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return count;
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user