Compare commits

...
12 Commits
5 changed files with 237 additions and 0 deletions

No files matched your search

+3
View File
@@ -60,3 +60,6 @@ dkms.conf
# macOS # macOS
.DS_Store .DS_Store
Startcode/testwords_delims.txt
Startcode/testwords_empty.txt
Startcode/testwords_simple.txt
+85
View File
@@ -6,6 +6,8 @@
#define MAX_RAND_TRIES_PER_WORD 10 #define MAX_RAND_TRIES_PER_WORD 10
#define EMPTY_CHAR 0 #define EMPTY_CHAR 0
//TODO: Spiellogik implementieren: //TODO: Spiellogik implementieren:
/* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren /* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren
* restliche Felder mit zufälligen Buchstaben füllen */ * restliche Felder mit zufälligen Buchstaben füllen */
@@ -13,5 +15,88 @@
// 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)
{ {
//Initialisiert den seed für rand()
srand(time(NULL));
//Initialisiere spielfeld auf '0'
// TODO: memset()
//memset(salad, '0', MAX_SEARCH_FIELD_LEN * MAX_SEARCH_FIELD_LEN);
for(int i = 0; i < MAX_SEARCH_FIELD_LEN; i++){
for (int j = 0; j < MAX_SEARCH_FIELD_LEN; j++){
salad[i][j] = '0';
}
}
int placedWords = 0;
//Iterate over words
for(int counter = 0; counter < wordCount; counter++){
size_t len = strlen(words[counter]);
//try to place a word
for(int try = 0; try < 10; try++){
int x = rand() % MAX_SEARCH_FIELD_LEN;
int y = rand() % MAX_SEARCH_FIELD_LEN;
int dir = rand() % 2;
// cant place vertically
if(dir == 1 && MAX_SEARCH_FIELD_LEN < (y + (int) len -1)){
dir = 0;
}
//cant place horizontally
if((x + (int) len -1) > MAX_SEARCH_FIELD_LEN){
continue;
}
//place word vertically
if(dir == 1){
//see if all neccessary spaces are empty
bool cont = false;
for (int i = 0; i < (int)len; i++){
if(salad[x][y+i] != '0'){
cont = true;
break;
}
}
//if not all spaces are empty try again
if(cont) continue;
//insert currentWord into playing field
for (int i = 0; i < (int)len; i++){
salad[x][y+i] = words[counter][i];
}
placedWords++;
break;
}else{
//place currentWord horizontally
//see if all neccessary spaces are emtry
bool cont = false;
for (int i = 0; i < (int)len; i++){
if(salad[x+1][y] != '0'){
cont = true;
break;
}
}
//if not all spaces are empty try again
if(cont) continue;
//place currentWord
for (int i = 0; i < (int)len; i++){
salad[x+1][y] = words[counter][i];
}
placedWords++;
break;
}
}
}
for (int i = 0; i < MAX_SEARCH_FIELD_LEN; i++){
for (int j = 0; j < MAX_SEARCH_FIELD_LEN; j++){
if(salad[i][j] == '0'){
char c = 'A' + (rand() % 26);
salad[i][j] = c;
}
}
}
return placedWords;
} }
+71
View File
@@ -0,0 +1,71 @@
//Torbens Kopie zum Testen
#include <string.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include "game.h"
#include "graphicalGame.h"
#define MAX_NUMBER_OF_WORDS 100
#define MAX_WORD_LEN 100
#define MAX_LINE_LEN 1024
#define SALAD_SIZE 20
// 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)
{
char *tmp;
int counter = 0;
char delimiters[] = ",!.;:? \n";
char *token;
while(fgets(tmp, MAX_LINE_LEN, file)){
printf("Input Line: %s\n", tmp);
token = strtok (tmp, delimiters);
while(token != NULL){
printf("Token:%s\n", token);
size_t len = strlen(token);
char copy[len+1];
strncpy(copy, token, len+1);
copy[len] = '\0';
strncpy(words[counter+1], copy, len);
words[counter+1][len] = '\0';
printf("Word: %s\n", words[counter+1]);
token = strtok (NULL, delimiters);
counter++;
}
}
return counter;
}
int main(void){
char words[MAX_NUMBER_OF_WORDS][MAX_WORD_LEN]; // Array to hold the words to be used in the game
unsigned int wordCount = 0;
FILE *file = fopen("words.txt", "r");
if(file != NULL)
{
unsigned int placedWords = 0;
char wordSalad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN]; // 2D array to store the word sala
// Read words from file and store in 'words' array
printf("readWords start\n");
wordCount = readWords(file, words, MAX_NUMBER_OF_WORDS);
printf("%d", wordCount);
fclose(file);
// Create the word salad by placing words into grid
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
}
return EXIT_SUCCESS;
}
+33
View File
@@ -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,37 @@
// 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++;
//schaue ob auch alles groß geschrieben ist
for(int i = 0; i < len; i++){
if(words[count][i] <= 122 && words[count][i] >= 97){
//mache Kleinbuchstaben zu Großbuchstaben //ASCII A = 65, a = 97, z = 122
words[count][i] -= 32;
}
}
token = strtok(NULL, delimit);
}
}
return count;
} }
+45
View File
@@ -1,5 +1,6 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <string.h>
#include "input.h" #include "input.h"
#include "game.h" #include "game.h"
#include "graphicalGame.h" #include "graphicalGame.h"
@@ -7,6 +8,42 @@
#define MAX_NUMBER_OF_WORDS 100 #define MAX_NUMBER_OF_WORDS 100
#define SALAD_SIZE 20 #define SALAD_SIZE 20
int checkSalad(char words[][MAX_WORD_LEN], char wordSalad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], int placedWords, int wordCount){
//Iteriere über alle verfügbaren Wörter
int foundWords = 0;
//suche horizontal
for(int i = 0; i < wordCount; i++){
for(int h = 0; h < MAX_SEARCH_FIELD_LEN; h++){
if(strstr(wordSalad[h], words[i]) != NULL){
foundWords++;
break;
}
}
}
//flip Board
char copySalad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN];
for (int i = 0; i< MAX_SEARCH_FIELD_LEN; i++){
for (int j = 0; j < MAX_SEARCH_FIELD_LEN; j++){
copySalad[i][j] = wordSalad[j][i];
}
}
// sucher vertikal
for(int i = 0; i < wordCount; i++){
for(int h = 0; h < MAX_SEARCH_FIELD_LEN; h++){
if(strstr(copySalad[h], words[i]) != NULL){
foundWords++;
break;
}
}
}
if(foundWords == placedWords) return 0;
return -1;
}
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
int exitCode = EXIT_SUCCESS; int exitCode = EXIT_SUCCESS;
@@ -41,6 +78,14 @@ int main(int argc, char *argv[])
// 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
int check = checkSalad(words, wordSalad, placedWords, wordCount);
//Fehlerfall
if(check != 0){
fprintf(stderr, "Not all words could be placed!\n");
exitCode = EXIT_FAILURE;
}
//keine Ahnung wiegroß pixelSize sein soll
startGame(wordSalad, MAX_SEARCH_FIELD_LEN, words, placedWords, MAX_SEARCH_FIELD_LEN * SALAD_SIZE);
} }
else else
{ {