Template
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b5d6a5d1b3 | ||
|
|
a0bd61a872 | ||
|
|
556a83c9af | ||
|
|
d2357c9040 | ||
|
|
69d0c0ee4d | ||
|
|
075a2ad50c | ||
|
|
b0b70e371d | ||
|
|
7df641beb3 | ||
|
|
88575ba8bf | ||
|
|
f1fdbb5424 | ||
|
|
84e854fdc2 | ||
|
|
d5be49e0ff | ||
|
|
8c4e01b75c | ||
|
|
20496fdfba |
No files matched your search
@@ -1,17 +0,0 @@
|
|||||||
#include "game.h"
|
|
||||||
#include <time.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
#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 */
|
|
||||||
|
|
||||||
// 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)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
#include "input.h"
|
|
||||||
#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)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,90 @@
|
|||||||
|
#include "game.h"
|
||||||
|
#include "input.h"
|
||||||
|
#include <time.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#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 */
|
||||||
|
|
||||||
|
|
||||||
|
// 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 placedWords = 0;
|
||||||
|
|
||||||
|
srand(time(NULL));
|
||||||
|
|
||||||
|
|
||||||
|
for (unsigned int r=0; r < searchFieldLen; r++) {
|
||||||
|
for (unsigned int c =0; c <searchFieldLen; c++) {
|
||||||
|
salad[r][c] = EMPTY_CHAR;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Wörter plazieren
|
||||||
|
for (unsigned int w = 0; w< wordCount; w++) {
|
||||||
|
int wordLen = strlen(words[w]);
|
||||||
|
if (wordLen > searchFieldLen) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
int tries = 0;
|
||||||
|
int placed = 0;
|
||||||
|
while (!placed && tries < MAX_RAND_TRIES_PER_WORD) {
|
||||||
|
int direction = rand() % 2;
|
||||||
|
int row;
|
||||||
|
int col;
|
||||||
|
int fits = 1;
|
||||||
|
if (direction == 0) {
|
||||||
|
row = rand() % searchFieldLen;
|
||||||
|
col = rand() % (searchFieldLen - wordLen +1);
|
||||||
|
for (int i = 0; i< wordLen; i++) {
|
||||||
|
if (salad[row][col +i] != EMPTY_CHAR) {
|
||||||
|
fits = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (fits) {
|
||||||
|
for (int i=0; i < wordLen; i++) {
|
||||||
|
salad[row][col +i] = words[w][i];
|
||||||
|
}
|
||||||
|
placed = 1;
|
||||||
|
placedWords++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
row = rand() % (searchFieldLen - wordLen +1);
|
||||||
|
col = rand() % searchFieldLen;
|
||||||
|
for (int i = 0; i< wordLen; i++) {
|
||||||
|
if (salad[row+i][col] != EMPTY_CHAR) {
|
||||||
|
fits = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (fits) {
|
||||||
|
for (int i = 0; i<wordLen; i++) {
|
||||||
|
salad[row+i][col] = words[w][i];
|
||||||
|
}
|
||||||
|
placed = 1;
|
||||||
|
placedWords++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
tries++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//Rest mit Zufallsbustaben füllen
|
||||||
|
for (unsigned int r=0; r < searchFieldLen; r++) {
|
||||||
|
for (int c=0; c < searchFieldLen; c++) {
|
||||||
|
if (salad[r][c] == EMPTY_CHAR) {
|
||||||
|
salad[r][c]='A' + rand() % 26;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return placedWords;
|
||||||
|
}
|
||||||
File renamed without changes.
File renamed without changes.
File renamed without changes.
@@ -0,0 +1,56 @@
|
|||||||
|
#include "input.h"
|
||||||
|
#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 line[MAX_LINE_LEN];
|
||||||
|
|
||||||
|
while (fgets(line, MAX_LINE_LEN, file)) {
|
||||||
|
char currentWord[MAX_LINE_LEN];
|
||||||
|
int wordPos = 0;
|
||||||
|
|
||||||
|
for ( int i = 0; line[i] != '\0'; i++) {
|
||||||
|
if (line[i] == ',' || line[i] == ';' || line[i] == ' ' || line[i] == '\n') {
|
||||||
|
|
||||||
|
if (wordPos > 0) {
|
||||||
|
|
||||||
|
currentWord[wordPos] = '\0';
|
||||||
|
|
||||||
|
if (wordCount < maxWordCount) {
|
||||||
|
|
||||||
|
strcpy(words[wordCount], currentWord);
|
||||||
|
wordCount++;
|
||||||
|
}
|
||||||
|
wordPos = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if (wordPos < MAX_WORD_LEN-1) {
|
||||||
|
|
||||||
|
currentWord[wordPos] = toupper(line[i]);
|
||||||
|
wordPos++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (wordPos > 0) {
|
||||||
|
|
||||||
|
currentWord[wordPos] = '\0';
|
||||||
|
|
||||||
|
if (wordCount < maxWordCount) {
|
||||||
|
|
||||||
|
strcpy(words[wordCount], currentWord);
|
||||||
|
wordCount++;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return wordCount;
|
||||||
|
}
|
||||||
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
@@ -40,7 +40,11 @@ int main(int argc, char *argv[])
|
|||||||
// Check if all words were successfully placed
|
// Check if all words were successfully placed
|
||||||
// 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
|
||||||
|
if (placedWords == wordCount){
|
||||||
|
startGame(wordSalad, SALAD_SIZE, words, wordCount, 800);
|
||||||
|
}else {
|
||||||
|
fprintf(stderr, "Could not open file %s for reading ...\n");
|
||||||
|
exitCode = EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Reference in new issue
Block a user