I've made mine first Java program and it was Guess a number game and got some great code review, so I've decided to first implement that suggestions and to make a game a bit more complex/advanced.
So now, the user can select between 3 options, 3 different intervals (1-10, 1-20, 1-100), but also, in each interval there are few mines he must avoid.
Eclipse suggested to set private int max;
to bet private static int max;
and some other private int's as well, wasn't sure why?
Let me know if something is not clear in explanation of game.
Here is the code:
Main.java
public class Main {
public static void main(String[] args) {
Game game = new Game();
game.options();
game.playGame();
}
}
Game.java
import java.util.Random;
import java.util.Scanner;
public class Game {
private static int min=1;
private static int max;
private static int numberOfMines;
private int randomNumber;
int mine[] = new int[20];
private int numGuesses;
private int userGuess;
public static int promptForInteger() {
Scanner input = new Scanner(System.in);
while(!input.hasNextInt()) {
System.out.println("Please enter valid number");
input.next();
}
return input.nextInt();
}
public void options(){
int option = 0;
do{
System.out.println("Please select your game: ");
System.out.println("1) Guess number between 1 and 10 with 1 mine.");
System.out.println("2) Guess number between 1 and 20 with 3 mines.");
System.out.println("3) Guess number between 1 and 100 with 20 mines.");
option = promptForInteger();
}while (option == 0 || option > 3);
switch (option) { // set max depending on user selection
case 1: max = 10;
numberOfMines = 0; // one mine
break;
case 2: max = 20;
numberOfMines = 2; // 3 mines
break;
case 3: max = 100;
numberOfMines = 19; // 20 mines
break;
}
createNumberAndMines();
}
public void createNumberAndMines(){
randomNumber = getRandom(); // get random number to guess
askForGuess(); // first user guess can't be mine, so I want to ask for first guess then set mines
for (int i = numberOfMines; i >= 0; i--){
do{ // we don't want to have same number for guessing and mine or first user guess to be a mine
mine[i] = getRandom();
for (int j = i+1; j<19; j++){ //check if generated number is duplicate mine
if (mine[i] == mine[j]){
i++; // if we find that this number is duplicated to previous one we want to do for loop again for this mine[i]
}
}
}while (mine[i] == randomNumber || mine[i] == userGuess);
}
}
public static int getRandom() {
Random rand = new Random();
return rand.nextInt((max - min) + 1) + min;
}
public void playGame() {
for (int numGuesses = 1; true; numGuesses++){
for (int i=numberOfMines; i>=0; i--){// check if user hits mine
if (mine[i] == userGuess){
System.out.println("You lose! You have hit the MINE");
return;
}
}
// tell user how close he is to right answer
if (userGuess < randomNumber){
System.out.println("Too low!");
}else if(userGuess > randomNumber){
System.out.println("Too high!");
}else if(userGuess == randomNumber){
System.out.println("You win! You tried " + numGuesses + " time(s) total");
break;
}
askForGuess();
}
}
public void askForGuess(){
System.out.println("You are guessing random number between 1 and " + this.max + ". Enter your guess: ");
Scanner inputGuess = new Scanner(System.in);
userGuess = Integer.parseInt(inputGuess.nextLine());
}
}
min
andmax
ingetRandom
which is static they need to be as well (orgetRandom
and any other static methods that reference them can be made non static). – forsvarir Jun 16 at 10:03