Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
25 views14 pages

Cricket Project

Download as txt, pdf, or txt
Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1/ 14

Hand Cricket Game Project Summary

This Java program simulates a hand-cricket game where the player competes against
the computer. The game involves batting and bowling phases, and the player’s goal
is to outscore the computer. The specifications and functionality are listed below:
1. Game Introduction:
* The program starts by welcoming the player and asking for their name.
* Basic game instructions are provided to the player, explaining the rules and how
to play the game.
2. Game Phases:
* Batting Phase:
* The player starts by batting and is prompted to enter a number between 1 and 10.
* The computer generates a random number in the same range.
* If the player’s number matches the computer’s number, the player is declared
“out.”
* The player’s score is the total sum of the numbers entered before getting out.
* Bowling Phase:
* After batting, the player bowls by again entering numbers between 1 and 10.
* The computer continues to generate random numbers.
* If the player’s number matches the computer’s number, the computer is declared
“out.”
* If the computer’s total score surpasses the player’s score, the computer wins.
3. Scoring and Results:
* The player’s score and the computer’s score are tracked throughout the game.
* After both the batting and bowling phases are completed, the scores are compared.
* The program announces whether the player won, the computer won, or if it’s a tie.
4. Input Validation:
* The player is restricted to entering numbers between 1 and 10.
* If the player enters an invalid number (outside the 1-10 range), the game ends
with a “cheat” detection, resulting in an automatic loss.
5. End Game Options:
* After each game, the player is given the option to play again or quit.
* If the player chooses to replay, the game resets and starts over.
* If the player chooses to quit, the program terminates with a goodbye message.
6. User Interaction:
* The game runs in a loop, allowing continuous play until the player decides to
exit.
* Clear instructions and feedback are provided at each stage of the game to guide
the player.
________________
Class Description
The Playing_Hand_Cricket_Against_Computer class simulates a simple hand-cricket
game where a human player competes against the computer. This class encapsulates
all the logic required to manage the flow of the game, handle user inputs, generate
random computer moves, and display the results.
Key Features:
1. Game Introduction & Instructions: The class begins by welcoming the player and
providing detailed instructions on how to play the game, including the rules and
how scores are calculated.
2. Player's Batting Phase:
* The player is prompted to input a number between 1 and 10.
* The computer generates a random number in the same range.
* If the player’s number matches the computer's number, the player is declared out,
and their total score is calculated.
* If an invalid number (outside 1-10) is entered, the game detects it as cheating
and terminates the game.
3. Computer's Batting Phase:
* After the player’s batting phase, roles reverse, and the player bowls by entering
a number between 1 and 10.
* The computer continues to bat, accumulating its score.
* The goal for the player is to guess the computer's number to get it out before it
exceeds the player’s score.
4. Scoring & Game Conclusion:
* The game compares the final scores of both the player and the computer.
* Based on the scores, the game announces the winner, whether it's the player, the
computer, or if the game ends in a tie.
5. Replay Option:
* After a game session concludes, the player is given the option to either replay
the game or exit.
* The game runs in a loop, allowing multiple sessions without restarting the
program.
6. Error Handling:
* The class includes mechanisms to detect invalid inputs (cheating) and handle them
by ending the game with an appropriate message.
________________
Variable Description

________________
Program Execution
* Step 1: Initial Setup
* The program begins by creating an instance of InputStreamReader and
BufferedReader to read input from the player.
* The player’s name is collected, and initial greetings along with game
instructions are displayed.
* Step 2: Batting Phase
* The player is asked to enter a number between 1 and 10.
* The program generates a random number for the computer’s move using
Math.random(), converting it to an integer (aa).
* If the player’s number matches the computer’s number, the player is out, and
their final score (score1) is displayed.
* If the player’s number does not match the computer’s number, the score is added
to the player’s total score (score1).
* If the player enters an invalid number (outside 1-10), the game detects a "cheat"
and ends the current session.
* Step 3: Bowling Phase
* After the player is out, the program moves to the bowling phase where the roles
are reversed.
* The player now tries to bowl by entering a number, and the computer generates a
random number for its move.
* If the player’s number matches the computer’s number, the computer is out,
and its final score (score2) is displayed.
* If the computer’s total score exceeds the player’s score, the computer wins, and
the game ends.
* Similar to the batting phase, entering an invalid number leads to the game
detecting a "cheat" and ending.
* Step 4: Result Announcement
* After both the player and computer have completed their turns, the scores are
compared.
* The program announces whether the player won, the computer won, or if it’s a tie
based on the scores.
* Step 5: Replay or Exit
* The program asks the player if they want to play again. If the player enters 1,
the game resets, and a new game begins.
* If the player enters any other number, the game exits, displaying a goodbye
message.
________________
Problem Definition
Objective:
Design and implement a Java console application that simulates a hand cricket game
between a human player and the computer. The game should mimic the basic rules of
the traditional hand cricket game played by children.
Requirements:
1. Game Phases:
* Batting Phase:
* The player bats first, entering numbers between 1 and 10.
* The computer generates a random number between 1 and 10 as its bowling input.
* If the player's number matches the computer's number, the player is declared out.
* The player's score is the sum of all numbers entered before getting out.
* Bowling Phase:
* After the player is out, the player bowls by entering numbers between 1 and 10.
* The computer bats, generating random numbers between 1 and 10.
* If the player's number matches the computer's number, the computer is declared
out.
* The computer's score is the sum of all its numbers entered before getting out.
* The computer wins if its score exceeds the player's score before getting out.
2. User Interaction:
* The game should greet the player and ask for their name.
* Display clear instructions about how to play the game.
* After each phase, display the current score and the final result (whether the
player or the computer won).
* After the game ends, provide an option for the player to replay or quit the game.
3. Input Validation:
* The player should only be allowed to enter numbers between 1 and 10.
* Any invalid input (e.g., numbers outside the range or non-numeric input) should
be flagged, and the game should handle it gracefully (e.g., by terminating the game
with a message).
4. Randomness:
* The computer's numbers should be generated randomly to ensure unpredictability
and fairness in the game.
5. Scoring and Result:
* The program should accurately calculate and display the player's and the
computer's scores.
* After both phases, the program should compare the scores and declare the winner
or announce a tie if the scores are equal.
6. Replay Option:
* After a game ends, prompt the player to play again or quit.
* If the player chooses to play again, restart the game with a fresh score.
Constraints:
* The game should be a console application.
* The game should loop continuously until the player decides to quit.
* The program should handle edge cases like invalid inputs and ensure a smooth user
experience.
Expected Outcome:
A functional Java console application that successfully simulates a hand cricket
game, allowing the player to compete against the computer, with a clear display of
scores, results, and an option to replay the game.
________________
SOURCE CODE
import java.io.*;

public class Playing_Hand_Cricket_Against_Computer {


public static void main(String args[]) throws IOException {
// Set up for input reading
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
// Variable declarations
String name;
int aa, ii, bb, cheat, score1, score2, c, choice, infinity = 1;
double rr;

// Main game loop


while (infinity > 0) {
// Resetting variables
name = "";
aa = ii = bb = cheat = score1 = score2 = c = choice = 0;

// Welcome Message
System.out.println("=======================");
System.out.println("| WELCOME TO THE GAME |");
System.out.println("=======================");
System.out.println("");
System.out.print("Enter Your Name: ");
name = br.readLine();

// Print game instructions


for (c = 0; c <= 2; c++) {
System.out.println("====================================");
}

System.out.println("Hello " + name + ",");


System.out.println(".");
System.out.println("WELCOME TO THE CRICKET-MANIA");
System.out.println(".");
System.out.println("Instructions:");
System.out.println(".");
System.out.println("This Game Is Similar To The Hand-Cricket Game
Played By Kids.");
System.out.println(".");
System.out.println("There Is No Limit On The Number Of Balls.");
System.out.println(".");
System.out.println("You Will First Bat. Enter Any Number From 1 To
10.");
System.out.println(".");
System.out.println("Enter The Numbers Carefully Without Making Any
Mistake.");
System.out.println(".");
System.out.println("If The Number Entered By The Computer Is Same As
The Number Entered By You,");
System.out.println("You Will Be Declared As Out.");
System.out.println(".");
System.out.println("Your Final Score Will Be The Sum Of The Numbers You
Entered Before Getting Out.");
System.out.println(".");
System.out.println("After Getting Out, You Will Have To Bowl.");
System.out.println(".");
System.out.println("Try To Enter A Number Similar To The Number Entered
By The Computer.");
System.out.println(".");
System.out.println("If The Sum Of Numbers Entered By The Computer
Becomes More Than Your Score, It Will Win.");
System.out.println(".");
System.out.println("But If You Enter A Number Same As The Number
Entered By The Computer, You Can Win!");
System.out.println(".");

for (c = 0; c <= 2; c++) {


System.out.println("====================================");
}

// Batting phase
System.out.println("YOU ARE BATTING:");
for (c = 0; c <= 2; c++) {
System.out.println("====================================");
}

// Batting loop
for (ii = 1; ii > 0; ii++) {
System.out.println("");
System.out.print("Enter Your Number: ");
bb = Integer.parseInt(br.readLine());

rr = (Math.random() * 10) + 1;
aa = (int) rr;

System.out.println("");
System.out.println("Computer's Number: " + aa);
System.out.println("**********");
System.out.println("");

if (bb == aa) {
System.out.println(" ");
System.out.println("HOWZATTT!!!!!!");
System.out.println(" ");
System.out.println("################");
System.out.println("YOUR FINAL SCORE = " + score1);
System.out.println("################");
System.out.println("################");
System.out.println(" ");
break;
} else if (bb > 0 && bb <= 10) {
score1 = score1 + bb;
} else {
for (c = 0; c <= 2; c++) {
System.out.println("====================================");
}
System.out.println("You have either tried to cheat or entered a
wrong input. Game Over!");
cheat++;
break;
}
System.out.println("Your Current Score Is " + score1);
System.out.println("");
System.out.println("**********");
}

// Bowling phase
if (cheat > 0) continue;

for (c = 0; c <= 2; c++) {


System.out.println("====================================");
}

System.out.println("YOU ARE BOWLING:");


for (c = 0; c <= 2; c++) {
System.out.println("====================================");
}

// Bowling loop
for (ii = 1; ii > 0; ii++) {
System.out.println("");
System.out.print("Enter Your Number: ");
bb = Integer.parseInt(br.readLine());

rr = (Math.random() * 10) + 1;
aa = (int) rr;

System.out.println("");
System.out.println("Computer's Number: " + aa);
System.out.println("**********");
System.out.println("");

if (aa == bb) {
System.out.println(" ");
System.out.println("COMPUTER IS OUTT!!!!");
System.out.println(" ");
System.out.println("################");
System.out.println("COMPUTER'S FINAL SCORE = " + score2);
System.out.println("################");
System.out.println("################");
System.out.println(" ");
break;
} else if (bb > 0 && bb <= 10) {
score2 = score2 + aa;
} else {
for (c = 0; c <= 2; c++) {
System.out.println("====================================");
}
System.out.println("You have either tried to cheat or entered a
wrong input. Game Over!");
cheat++;
break;
}
if (score2 > score1) {
System.out.println(" ");
System.out.println("THE COMPUTER HAS SCORED MORE THAN
YOUU!!!");
System.out.println("################");
System.out.println("COMPUTER'S FINAL SCORE = " + score2);
System.out.println("################");
System.out.println(" ");
break;
}

System.out.println("Computer's Current Score Is " + score2);


System.out.println("");
System.out.println("**********");
}

// Display final scores and results


if (cheat > 0) continue;

System.out.println(" ");
System.out.println("################");
System.out.println("YOUR FINAL SCORE = " + score1);
System.out.println("COMPUTER'S FINAL SCORE = " + score2);
System.out.println("################");
System.out.println("################");
System.out.println(" ");

if (score1 > score2) {


System.out.println("Congratulations " + name + "!! You Have
Defeated The Almighty, Computer!!");
} else if (score1 < score2) {
System.out.println("Sorry " + name + ", But The Computer Has
Defeated You..!");
} else {
System.out.println("It's a Tie..! :(");
}

// Ask user if they want to play again


System.out.println("");
System.out.println("Enter 1 To Play This Game Again.");
System.out.println("Enter 0 Or Any Other Number To Quit.");
choice = Integer.parseInt(br.readLine());
System.out.println("");

if (choice == 1) {
System.out.print('\f'); // Clear screen
} else {
System.out.print('\f');
System.out.println("Good Bye!");
break;
}
}
}
}

________________
OUTPUT
=======================
| WELCOME TO THE GAME |
=======================

Enter Your Name: John


====================================
====================================
====================================
Hello John,

WELCOME TO THE CRICKET-MANIA

Instructions:

This Game Is Similar To The Hand-Cricket Game Played By Kids.

There Is No Limit On The Number Of Balls.

You Will First Bat. Enter Any Number From 1 To 10.

Enter The Numbers Carefully Without Making Any Mistake.

If The Number Entered By The Computer Is Same As The Number Entered By You,
You Will Be Declared As Out.

Your Final Score Will Be The Sum Of The Numbers You Entered Before Getting Out.

After Getting Out, You Will Have To Bowl.

Try To Enter A Number Similar To The Number Entered By The Computer.

If The Sum Of Numbers Entered By The Computer Becomes More Than Your Score, It Will
Win.

But If You Enter A Number Same As The Number Entered By The Computer, You Can Win!
====================================
====================================
====================================

YOU ARE BATTING:


====================================
====================================
====================================

Enter Your Number: 4

Computer's Number: 7
**********

Your Current Score Is 4


**********

Enter Your Number: 6

Computer's Number: 6
**********

HOWZATTT!!!!!!

################
YOUR FINAL SCORE = 4
################
################

====================================
====================================
====================================

YOU ARE BOWLING:


====================================
====================================
====================================

Enter Your Number: 4

Computer's Number: 8
**********

Computer's Current Score Is 8


**********
Enter Your Number: 5

Computer's Number: 3
**********

Computer's Current Score Is 11


**********

Enter Your Number: 2

Computer's Number: 4
**********

Computer's Current Score Is 15


**********

THE COMPUTER HAS SCORED MORE THAN YOUU!!!

################
COMPUTER'S FINAL SCORE = 15
################

==================
YOUR FINAL SCORE = 4
COMPUTER'S FINAL SCORE = 15
==================

Sorry John, But The Computer Has Defeated You..!

Enter 1 To Play This Game Again.

Enter 0 Or Any Other Number To Quit.

Comprehensive Test Plan


Test Objective:
Verify that the Hand Cricket game program functions correctly and meets the
requirements.
Test Scope:
This test plan covers the entire Hand Cricket game program, including:
1. Game initialization
2. User input handling
3. Game logic (batting, bowling, scoring)
4. Computer opponent AI
5. Game modes (single-player)
6. Error handling
7. User interface (text-based)
Test Environment:
1. Operating System: Windows/Linux/Mac
2. Java Runtime Environment (JRE): 8 or higher
3. IDE: Eclipse/NetBeans/IntelliJ
Test Cases: Positive Test Cases:
1. Successful game initialization
2. User input validation (valid numbers)
3. Correct scoring and display
4. Computer opponent AI (random number generation)
5. Game mode selection (single-player)
6. Error-free gameplay
Negative Test Cases:
1. Invalid user input (non-numeric, out-of-range)
2. Error handling (division by zero, null pointer exceptions)
3. Game logic errors (incorrect scoring, wrong winner)
4. Computer opponent AI errors (non-random number generation)
5. Game mode selection errors (invalid input)
Test Data:
1. Valid user inputs: numbers 1-10
2. Invalid user inputs: non-numeric characters, numbers outside 1-10 range
3. Test game scenarios: different batting and bowling combinations Test Procedures:
1. Run the program and verify game initialization.
2. Test user input validation with valid and invalid inputs.
3. Play multiple games to verify correct scoring and display.
4. Test computer opponent AI with different game scenarios.
5. Verify error handling for invalid inputs and game logic errors.
Test Deliverables:
1. Test report documenting test results
2. List of defects found during testing
3. Recommendations for program improvement
Test Schedule:
1. Test planning: 1 day
2. Test execution: 3 days
3. Test reporting: 1 day
Test Resources:
1. Test engineer
2. Java development environment
3. Test data and scenarios
Risks and Assumptions:
1. Assumption: Program is developed according to requirements.
2. Risk: Insufficient testing may lead to undetected defects.
Test Metrics:
1. Test coverage percentage
2. Defect density
3. Test efficiency
Test Completion Criteria:
1. All test cases executed successfully.
2. No critical defects found.
3. Program meets requirements.
By following this comprehensive test plan, you can ensure that the Hand Cricket
game program is thoroughly tested and meets the required standards.
Limitations
Technical Limitations:
1. Simple AI: The computer opponent's AI is basic and doesn't adapt to user
behaviour.
2. Limited Game Modes: Only one game mode is available.
3. No Multiplayer: No option for multiple users to play together.
4. Text-Based Interface: The interface is text-based, which may not be engaging for
some users.
5. Limited Error Handling: Input validation and error handling are minimal.
Functional Limitations:
1. No Player Profiles: User data and statistics are not stored.
2. Limited Game Settings: No options to customise game settings (e.g., difficulty
level).
3. No Pause/Resume: Game cannot be paused or resumed.
4. No Undo Feature: No option to undo mistakes.
Design Limitations:
1. Basic Graphics: No graphical representation of the game environment.
2. Limited Sound Effects: No sound effects for various game events.
3. No Animations: No animations for batting, bowling, or scoring.
Performance Limitations:
1. Slow Response: Program may respond slowly to user inputs.
2. Memory Consumption: Program may consume excessive memory.
Security Limitations:
1. No Data Encryption: User data is not encrypted.
2. No Authentication: No user authentication mechanism.
Scalability Limitations:
1. Single-Player Focus: Program is designed for single-player experience.
2. Limited Platform Support: Program may not run on all platforms.
Future Development Limitations:
1. Code Complexity: Code structure may become complex with future additions.
2. Maintenance Challenges: Program may require significant refactoring.
Addressing these limitations can enhance the program's overall quality, user
experience, and scalability.
Aims and Objectives
Aim:
To design and develop a simple text-based Hand Cricket game program in Java that
allows users to play against the computer.
Primary Objectives:
1. To create a user-friendly interface for users to interact with the game.
2. To implement game logic for batting and bowling phases.
3. To generate random numbers for a computer opponent.
4. To keep track of user and computer scores.
5. To declare the winner based on game rules.
Specific Objectives:
1. To develop a program that initialises the game and sets up the computer
opponent.
2. To implement input validation for user inputs (numbers 1-10).
3. To display game instructions and rules.
4. To provide options for users to play again or quit.
5. To handle errors and exceptions.
Functional Objectives:
1. To simulate a game of Hand Cricket between the user and computer.
2. To generate random bowling scores for the computer.
3. To determine outs and update scores accordingly.
4. To declare the winner based on final scores.
Non-Functional Objectives:
1. To ensure the program is user-friendly and easy to navigate.
2. To optimise program performance and efficiency.
3. To maintain code readability and modularity.
Learning Objectives:
1. To understand Java programming fundamentals.
2. To apply object-oriented programming concepts.
3. To practise problem-solving and logical thinking.
4. To develop debugging and testing skills.
By achieving these aims and objectives, the Hand Cricket game program demonstrates
a simple yet engaging implementation of a popular game in Java.
Comprehensive Future Scope
Short-term (Next 6-12 months)
1. Enhanced Graphics: Integrate graphics and animations to improve user experience.
2. Multiplayer Support: Allow multiple users to play against each other online.
3. Mobile App Development: Develop mobile apps for Android and iOS.
4. Game Modes: Introduce new game modes (e.g., Limited Overs, T20).
5. Leaderboards: Implement leaderboards to track player rankings.
Mid-term (1-2 years)
1. Artificial Intelligence (AI): Implement AI-powered opponents for a more
challenging experience.
2. Virtual Reality (VR) Integration: Integrate VR technology for immersive
gameplay.
3. Online Tournaments: Organise online tournaments with prizes.
4. Social Media Integration: Allow players to share achievements on social media.
5. In-Game Purchases: Introduce in-game purchases for virtual currency or premium
features.
Long-term (2-5 years)
1. Esports Platform: Establish the game as an esports platform.
2. Machine Learning: Utilise machine learning algorithms to analyse player
behaviour.
3. Cloud Gaming: Transition to cloud gaming for seamless gameplay.
4. Cross-Platform Play: Enable cross-platform play between PC, console, and mobile.
5. Virtual Stadiums: Create virtual stadiums for immersive gameplay.
Technical Advancements
1. Java Upgrade: Upgrade to latest Java versions for improved performance.
2. Database Integration: Integrate databases for player data and statistics.
3. Security Enhancements: Implement robust security measures to prevent cheating.
4. Performance Optimization: Optimise game performance for low-end hardware.
5. Modular Architecture: Refactor code for a modular architecture.
Business Expansion
1. Marketing Strategies: Develop targeted marketing strategies.
2. Partnerships: Collaborate with cricket organisations and brands.
3. Sponsorships: Secure sponsorships for online tournaments.
4. Merchandise: Sell branded merchandise.
5. Expansion to New Markets: Expand to new geographic markets.
By focusing on these areas, the Hand Cricket game program can evolve into a leading
online gaming platform.
Method
This Java program implements a simple text-based Hand Cricket game between a user
and the computer.
Methods:
1. main(String args[]): The entry point of the program. Main Game Loop:
1. setupGame(): Initialises variables, prints game instructions, and asks for
user's name.
2. battingPhase(): User bats, entering numbers between 1-10. Computer generates
random numbers.
1. If the user's number matches computer's, user is out.
2. User's score increments by their entered number.
3. bowlingPhase(): User bowls, entering numbers between 1-10. Computer generates
random numbers.
1. If the computer's number matches user's, computer is out.
2. Computer's score is incremented by its generated number.
4. displayScores(): Shows final scores and declares winner.
5. playAgain(): Asks users if they want to play again.
Helper Methods:
1. printInstructions(): Prints game instructions.
2. printScores(): Displays current scores.
3. generateComputerNumber(): Returns a random number between 1-10.
4. checkOut(): Checks if the user or computer is out.
5. updateScores(): Updates user or computer's score.
Variables:
1. name: User's name.
2. score1, score2: User and computer's scores.
3. aa, bb: Computer and user's numbers.
4. cheat: Flag to check for invalid inputs.
5. choice: User's input to play again.
Game Logic:
1. User bats until out.
2. Computer generates random numbers.
3. User bowls until computer is out or scores more

You might also like