Avatar billede patterman Nybegynder
01. maj 2006 - 11:35 Der er 1 løsning

færdig hangman spil i C og kun C

hey

jeg sidder og skal bruge et hangman spil som opfylder følgende krav:
point system
hente random ord fra en fil
omskrive bogstaverne i ordret til samme system som på en telefon
og det skal være skrevet i en form der er nem at forstå, OG DET SKAL VÆRE I C, IKK ANDET END C!!!
jeg er villig til at give mange point da det er yderst vigtigt at jeg får et svar hurtigst muligt
Avatar billede saymyname Nybegynder
01. maj 2006 - 12:20 #1
There you go:



#include <stdio.h>
#include <stdlib.h>

/* Defines. */
#define MAX_WORD_LEN 255
#define SCREEN_HEIGHT 25
#define MAX_GUESSES 6    /* If you change this value, you must update drawhangman(). */
#define BLANK_CHAR '.'


/* Function declarations. */
static int guess(const char *word, char letter, char *display);
static void drawhangman(int count);
static void hangman(const char *word);


/* Function definitions. */

/*
* If 'letter' occurs in 'word', then all occurances of it are
* revealed in 'display'. For example, if word[5] == letter,
* then display[5] is set to letter.
* The function returns the number of characters in 'display'
* which have remain set to BLANK_CHAR.
* Parameters:
* word - the word to be guessed
* letter - the current guess
* display - the string showing letters guessed so far
*/
static int guess(const char *word, char letter, char *display)
{
    int blanks, i;

    blanks = 0;
    for (i = 0; word[i] != '\0'; i++) {
        if (word[i] == letter) {
            display[i] = letter;
        }
        if (display[i] == BLANK_CHAR) {
            blanks++;
        }
    }
    return blanks;
}


/*
* This function draws a picture of gallows. Each time the player
* guesses a letter incorrectly, more of the person is shown.
* After six wrong guesses, the person is dead.
* Parameters:
* count - the number of wrong guesses so far
* Restrictions:
* 'count' must be between 0 and MAX_GUESSES (inclusive).
*/
static void drawhangman(int count)
{
    switch (count) {
    case 0:
        printf("#    +-----+\n");
        printf("#    |    |\n");
        printf("#          |\n");
        printf("#          |\n");
        printf("#          |\n");
        printf("#          |\n");
        printf("#          |\n");
        printf("#          |\n");
        printf("#          |\n");
        printf("#----------+\n");
        break;
    case 1:
        printf("#    +-----+\n");
        printf("#    |    |\n");
        printf("#    O    |\n");
        printf("#          |\n");
        printf("#          |\n");
        printf("#          |\n");
        printf("#          |\n");
        printf("#          |\n");
        printf("#          |\n");
        printf("#----------+\n");
        break;
    case 2:
        printf("#    +-----+\n");
        printf("#    |    |\n");
        printf("#    O    |\n");
        printf("#    |    |\n");
        printf("#    |    |\n");
        printf("#    |    |\n");
        printf("#          |\n");
        printf("#          |\n");
        printf("#          |\n");
        printf("#----------+\n");
        break;
    case 3:
        printf("#    +-----+\n");
        printf("#    |    |\n");
        printf("#    O    |\n");
        printf("# ---|    |\n");
        printf("#    |    |\n");
        printf("#    |    |\n");
        printf("#          |\n");
        printf("#          |\n");
        printf("#          |\n");
        printf("#----------+\n");
        break;
    case 4:
        printf("#    +-----+\n");
        printf("#    |    |\n");
        printf("#    O    |\n");
        printf("# ---|---  |\n");
        printf("#    |    |\n");
        printf("#    |    |\n");
        printf("#          |\n");
        printf("#          |\n");
        printf("#          |\n");
        printf("#----------+\n");
        break;
    case 5:
        printf("#    +-----+\n");
        printf("#    |    |\n");
        printf("#    O    |\n");
        printf("# ---|---  |\n");
        printf("#    |    |\n");
        printf("#    |    |\n");
        printf("#  /      |\n");
        printf("#  /      |\n");
        printf("#          |\n");
        printf("#----------+\n");
        break;
    case 6:
        printf("#    +-----+\n");
        printf("#    |    |\n");
        printf("#    O    |\n");
        printf("# ---|---  |\n");
        printf("#    |    |\n");
        printf("#    |    |\n");
        printf("#  / \\    |\n");
        printf("#  /  \\  |\n");
        printf("#          |\n");
        printf("#----------+\n");
        break;
    default:
        printf("error: drawhangman() was called with 'count' == %d, which is out of range.\n", MAX_GUESSES);
        exit(1);
    }
}


/*
* This function plays a game of hangman with the user.
* Parameters:
* word - the word to guess
*/
static void hangman(const char *word)
{
    int count, blanks, oldblanks, len, i;
    char display[MAX_WORD_LEN+1];
    char letter;

    /* Blank out the display string. */
    len = strlen(word);
    for (i = 0; i < len; i++) {
        display[i] = BLANK_CHAR;
    }
    display[i] = '\0';

    /* Play hangman. */
    count = 0;      /* number of incorrect guesses */
    oldblanks = len; /* number of blanks remaining in the word */
    while ((oldblanks > 0) && (count < MAX_GUESSES)) {
        drawhangman(count);
        printf("%s\n", display);
        printf("Guess a letter:\n");
        scanf(" %c", &letter);
        blanks = guess(word, letter, display);
        if (oldblanks == blanks) {
            /* No new letters were uncovered, so the guess was incorrect. */
            count++;
        }
        oldblanks = blanks;
    }
    drawhangman(count);
    if (blanks == 0) {
        printf("YOU WIN!\n");
    } else {
        printf("YOU LOSE!\n");
    }
    printf("The word was \"%s\".\n", word);
}


/*
* This is the main program.
*/
int main(void)
{
    int i;
    char word[MAX_WORD_LEN+1];

    /* Get the word for the game. */
    printf("Enter a word:\n");
    scanf(" %s", word);

    /* "Clear" the screen by printing 25 blank lines. */
    for (i = 0; i < SCREEN_HEIGHT; i++) {
        printf("\n");
    }

    /* Play hangman. */
    hangman(word);

    return 0;
}
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester