[英]Number guessing game in JavaFX
I'm currently stuck on a simple program which has a Guess and Reset button for guess a number from 1-100. 我目前停留在一个简单的程序上,该程序具有Guess and Reset按钮,用于猜测1-100之间的数字。 The user is given 5 turns. 给用户5回合。 My current issue is that I can't get my Reset button to correctly function. 我当前的问题是我无法使“ 重置”按钮正常工作。 When it is pressed, rather than generating a new game entirely, it instead just generates a new number but when Guess is then pressed the original number that was generated is used again. 当按下它时,而不是完全生成一个新游戏,它只是生成一个新数字,但是当您按下Guess时,将再次使用生成的原始数字。
I'll include some code snippets where I feel I am making big logic errors. 我将提供一些代码片段,让我感觉自己在犯逻辑错误。
So just my counter and initial random number variables and then the Guess and Reset button 所以只有我的计数器和初始随机数变量,然后是Guess and Reset按钮
int counter = 0;
int num = genRan();
/////////////////////////////////////
guessButton.setOnAction(e -> {
int numToGuess = num;
gameSetUp(guessField, counter, numToGuess);
});
//////////////////////////////////////////////////////////
resetButton.setOnAction(e -> {
System.out.println("Generating new game...");
int newNum = genRan();
int newCounter = 0;
gameSetUp(guessField, newCounter, newNum);
});
My random number generator. 我的随机数生成器。
public int genRan() {
Random rand = new Random();
int numToGuess = rand.nextInt(100) + 1;
System.out.println("number is" + numToGuess);
return numToGuess;
}
And where I think it's all going wrong... 而且我认为这一切都错了...
public void gameSetUp(TextField guessField, int counter, int num) {
int numToGuess = num, maxGuesses = 5;
try {
if (counter != 5) {
int guessNum = Integer.valueOf(guessField.getText());
if (guessNum == numToGuess) {
System.out.println("Correct!");
} else if (guessNum < numToGuess) {
counter++;
System.out.println("You guessed " + guessNum + ", which was too low. " + (maxGuesses - counter)
+ " guesses left!");
} else if (guessNum > numToGuess) {
counter++;
System.out.println("You guessed " + guessNum + ", which was too high. " + (maxGuesses - counter)
+ " guesses left!");
}
}
} finally {
if (counter == maxGuesses) {
System.out.println("\nGame Over...");
}
}
}
I'd appreciate any help at all, thanks. 非常感谢您的帮助。
Here is an example app with the Initial State
and the Game Play State
separated. 这是一个示例应用,其中Initial State
和Game Play State
分开。
import java.util.Random;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
/**
*
* @author Sedrick
*/
public class JavaFXApplication47 extends Application {
TextField guessField = new TextField();
int counter = 0;
int numberToGuess = -1;
@Override
public void start(Stage primaryStage) {
guessField.setPromptText("Press New Game Button!");
Button btnNewGame = new Button("New Game");
btnNewGame.setOnAction(actionEvent ->{
gameSetup();
});
Button btnGuess = new Button("Guess");
btnGuess.setOnAction(actionEvent -> {
gamePlay();
});
VBox root = new VBox();
root.getChildren().addAll(guessField, new HBox(btnNewGame, btnGuess));
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
btnNewGame.requestFocus();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
public void gameSetup()
{
guessField.setPromptText("Enter your guess");//set guessField to zero
counter = 0; //set counter to zero
//set numberToGuess to a new random number
Random random = new Random();
numberToGuess = random.nextInt(100) + 1;
System.out.println("Number to guess: " + numberToGuess);
}
public void gamePlay()
{
int guess = Integer.parseInt(guessField.getText());
if(counter == 5)//Check it counter == 5
{
System.out.println("Game over! You lose!");
guessField.setPromptText("Game over! You lose!");
guessField.setText("");
}
else{
if(guess == numberToGuess)
{
System.out.println("You win");
guessField.setPromptText("Your guess of " + guess + " is correct! You win!");
guessField.setText("");
}
else if(guess < numberToGuess)
{
System.out.println("You guessed to low");
guessField.setPromptText("You guessed to low! Try again.");
guessField.setText("");
counter++;
}
else
{
System.out.println("You guessed to high! Try again.");
guessField.setPromptText("You guessed to high! Try again.");
guessField.setText("");
counter++;
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.