Thursday, 29 November 2012

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. :)

Wednesday, 21 November 2012

Hangman Game *In Progress*

*Update 26/11/2012* - Some of you have been coming up to me and saying stuff like "It doesn't work!!" Well, that's kinda what "*In Progress*" means. The whole point of me writing "*In Progress*" in the title was to indicate that my hangman game is, well, In Progress!
Anyway, that's my rant over....

I have been creating a hangman game. The code is below, but it doesn't work that well yet...

/* 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() {
    char cWord[6] = "CHEESE", // This is the word that the user has to guess
        *cGuessed = malloc(1), // This is a DYNAMIC CHAR ARRAY.
        cGuess;
    printf("%d", sizeof(cGuessed));
    int iSuccess = 0,
        iGuesses = 10,
        iInWord = 0,
        iLettersLeft = sizeof(cWord),
        iNLettersGuessed = 0,
        iLetterGuessed = 0;
    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 are %d %s left.\n", 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;
}


And the Java version... :)
/* 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........
*/
import java.util.Scanner;

class Game {
    /* 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.
 */
 public static void main(String[] args) {
     char cWord[6] = "CHEESE",
         cGuess;
     boolean bSuccess = false;
      int iGuesses = 10,
        iInWord = 0,
        iLettersLeft = cWord.length,
        iNLettersGuessed = 0,
        iLetterGuessed = 0;
        Scanner scanner = new Scanner(System.in);
    System.out.println("********Welcome to...********");
        try {
        Thread.sleep(2);
        } catch (InterruptedException e) {
            System.err.println("PROBLEM! PROBLEM! PROBLEM! This program will now exit.");
        }
    System.out.println("********Ming's amaaaazzzziiiinnnnggg hangman game!!!!********");
    try {
        Thread.sleep(1);
        } catch (InterruptedException e) {
            System.err.println("PROBLEM! PROBLEM! PROBLEM! This program will now exit.");
        }
    System.out.println("This word has " + iLettersLeft + " letters.");
    try {
        Thread.sleep(1);
        } catch (InterruptedException e) {
            System.err.println("PROBLEM! PROBLEM! PROBLEM! This program will now exit.");
        }
    System.out.println("You have " + iGuesses + " guesses.");
    System.out.println("Guess a letter, any letter.");
    while (!bSuccess && iGuesses > 0) {
        cGuess = scanner.findWithinHorizon(".", 0).charAt(0);
        cGuess = Character.toUpperCase(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;
        }*/
       
        for (int i = 0; i < cWord.length; i++) {
            if (cGuess == cWord[i]) {
                iInWord++;
            }
            if (i == sizeof(cWord) - 1) {
                if (!iInWord) {
                    System.out.println("Sorry, that letter is not in the word. Try again.");
                } else {
                    System.out.println("That letter appears " + iInWord + " " + (iInWord == 1) ? "time" : "times" + " in the word. Felicitations!");
                    iLettersLeft -= iInWord;
                    System.out.println("There are " + iLettersLeft + " " + iLettersLeft, (iLettersLeft == 1) ? "letter" : "letters" + " left.");
                }
               
            }
        }
        if (iLettersLeft == 0) {
            System.out.println("You, my (wo)man, have completed the game! Congrats!!!");
            bSuccess = true;
            break;
        }
        if (!iInWord) {
            iGuesses--;
        }
        if (iGuesses == 0) {
            System.out.println("Well, it IS true that there " + (iLettersLeft == 1) ? "is" : "are" + " only " + iLettersLeft + " " + (iLettersLeft == 1) ? "letter" : "letters" + " left, but you've run out of guesses. YOU'VE BEEN HANGED!!!!!");
            break;
        } else {
            System.out.println("Du hast " + iGuesses + " Schaetzungen mehr (You have " + iGuesses + " " + (iGuesses == 1) ? "guess" : "guesses" + " left).");
        }
        iInWord = 0;
    }
    if (bSuccess) {
        System.out.println("Well done! You correctly guessed the word " + cWord + "!! I'm, like, soooo proud of you, O.M.G.!");
        System.out.println("Here, have a cookie....");
    } else {
        System.out.println("OH MY GOD, YOU DEAD! Seriously, can you not even guess the word " + cWord + "?! WTF?!\n", cWord);
        System.out.println("As Taran would say, Let-down......");
    }
}
}

Wednesday, 7 November 2012

Just some bit more work on strings and char arrays... (And Gangnam Style :))

I did something similar some time ago, but just in case you love my blog so much that you just can't resist some more juicy, fat code, here we go....

Original Code:
#include <stdio.h>
int main()
{
    char *string = "Herro World!";
    char myName[10];
    myName[0] = 'M';
    myName[1] = 'I';
    myName[2] = 'N';
    myName[3] = 'G';
    myName[4] = 'A';
    myName[5] = 'L';
    myName[6] = 'I';
    myName[7] = 'N';
    myName[8] = 'G';
    myName[9] = '\0';
    char psyOppa[20] = {'O', 'p', 'p', 'a', ' ', 'G', 'a', 'n', 'g', 'n', 'a', 'm', ' ', 'S', 't', 'y', 'l', 'e', '\0'};
    int i = 0;
    for (i = 0; i < sizeof(psyOppa); i++) // sizeof() returns the size of an array
    {
        printf("%s, %s, %s\n", string, myName, psyOppa);
    }
    return 0;
}

Output:
Herro World!, MINGALING, Oppa Gangnam Style
Herro World!, MINGALING, Oppa Gangnam Style
Herro World!, MINGALING, Oppa Gangnam Style
Herro World!, MINGALING, Oppa Gangnam Style
Herro World!, MINGALING, Oppa Gangnam Style
Herro World!, MINGALING, Oppa Gangnam Style
Herro World!, MINGALING, Oppa Gangnam Style
Herro World!, MINGALING, Oppa Gangnam Style
Herro World!, MINGALING, Oppa Gangnam Style
Herro World!, MINGALING, Oppa Gangnam Style
Herro World!, MINGALING, Oppa Gangnam Style
Herro World!, MINGALING, Oppa Gangnam Style
Herro World!, MINGALING, Oppa Gangnam Style
Herro World!, MINGALING, Oppa Gangnam Style
Herro World!, MINGALING, Oppa Gangnam Style
Herro World!, MINGALING, Oppa Gangnam Style
Herro World!, MINGALING, Oppa Gangnam Style
Herro World!, MINGALING, Oppa Gangnam Style
Herro World!, MINGALING, Oppa Gangnam Style
Herro World!, MINGALING, Oppa Gangnam Style


This simple program creates char arrays - a.k.a. strings. The general syntax for arrays in C is:

<the-type-of-variable-that-the array-is-full-of> <name>[<number-of-variables-in-array>];
<name>[0] = .......;
<name>[1] = .......;
etc.....

For example, to create an array of 3 integers, write:
int intArray[3];
intArray[0] = 1;
intArray[1] = 2;
intArray[2] = 3;

A shorthand way of creating an array is:
int intArray[3] = {1, 2, 3};