advice(I guess)

Hi guys,

so I seen a challenge online and it looked like a challenge I could get done in 30 minutes to an hour but clearly not :(

I made a hangman program with a sort of AI or computer guesses,pretty much you pick a word in your head with the length asked by the computer,the computer will then try narrow down the words given the information you supply it eventually (hopefully) guessing the word.

so my question(s), First I am happy (I guess) that I got the code working I spent a full day on it 12 hours,I am not happy with the amount of time I spent on it it's kind of demotivating I honestly thought I could have got this done in under an hour or an hour max,is it bad it took me approx 12 hours to complete this task?

second does this code look messy(spaghetti code), to me it does it looks very bloated and personally I thought it wouldn't take 600 lines of code to get it done I expected maybe half that at most,I could have used classes but opted not to and just use functions instead

some functions are badly written and had to write other functions that accomplished the same or similar task,also I personally think my naming of variables are pretty weak and don't describe what exactly they are doing(in other words pretty vague), also I may have too many variables which may lead to confusion,

as mentioned I am happy the code is working(for now not fully finished) after all the time I spent on it but not really happy with the outcome,but I guess I can learn from my mistakes,

please tell me your advice please don't be too harsh lol

thanks

(I used pastebin as code well exceeded 9000 char limits) link below

https://pastebin.com/Kg69U0TC
Are you saying that it actually works? Can it ever guess the word "zoo"?

It's pretty repetitive. And your spacing is all over the place (3 space, 4 spaces, 5 spaces).

Overall I imagine the code should be 100 lines or so, assuming your AI strategy is to either guess letters in the order of the alphabet or from a string storing them in order of frequency.

A couple of points that your compiler should complain about (mine did):
* You dynamically allocate a vector on line 361 that you do nothing with. You don't even delete it.
* guess should have a final return statement, perhaps returning 0.
This happens all the time in the industry too. You think something looks terribly easy, and you find a complexity you didnt know about or a bug that takes a while to fix and your quick effort becomes a major one. There is probably nothing to see here, but some ideas..

some of this is probably true, others not:

- you overestimated your ability
- you underestimated the problem (this seems likely; Ive programmed for decades and I CAN write a program in half an hour, but most of those are grabbing stuff I already have and gluing it together or very simple programs, I make a lot of throw-away parsers for work spew rather than muddle through it by hand)
- you injected a bug that was difficult to find and fix (did you learn anything along the way? Some types of errors are very hard to find and fix, like pointer goofs, which is why people avoid pointers when not necessary)
- you got 'into' the program and added extra features that were not originally in scope
- your design/algorithm/something was complicated without any benefits for this cost
- you estimate was for someone else who you wanted to please

There are others but those are some of the top things I see when estimates are missed.

99% of missed estimates where I am are due to failing to understand the complexity of the problem. Basically goes like this... management/customer/etc asks you to do X, and in the same breath they want an estimate on how long X will take by monday of next week and you know just about zero about X. You have to figure out how long a multi month project that you know very little about will take 20 people to do (vacations, sick leave, people problems) ... and the answer they want is the debugged, go-live version of the program, so every issue found and fixed, every bug fixed, performance good, and so on. If you fudge it too much and say 20 years, you look bad to management. If you say 10 months and it takes 20, you look incompetent. Its pure often pure guesswork!

The best I can give you is to know thyself. Know how long it takes YOU to do things completely and correctly. Then when asked, double what you think it will really take, offer that.
Last edited on
Here's an example of writing something similar in less code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <limits>
#include <cctype>
using namespace std;

class Scaffold {
    string       word;
    vector<bool> showing;
    int          lettersLeft;
    string       letters      = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    constexpr static int index(char ch) {
        int i = toupper(ch) - 'A';
        return i < 0 || i >= 26 ? -1 : i;
    }

public:
    Scaffold(const string& w)
        : word(w), showing(w.size()), lettersLeft(w.size()) {
        for(char& ch: word) {
            if (isalpha(ch)) // ignoring non-alpha for now
                ch = toupper(ch);
        }
    }

    bool solved() const { return lettersLeft == 0; }

    void show() const {
        cout << '\n';
        for (size_t i = 0; i < word.size(); ++i)
            cout << (showing[i] ? word[i] : '_') << ' ';
        cout << "    " << letters << '\n';
    }

    int test(char ch) {
        int nfound = 0;
        ch = toupper(ch);
        int i = index(ch);
        if (i >= 0 && letters[i] != '.') {
            letters[i] = '.';
            for (size_t i = 0; i < word.size(); ++i)
                if (word[i] == ch) {
                    showing[i] = true;
                    ++nfound;
                }
        }
        lettersLeft -= nfound;
        return nfound;
    }
};

void skipline(istream& in) {
    in.ignore(numeric_limits<streamsize>::max(), '\n');
}

string chooseWord() {
    string word;
    cout << "Word: ";
    cin >> word;
    skipline(cin);
    return word;
}

int guessWord(Scaffold& scaffold) {
    string letters {"ETAOINSRHDLUCMFYWGPBVKXQJZ"};
    int iguess = 0;
    while (!scaffold.solved() && iguess < 26) {
        scaffold.show();
        char guess = letters[iguess++];
        cout << "\nGuess #" << iguess << ": " << guess;
        skipline(cin); // pause
        int n = scaffold.test(guess);
        if (n > 0)
            cout << "There " << (n > 1 ? "were " : "was ") << n << ' '
                 << guess << (n > 1 ? "'s" : "") << ".\n";
    }
    scaffold.show();
    return iguess;
}

int main() {
    Scaffold scaffold(chooseWord());
    int result = guessWord(scaffold);
    if (result < 26)
        cout << "\nGot it in " << result << " guesses.\n";
    else
        cout << "\nDidn't guess it.\n";
}

Last edited on
Sometimes it's useful to think first how it should work - a kind of use scenario.

Comp => How long is your word ? 
User=> 5
// guessWord = "_____"
Comp => Does it contain 'a'  
User => yes
Comp => At what location(s) 1-5
User=>1
// guessWord = "a____"
Comp => Does it contain 's'
User => no
Comp => Does it contain 'p'  
User => yes
Comp => At what location(s) 1-5
User=> 2 3
// guessWord = "app__"
Comp => Does it contain 'e'  
User => yes
Comp => At what location(s) 1-5
User=> 5
// guessWord = "app_e"
Comp => Does it contain 'l'  
User => yes
Comp => At what location(s) 1-5
User=> 4
Comp => I solved it. The word is apple.
Hey Tpb

Are you saying that it actually works? Can it ever guess the word "zoo"?



yes it can indeed tested it



A couple of points that your compiler should complain about (mine did):
* You dynamically allocate a vector on line 361 that you do nothing with. You don't even delete it.
* guess should have a final return statement, perhaps returning 0.


true good point I actually forgot to remove that vector.

and wow your code is much cleaner and elegant,that makes me disheartened lol but does it implement an AI which can choose (kind of dynamically) a letter based on the info you give the program?

and true Jonnin I think it was a combination of many of them factors.

thanks Thomas I will look in to it :)

thanks guys
@tpb

my compilers throwing up this error with your code

main.cpp|18|error: body of constexpr function 'static constexpr int Scaffold::index(char)' not a return-statement|
I removed the constexpr, I like your program it's clean very presentable but the only thing that I would try to implement would be for it to be more dynamic ie don't give the word to the program instead have the word in your head,you just supply information about the word.
Topic archived. No new replies allowed.