A basic Java guessing game I made to show you how it could be done. You can specify the amount of numbers to guess and shows the total guesses you made to guess the correct number.
I've commented out the "continue" part because I'm too lazy to fix the small error that but if you uncomment the code you will see whats wrong. I suppose i'll fix that later.
Anyway here's the code:
JAVA:
-
import java.util.InputMismatchException;
-
import java.util.Scanner;
-
public class GuessGame {
-
-
int[] secretNumber ;
-
boolean guessedCorrectNumber = false, continueOrExitGame = false, debug = true;
-
int tries = 0, difficulty, rounds = 1;
-
//int difficulty;
-
public static void main
(String[] args
) {
-
GuessGame GG = new GuessGame();
-
GG.usage();
-
GG.playGame();
-
}
-
public void playGame(){
-
Scanner sc =
new Scanner
(System.
in);
-
System.
out.
print("How many numbers you want to guess? ");
-
try{
-
difficulty = sc.nextInt();
-
this.generateSecretNumber(difficulty);
-
Scanner sd =
new Scanner
(System.
in);
-
while(!this.guessedCorrectNumber){
-
System.
out.
print("Guess the secret number: ");
-
-
if(guess.length()> difficulty || guess.length()
-
System.
out.
println("Oops, please type " + difficulty +
" numbers!");
-
}
-
if(guess.length() == difficulty){
-
this.checkSecretNumber(guess);
-
}
-
this.tries++;
-
if(guess.equals(this.guessThisNumber)){
-
this.guessedCorrectNumber = true;
-
-
System.
out.
println("Concratulations! the secret number is indeed " +
this.
guessThisNumber);
-
System.
out.
println("Total tries: " +
this.
tries);
-
-
/*
-
Scanner quitGameChoice = new Scanner(System.in);
-
System.out.print("Again? Y/N: ");
-
String continueOrExit = quitGameChoice.nextLine();
-
while(!continueOrExitGame){
-
if(continueOrExit.equals("n") || continueOrExit.equals("N")){
-
continueOrExitGame = true;
-
System.out.println("Thanks for playing!");
-
System.exit(0);
-
}
-
if(continueOrExit.equals("y") || continueOrExit.equals("Y")){
-
continueOrExitGame = true;
-
guessedCorrectNumber = false;
-
tries = 0;
-
rounds++;
-
System.out.println("Alright! Ready for round " + rounds + "?");
-
System.out.println();
-
this.playGame();
-
}
-
else{
-
System.out.println("Please make a choice, again?: Y/N");
-
continueOrExit = quitGameChoice.nextLine();
-
}
-
}
-
*/
-
}
-
}
-
}
-
catch(InputMismatchException IME){
-
System.
out.
println("That is not a valid number");
-
}
-
}
-
public void generateSecretNumber(int totalNumbers){
-
guessThisNumber = "";
-
secretNumber = new int[totalNumbers];
-
for (int i = 0; i <totalNumbers; i++){
-
int randomNumber =
(int) (0+
Math.
random()*
10);
-
secretNumber[i] = randomNumber;
-
}
-
for(int i = 0; i <secretNumber.length; i++){
-
guessThisNumber +=secretNumber[i];
-
//System.out.print(secretNumber[i]);
-
}
-
if(debug){
-
-
System.
out.
println("Secret number is: " + guessThisNumber
);
-
}
-
System.
out.
println("Alright, the secret number(s) are generated!");
-
-
}
-
public void checkSecretNumber
(String userInput
){
-
int[] compareNumber = new int[userInput.length()];
-
for (int i = 0; i <userInput.length(); i++){
-
compareNumber[i] = userInput.charAt(i);
-
}
-
/*
-
System.out.print(compareNumber[0]-48); // -48?
-
*/
-
for (int i = 0; i <userInput.length(); i++){
-
if(compareNumber[i]-48 <secretNumber[i]){
-
System.
out.
print(compareNumber
[i
]-
48 +
"+ ");
-
}
-
if(compareNumber[i]-48> secretNumber[i]){
-
System.
out.
print(compareNumber
[i
]-
48 +
"- ");
-
}
-
if(compareNumber[i]-48 == secretNumber[i]){
-
System.
out.
print(compareNumber
[i
]-
48 +
"! ");
-
}
-
}
-
-
}
-
public void usage(){
-
System.
out.
println("Welcome to the guessing game, guess the correct number!");
-
System.
out.
println("By: HappyFace - www.engineeringserver.com :: v1.0 initial release");
-
-
System.
out.
println("Legenda: ");
-
System.
out.
println("+ means higher");
-
System.
out.
println("- means lower");
-
System.
out.
println("! means correct");
-
-
}
-
}
Output:
Welcome to the guessing game, guess the correct number!
By: HappyFace - www.engineeringserver.com :: v1.0 initial release
Legenda:
+ means higher
- means lower
! means correct
How many numbers you want to guess? 4
Secret number is: 6303
Alright, the secret number(s) are generated!
Guess the secret number: 6303
6! 3! 0! 3!
Concratulations! the secret number is indeed 6303
Total tries: 1
//edit
Oh and before I forget to mention it.. to disable the secretcode that shows on the screen just change "debug = true" into "debug = false" and the secretcode won't be visible.