Tuesday, 27 November 2012

*In Progress* (Yes, that means it's In Progress!) Hangman Game - PART 2

Today, I attempted to read words from a file and put them into a char array. I wanted to randomly select one of these words and use it in the game. Unfortunately, C is just doing my head in because it is, like, soooooooooo difficult to read lines from a file! Java is much easier in this way.... *Sigh* :(

I WILL TRY TO GET THIS CODE WORKING OVER THE WEEKEND AND FINISH IT NEXT LESSON.

Code:
/* Copyright (C) Ming Yin Code Stuff 2012. All Rights Reserved.
* Stealing any fragments, or the whole, of this code is strictly against the UK law and is liable for prosecution.
* IGNORANCE OF THE LAW IS NO DEFENCE.
* YOU HAVE BEEN WARNED. MWAHAHAHAHA........
*/
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>

/* This is a simple hangman game console application. The user is required to input a letter when prompted. If that letter is part of the word that the user needs to guess, the user is notified of this and is asked to guess another letter.
* The user has 10 guesses to correctly guess all the letters in the word. If that limit is succeeded, the user has failed and is notified of this. If the user succeeds, the program will print a message of congratulations.
*/
int main() {
    FILE *file_ptr = fopen("hardwords.txt", "rt"); // This line declares a file pointer which points to the "hardwords.txt" file, which contains the hardest words to guess in hangman.
    if (file_ptr == NULL) {
        printf("An error has occurred while attempting to open a file. This program will now commit suicide. :)\n");
        printf("Seriously though, this program will now have to shut down because it can't randomly select a word for you to guess. I'm sorry.");
        return 1;
    }
    char cWords[50], // This is the char array that will contain all the words in the "hardwords.txt" file.
        *cWord, // This is the word that the user has to guess. It will be chosen from the words in the "hardwords.txt" file.
        *cGuessed = malloc(1), // This is a DYNAMIC CHAR ARRAY.
        cGuess;
    // printf("%ld", sizeof(cGuessed));
    int iSuccess = 0,
        iGuesses = 10,
        iInWord = 0,
        iLettersLeft = sizeof(cWord),
        iNLettersGuessed = 0,
        iLetterGuessed = 0;
       
    while (fgets(cWords, 50, file_ptr) != NULL) {
        printf("%s", cWords);
    }
    printf("%c\n", cWords[1]);
       
    printf("********Welcome to...********\n");
    sleep(2);
    printf("********Ming's amaaaazzzziiiinnnnggg hangman game!!!!********\n");
    sleep(1);
    printf("This word has %d letters.\n", iLettersLeft);
    sleep(1);
    printf("You have %d guesses.\n", iGuesses);
    printf("Guess a letter, any letter.\n");
    while (!iSuccess && iGuesses > 0) {
        scanf(" %c", &cGuess);
        cGuess = toupper(cGuess);
        /*if (iNLettersGuessed == 0) {
            printf("%d\n", sizeof(cGuessed) - 1);
            cGuessed[sizeof(cGuessed) - 1] = cGuess;
            iNLettersGuessed++;
            printf("Stuff and stuff: %d %c\n", iNLettersGuessed, cGuessed[0]);
        } else {
            int i;
            for (i = 0; i < sizeof(cGuessed); i++) {
                if (cGuess == cGuessed[i]) {
                    iLetterGuessed = 1;
                }
            }
            if (!iLetterGuessed) {
                cGuessed = realloc(cGuessed, iNLettersGuessed*sizeof(char));
                cGuessed[sizeof(cGuessed) - 1] = cGuess;
                iNLettersGuessed++;
                printf("%d %s", iNLettersGuessed, cGuessed);
            } else {
                printf("That letter has already been guessed.\n");
                printf("cGuessed: %s", cGuessed);
                continue;
            }
            iLetterGuessed = 0;
        }*/
       
        int i;
        for (i = 0; i < sizeof(cWord); i++) {
            if (cGuess == cWord[i]) {
                iInWord++;
            }
            if (i == sizeof(cWord) - 1) {
                if (!iInWord) {
                    printf("Sorry, that letter is not in the word. Try again.\n");
                } else {
                    printf("That letter appears %d %s in the word. Felicitations!\n", iInWord, (iInWord == 1) ? "time" : "times");
                    iLettersLeft -= iInWord;
                    printf("There %s %d %s left.\n", (iLettersLeft == 1) ? "is" : "letters", iLettersLeft, (iLettersLeft == 1) ? "letter" : "letters");
                }
               
            }
        }
        if (iLettersLeft == 0) {
            printf("You, my (wo)man, have completed the game! Congrats!!!\n");
            iSuccess = 1;
            break;
        }
        if (!iInWord) {
            iGuesses--;
        }
        if (iGuesses == 0) {
            printf("Well, it IS true that there %s only %d %s left, but you've run out of guesses. YOU'VE BEEN HANGED!!!!!\n", (iLettersLeft == 1) ? "is" : "are", iLettersLeft, (iLettersLeft == 1) ? "letter" : "letters");
            break;
        } else {
            printf("Du hast %d Schaetzungen mehr (You have %d %s left).\n", iGuesses, iGuesses, (iGuesses == 1) ? "guess" : "guesses");
        }
        iInWord = 0;
    }
    if (iSuccess) {
        printf("Well done! You correctly guessed the word %s!! I'm, like, soooo proud of you, O.M.G.!\n", cWord);
        printf("Here, have a cookie....");
    } else {
        printf("OH MY GOD, YOU DEAD! Seriously, can you not even guess the word %s?! WTF?!\n", cWord);
        printf("As Taran would say, Let-down......");
    }
    return 0;
}


BY THE WAY, IF ANYONE HAS ANY TIPS ON HOW I COULD IMPROVE THE CODE IN TERMS OF SIMPLIFYING IT OR MAKING IT MORE CONSISTENT, PLEASE COMMENT ON THIS POST!! In return, I will +1 and follow your blog and you will live your wonderful life knowing that a sad, lonely geek has acknowledged your contribution to his program. :)

2 comments:

  1. youve not put any effort into this, the best one ive seen in my life is by this i guy a randomly found on the internet to whom i have no connection:
    http://rstech12-mkhan-92b.blogspot.co.uk/

    ReplyDelete
  2. You have not referenced to your command "sleep" here.

    ReplyDelete