Login
Order Now
Support
C++ Program on Structural/Behavioral Requirements

C++ Program on Structural/Behavioral Requirements

  • 19th Oct, 2022
  • 18:29 PM

#include <iostream>
#include<string.h>
#include<list>
#include<time.h>
#include<stdlib.h>
#include<iterator>
#include<vector>
#include<fstream>
#include<sstream>
#include<time.h>

#include"baseball.h"
#include"game.h"
#include"wordGuess.h"

using namespace std;

int main()
{
    srand(time(0));
    int won = 1;
    fstream fp;
    list<game> gameData;
    vector<string> word;

    
    fp.open("wordPool.txt", ios::in);
    if (fp.is_open())
    {

        string op;
        while (getline(fp, op))
        {
            word.push_back(op);
        }
    }
    else
    {
        cout << "File can't be open";
    }
    while (1)
    {
        int choice;
        int randomNumber;
        int tempAttempts;
        int guess;
        string guessTemp;
        string temp;
        cout << "\n0.Exit";
        cout << "\n1.BaseBall";
        cout << "\n2.WordGuess";
        cout << "\n3.Stats";
        cout << "\nEnter your choice : ";
        cin >> choice;
        if (choice == 0)
            exit(0);
        else if (choice == 1)
        {


            tempAttempts = 1;
            while (1)
            {
                randomNumber = (rand() % (998 - 100 + 1)) + 100;


                temp = to_string(randomNumber);
                

                
                if ((temp[0] != temp[1]) && (temp[1] != temp[2]) && (temp[2] != temp[0]))
                {
                    break;
                }
            }
            cout << "\nDebug Key : " << temp<<endl;
            while (1)
            {
                int ball = 0;
                int strike = 0;
                cout << "\n[" << tempAttempts << "] Your Guess : ";
                cin >> guess;
                guessTemp = to_string(guess);
                
                if (guessTemp[0] == temp[0])
                    strike++;
                else
                {

                    if (guessTemp[0] == temp[1])
                        ball++;
                    if (guessTemp[0] == temp[2])
                        ball++;
                }
                if (guessTemp[1] == temp[1])
                    strike++;
                else
                {
                    if (guessTemp[1] == temp[2])
                        ball++;
                    if (guessTemp[1] == temp[0])
                        ball++;
                }
                if (guessTemp[2] == temp[2])
                    strike++;
                else
                {
                    if (guessTemp[2] == temp[0])
                        ball++;
                    if (guessTemp[2] == temp[1])
                        ball++;
                }
                if ((guessTemp[0] == temp[0]) && (guessTemp[1] == temp[1]) && (guessTemp[2] == temp[2]))
                {
                    cout << "\n  B - " << ball << ",  S - " << strike;
                    cout << "\nStrike Out!!! " << tempAttempts << " Attempts";
                    baseball bobj("BaseBall", tempAttempts);
                    gameData.push_back(bobj);
                    break;
                }
                cout << "\n  B - " << ball << ",  S - " << strike;
                tempAttempts++;
            }


        }
        else if (choice == 2)
        {

            vector<char> guessWord;
            vector<char> wordToGuess;
            vector<char> total;
            char p[26] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
            
            for (int i = 0; i < 26; i++)
            {
                total.push_back(p[i]);

            }
            tempAttempts = 1;
            cout << "\nPlay WordGuess\n";
            try
            {
                randomNumber = (rand() % ((word.size() - 1) - 0 + 1)) + 0;
            }
            catch (exception ex)
            {

            }
            string temp2 = word[randomNumber - 1];
            cout << "\nDebug Key : " << temp2 << endl;
            for (int i = 0; i<temp2.length(); i++)
            {
                wordToGuess.push_back(temp2[i]);
            }

            while (1)
            {
                char guessTemp2;
                won = 0;

                cout << "[" << tempAttempts << "] Your Guess [";
                for (int i = 0; i<wordToGuess.size(); i++)
                {
                    int found = 0;
                    for (int j = 0; j<guessWord.size(); j++)
                    {
                        if (wordToGuess[i] == guessWord[j])
                            found = 1;
                    }
                    if (found == 0)
                    {
                        won = 1;
                        cout << "*";
                    }
                    else
                    {

                        cout << wordToGuess[i];
                    }
                }
                cout << "] choose : [";
                for (int i = 0; i<total.size(); i++)
                {
                    cout << total[i];
                }
                cout << "]";
                if (won == 0)
                {
                    cout << "\n You Won ";
                    wordGuess wobj("wordGuess", tempAttempts - 1);
                    gameData.push_back(wobj);
                    break;
                }
                cin >> guessTemp2;
                int flag = 0;
                for (int i = 0; i<total.size(); i++)
                {
                    if (total[i] == guessTemp2)
                    {
                        total[i] = '*';
                        flag++;
                    }

                }
                if (flag == 0)
                {
                    cout << "\n" << guessTemp2 << " Already Selected \n";
                }
                else{ guessWord.push_back(guessTemp2); }
                tempAttempts++;

            }
        }
        else if (choice == 3)
        {
            list <game> ::iterator it;
            cout << "Game \t\t Attempts\n";
            int j = 0;
            float tot = 0;
            float avg;
            for (it = gameData.begin(); it != gameData.end(); ++it)
            {
                j++;
                int temp = it->getAttempts();
                string name = it->getGameName();
                tot += temp;

                cout << "\n[" << j << "]" << name << "\t" << temp;

            }
            
            avg = tot / j;
            cout << "\n\nCombined Total Attempts : " << tot << " , Average attempts per game : " << avg;
        }

        else
        {
            cout << "Wrong Choice";
        }
    }
    return 0;
}
 

Share this post

assignment helpassignment helperassignment expertsassignment writing services