Cricket Project
Cricket Project
Cricket Project
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.*;
// 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();
// 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;
// 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(" ");
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 (choice == 1) {
System.out.print('\f'); // Clear screen
} else {
System.out.print('\f');
System.out.println("Good Bye!");
break;
}
}
}
}
________________
OUTPUT
=======================
| WELCOME TO THE GAME |
=======================
Instructions:
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.
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!
====================================
====================================
====================================
Computer's Number: 7
**********
Computer's Number: 6
**********
HOWZATTT!!!!!!
################
YOUR FINAL SCORE = 4
################
################
====================================
====================================
====================================
Computer's Number: 8
**********
Computer's Number: 3
**********
Computer's Number: 4
**********
################
COMPUTER'S FINAL SCORE = 15
################
==================
YOUR FINAL SCORE = 4
COMPUTER'S FINAL SCORE = 15
==================