Merge branch 'wortsalat_tommy'
This commit is contained in:
commit
6ea1ec75e0
16 files changed
+179
-43
No files matched your search
+88
-1
@@ -1,7 +1,7 @@
|
||||
#include "game.h"
|
||||
#include <time.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
|
||||
#define MAX_RAND_TRIES_PER_WORD 10
|
||||
#define EMPTY_CHAR 0
|
||||
@@ -13,5 +13,92 @@
|
||||
// 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)
|
||||
{
|
||||
unsigned int placedWords = 0;
|
||||
|
||||
emptyField(searchFieldLen, salad);
|
||||
|
||||
for(int i = 0; i < wordCount; i++)
|
||||
{
|
||||
placedWords += placement(salad, searchFieldLen, words[i]);
|
||||
}
|
||||
|
||||
fillRandAlpha(searchFieldLen, salad);
|
||||
|
||||
return placedWords;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
char alphaRand()
|
||||
{
|
||||
int letter = (rand() % 26) + 'A';
|
||||
|
||||
return (char) letter;
|
||||
}
|
||||
|
||||
int placement(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char *word)
|
||||
{
|
||||
int saladX = rand() % ( searchFieldLen - strlen(word) );
|
||||
int saladY = rand() % ( searchFieldLen - strlen(word) );
|
||||
|
||||
if(rand() % 2)
|
||||
{
|
||||
for(int i = 0; i < strlen(word); i++)
|
||||
{
|
||||
if(isalpha(salad[saladX][saladY + i]))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
||||
for(int i = 0; i < strlen(word); i++)
|
||||
{
|
||||
salad[saladX][saladY + i] = word[i];
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
for(int i = 0; i < strlen(word); i++)
|
||||
{
|
||||
if(isalpha(salad[saladX + i][saladY]))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
||||
for(int i = 0; i < strlen(word); i++)
|
||||
{
|
||||
salad[saladX + i][saladY] = word[i];
|
||||
}
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
void emptyField(unsigned int searchFieldLen, char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN])
|
||||
{
|
||||
for(size_t i = 0; i < searchFieldLen; i++)
|
||||
{
|
||||
for(size_t j = 0; j < searchFieldLen; j++)
|
||||
{
|
||||
salad[i][j] = '\0';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
char fillRandAlpha(unsigned int searchFieldLen, char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN])
|
||||
{
|
||||
for(size_t i = 0; i < searchFieldLen; i++)
|
||||
{
|
||||
for(size_t j = 0; j < searchFieldLen; j++)
|
||||
{
|
||||
if(!isalpha(salad[i][j]))
|
||||
{
|
||||
salad[i][j] = alphaRand();
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -7,4 +7,12 @@
|
||||
|
||||
int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount);
|
||||
|
||||
void emptyField(unsigned int searchFieldLen, char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN]);
|
||||
|
||||
char fillRandAlpha(unsigned int searchFieldLen, char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN]);
|
||||
|
||||
int placement(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char *word);
|
||||
|
||||
char alphaRand();
|
||||
|
||||
#endif
|
||||
Binary file not shown.
Binary file not shown.
+37
-1
@@ -2,11 +2,47 @@
|
||||
#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
|
||||
|
||||
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
||||
{
|
||||
int wordcount = 0;
|
||||
char buffer[256];
|
||||
char *line = fgets(buffer, sizeof(buffer), file);
|
||||
|
||||
}
|
||||
while(line != NULL)
|
||||
{
|
||||
char* word = strtok(line, " ,;");
|
||||
|
||||
while (word != NULL)
|
||||
{
|
||||
strncpy(words[wordcount], word, strlen(word));
|
||||
|
||||
wordcount += 1;
|
||||
|
||||
word = strtok(NULL, " ,;\t\n");
|
||||
}
|
||||
|
||||
line = fgets(buffer, sizeof(buffer), file);
|
||||
}
|
||||
|
||||
for(int i = 0; i < wordcount; i++)
|
||||
{
|
||||
for(int j = 0; j < strlen(words[i]); j++)
|
||||
{
|
||||
words[i][j] = toupper(words[i][j]);
|
||||
|
||||
if(words[i][j] == '\n')
|
||||
{
|
||||
words[i][j] = '\0';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return wordcount;
|
||||
}
|
||||
Binary file not shown.
+26
-4
@@ -3,6 +3,9 @@
|
||||
#include "input.h"
|
||||
#include "game.h"
|
||||
#include "graphicalGame.h"
|
||||
#include <errno.h>
|
||||
#include <time.h>
|
||||
|
||||
|
||||
#define MAX_NUMBER_OF_WORDS 100
|
||||
#define SALAD_SIZE 20
|
||||
@@ -10,6 +13,8 @@
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int exitCode = EXIT_SUCCESS;
|
||||
|
||||
srand(time(NULL));
|
||||
|
||||
// Check if the correct number of arguments is provided
|
||||
if(argc != 2)
|
||||
@@ -34,13 +39,30 @@ int main(int argc, char *argv[])
|
||||
fclose(file);
|
||||
|
||||
// Create the word salad by placing words into grid
|
||||
placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, wordCount);
|
||||
//placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, wordCount);
|
||||
|
||||
// TODO:
|
||||
// Check if all words were successfully placed
|
||||
// Start the game if successful
|
||||
// error message if some words couldn't be placed
|
||||
for(int i = 0; i < 10000; i++)
|
||||
{
|
||||
placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, wordCount);
|
||||
|
||||
if(placedWords == wordCount)
|
||||
{
|
||||
|
||||
// Start the game if successful
|
||||
startGame(wordSalad, SALAD_SIZE, words, wordCount, 800);
|
||||
|
||||
return exitCode;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// error message if some words couldn't be placed
|
||||
fprintf(stderr, "Could not start wordsalad. ");
|
||||
printf("Out of %d words only %d could be placed.\n", wordCount, placedWords);
|
||||
puts("GOOD LUCK for getting this game started :)");
|
||||
exitCode = EXIT_FAILURE;
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -51,4 +73,4 @@ int main(int argc, char *argv[])
|
||||
}
|
||||
|
||||
return exitCode;
|
||||
}
|
||||
}
|
||||
Binary file not shown.
Binary file not shown.
@@ -0,0 +1 @@
|
||||
Hund,Katze; Maus
|
||||
Whitespace-only changes.
@@ -0,0 +1,3 @@
|
||||
Apfel
|
||||
Banane
|
||||
Kiwi
|
||||
+15
-36
@@ -32,51 +32,29 @@ void print_test_result(int result) {
|
||||
void test_readWords_simple(void) {
|
||||
FILE *f = fopen("testwords_simple.txt", "r");
|
||||
|
||||
char words[14][MAX_WORD_LEN];
|
||||
int count = readWords(f, words, 14);
|
||||
char words[10][MAX_WORD_LEN];
|
||||
int count = readWords(f, words, 10);
|
||||
fclose(f);
|
||||
|
||||
TEST_ASSERT_EQUAL_INT(15, count);
|
||||
TEST_ASSERT_EQUAL_STRING("Yeti", words[0]);
|
||||
TEST_ASSERT_EQUAL_STRING("Nessie", words[1]);
|
||||
TEST_ASSERT_EQUAL_STRING("Werwolf", words[2]);
|
||||
TEST_ASSERT_EQUAL_STRING("Vampir", words[3]);
|
||||
TEST_ASSERT_EQUAL_STRING("Monster", words[4]);
|
||||
TEST_ASSERT_EQUAL_STRING("Hydra", words[5]);
|
||||
TEST_ASSERT_EQUAL_STRING("Frankenstein", words[6]);
|
||||
TEST_ASSERT_EQUAL_STRING("Dracula", words[7]);
|
||||
TEST_ASSERT_EQUAL_STRING("KingKong", words[8]);
|
||||
TEST_ASSERT_EQUAL_STRING("Gremlin", words[9]);
|
||||
TEST_ASSERT_EQUAL_STRING("Kobold", words[10]);
|
||||
TEST_ASSERT_EQUAL_STRING("Hexe", words[11]);
|
||||
TEST_ASSERT_EQUAL_STRING("Poltergeist", words[12]);
|
||||
TEST_ASSERT_EQUAL_STRING("Gespenst", words[13]);
|
||||
TEST_ASSERT_EQUAL_STRING("Oger", words[14]);
|
||||
TEST_ASSERT_EQUAL_INT(3, count);
|
||||
TEST_ASSERT_EQUAL_STRING("APFEL", words[0]);
|
||||
TEST_ASSERT_EQUAL_STRING("BANANE", words[1]);
|
||||
TEST_ASSERT_EQUAL_STRING("KIWI", words[2]);
|
||||
}
|
||||
|
||||
void test_readWords_with_delimiters(void) {
|
||||
FILE *f = fopen("testwords_delims.txt", "r");
|
||||
|
||||
char words[14][MAX_WORD_LEN];
|
||||
int count = readWords(f, words, 14);
|
||||
char words[10][MAX_WORD_LEN];
|
||||
int count = readWords(f, words, 10);
|
||||
fclose(f);
|
||||
|
||||
TEST_ASSERT_EQUAL_INT(15, count);
|
||||
TEST_ASSERT_EQUAL_STRING("Yeti", words[0]);
|
||||
TEST_ASSERT_EQUAL_STRING("Nessie", words[1]);
|
||||
TEST_ASSERT_EQUAL_STRING("Werwolf", words[2]);
|
||||
TEST_ASSERT_EQUAL_STRING("Vampir", words[3]);
|
||||
TEST_ASSERT_EQUAL_STRING("Monster", words[4]);
|
||||
TEST_ASSERT_EQUAL_STRING("Hydra", words[5]);
|
||||
TEST_ASSERT_EQUAL_STRING("Frankenstein", words[6]);
|
||||
TEST_ASSERT_EQUAL_STRING("Dracula", words[7]);
|
||||
TEST_ASSERT_EQUAL_STRING("KingKong", words[8]);
|
||||
TEST_ASSERT_EQUAL_STRING("Gremlin", words[9]);
|
||||
TEST_ASSERT_EQUAL_STRING("Kobold", words[10]);
|
||||
TEST_ASSERT_EQUAL_STRING("Hexe", words[11]);
|
||||
TEST_ASSERT_EQUAL_STRING("Poltergeist", words[12]);
|
||||
TEST_ASSERT_EQUAL_STRING("Gespenst", words[13]);
|
||||
TEST_ASSERT_EQUAL_STRING("Oger", words[14]);
|
||||
char cwd[PATH_MAX];
|
||||
|
||||
TEST_ASSERT_EQUAL_INT(3, count);
|
||||
TEST_ASSERT_EQUAL_STRING("HUND", words[0]);
|
||||
TEST_ASSERT_EQUAL_STRING("KATZE", words[1]);
|
||||
TEST_ASSERT_EQUAL_STRING("MAUS", words[2]);
|
||||
}
|
||||
|
||||
void test_readWords_empty_file(void) {
|
||||
@@ -195,3 +173,4 @@ int main(void) {
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
+1
-1
@@ -2,4 +2,4 @@ Yeti,Nessie Werwolf; Vampir
|
||||
Monster
|
||||
Hydra;Frankenstein
|
||||
Dracula;KingKong;Gremlin;Kobold,Hexe;Poltergeist
|
||||
Gespenst, Oger
|
||||
Gespenst, Oger
|
||||
Executable
BIN
Binary file not shown.
Executable
BIN
Binary file not shown.
Reference in new issue
Block a user