0% found this document useful (0 votes)
36 views6 pages

CS304P Assignment 2 Solution Fall 2024

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views6 pages

CS304P Assignment 2 Solution Fall 2024

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

CS304P ASSIGNMENT 2 SOLUTION FALL 2024

Due Date: 30-Dec-2024


Total Marks: 20

DO NOT COPY PASTE THE SAME

FOR MORE CORRECT PAID SOLUTION CONTACT


WHATSAPP +923162965677

Assignment Submission Instruction:


Note: Submit your solutions in .cpp file.

SOLUTION

CODE
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

class Score {
private:
int score;

public:
// Constructor
Score(int initialScore) : score(initialScore) {}

// Overload pre-increment for correct guess


Score& operator++() {
++score;
return *this;
}

// Overload pre-decrement for guesses greater than the target


Score& operator--() {
--score;
return *this;
}

// Overload post-decrement for guesses smaller than the target


Score operator--(int) {
Score temp = *this;
score--;
return temp;
}

// Get the current score


int getScore() const {
return score;
}
};

class GuessingGame {
private:
int targetNumber;
Score playerScore;

// Generate a random number between min and max


int generateRandomNumber(int min, int max) {
return rand() % (max - min + 1) + min;
}

public:
// Constructor to initialize the game
GuessingGame() : playerScore(5) {
targetNumber = generateRandomNumber(1, 10);
}

// Simulate the game


void play() {
cout << "Virtual Guessing Game Starts!" << endl;
cout << "The virtual player will guess a number between 1 and 10." << endl;
cout << "Score decreases for incorrect guesses and increases for correct ones." << endl;

int attempt = 1;
while (playerScore.getScore() > 0) {
int guess = generateRandomNumber(1, 10);
cout << "Attempt " << attempt << ": Virtual player guesses " << guess << ". ";

if (guess > targetNumber) {


--playerScore;
if (playerScore.getScore() == 0) {
cout << "Game Over! The virtual player ran out of score." << endl;
} else {
cout << "The virtual player guessed the correct number: " << targetNumber << endl;
}

cout << "The correct number was: " << targetNumber << endl;
cout << "Final score: " << playerScore.getScore() << endl;
cout << "-----------------------------------------" << endl;
}
};

int main() {
srand(static_cast<unsigned>(time(0))); // Seed the random number generator
GuessingGame game;
game.play();

return 0;
}

SCREENSHOT OUTPUT
REGARD - SARIM
WHATSAPP +923162965677

PLEASE NOTE:
Don't copy-paste the same answer.
Make sure you can make some changes to your solution file before
submitting copy paste solution will be marked zero.
If you found any mistake then correct yourself and inform me.
Before submitting an assignment must check your assignment requirement
file.
If you need some help or question about file and solutions feel free to ask.

FOR FREE ASSIGNMENTS SOLUTIONS VISIT

VUAnswer.pk

You might also like