Skip to content

Commit b0ef637

Browse files
authoredOct 30, 2017
Guess the number Mini game with Java
A simple Console mini game made with java.
1 parent 4f7a82e commit b0ef637

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed
 

‎guess_the_number_minigame.java

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
2+
/**
3+
* Created by yaito on 29/10/2017
4+
*/
5+
6+
import java.io.*;
7+
8+
class guessNumber {
9+
public static void main (String[]args) throws IOException
10+
{
11+
engine run = new engine();
12+
run.menu();
13+
}
14+
}
15+
16+
class engine {
17+
String player_name;
18+
19+
void menu () throws IOException {
20+
boolean on = true;
21+
22+
//Getting player name
23+
BufferedReader reader = new BufferedReader (new InputStreamReader(System.in));
24+
System.out.println("Input your name");
25+
player_name = reader.readLine();
26+
27+
//The game begin here
28+
while (on){
29+
int answer,userAnswer,tries = 0;
30+
System.out.println("\t Menu");
31+
System.out.println("Welcome to the guess the number game, "+player_name+", select the an option:");
32+
System.out.println("\t1. Games Instruction");
33+
System.out.println("\t2. Play");
34+
System.out.println("\t3. Close");
35+
int choice = Integer.parseInt(reader.readLine());
36+
37+
switch(choice){
38+
case 1 : instruction();
39+
break;
40+
case 2 : answer = (int) (Math.random()*5000+1); //Create an random number from 1 - 5000
41+
System.out.println("\nLet the fun begin\nNow guess!\n");
42+
43+
while (tries<15){
44+
userAnswer = Integer.parseInt(reader.readLine());
45+
tries = tries+1;
46+
int end = 15-tries; // tries left to end the game
47+
48+
//if the player is lucky enough to guess the number it enter the if sequence
49+
if (userAnswer == answer){
50+
System.out.println("\nCongratulation "+player_name+", you WON!!!");
51+
System.out.println("You did it on your "+tries+" try\n");
52+
tries = tries+end;
53+
//if the player successfully guess the number, you just add the your current tries with the tries left to end the while loop inmediatly
54+
}
55+
//if the player fail to guess the number after 15 tries
56+
else{
57+
if (tries==15){
58+
System.out.println("\nYou have "+end+" tries left\n");
59+
System.out.println("The number was: "+answer+"\n");
60+
System.out.println("You lose, Try again and better luck next time\n");
61+
tries = tries+end;
62+
//if the player fail guess the number, you just add the your current tries with the tries left to end the while loop
63+
}
64+
65+
//everytime the player guess a number, the game will give a hint, if the number is too low or is too high.
66+
else{
67+
if(userAnswer>answer){
68+
System.out.println("\nToo high, try with a lower number");
69+
System.out.println("You have "+end+" tries left\n");
70+
}
71+
else {
72+
if (userAnswer<answer){
73+
System.out.println("\nToo low, try with a higher number");
74+
System.out.println("You have "+end+" tries left\n");
75+
}
76+
}
77+
}
78+
}
79+
}
80+
break;
81+
case 3 : on = false;
82+
break;
83+
default : System.out.println("Invalid Option \nTry again");
84+
}
85+
86+
}
87+
}
88+
89+
void instruction(){
90+
System.out.println("\nWelcome to the game "+player_name+",\nThese are the rules of the game,\nThe system will choose a random number between 1 to 5000");
91+
System.out.println("You must guess which number it is,\nYou will have 15 tries to guess,");
92+
System.out.println("Each time you guess a number, if it is wrong the system will give you a hint.\nGood Luck!!\n");
93+
}
94+
}

0 commit comments

Comments
 (0)
Please sign in to comment.