Template
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ce4be9f902 | ||
|
|
ce17403a1b | ||
|
|
a02b363e20 | ||
|
|
d61687f8df | ||
|
|
d368eea2f3 | ||
|
|
947054bb51 |
No files matched your search
@@ -60,3 +60,6 @@ dkms.conf
|
||||
|
||||
# macOS
|
||||
.DS_Store
|
||||
Startcode/testwords_delims.txt
|
||||
Startcode/testwords_empty.txt
|
||||
Startcode/testwords_simple.txt
|
||||
@@ -6,6 +6,8 @@
|
||||
#define MAX_RAND_TRIES_PER_WORD 10
|
||||
#define EMPTY_CHAR 0
|
||||
|
||||
|
||||
|
||||
//TODO: Spiellogik implementieren:
|
||||
/* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren
|
||||
* restliche Felder mit zufälligen Buchstaben füllen */
|
||||
@@ -13,5 +15,96 @@
|
||||
// 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)
|
||||
{
|
||||
//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
|
||||
unsigned int counter = 0;
|
||||
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);
|
||||
//try to place a word
|
||||
int try = 0;
|
||||
while(try < 10){
|
||||
int x = rand() % MAX_SEARCH_FIELD_LEN;
|
||||
int y = rand() % MAX_SEARCH_FIELD_LEN;
|
||||
|
||||
int dir = rand() % 2;
|
||||
|
||||
if ((MAX_SEARCH_FIELD_LEN - x) < len){
|
||||
if((MAX_SEARCH_FIELD_LEN - y) < len){
|
||||
// cant place word here (too far in the bottom right corner)
|
||||
try++;
|
||||
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;
|
||||
try++;
|
||||
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] = currentWord[i];
|
||||
}
|
||||
placedWords++;
|
||||
break;
|
||||
}
|
||||
|
||||
//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;
|
||||
try++;
|
||||
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] = currentWord[i];
|
||||
}
|
||||
placedWords++;
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
+1
-1
@@ -8,5 +8,5 @@
|
||||
// Read words from file and store in 'words' array
|
||||
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user