-
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!");
-