Saturday, February 7, 2015

Guessing Game

//So, I mentioned that I'd be in a programming class this quarter. I've written a few programs since. Here is the first one I've done on my own, without any prompting from my class! (I do hope I can use it for my next exam! XD)
//I wrote this program because I was bored.
//I use the IDE Code::Blocks to write and run these things.
//It's C++
//Here's the guessing game!
//...and yes, some of it is written in German. I wasn't planning on sharing. XD It helped me with my german homework (erste = first, zweite = second, so on and so forth).
//All the comments. You shall find none in the code itself.
//I am CERTAIN there's an easier way to choose something from a pre-created list, I just have no idea how to do it.
//This game is meant to display ten random numbers (between 1-100). The program will pick one of the numbers created. The user will try to guess which number was chosen. They will get 5 chances.

#include <iostream>
#include <iomanip>
#include <time.h>
#include <cstdlib>

using namespace std;

int main()
{
    int erste;
    int zweite;
    int dritte;
    int vierte;
    int funfte;
    int sechste;
    int siebte;
    int achte;
    int neunte;
    int zehnte;
    int guess;
    int answer;
    char playAgain;

    do
    {
        srand(time(0));
        erste = rand() % 100 + 1;

        srand(erste);
        zweite = rand() % 100 + 1;

        srand(zweite);
        dritte = rand() % 100 + 1;

        srand(dritte);
        vierte = rand() % 100 + 1;

        srand(vierte);
        funfte = rand() % 100 + 1;

        srand(funfte);
        sechste = rand() % 100 + 1;

        srand(sechste);
        siebte = rand() % 100 + 1;

        srand(siebte);
        achte = rand() % 100 + 1;

        srand(achte);
        neunte = rand() % 100 + 1;

        srand(neunte);
        zehnte = rand() % 100 + 1;

        cout << "I've chosen one of the following numbers for a game we're going to play: \n";

        cout << erste << endl;
        cout << zweite << endl;
        cout << dritte << endl;
        cout << vierte << endl;
        cout << funfte << endl;
        cout << sechste << endl;
        cout << siebte << endl;
        cout << achte << endl;
        cout << neunte << endl;
        cout << zehnte << endl;

        srand(zehnte);
        answer = rand() % 10 + 1;

        if (answer == 1)
            answer = erste;
        else if (answer == 2)
            answer = zweite;
        else if (answer == 3)
            answer = dritte;
        else if (answer == 4)
            answer = vierte;
        else if (answer == 5)
            answer = funfte;
        else if (answer == 6)
            answer = sechste;
        else if (answer == 7)
            answer = siebte;
        else if (answer == 8)
            answer = achte;
        else if (answer == 9)
            answer = neunte;
        else if (answer == 10)
            answer = zehnte;

        cout << "\n" << "You have 5 chances to guess which number I've chosen. What do you think it is? ";
        cin >> guess;
        cin.clear();
        cin.ignore(100, '\n');

        for (int i = 0; i < 4; i++)
        {
            if (guess != erste && guess != zweite && guess != dritte && guess != vierte && guess != funfte && guess != sechste && guess != siebte && guess != achte && guess != neunte && guess != zehnte)
                {
                    cout << "\n" << "That's not one of the answers, idiot. Try again. You have " << (4 - i) << " chance(s) left! Your guess: ";
                    cin >> guess;
                    cin.clear();
                    cin.ignore(100, '\n');
                }
            else if (guess != answer)
                {
                    cout << "\n" << "WRONG. Try again! You have " << (4 - i) << " chance(s) left! Your guess: ";
                    cin >> guess;
                    cin.clear();
                    cin.ignore(100, '\n');
                }
            else if (guess == answer)
               {
                cout << "\n" << "That's correct, you leg. \n";
                i = 4;
               }
        }

        if (guess != answer)
        {
            cout << "\n" << "You ran out of chances. I WIN, HA-HA. \n";
            cout << "Play again? (Y/N): ";
            cin >> playAgain;
            cin.ignore(100, '\n');
            cin.clear();
        }
        else if (guess == answer)
        {
            cout << " Play again? (Y/N): ";
            cin >> playAgain
            cin.ignore(100, '\n');
            cin.clear();
        }

        cout << "\n" << endl;

    } while (playAgain == 'Y' || playAgain == 'y');

        return 0;
}

No comments:

Post a Comment