Advertisement
Nick-O-Rama

SlotMachine

Sep 14th, 2015
439
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.52 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package slotmachine;
  7.  
  8. import java.net.URL;
  9. import java.util.Random;
  10. import java.util.ResourceBundle;
  11. import javafx.event.ActionEvent;
  12. import javafx.fxml.FXML;
  13. import javafx.fxml.Initializable;
  14. import javafx.scene.control.*;
  15. import javafx.scene.image.Image;
  16. import javafx.scene.image.ImageView;
  17.  
  18. /**
  19.  *
  20.  * @author Nick
  21.  */
  22. public class FXMLDocumentController implements Initializable {
  23.    
  24.     @FXML
  25.     private Label spinWin;
  26.     @FXML
  27.     private Label totalWin;
  28.     @FXML
  29.     private TextField textBox;
  30.     @FXML
  31.     private ImageView left;
  32.     @FXML
  33.     private ImageView right;
  34.     @FXML
  35.     private ImageView middle;
  36.    
  37.     private Random rand = new Random();
  38.     private String[] diceImages = {"file:/C:/Users/Nicolas/Documents/_JAVA/Source%20Code/chapter%2014/Images/Fruit%20Symbols/Apple.png",
  39.                                    "file:/C:/Users/Nicolas/Documents/_JAVA/Source%20Code/chapter%2014/Images/Fruit%20Symbols/Banana.png",
  40.                                    "file:/C:/Users/Nicolas/Documents/_JAVA/Source%20Code/chapter%2014/Images/Fruit%20Symbols/Cherries.png",
  41.                                    "file:/C:/Users/Nicolas/Documents/_JAVA/Source%20Code/chapter%2014/Images/Fruit%20Symbols/Grapes.png",
  42.                                    "file:/C:/Users/Nicolas/Documents/_JAVA/Source%20Code/chapter%2014/Images/Fruit%20Symbols/Lemon.png",
  43.                                    "file:/C:/Users/Nicolas/Documents/_JAVA/Source%20Code/chapter%2014/Images/Fruit%20Symbols/Lime.png",
  44.                                    "file:/C:/Users/Nicolas/Documents/_JAVA/Source%20Code/chapter%2014/Images/Fruit%20Symbols/Orange.png",
  45.                                    "file:/C:/Users/Nicolas/Documents/_JAVA/Source%20Code/chapter%2014/Images/Fruit%20Symbols/Pear.png",
  46.                                    "file:/C:/Users/Nicolas/Documents/_JAVA/Source%20Code/chapter%2014/Images/Fruit%20Symbols/Strawberry.png",
  47.                                    "file:/C:/Users/Nicolas/Documents/_JAVA/Source%20Code/chapter%2014/Images/Fruit%20Symbols/Watermelon.png"};
  48.     private int[] game = new int[3];
  49.     private int total = 0;
  50.     @FXML
  51.     private void handleButtonAction(ActionEvent event) {
  52.         spin();
  53.         payout();
  54.        
  55.     }
  56.     @FXML
  57.     private void instaWin(ActionEvent event) {
  58.         left.setImage(new Image(diceImages[0]));
  59.         middle.setImage(new Image(diceImages[0]));
  60.         right.setImage(new Image(diceImages[0]));
  61.         payout();
  62.     }
  63.    
  64.     public void spin() {
  65.         game[0] = rand.nextInt(10);
  66.         game[1] = rand.nextInt(10);
  67.         game[2] = rand.nextInt(10);
  68.         left.setImage(new Image(diceImages[game[0]]));
  69.         middle.setImage(new Image(diceImages[game[1]]));
  70.         right.setImage(new Image(diceImages[game[2]]));
  71.     }
  72.    
  73.     public void payout() {
  74.         int payout = 0;
  75.         if (game[0] == game[1] && game[1] == game[2])
  76.             payout = Integer.parseInt(textBox.getText()) * 3;
  77.         else if (game[0] == game[1] || game[0] == game[2] || game[1] == game[2])
  78.             payout = Integer.parseInt(textBox.getText()) * 2;
  79.         total += payout;
  80.         spinWin.setText(Integer.toString(payout));
  81.         totalWin.setText(Integer.toString(total));
  82.     }
  83.    
  84.     @Override
  85.     public void initialize(URL url, ResourceBundle rb) {
  86.         // TODO
  87.     }    
  88.    
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement