Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[C++ - Win32] Hangman
#1
This is an old project that is full of errors; do not use it as something to learn from.

- main.cpp
Code:
#include <iostream>
#include <TextControl.h>
using namespace std;

void MissedLetter(int miss) {

    switch (miss) {
        case 1: PlaceCursor(55, 4); printf("O"); break;
        case 2: PlaceCursor(55, 5); printf("|"); break;
        case 3: PlaceCursor(54, 5); printf("\\"); break;
        case 4: PlaceCursor(56, 5); printf("/"); break;
    }

    return;
}

int main() {

    RemoveCursor();

    char *WordLibrary[] = {
        "cat",
        "dog",
        "pan"
    };

    srand(time(NULL));
    int Selected = rand() % 3;
    int miss = 0;
    char *Word = WordLibrary[Selected];
    int WordLength = strlen(WordLibrary[Selected]);
    char Guess[1] = {'\0'};
    char CorrectlyPicked[WordLength];
    strset(CorrectlyPicked, '\0');

    int Completion = WordLength;
    bool GotOne = false;
    bool GotAlready = false;

    SetColor(WHITE);
    PlaceText("- Hangman -", 30, 0);
    DrawBox(15, 15, 50, 2);
    PlaceCursor(0, 2);
    printf("Word: ");

    PlaceCursor(0, 3);
    printf("Victory status: FALSE");

    for (int x = 0; x < WordLength; x++) { PlaceCursor(x + 6, 2); printf("_"); }

    while (true) {

        PlaceCursor(0, 1);
        printf("Your guess: _\b");
        scanf("%c", &Guess[0]);
        _flushall();

        for (int x = 0; x < WordLength; x++) {

            if (Guess[0] == Word[x]) {
                if (CorrectlyPicked[x] == Word[x]) { GotAlready = true; break; }
                CorrectlyPicked[x] = Word[x];

                PlaceCursor(x + 6, 2);
                printf("%c", Word[x]);
                Completion--;
                GotOne = true;
                break;
            }
        }

        if (GotOne == false && GotAlready == false) { miss++; MissedLetter(miss); }
        else { GotOne = false; GotAlready = false; }

        if (Completion == 0) { break; }
    }

    PlaceCursor(0, 3);
    printf("Victory status:");
    SetColor(RED);
    printf(" TRUE ");
    cin.get();
    return 0;
}
Discord moderator
Reply
#2
This thread caught my attention thank you for sharing em here. I'm sure alot of people would appreciate this. Because people learn form there mistakes
[Image: BezlSXT.gif]
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)