generated from freudenreichan/info2Praktikum-Wortsalat
Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 56b3b9efa1 | |||
| b4cc13d96a | |||
| 765879d085 | |||
| 3d0b1d6729 |
@ -2,6 +2,7 @@
|
||||
#include <time.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
|
||||
#define MAX_RAND_TRIES_PER_WORD 10
|
||||
#define EMPTY_CHAR 0
|
||||
@ -13,11 +14,111 @@
|
||||
// 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));
|
||||
int placedWords = 0;
|
||||
|
||||
// Array mit . füllen
|
||||
for (int r = 0; r < searchFieldLen; r++){
|
||||
for(int s = 0; s < searchFieldLen; s++){
|
||||
salad[r][s] = '.';
|
||||
}
|
||||
}
|
||||
|
||||
// -> erst wenn die ganze Länge des Wortes frei ist, array hinzufügen
|
||||
// MAX Versuche das Wort zu platzieren == 10
|
||||
|
||||
|
||||
// Wörter in salad einsortieren
|
||||
for (int num_words = 0; num_words < wordCount; num_words++){
|
||||
|
||||
int laenge = strlen(words[num_words]);
|
||||
|
||||
// muss noch prüfen, ob in der Zeile schon was ist
|
||||
// 1 ist horizontal, 0 ist vertikal
|
||||
// isalpha == 0 -> kein Buchstabe
|
||||
|
||||
for(int versuch = 0; versuch < MAX_RAND_TRIES_PER_WORD; versuch++){
|
||||
int richtung = rand()% 2;
|
||||
int belegt = 0;
|
||||
|
||||
// horizontale Eingabe
|
||||
if (richtung == 1){
|
||||
int zeile = rand()% searchFieldLen;
|
||||
int spalte = rand()% (searchFieldLen- laenge +1);
|
||||
|
||||
//prüft, ob die herausgesuchte Zeile noch frei ist
|
||||
// belegt == 1 -> Zeile nicht frei
|
||||
for (int o = spalte; o < spalte + laenge; o++){
|
||||
if(salad[zeile][o] != '.'){
|
||||
belegt = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
// setzt in das Array ein, wenn frei oder erhöht die Versuchsanzahl
|
||||
if (belegt == 0){
|
||||
for(int k = 0; k < laenge; k++){
|
||||
salad[zeile][spalte + k] = words[num_words][k];
|
||||
|
||||
}
|
||||
placedWords++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
// vertikale Eingabe
|
||||
else if (richtung == 0){
|
||||
|
||||
int zeile = rand()% (searchFieldLen - laenge +1);
|
||||
int spalte = rand()% searchFieldLen;
|
||||
|
||||
//prüft, ob die herausgesuchte Zeile noch frei ist
|
||||
for (int n = zeile; n < zeile + laenge; n++){
|
||||
if(salad[n][spalte] != '.'){
|
||||
belegt = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
// setzt in das Array ein, wenn frei oder erhöht die Versuchsanzahl
|
||||
if (belegt == 0){
|
||||
for(int j = 0; j < laenge; j++){
|
||||
salad[zeile + j][spalte] = words[num_words][j];
|
||||
|
||||
}
|
||||
placedWords++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
else{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// fügt zufällige Buchstaben ein
|
||||
for(int l = 0; l < searchFieldLen; l++){
|
||||
for(int m = 0; m < searchFieldLen; m++ ){
|
||||
if(isalpha(salad[l][m]) == 0 ){
|
||||
// zufällige Buchstaben erzeugen
|
||||
char alphabet [] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
int laenge = strlen(alphabet);
|
||||
int zufallszahl = rand()% laenge;
|
||||
char zufallsbuchstabe = alphabet[zufallszahl];
|
||||
// zufällige Buchstaben einfügen
|
||||
salad[l][m] = zufallsbuchstabe;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return placedWords;
|
||||
}
|
||||
|
||||
// Prints the word salad to console
|
||||
void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen)
|
||||
{
|
||||
|
||||
for(int p = 0; p < searchFieldLen; p++){
|
||||
for(int q = 0; q < searchFieldLen; q++){
|
||||
printf("%c", salad[p][q]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
BIN
Start_Windows/game.o
Normal file
BIN
Start_Windows/game.o
Normal file
Binary file not shown.
@ -8,5 +8,132 @@
|
||||
// Read words from file and store in 'words' array
|
||||
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
||||
{
|
||||
// MAX_WORD_LEN 100
|
||||
// MAX_LINE_LEN 1024
|
||||
// *file ist Datei
|
||||
// words ist Array
|
||||
// MAX_WORD_LEN ist maximale Wortlänge
|
||||
// maxWordCount ist maximale Anzahl an Wörtern
|
||||
char puffer[MAX_LINE_LEN];
|
||||
int counter = 0;
|
||||
|
||||
}
|
||||
while(fgets(puffer, MAX_LINE_LEN-1, file) != NULL && counter < maxWordCount)
|
||||
{
|
||||
char *parts = strtok(puffer, ",;\n\t/. ");
|
||||
|
||||
while(parts != NULL && counter < maxWordCount)
|
||||
{
|
||||
//strncpy(words[counter][MAX_WORD_LEN], puffer, MAX_LINE_LEN-1);
|
||||
//words[counter][MAX_WORD_LEN-1] = "\0";
|
||||
//Großbuchstaben
|
||||
for(int i = 0; i < MAX_WORD_LEN -1 && parts[i] != '\0'; i++)
|
||||
{
|
||||
words[counter][i] = toupper(parts[i]);
|
||||
words[counter][i] = '\0';
|
||||
}
|
||||
}
|
||||
counter++; // Wort eingelesen -> Wortzähler erhöhen
|
||||
parts = strtok(NULL, ",;\n\t/. ");
|
||||
|
||||
}
|
||||
return counter;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/* #include "input.h"
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
|
||||
#define MAX_NUMBER_OF_WORDS 100
|
||||
#define MAX_WORD_LEN 100
|
||||
// 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 line [MAX_WORD_LEN];
|
||||
printf("H");
|
||||
|
||||
int j = 0;
|
||||
int count = 0;
|
||||
char single_word[MAX_WORD_LEN];
|
||||
|
||||
while(fgets(line, MAX_WORD_LEN, file) != NULL ){
|
||||
|
||||
printf("A");
|
||||
for(int i = 0; line[i] != '\0'; i++){
|
||||
printf("L");
|
||||
|
||||
|
||||
//prüft, ob das gesammelte Zeichen ein Zeichen ist, schiebt es dann Großgeschrieben in das Array word
|
||||
if (isalpha(line[i]) != 0){
|
||||
single_word[j++] = toupper(line[i]);
|
||||
}
|
||||
// wenn das Wort fertig ist, dann wird dem Wort eine 0 angefügt um das Ende des Strings zu signalisieren, dann wird es in das Array words kopiert
|
||||
else{
|
||||
single_word[j] = '\0';
|
||||
strncpy(words[count], single_word, MAX_WORD_LEN);
|
||||
count++;
|
||||
j = 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
for(int n = 0; n < count ; n++)
|
||||
{
|
||||
printf("%s \n", words[n]);
|
||||
printf("O");
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
int main(){
|
||||
int wordCount = 0;
|
||||
FILE *file = fopen("word.txt", "r");
|
||||
char words[MAX_NUMBER_OF_WORDS][MAX_WORD_LEN];
|
||||
wordCount = readWords(file, words, MAX_NUMBER_OF_WORDS);
|
||||
fclose(file);
|
||||
return wordCount;
|
||||
}
|
||||
|
||||
/* 1. Versuch
|
||||
while(fgets(line, maxWordCount, file) != NULL ){
|
||||
|
||||
int i = 0;
|
||||
int count = 0;
|
||||
char line [MAX_WORD_LEN];
|
||||
|
||||
|
||||
while(line[i] != '\0' && line[i+1] != '\0'){
|
||||
|
||||
char single_word[MAX_WORD_LEN];
|
||||
|
||||
//prüft, ob das gesammelte Zeichen ein Zeichen ist, schiebt es dann Großgeschrieben in das Array word
|
||||
if (isalpha(line[i]) != 0){
|
||||
single_word[i++] = toupper(line[i]);
|
||||
|
||||
}
|
||||
// wenn das Wort fertig ist, dann wird dem Wort eine 0 angefügt um das Ende des Strings zu signalisieren, dann wird es in das Array words kopiert
|
||||
else{
|
||||
single_word[i++] = '\0';
|
||||
strncpy(single_word, words, i)
|
||||
words[i][0] = '\0';
|
||||
}
|
||||
i++;
|
||||
|
||||
}
|
||||
|
||||
*/
|
||||
|
||||
BIN
Start_Windows/input.o
Normal file
BIN
Start_Windows/input.o
Normal file
Binary file not shown.
@ -36,11 +36,19 @@ int main(int argc, char *argv[])
|
||||
// Create the word salad by placing words into grid
|
||||
placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, wordCount);
|
||||
|
||||
// TODO:
|
||||
// TODO: (if Schleife implementieren, die das prüft)
|
||||
// Check if all words were successfully placed
|
||||
// Start the game if successful
|
||||
// error message if some words couldn't be placed
|
||||
|
||||
if (placedWords == wordCount){
|
||||
showWordSalad(wordSalad, placedWords);
|
||||
}
|
||||
else{
|
||||
printf("Error. The words couldn't be placed.\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -51,4 +59,6 @@ int main(int argc, char *argv[])
|
||||
}
|
||||
|
||||
return exitCode;
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
@ -35,7 +35,7 @@ graphicalGame.o: graphicalGame.c
|
||||
# --------------------------
|
||||
TEST_BIN = runTests
|
||||
|
||||
test: input.o game.o unit_tests.c
|
||||
test: input.o game.o unit_tests.c
|
||||
$(CC) $(CFLAGS) -I$(unityfolder) -o $(TEST_BIN) input.o game.o unit_tests.c $(BINARIES)/libunity.a
|
||||
|
||||
# --------------------------
|
||||
@ -43,3 +43,6 @@ test: input.o game.o unit_tests.c
|
||||
# --------------------------
|
||||
clean:
|
||||
del /f *.o *.exe
|
||||
|
||||
wordsalad_myversion: main.o input.o game.o graphicalGame.o $(BINARIES)/libwordsalad.a
|
||||
$(CC) $(CFLAGS) -o wordsalad_myversion main.o input.o game.o graphicalGame.o $(BINARIES)/libwordsalad.a $(LDFLAGS)
|
||||
|
||||
70
Start_Windows/test.c
Normal file
70
Start_Windows/test.c
Normal file
@ -0,0 +1,70 @@
|
||||
#include "input.h"
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
|
||||
|
||||
// launch
|
||||
#define MAX_NUMBER_OF_WORDS 100
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
||||
{
|
||||
char line [MAX_WORD_LEN];
|
||||
|
||||
int i = 0;
|
||||
int j = 0;
|
||||
int count = 0;
|
||||
file = fopen("words.txt", "r");
|
||||
char single_word[MAX_WORD_LEN];
|
||||
|
||||
while(fgets(line, maxWordCount, file) != NULL ){
|
||||
|
||||
|
||||
while(line[i] != '\0' && line[i+1] != '\0'){
|
||||
|
||||
|
||||
|
||||
//prüft, ob das gesammelte Zeichen ein Zeichen ist, schiebt es dann Großgeschrieben in das Array word
|
||||
if (isalpha(line[i]) != 0){
|
||||
single_word[j++] = toupper(line[i]);
|
||||
|
||||
}
|
||||
// wenn das Wort fertig ist, dann wird dem Wort eine 0 angefügt um das Ende des Strings zu signalisieren, dann wird es in das Array words kopiert
|
||||
else{
|
||||
single_word[j++] = '\0';
|
||||
strncpy(words[count], single_word, i);
|
||||
i = 0;
|
||||
count++;
|
||||
int j = 0;
|
||||
}
|
||||
i++;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
for(int n = 0; n < count ; n++)
|
||||
{
|
||||
printf("%s \n", words[n]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
char words[MAX_NUMBER_OF_WORDS][MAX_WORD_LEN];
|
||||
FILE*file;
|
||||
unsigned int wordCount = 0;
|
||||
wordCount = readWords(file, words, MAX_NUMBER_OF_WORDS);
|
||||
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user