mercredi 20 janvier 2021

Question about making a high score table in a word guessing game

It is my first few times to practice c programme project and I am trying to finish a word guessing game with a high score table. Although it can run the part of word guessing game smoothly, I cannot fix the part of the high score table at the moment, due to the unknown reasons.

Here is my code:

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

const int true = 1;
const int false = 0;


struct highscore {
    char name[20];
    int numofwords;
};


int readwordlist(char wordlist[][20])
{
    FILE* fp;
    int i;
    char word[20];
    
    fp = fopen("wordlist.txt", "r");
    if (fp == NULL) {
        printf("Please check your file before starting the game.");
        return false;
    }

    for (i = 0; i < 848; i++) {
        fscanf(fp, "%s", word);
        strcpy(wordlist[i], word);
    }

    fclose(fp);

    return true;
}


void printshuffleword(char word[])
{
    char wordcopy[20];
    int letternum;
    int index;

    strcpy(wordcopy, word);

    letternum = 0;
    while (letternum < strlen(word)) {
        // Pick a letter randomly
        index = rand() % strlen(word);

        if (wordcopy[index] != '\0') {
            printf("%c", wordcopy[index]);
            wordcopy[index] = '\0';
            letternum++;
        }
    }
}

void readhighscoretable(struct highscore table[])
{

    FILE* fp;
    int numofwords;
    char name[20];

    fp = fopen("highscoretable.txt", "w");
    if (fp == NULL) {
        printf("You cannot check the table before adding the proper file.");
        return false;
    }

    while (fscanf(fp, "%s %d", name, &numofwords) != EOF) {
        printf("%s\t\t\t%d\n", name, numofwords);
    }

     do { printf("0\t\t\t0\n");
        } while (strlen(table) >= 10);
    
    fclose(fp);
    return true;

}

void inserthighscoretable(struct highscore table[], int numofwords)
{
    int i, j;
    char name[20];

    printf("Please enter your name: ");
    scanf("%s", name);

    i = 0;
    while (table[i].numofwords > numofwords && i < 10) {
        i++;
    }

    if (i < 10) {
        // Push down all the rest of the entries
        for (j = 9; j > i; j--) {
            strcpy(table[j].name, table[j - 1].name);
            table[j].numofwords = table[j - 1].numofwords;
        }

        strcpy(table[i].name, name);
        table[i].numofwords = numofwords;
    }
}

void savehighscoretable(struct highscore table[])
{
    FILE* fp;
    int numofwords;
    char name[20];

    fp = fopen("highscoretable.txt", "w");

    fprintf(fp, "%s %d\n", name, numofwords);

    fclose(fp);
}

void printhighscoretable(struct highscore table[])
{
    int i;

    if (table[0].numofwords > 0) {
        printf("\nHighscore Table\n");
        printf("--------------------------\n");
    }

    i = 0;
    while (table[i].numofwords > 0 && i < 10) {
        printf("%-3d%-20s%3d\n", i + 1, table[i].name, table[i].numofwords);
        i++;
    }
    printf("\n");
}

int main()
{
    char wordlist[849][20];

    int numofwords = 0;

    int targetwordindex;

    char guess[20];

    int numofguesses;

    struct highscore table[10];

    if (!readwordlist(wordlist)) {
        return 0;
    }

    srand(time(NULL));

    printf("Welcome to the word building game!\n");
    printf("----------------------------------\n\n");

    printf("In this game, you have to build a secret word using the letters given to you!\n\n");
    printf("You have got three chances for each word. If you use up your chances,\nthe game will be over.\n\n");

    do {
        targetwordindex = rand() % 849;

        printf("Word %d - ", numofwords + 1);

        printshuffleword(wordlist[targetwordindex]);
        printf("\n\n");

        numofguesses = 0;
        do {
            if (numofguesses == 1) {
                printf("Hint: the word starts with '%c'.\n",
                       wordlist[targetwordindex][0]);
            }
            else if (numofguesses == 2) {
                printf("Hint: the word ends with '%c'.\n",
                       wordlist[targetwordindex][strlen(wordlist[targetwordindex]) - 1]);
            }

            printf("Please enter your guess: ");
            scanf("%s", guess);

            numofguesses++;

        } while (strcmp(wordlist[targetwordindex], guess) != 0 && numofguesses < 3);

        if (strcmp(wordlist[targetwordindex], guess) == 0) {
            numofwords++;
        }

        printf("\n");
    } while (strcmp(wordlist[targetwordindex], guess) == 0);

    printf("Game over! You have guessed %d word(s) correctly.\n\n", numofwords);

    readhighscoretable(table);

    if (numofwords > 0 && numofwords >= table[9].numofwords) {
        inserthighscoretable(table, numofwords);

        savehighscoretable(table);
    }

    printhighscoretable(table);
}

Please help me find some problematic points. That would mean so much for me.

Aucun commentaire:

Enregistrer un commentaire