main game und input mac
This commit is contained in:
parent
9a7d3d3766
commit
5fe13e782a
@ -13,11 +13,91 @@
|
||||
// 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));
|
||||
unsigned int placedWordsCount = 0;
|
||||
//salad = spielfeld (100*100)
|
||||
//searchFieldLen = salad Size = 20
|
||||
//words = array mit den zu plazierenden wörtern
|
||||
//anzahl an zu plazierenden wörtern
|
||||
const char buchstaben[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
const int anzahlBuchstaben = 26;
|
||||
|
||||
for (int i = 0; i < wordCount; i++) {
|
||||
int vertikal_horizontal = rand() % 2; //1 = vertikal //0 = horizontal
|
||||
int leange = strlen(words[i]);
|
||||
int positionX = 0;
|
||||
int positionY = 0;
|
||||
int voll = 0;
|
||||
int tries = 0;
|
||||
|
||||
if (vertikal_horizontal == 1) {
|
||||
|
||||
tries = 0;
|
||||
do {
|
||||
voll = 0;
|
||||
positionX = rand() % (searchFieldLen);
|
||||
positionY = rand() % (searchFieldLen-leange);
|
||||
for(int j = positionY; j < positionY+leange ; j++) { //überprüfung ob alle positionen 0 sind
|
||||
if(salad[j][positionX] != '0') {
|
||||
voll = 1;
|
||||
}
|
||||
}
|
||||
tries++;
|
||||
} while (voll == 1 && tries <= MAX_RAND_TRIES_PER_WORD);
|
||||
if (voll == 0) {
|
||||
for(int k = positionY, l = 0; k < positionY+leange; k++, l++) { //buchstaben holen und setzen
|
||||
salad[k][positionX] = words[i][l];
|
||||
}
|
||||
placedWordsCount++;
|
||||
} else if (voll == 1)
|
||||
printf("Das Wort '%s' konnte nicht plaziert werden", words[i]);
|
||||
|
||||
} else if (vertikal_horizontal == 0) {
|
||||
|
||||
tries = 0;
|
||||
do {
|
||||
voll = 0;
|
||||
positionY = rand() % (searchFieldLen);
|
||||
positionX = rand() % (searchFieldLen-leange);
|
||||
for(int j = positionX; j < positionX+leange ; j++) { //überprüfung ob alle positionen 0 sind
|
||||
if(salad[positionY][j] != '0') {
|
||||
voll = 1;
|
||||
}
|
||||
}
|
||||
tries++;
|
||||
} while (voll == 1 && tries <= MAX_RAND_TRIES_PER_WORD);
|
||||
if (voll == 0) {
|
||||
for(int k = positionX, l = 0; k < positionX+leange; k++, l++) { //buchstaben holen und setzen
|
||||
salad[positionY][k] = words[i][l];
|
||||
}
|
||||
placedWordsCount++;
|
||||
} else if (voll == 1)
|
||||
printf("Das Wort '%s' konnte nicht plaziert werden", words[i]);
|
||||
|
||||
} else
|
||||
printf("Fehler bei Vertikal Horizontal Wert: %d", vertikal_horizontal);
|
||||
}
|
||||
|
||||
for(int i = 0; i < searchFieldLen; i++) {
|
||||
for(int j = 0; j < searchFieldLen; j++) {
|
||||
if(salad[i][j] == '0') {
|
||||
salad[i][j] = buchstaben[rand() % anzahlBuchstaben];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return placedWordsCount;
|
||||
|
||||
}
|
||||
|
||||
// Prints the word salad to console
|
||||
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 j = 0; j < searchFieldLen; j++) {
|
||||
printf(" %c", salad[i][j]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
BIN
Start_Mac/game.o
BIN
Start_Mac/game.o
Binary file not shown.
@ -11,7 +11,7 @@ int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
||||
unsigned int count = 0;
|
||||
|
||||
char line[MAX_LINE_LEN];
|
||||
char *teiler = " ,;";
|
||||
char *teiler = " ,;\n\t";
|
||||
char *token;
|
||||
|
||||
while(fgets(line, sizeof(line), file)){
|
||||
@ -31,6 +31,8 @@ int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
||||
return count;
|
||||
}
|
||||
|
||||
//hier müsste man den token string in großbuchstaben formatieren
|
||||
|
||||
strncpy(words[count], token, MAX_WORD_LEN-1);
|
||||
words[count][MAX_WORD_LEN-1] = '\0';
|
||||
|
||||
|
||||
Binary file not shown.
@ -28,7 +28,12 @@ int main(int argc, char *argv[])
|
||||
{
|
||||
unsigned int placedWords = 0;
|
||||
char wordSalad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN]; // 2D array to store the word salad
|
||||
|
||||
for(int i = 0; i < MAX_SEARCH_FIELD_LEN; i++) {
|
||||
for(int j = 0; j < MAX_SEARCH_FIELD_LEN; j++) {
|
||||
wordSalad[i][j] = '0';
|
||||
}
|
||||
}
|
||||
|
||||
// Read words from file and store in 'words' array
|
||||
wordCount = readWords(file, words, MAX_NUMBER_OF_WORDS);
|
||||
fclose(file);
|
||||
|
||||
BIN
Start_Mac/main.o
BIN
Start_Mac/main.o
Binary file not shown.
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user