Guessing Game Java
Guessing Game Java
GUESSING GAME
Group member:
Karam Haggui 200209929
Clarisse Tuyizere 210209398
Rakan Abunamoos 200209330
List of Figures:
1 Picture 2.1: ................................................................................................................................................. 5
2 Picture 2.2................................................................................................................................................... 6
3 Picture 2.3................................................................................................................................................... 7
4 Picture 2.4................................................................................................................................................... 7
5 Picture 2.5................................................................................................................................................... 8
6 Picture 2.6................................................................................................................................................... 9
7 picture 2.7................................................................................................................................................... 9
8 picture 2.8................................................................................................................................................. 10
1|Page
SE 204
Software Construction
GUESSING GAME
Contents
Group member: ............................................................................................................................................. 1
List of Figures:................................................................................................................................................ 1
Chapter 1 Introduction .................................................................................................................................. 2
1. The general concept of the Guessing game .......................................................................................... 2
2. Why use Java as the programming language for the game? ............................................................ 3
3. How to play the guessing game ........................................................................................................ 3
Chapter 2 System Demonstration ................................................................................................................. 4
Chapter 3 Source Code ................................................................................................................................ 10
1. Software System Source Code ............................................................................................................ 10
References ............................................................................................................................................... 20
Chapter 1 Introduction
The guessing game is a popular game that involves one player trying to guess a
secret number or word chosen by another player Or a computer program. The game
usually involves the player making guesses and receiving feedback on whether their
guess is higher or low, or until they reach a predetermined number of guesses
without guessing correctly.
2|Page
SE 204
Software Construction
GUESSING GAME
Java is a popular programming language that is widely used for developing various
types of applications, including games. Here are some reasons why Java might be
a good choice for developing a guessing game.
The guessing game is a game where one player thinks of a secret word, phrase, or
number and the other player(s) tries to guess it. In this guessing game user will run
the program and the game will use random number or alphabet and ask user to
guess it can be played in many different ways, but here is a general concept of how
to play:
1. One player chooses a secret word, phrase, or number and writes it down or
remembers it.
2. The other player(s) take turns guessing what the secret word, phrase, or number
is.
3. After each guess, the player who chose the secret word, phrase, or number gives
a clue about whether the guess was correct or not. For example, they might say
3|Page
SE 204
Software Construction
GUESSING GAME
"higher" or "lower" if the guess was a number, or they might say "you're getting
warmer" or "you're getting colder" if the guess was a word or phrase.
4. The game continues until the player(s) correctly guess the secret word, phrase, or
number.
5. And it has limited guessing time if your time finish it will tell
6. If you choose the correct number or alphabet you will receive message telling that
you guessed the correct word
The game can be played with any number of players and can be adapted to different
themes or variations.
If there is only one player in the guessing game, the game can still be played by
having the player guess a randomly generated number or word. The player would
have to guess the number or word within a certain number of attempts or within a set
time limit.
For example, the game could generate a random number between 1 and 100, and
the player would have to guess the number within 10 attempts. After each guess, the
game would provide a hint such as "higher" or "lower" to help the player narrow
down their guesses.
However, with only one player, the game may not be as interactive or competitive as
it would be with multiple players.
SE 204
Software Construction
GUESSING GAME
4. Credits
5. Exit
1 Picture 2.1:
5. Let’s start with option one Guessing number after choosing 1 the game will
display another screen asking you to select level.
Easy
Medium
Hard
Return to main menu
5|Page
SE 204
Software Construction
GUESSING GAME
2 Picture 2.2
In the above picture, the player tries to guess number in the given Lange
and then the game showed how many tries he left with. From 6 to 8 will
repeat.
6|Page
SE 204
Software Construction
GUESSING GAME
3 Picture 2.3
4 Picture 2.4
SE 204
Software Construction
GUESSING GAME
10. Option 1 easy level for guessing alphabet the alphabet is only in capital
letter
5 Picture 2.5
8|Page
SE 204
Software Construction
GUESSING GAME
6 Picture 2.6
12. Option 4 for credits this option will show the Credits the people who write
the code.
7 picture 2.7
9|Page
SE 204
Software Construction
GUESSING GAME
13. The last one is Exit, after choosing the 5 the game will finish.
8 picture 2.8
Thank you for your time.
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
*/
package com.mycompany.guessing_game;
import java.util.Arrays;
import java.util.Scanner;
import java.util.Random;
/**
*
* @author user
*/
public class Guessing_game {
SE 204
Software Construction
GUESSING GAME
--------------------------------------
Student Name : Rakan Abunamoos
Student ID : 200209330
Course of study : Software Engineering""";
// String textRules;
public static int findIndex(String arr[], String t)
{
// if array is Null
if (arr == null) {
return -1;
}
while(true){
System.out.println("""
MENU
----
1. Guess the Number
2. Guess the Alphabet
3. Highest score
4. Credits
5. Exit
11 | P a g e
SE 204
Software Construction
GUESSING GAME
===>""");
String numAnswer = input.nextLine();
if(numAnswer.equals("1")){
int easyNumChances = 5;
int easyFinalAnswer = rand.nextInt(15)+1;
System.out.println("""
Guess the Number
----------------
Instructions
1. Guess the number based on the given range.
2. You are allowed to make one guess at a time.
3. Each game has 5 chances to guess the correct answer.
4. Once you have used up all your chances, you lose the game.
Good Luck!
Easy:""");
while(true){
if(easyNumChances>0){
System.out.println("Enter guess number from 1 to 15. "+easyNumChances+"
chances left.");
int easyNumAnswer = Integer.parseInt(input.nextLine());
if(easyNumAnswer < easyFinalAnswer){
System.out.println("Wrong! Your guess was too low");
}else if(easyNumAnswer > easyFinalAnswer){
System.out.println("Wrong! Your guess was too high");
}else if(easyNumAnswer == easyFinalAnswer){
easyNumChances--;
System.out.println("Correct! You have won the game with just "+(5-
easyNumChances)+" times");
if(numHighScore>0){
if(5-easyNumChances<numHighScore){
12 | P a g e
SE 204
Software Construction
GUESSING GAME
numHighScore=5-easyNumChances;
}
}else{
numHighScore=5-easyNumChances;
}
break;
}
easyNumChances--;
}else{
System.out.println("You lost the game");
break;
}
}
}
else if(numAnswer.equals("2")){
int mediumNumChances = 4;
int mediumFinalAnswer = rand.nextInt(20)+1;
System.out.println("""
Guess the Number
----------------
Instructions
1. Guess the number based on the given range.
2. You are allowed to make one guess at a time.
3. Each game has 4 chances to guess the correct answer.
4. Once you have used up all your chances, you lose the game.
Good Luck!
Medium:""");
while(true){
if(mediumNumChances>0){
System.out.println("Enter guess number from 1 to 20. "+mediumNumChances+"
chances left.");
int mediumNumAnswer = Integer.parseInt(input.nextLine());
if(mediumNumAnswer < mediumFinalAnswer){
System.out.println("Wrong! Your guess was too low");
}else if(mediumNumAnswer > mediumFinalAnswer){
System.out.println("Wrong! Your guess was too high");
}else if(mediumNumAnswer == mediumFinalAnswer){
mediumNumChances--;
System.out.println("Correct! You have won the game with just "+(4-
mediumNumChances)+" times");
if(numHighScore>0){
if(4-mediumNumChances<numHighScore){
numHighScore=4-mediumNumChances;
}
}else{
13 | P a g e
SE 204
Software Construction
GUESSING GAME
numHighScore=4-mediumNumChances;
}
break;
}
mediumNumChances--;
}else{
System.out.println("You lost the game");
break;
}
}
}
else if(numAnswer.equals("3")){
int hardNumChances = 3;
int hardFinalAnswer = rand.nextInt(40)+1;
System.out.println("""
Guess the Number
----------------
Instructions
1. Guess the number based on the given range.
2. You are allowed to make one guess at a time.
3. Each game has 3 chances to guess the correct answer.
4. Once you have used up all your chances, you lose the game.
Good Luck!
Hard:""");
while(true){
if(hardNumChances>0){
System.out.println("Enter guess number from 1 to 40. "+hardNumChances+"
chances left.");
int hardNumAnswer = Integer.parseInt(input.nextLine());
if(hardNumAnswer < hardFinalAnswer){
System.out.println("Wrong! Your guess was too low");
}else if(hardNumAnswer > hardFinalAnswer){
System.out.println("Wrong! Your guess was too high");
}else if(hardNumAnswer == hardFinalAnswer){
hardNumChances--;
System.out.println("Correct! You have won the game with just "+(3-
hardNumChances)+" times");
if(numHighScore>0){
if(3-hardNumChances<numHighScore){
numHighScore=3-hardNumChances;
}
}else{
numHighScore=3-hardNumChances;
}
break;
14 | P a g e
SE 204
Software Construction
GUESSING GAME
}
hardNumChances--;
}else{
System.out.println("You lost the game");
break;
}
}
}
else if(numAnswer.equals("4")){
break;
}
else{
System.out.println("Please enter a valid value");
}
}
// break; //optional
}
case "2" ->{
while(true){
System.out.println("""
Start Menu
----------
Select a difficulty level
1. Easy
2. Medium
3. Hard
4. Return to main menu
===>""");
String numAnswer = input.nextLine();
if(numAnswer.equals("1")){
int easyAlphChances = 2;
String[] CharArray = {"A", "E", "I", "O", "U"};
String easyFinalAnswer = CharArray[rand.nextInt(5)];
System.out.println("""
Guess the Number
----------------
Instructions
1. Guess the vowel alphabet.
2. You are allowed to make one guess at a time.
3. Each game has 2 chances to guess the correct vowel.
4. Once you have used up all your chances, you lose the game.
Good Luck!
Easy:""");
while(true){
15 | P a g e
SE 204
Software Construction
GUESSING GAME
if(easyAlphChances>0){
System.out.println("Enter a character: "+easyAlphChances+" chances left. what is
the alphabet?");
String easyAlphAnswer = input.nextLine().toUpperCase();
if(findIndex(CharArray,easyAlphAnswer) != -1){
if(!easyAlphAnswer.equals(easyFinalAnswer)){
if( findIndex(CharArray,easyAlphAnswer) <
findIndex(CharArray,easyFinalAnswer)){
System.out.println("Wrong! Your guess was too low");
}else if( findIndex(CharArray,easyAlphAnswer) >
findIndex(CharArray,easyFinalAnswer)){
System.out.println("Wrong! Your guess was too high");
}
easyAlphChances--;
}else{
easyAlphChances--;
System.out.println("Correct! You have won the game with just "+(2-
easyAlphChances)+" times");
if(alphHighScore>0){
if(2-easyAlphChances<alphHighScore){
alphHighScore=2-easyAlphChances;
}
}else{
alphHighScore=2-easyAlphChances;
}
break;
}
}else{
System.out.println("Please enter a valid value");
}
}else{
System.out.println("You lost the game");
break;
}
}
}
else if(numAnswer.equals("2")){
int mediumAlphChances = 4;
String[] CharArray = {"B", "C", "D", "F", "G", "H", "J", "K", "L", "M", "N", "P", "Q", "R",
"S", "T", "V", "W", "X", "Y", "Z"};
String mediumFinalAnswer = CharArray[rand.nextInt(21)];
System.out.println("""
Guess the Number
----------------
Instructions
1. Guess the consenant alphabet.
16 | P a g e
SE 204
Software Construction
GUESSING GAME
Good Luck!
Medium:""");
while(true){
if(mediumAlphChances>0){
System.out.println("Enter a character: "+mediumAlphChances+" chances left.
what is the alphabet?");
String mediumAlphAnswer = input.nextLine().toUpperCase();
if(findIndex(CharArray,mediumAlphAnswer) != -1){
if(!mediumAlphAnswer.equals(mediumFinalAnswer)){
if( findIndex(CharArray,mediumAlphAnswer) <
findIndex(CharArray,mediumFinalAnswer)){
System.out.println("Wrong! Your guess was too low");
}else if( findIndex(CharArray,mediumAlphAnswer) >
findIndex(CharArray,mediumFinalAnswer)){
System.out.println("Wrong! Your guess was too high");
}
mediumAlphChances--;
}else{
mediumAlphChances--;
System.out.println("Correct! You have won the game with just "+(4-
mediumAlphChances)+" times");
if(alphHighScore>0){
if(4-mediumAlphChances<alphHighScore){
alphHighScore=4-mediumAlphChances;
}
}else{
alphHighScore=4-mediumAlphChances;
}
break;
}
}else{
System.out.println("Please enter a valid value");
}
}else{
System.out.println("You lost the game");
break;
}
}
}
else if(numAnswer.equals("3")){
int hardAlphChances = 5;
17 | P a g e
SE 204
Software Construction
GUESSING GAME
String[] CharArray = {"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"};
String hardFinalAnswer = CharArray[rand.nextInt(26)];
System.out.println("""
Guess the Number
----------------
Instructions
1. Guess the alphabet.
2. You are allowed to make one guess at a time.
3. Each game has 5 chances to guess the correct answer.
4. Once you have used up all your chances, you lose the game.
Good Luck!
Hard:""");
while(true){
if(hardAlphChances>0){
System.out.println("Enter a character: "+hardAlphChances+" chances left. what is
the alphabet?");
String hardAlphAnswer = input.nextLine().toUpperCase();
if(findIndex(CharArray,hardAlphAnswer) != -1){
if(!hardAlphAnswer.equals(hardFinalAnswer)){
if( findIndex(CharArray,hardAlphAnswer) <
findIndex(CharArray,hardFinalAnswer)){
System.out.println("Wrong! Your guess was too low");
}else if( findIndex(CharArray,hardAlphAnswer) >
findIndex(CharArray,hardFinalAnswer)){
System.out.println("Wrong! Your guess was too high");
}
hardAlphChances--;
}else{
hardAlphChances--;
System.out.println("Correct! You have won the game with just "+(5-
hardAlphChances)+" times");
if(alphHighScore>0){
if(5-hardAlphChances<alphHighScore){
alphHighScore=5-hardAlphChances;
}
}else{
alphHighScore=5-hardAlphChances;
}
break;
}
}else{
System.out.println("Please enter a valid value");
}
}else{
18 | P a g e
SE 204
Software Construction
GUESSING GAME
default ->{
System.out.println("Please enter a valid value");
}
}
}
}
}
19 | P a g e
SE 204
Software Construction
GUESSING GAME
References
Effective Java book
Head First Java book
www.w3schools.com/java
Www.javapoint.com
www.geeksforgeeks.org
20 | P a g e
SE 204
Software Construction