Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I have a question regarding my code. It uses JavaFX and GUI, but I can't get a specific value to update. If you copy and
I have a question regarding my code. It uses JavaFX and GUI, but I can't get a specific value to update. If you copy and paste(and run) the two classes I'll paste in below, then you'll notice that the profits/losses value doesn't update after the user wins a match after placing a bet. I was wondering if someone could help me fix this.
//Start of GUI CLASS import javafx.application.Application; import javafx.event.ActionEvent; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Alert; import javafx.scene.control.Button; import javafx.scene.control.TextInputDialog; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; import javafx.scene.paint.Color; import javafx.scene.text.Font; import javafx.scene.text.Text; import javafx.stage.Stage; import java.util.Optional; public class RockPaperScissorsFXGUI extends Application { private ImageView computerMoveImageView, userMoveImageView; private Image rockImage, paperImage, scissorsImage; private Text matchOutcomeText, cWinsText, uWinsText, tieText; private Text betText; private Button rockButton, paperButton, scissorsButton; private HBox labelBox; private Text yourLabel; private Text computerLabel; private RPSGame.Moves userMoveC; private RPSGame game; public double userNumber2; public void start(Stage primaryStage) { // int betAmount = getBetAmount(); // only if completing the extra credit! TextInputDialog betDialog = new TextInputDialog(); betDialog.setHeaderText(null); betDialog.setTitle("Money Maker 3000"); betDialog.setContentText("Would you like to place a bet? Enter\"yes\" to continue..."); Optional userInput= betDialog.showAndWait(); //dialog box to determine if user wants to bet if(userInput.isPresent()) { String userInputString = userInput.get(); if (userInputString.equalsIgnoreCase("yes")) { betDialog.setHeaderText("Go big or go home!"); betDialog.setTitle("Money Maker 3000"); betDialog.setContentText("How much would you like to bet?"); betDialog.getEditor().clear(); Optional userInput2 = betDialog.showAndWait(); if (userInput2.isPresent()) { String userInputString2 = userInput2.get(); userNumber2 = Double.parseDouble(userInputString2); Alert resultAlert = new Alert(Alert.AlertType.INFORMATION); resultAlert.setTitle("Bet Summary: "); resultAlert.setHeaderText(null); resultAlert.setContentText("You've placed down: " + userNumber2); resultAlert.showAndWait(); } } else{ System.out.println("Thank you for your time!"); } } else{ System.out.println("No better input"); } game = new RPSGame(); /* the image and labels for the computer and user move */ rockImage = new Image("rock.jpg"); paperImage = new Image("paper.jpg"); scissorsImage = new Image("scissors.jpg"); computerMoveImageView = new ImageView(rockImage); computerMoveImageView.setVisible(false); // used to make the initial screen layout appear the same as when the game starts userMoveImageView = new ImageView(rockImage); userMoveImageView.setVisible(false); HBox imageBox = new HBox(computerMoveImageView, userMoveImageView); imageBox.setAlignment(Pos.CENTER); imageBox.setSpacing(20); computerLabel = new Text("Computer's Move"); computerLabel.setFont(Font.font("Helvetica", 14)); yourLabel = new Text("Your Move"); yourLabel.setFont(Font.font("Helvetica", 14)); labelBox = new HBox(computerLabel, yourLabel); labelBox.setAlignment(Pos.CENTER); labelBox.setSpacing(30); labelBox.setVisible(false); // /* the results of each match */ matchOutcomeText = new Text(); matchOutcomeText.setFill(Color.GREEN); matchOutcomeText.setFont(Font.font("Helvetica", 20)); // /* the buttons for the user's play */ rockButton = new Button("Play Rock"); rockButton.setOnAction(this::handleUserPlay); paperButton = new Button("Play Paper"); paperButton.setOnAction(this::handleUserPlay); scissorsButton = new Button("Play Scissors"); scissorsButton.setOnAction(this::handleUserPlay); HBox buttonBox = new HBox(rockButton, paperButton, scissorsButton); buttonBox.setSpacing(10); buttonBox.setAlignment(Pos.CENTER); // // /* the game statistics */ cWinsText = new Text("Computer Wins: " + game.getNumOfComputerWins()); cWinsText.setFont(Font.font("Helvetica", 16)); cWinsText.setFill(Color.BLUE); uWinsText = new Text("User Wins: " + game.getNumOfUserWins()); uWinsText.setFont(Font.font("Helvetica", 16)); uWinsText.setFill(Color.BLUE); tieText = new Text("Ties: " + game.getNumOfTies()); tieText.setFont(Font.font("Helvetica", 16)); tieText.setFill(Color.BLUE); HBox statsBox = new HBox(cWinsText, uWinsText, tieText); statsBox.setSpacing(10); statsBox.setAlignment(Pos.CENTER); RPSGame balance = new RPSGame(); double balance1 = balance.getBalance(); double betText1 = balance1; betText = new Text("Your profits/losses are: " + betText1); betText.setFont(Font.font("Helvetica", 14)); betText.setFill(Color.BLUE); HBox betBox = new HBox(betText); betBox.setAlignment(Pos.CENTER); VBox pane = new VBox(imageBox, labelBox, matchOutcomeText, buttonBox, statsBox, betBox); pane.setAlignment(Pos.CENTER); pane.setSpacing(20); pane.setStyle("-fx-background-color: beige"); Scene scene = new Scene(pane, 400, 400, Color.TRANSPARENT); primaryStage.setTitle("Rock, Paper, Scissors, Go!"); primaryStage.setResizable(false); primaryStage.setScene(scene); primaryStage.show(); } // only implemented this method if completing the extra credit public double getBetAmount() { return userNumber2; } private void findUserMoveSetImage(String userMove) { if(userMove.charAt(5) == 'S') { userMoveC = RPSGame.Moves.SCISSORS; userMoveImageView.setImage(scissorsImage); } else if(userMove.charAt(5) == 'P') { userMoveC = RPSGame.Moves.PAPER; userMoveImageView.setImage(paperImage); } else if(userMove.charAt(5) == 'R'){ userMoveC = RPSGame.Moves.ROCK; userMoveImageView.setImage(rockImage); } } private void setImageForComputerMove(RPSGame.Moves compMove) { if(RPSGame.Moves.ROCK == compMove) { computerMoveImageView.setImage(rockImage); } else if(RPSGame.Moves.SCISSORS== compMove) { computerMoveImageView.setImage(scissorsImage); } else if(RPSGame.Moves.PAPER == compMove) { computerMoveImageView.setImage(paperImage); } } private void updateWinnerOfMatchDisplay(RPSGame.Outcomes winner) { if(winner == RPSGame.Outcomes.COMPUTER_WIN) { matchOutcomeText.setText("Computer wins"); } else if(winner == RPSGame.Outcomes.USER_WIN) { matchOutcomeText.setText("User wins"); } else { matchOutcomeText.setText("Tie"); } } private void updateGameStats() { int cWins = game.getNumOfComputerWins(); int uWins= game.getNumOfUserWins(); int ties = game.getNumOfTies(); cWinsText.setText("Computer Wins: " + cWins); uWinsText.setText("User Wins: " + uWins); tieText.setText("Ties: "+ ties); } private void handleUserPlay(ActionEvent event) { // to make all aspects of the display visible userMoveImageView.setVisible(true); computerMoveImageView.setVisible(true); labelBox.setVisible(true); // get the move from the user (determine which button was clicked) String userMove = ((Button)event.getSource()).getText(); findUserMoveSetImage(userMove); RPSGame.Moves compMove = game.computerPlay(); setImageForComputerMove(compMove); RPSGame.Outcomes winner = game.findWinner(userMoveC, compMove); updateWinnerOfMatchDisplay(winner); updateGameStats(); } public static void main(String[] args) { launch(args); } }//end of GUI class
//Beginning of the game class
import java.util.Random; public class RPSGame { private int numOfUserWins; private int numOfComputerWins; private int numOfTies; private double balance; RockPaperScissorsFXGUI betAmount = new RockPaperScissorsFXGUI(); double betAmount1= betAmount.getBetAmount(); public enum Moves{ ROCK, PAPER, SCISSORS}; public enum Outcomes{COMPUTER_WIN, USER_WIN, TIE}; public int getNumOfUserWins() { return numOfUserWins; } public int getNumOfComputerWins() { return numOfComputerWins; } public int getNumOfTies() { return numOfTies; } // public void setNumOfUserWins(int newNumOfUserWins) { // numOfUserWins = newNumOfUserWins; // } // public void setNumOfComputerWins(int newNumOfComputerWins) { // numOfComputerWins = newNumOfComputerWins; // } // public void setNumOfTies(int newNumOfTies) { // numOfTies = newNumOfTies; // } public Moves computerPlay() { Random random = new Random(); int numOfMove = random.nextInt(3); return Moves.values()[numOfMove]; } public Outcomes findWinner(Moves userMove, Moves computerMove) { if (userMove == computerMove) { numOfTies++; return Outcomes.TIE; } else if (userMove != computerMove) { if (userMove == Moves.ROCK) { if (computerMove == Moves.PAPER) { numOfComputerWins++; balance =- betAmount1; return Outcomes.COMPUTER_WIN; } else if (computerMove == Moves.SCISSORS) { numOfUserWins++; balance=+ betAmount1; return Outcomes.USER_WIN; } } if (userMove == Moves.PAPER) { if (computerMove == Moves.ROCK) { numOfUserWins++; balance=betAmount1; return Outcomes.USER_WIN; } else if (computerMove == Moves.SCISSORS) { numOfComputerWins++; balance=-betAmount1; return Outcomes.COMPUTER_WIN; } } if (userMove == Moves.SCISSORS) { if (computerMove == Moves.ROCK) { numOfComputerWins++; balance=-betAmount1; return Outcomes.COMPUTER_WIN; } else if (computerMove == Moves.PAPER) { numOfUserWins++; balance+=betAmount1; return Outcomes.USER_WIN; } } } return Outcomes.TIE; // should never get here but added this here because compiler was complaining } public double getBalance(){ return balance; } public static void main(String[] args) { RPSGame rp = new RPSGame(); Moves move = rp.computerPlay(); System.out.println(move); System.out.println(rp.findWinner(Moves.SCISSORS, move)); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started