0% found this document useful (0 votes)
22 views

Oop Krishna

Uploaded by

Kanehaul
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)
22 views

Oop Krishna

Uploaded by

Kanehaul
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/ 18

A

Project Report
On
“Guess the Number“

by
Krishna Marfatia
2023-B-29082005
(1st year, CSE, Semester -II, Division-C)
School Of Engineering
Ajeenkya D.Y Patil University, Pune

Under the supervision of


Dr. Aarti Kothari
Contents

• Abstract

• Chapter-1
Introduction
1. Usage Introduction
2. Function Overview
3. Methodology
4. Advantages of the project

• Chapter -2
Sample code

• Chapter-3
Screenshot

• Chapter-4
1. Conclusion
2. Reference
Abstract :- The Guessing Game is a simple yet engaging interactive application that pits a
human player against a computer in a challenge of deduction and intuition. The objective of
the game is for the human player to correctly guess a randomly generated number chosen by
the computer within a limited number of attempts. The game leverages object-oriented
programming concepts, including polymorphism and inheritance, to facilitate player
interactions and manage game logic.The core components of the game include the Player
class hierarchy, comprising a base class (Player) and two derived classes (HumanPlayer and
ComputerPlayer). HumanPlayer allows user input for guesses via standard input, while
ComputerPlayer generates a random secret number upon initialization. The Game class
orchestrates the gameplay by controlling the interaction between the human player and the
computer player, enforcing the maximum number of attempts, and providing feedback on
each guess. During gameplay, the human player iteratively makes guesses, receiving
feedback from the computer player about the correctness of each guess. If the human player
guesses the correct number within the allotted attempts, they are congratulated for their
success. Conversely, if the human player exhausts all attempts without guessing correctly, the
game reveals the secret number chosen by the computer, concluding the game session.The
Guessing Game offers an entertaining and educational experience, showcasing fundamental
programming concepts such as abstraction, encapsulation, and inheritance in a practical
context. Additionally, it provides an opportunity for players to exercise logical reasoning and
problem-solving skills while enjoying a fun and challenging gaming experience.
CHAPTER-1

INTRODUCTION
The Guessing Game represents a classic example of interactive entertainment and
computational problem-solving within the realm of computer programming. Rooted in the
principles of object-oriented design and gameplay mechanics, this game encapsulates the
essence of user engagement, decision-making, and algorithmic implementation. In this game,
players are invited to embark on a journey of deduction and intuition as they attempt to
uncover a hidden secret number chosen by the computer. Through a series of iterative
guesses, players navigate the space of possible solutions, leveraging feedback from the
system to refine their hypotheses and converge towards the elusive answer.By embracing
object-oriented programming paradigms, the Guessing Game introduces players to the
concept of abstraction through the delineation of player roles (HumanPlayer and
ComputerPlayer) and the encapsulation of game logic within the Game class. Furthermore,
inheritance mechanisms facilitate code reuse and modularity, enabling the seamless
integration of diverse player strategies and game variations. Beyond its entertainment value,
the Guessing Game serves as a pedagogical tool for aspiring programmers, offering insights
into algorithmic thinking, user interface design, and software architecture. Through
interactive gameplay and hands-on experimentation, players gain practical experience in
problem decomposition, algorithmic optimization, and iterative refinement.As we delve
deeper into the mechanics and implementation of the Guessing Game, we uncover a rich
tapestry of computational concepts and interactive dynamics, culminating in a captivating and
intellectually stimulating gaming experience. Join us as we unravel the mysteries of the
Guessing Game and explore the intersection of code, creativity, and human ingenuity.

(1)Usage Introduction :-
• To start the game, simply run the executable or compile the source code provided.
• Upon launching the game, you'll be greeted with a prompt inviting you to guess a
randomly chosen number between 1 and 1000.
• Enter your guess using the keyboard and press Enter to submit.
• Receive immediate feedback from the computer player, who will inform you whether
your guess is too high, too low, or correct.
• Continue making guesses until you correctly identify the secret number or exhaust the
allotted number of attempts.
• Celebrate your victory upon guessing the number or receive the solution if you're unable
to guess within the specified attempts.
(2)Function Overview :-
• ‘makeGuess()’ - A pure virtual function that requires implementation in derived classes,
dictating how guesses are made
• Instantiates the ‘Game’ object with a specified number of maximum attempts (e.g., 10).
• Calls the ‘start()’ method to begin the game

(3)Methodology :-

The methodology for developing and operating the Guessing Game encompasses several
key areas: software design, game mechanics, and user interaction. This section outlines
the structured approach used in creating the game, focusing on the principles of object-
oriented programming (OOP), user experience design, and iterative development.

1. Object-Oriented Programming (OOP)


Design Principles:

• Abstraction: Abstract base classes and pure virtual functions (e.g.,


Player::makeGuess()) define what operations can be performed, which is critical for
defining a uniform interface in derived classes.
• Encapsulation: Each class encapsulates specific behaviors and data necessary for its
operations, minimizing interdependencies and enhancing maintainability.
• Inheritance: Deriving HumanPlayer and ComputerPlayer from Player promotes code
reuse and polymorphic behavior.
• Polymorphism: The game utilizes polymorphism to handle different types of players
under a unified interface, allowing the same function call to behave differently depending
on the player type (human or computer).

Implementation Steps:

• Define player behaviors through a base class and extend functionalities in derived
classes.
• Utilize constructor initializations for setting up game settings and states.
• Implement game logic centrally within the Game class to control the flow and rules.

2. Game Mechanics
Core Mechanics:

• Random Number Generation: The computer player generates a secret number using
rand() seeded by time(0) to ensure randomness.
• Guess Evaluation: Each guess mawith a basic version of the game to establish core
functionalities.
• Incremental Development: Add enhancements and refine the interface based on initial
feedback and testing results.
• Testing: Conduct both unit testing (for individual classes and methods) and integration
testing (for the overall game flow and logic).

User Testing:

• Gather feedback on the game’s usability and enjoyment factor from a sample of users.
• Make adjustments based on real user experiences to improve the interface and game
mechanics.

o The developed system undergoes rigorous testing to ensure its functionality,


reliability, and performance. Test cases are designed to validate each aspect of the
system, including input validation, data integrity, and error handling. Feedback from
users and stakeholders is solicited to identify and address any issues or shortcomings.

(4)Advantages of the project

→ Educational Value: It serves as an excellent educational tool for learning object-


oriented programming concepts such as inheritance, polymorphism, and
encapsulation. Students can study and understand these concepts in a practical
context, which enhances their comprehension and retention.
→ Engaging Learning Experience: The interactive nature of the game makes learning
programming more enjoyable and engaging. Users can actively participate in the
game, which promotes hands-on learning and problem-solving skills.
→ Accessible to Beginners: The game's simplicity makes it accessible to beginners in
programming. It provides a gentle introduction to C++ syntax and basic programming
principles, making it suitable for individuals with little to no prior programming
experience.
→ Versatility: The project can be easily modified and expanded upon to incorporate
additional features or functionality. Developers can experiment with different game
mechanics, user interfaces, or difficulty levels to tailor the game to their preferences
or learning objectives.
CHAPTER -2

Sample code :-

class ComputerPlayer : public Player {


private:
int number;
public:
ComputerPlayer() {
std::srand(std::time(0));
number = std::rand() % 1000 + 1;
}

int getSecretNumber() const {


return number;
}

int makeGuess() override {


return number;
}
};
class Game {
private:
ComputerPlayer computer;
HumanPlayer human;
int maxAttempts;

public:
Game(int attempts) : maxAttempts(attempts) {}

void start() {
int guess;
int attempts = 0;

while (attempts < maxAttempts) {


guess = human.makeGuess();
if (guess == computer.getSecretNumber()) {
std::cout << "Congratulations! You guessed the number!\n";
return;
} else if (guess < computer.getSecretNumber()) {
std::cout << "Too low!\n";
} else {
std::cout << "Too high!\n";
}
attempts++;
}

std::cout << "Sorry, you've used all your attempts. The number was "
<< computer.getSecretNumber() << ".\n";
}};
CHAPTER - 3

Screenshot :-

Figure(1). Start of the game with first guess


Figure(2). Attempt 2
Figure(3). Attempt 3
Figure(4). Attempt 4
Figure(5). Attempt 5
Figure(6). Attempt 6
Figure(7). Attempt 7
Figure(8). Attempt 8
Figure(9). Won the game at Attempt 9
CHAPTER - 4

Conclusion :-
In conclusion, the C++ number-guessing game presented in this report serves as an effective
illustration of object-oriented programming principles and basic game development concepts.
By dissecting the game's structure and mechanics, we've gained insights into how inheritance,
polymorphism, and encapsulation are utilized to create a cohesive and interactive
application.Through the game's execution, players engage in an enjoyable guessing
experience while simultaneously learning about the underlying logic of the program. The
game's simplicity makes it accessible to beginners while providing a foundation for more
complex game development endeavors.Overall, this number-guessing game demonstrates the
versatility and practicality of object-oriented programming in C++, making it a valuable
learning tool for aspiring programmers and game developers alike. As technology continues
to evolve, projects like these serve as stepping stones for building more advanced and
sophisticated applications in the future.

References:-
[1] https://www.cppforschool.com/assignment/library-functions-sol/guess-my-number.html
[2] https://youtu.be/yrtiq3iy4U4?si=9dgFUo5MpDOZqeup
[3] https://www.geeksforgeeks.org/number-guessing-game-in-cpp-using-rand-function/
[4] https://youtu.be/46DC6OSwY2s?si=aCgjkj_SAUkp51FK
[5] https://www.algolist.net/Cpp/Guess_game
[6] https://cplusplus.com/forum/beginner/184935/

You might also like