- 19th Oct 2022
- 05:59 am
- Admin
Student's C++ Programming Solution on Structural/Behavioral Requirements
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include"baseball.h"
#include"game.h"
#include"wordGuess.h"
using namespace std;
int main()
{
srand(time(0));
int won = 1;
fstream fp;
list gameData;
vector 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< 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 guessWord;
vector wordToGuess;
vector 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 {
wordToGuess.push_back(temp2[i]);
}
while (1)
{
char guessTemp2;
won = 0;
cout << "[" << tempAttempts << "] Your Guess [";
for (int i = 0; i {
int found = 0;
for (int j = 0; 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 {
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 {
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 ::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;
}
Programming Assignment Expert's - paraphrased version of the provided C++ code
#include
#include
#include
#include
#include
#include
#include "baseball.h"
#include "game.h"
#include "wordGuess.h"
using namespace std;
int main()
{
srand(time(0));
int won = 1;
fstream fp;
list gameData;
vector 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)
{
// ... (code for the BaseBall game)
}
else if (choice == 2)
{
// ... (code for the WordGuess game)
}
else if (choice == 3)
{
// ... (code to display game statistics)
}
else
{
cout << "Wrong Choice";
}
}
return 0;
}