*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......");
}
}
}
No comments:
Post a Comment