Second commit

This commit is contained in:
Tubui 2025-10-21 07:51:27 +02:00
parent 5d267c1709
commit 0472094860
3 changed files with 33 additions and 23 deletions

22
game.c
View File

@ -22,19 +22,19 @@
// }
// Hàm tạo ngẫu nhiên một chữ cái in hoa
// Funktion, die einen zufälligen Großbuchstaben generiert
char randomLetter()
{
return 'A' + rand() % 26;
}
// Hàm chính: Tạo trò chơi word salad
// Hauptfunktion: Erstellt das Wortsalat-Spiel
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. Khởi tạo lưới trống
// 1. Leeres Spielfeld initialisieren
for (unsigned int i = 0; i < searchFieldLen; i++)
{
for (unsigned int j = 0; j < searchFieldLen; j++)
@ -45,25 +45,25 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN],
srand((unsigned int)time(NULL));
// 2. Đặt từng từ vào lưới
// 2. Jedes Wort zufällig im Spielfeld platzieren
for (unsigned int w = 0; w < wordCount; w++)
{
const char *word = words[w];
size_t len = strlen(word);
if (len > searchFieldLen)
continue; // Bỏ qua nếu từ quá dài
continue; // Überspringen, falls das Wort zu lang ist
int placed = 0;
for (int attempt = 0; attempt < MAX_RAND_TRIES_PER_WORD && !placed; attempt++)
{
int dir = rand() % 2; // 0 = ngang, 1 = dọc
int dir = rand() % 2; // 0 = horizontal, 1 = vertikal
int row = rand() % searchFieldLen;
int col = rand() % searchFieldLen;
// Kiểm tra xem từ có vừa vị trí không
// Prüfen, ob das Wort an die Position passt
if (dir == 0 && col + len <= searchFieldLen)
{
// Kiểm tra xem có bị đè chữ khác không
// Prüfen, ob keine Überschneidungen mit anderen Buchstaben auftreten
int ok = 1;
for (size_t i = 0; i < len; i++)
{
@ -101,7 +101,7 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN],
}
}
// 3. Điền ký tự ngẫu nhiên vào các ô trống
// 3. Leere Felder mit zufälligen Buchstaben füllen
for (unsigned int i = 0; i < searchFieldLen; i++)
{
for (unsigned int j = 0; j < searchFieldLen; j++)
@ -111,10 +111,10 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN],
}
}
return 0; // 0 = thành công
return 0; // 0 = erfolgreich
}
// Hàm in lưới ra màn hình
// Funktion zur Anzeige des Spielfelds in der Konsole
void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN],
unsigned int searchFieldLen)
{

18
input.c
View File

@ -11,8 +11,8 @@
// }
// Đọc danh sách các từ từ tệp và lưu vào mảng 'words'.
// Trả về số lượng từ đã đọc được, hoặc -1 nếu lỗi.
// Wörter aus einer Datei lesen und im Array 'words' speichern
// Gibt die Anzahl der gelesenen Wörter zurück oder -1 im Fehlerfall.
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
{
@ -23,19 +23,19 @@ int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
}
unsigned int count = 0;
char line[MAX_WORD_LEN + 5]; // thêm chút dư để tránh tràn
char line[MAX_WORD_LEN + 5]; // etwas Puffer, um Überlauf zu vermeiden
// Đọc từng dòng cho đến khi hết file hoặc đạt giới hạn
// Zeilen einzeln lesen, bis EOF erreicht oder das Limit erreicht ist
while (fgets(line, sizeof(line), file) != NULL && count < maxWordCount)
{
// Xóa ký tự xuống dòng '\n' nếu có
// Zeilenumbruch '\n' oder '\r\n' entfernen
line[strcspn(line, "\r\n")] = '\0';
// Bỏ qua dòng trống
// Leere Zeilen überspringen
if (strlen(line) == 0)
continue;
// Loại bỏ khoảng trắng đầu/cuối (nếu có)
// Leerzeichen am Anfang und Ende entfernen
char *start = line;
while (isspace((unsigned char)*start))
start++;
@ -47,9 +47,9 @@ int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
end--;
}
// Sao chép an toàn vào mảng words
// Sichere Kopie in das Array 'words'
strncpy(words[count], start, MAX_WORD_LEN - 1);
words[count][MAX_WORD_LEN - 1] = '\0'; // đảm bảo kết thúc chuỗi
words[count][MAX_WORD_LEN - 1] = '\0'; // Nullterminierung sicherstellen
count++;
}

16
main.c
View File

@ -12,7 +12,7 @@ int main(int argc, char *argv[])
int exitCode = EXIT_SUCCESS;
// Check if the correct number of arguments is provided
if(argc != 2)
if (argc != 2)
{
fprintf(stderr, "Usage: %s <path to file with search words>\n", argv[0]);
exitCode = EXIT_FAILURE;
@ -24,7 +24,7 @@ int main(int argc, char *argv[])
FILE *file = fopen(argv[1], "r");
if(file != NULL)
if (file != NULL)
{
unsigned int placedWords = 0;
char wordSalad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN]; // 2D array to store the word salad
@ -38,9 +38,19 @@ int main(int argc, char *argv[])
// TODO:
// Check if all words were successfully placed
if (placedWords < wordCount)
{
fprintf(stderr, "Warning: Only %d out of %d words could be placed in the salad.\n",
placedWords, wordCount);
fprintf(stderr, "Some words could not be placed.\n");
}
else
{
printf("All %u words placed successfully!\n", wordCount);
}
// Start the game if successful
showWordSalad(wordSalad, SALAD_SIZE);
// error message if some words couldn't be placed
}
else
{