0% found this document useful (0 votes)
71 views20 pages

Guessing Game Java

Uploaded by

Rakan Sami
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)
71 views20 pages

Guessing Game Java

Uploaded by

Rakan Sami
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/ 20

GUESSING GAME

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

1. The general concept of the Guessing game

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.

The guessing game can be implemented in various ways, such as


Guessing a number between a range of numbers, guessing an alphabet, guessing a
color, or guessing a word from a category. The game is often used as a fun way to
challenge players’ cognitive abilities, as they must use their logic and deduction skills
to guess the correct answer within the given constraints.

2|Page

SE 204
Software Construction
GUESSING GAME

2. Why use Java as the programming language for the 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.

 Platform independence: Java is platform-independent, meaning that once


the code is written, it can be run on any platform, whether it is Windows,
Mac, or Linux.
 Large developer community: Java has a large developer community and a
vast amount of resources available online, including libraries and
frameworks that can be used to speed up game development.
 Object-oriented programming: Java is an object-oriented programming
language, which allows developers to write reusable code and organize it in
a logical way.
 Garbage collection: Java has built-in garbage collection, which frees
developers from the need to manually manage memory, making it easier to
write stable, error-free code.
 Scalability collection: Java is a scalable language, meaning that it can be
used to develop small games as well as large, complex games that require
multiple threads and advanced graphics.
 Performance: Java is a high-performance language, thanks to its just-in-
time compiler, which translates Java bytecode to machine code at runtime.
 Java is still used today by game developers because of the attributes listed
above and that is why java remains a recognized programming language.

3. How to play the 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.

Chapter 2 System Demonstration

1. Open the Java code in the integrated development environment(IDE)


We are going to use NetBeans
2. Open the code to start the game
3. After opening the code we run it the game will display a welcome screen
4. With the menus option
MENU
----
1. Guess the Number
2. Guess the Alphabet
3. Highest score
4|Page

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

6. After selecting 1 game will give you instruction


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.
Here is the option one in guessing number which is Easy level

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.

7. Let go with option 2 in guessing number (medium level)

6|Page

SE 204
Software Construction
GUESSING GAME

3 Picture 2.3

8. Option 3 in guessing numbers (Hard level)

4 Picture 2.4

9. Selecting option 2 which is Alphabet


Same as guessing number this menu also have 3 levels (Easy, Medium, and
Hard).
this will repeat between 10 to 12
After the selection level, you will see also instructions:
7|Page

SE 204
Software Construction
GUESSING GAME

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.

10. Option 1 easy level for guessing alphabet the alphabet is only in capital
letter

5 Picture 2.5

11. Let’s see option 3 in the guessing game menu


The highest score for the highest score game keeps track of player’s high
score, which is lowest number of guesses it took them to guess the number
or alphabet. And after it will take you back to main menu.

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.

Chapter 3 Source Code

1. Software System Source Code

/*
* 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 {

public static int numHighScore = 0;


public static int alphHighScore = 0;
public static String credits = """
Student Name : karam haggui
Student ID : 200209929
Course of study : Software Engineering
--------------------------------------
Student Name : Clarisse Tuizere
Student ID : 210209398
Course of study : Software Engineering
10 | P a g e

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;
}

// find length of array


int len = arr.length;
int i = 0;

// traverse in the array


while (i < len) {

// if the i-th element is t


// then return the index
if (arr[i].equals(t)) {
return i;
}
else {
i = i + 1;
}
}
return -1;
}

public static void main(String[] args) {

Scanner input = new Scanner(System.in);


Random rand = new Random();

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

Enter your choice:""");


String answer = input.nextLine();
switch(answer){
case "1" ->{
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 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

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(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

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 "3" ->{
if(numHighScore>0){
System.out.println("Guess the number best Score: "+numHighScore+" chances used
up");
}else{
System.out.println("Guess the number best Score: have not played yet");
}
if(alphHighScore>0){
System.out.println("Guess the alphabet best Score: "+alphHighScore+" chances used
up");
}else{
System.out.println("Guess the alphabet best Score: have not played yet");
}
break; //optional
}
case "4" ->{
System.out.println(credits);
break; //optional
}
case "5" ->{
System.exit(0);
}

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

You might also like