Egor_Vakar

javaDoc

Nov 27th, 2021
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 20.79 KB | None | 0 0
  1. package com.example.mendeleevtable;
  2.  
  3. import javafx.event.ActionEvent;
  4. import javafx.fxml.FXML;
  5. import javafx.fxml.FXMLLoader;
  6. import javafx.scene.Node;
  7. import javafx.scene.Parent;
  8. import javafx.scene.Scene;
  9. import javafx.scene.control.Button;
  10. import javafx.scene.control.Label;
  11. import javafx.scene.control.TextField;
  12. import javafx.scene.image.ImageView;
  13. import javafx.scene.paint.Color;
  14. import javafx.stage.Stage;
  15.  
  16. import java.io.BufferedReader;
  17. import java.io.File;
  18. import java.io.FileReader;
  19. import java.io.IOException;
  20. import java.security.SecureRandom;
  21. import java.util.Objects;
  22.  
  23. /**
  24.  * This is main controller class, here is the main program code
  25.  * @author Written by Egor Vakar, Denis Talalaev, Vitushko Sergei for our project
  26.  * @version 1.1
  27.  * @since 1.0
  28.  */
  29.  
  30. public class Controller extends HelloApplication{
  31.  
  32.     /**
  33.      * massL Label for element's mass output
  34.      */
  35.     @FXML
  36.     public Label massL;
  37.  
  38.     /**
  39.      * nameR Label for element's name output
  40.      */
  41.  
  42.     @FXML
  43.     public Label nameR;
  44.  
  45.     /**
  46.      * numL Label for element's number in table output
  47.      */
  48.  
  49.     @FXML
  50.     public Label numL;
  51.  
  52.     /**
  53.      * logoL Label for element's logo output
  54.      */
  55.  
  56.     @FXML
  57.     public Label logoL;
  58.  
  59.     /**
  60.      * infoL Label for element's info output
  61.      */
  62.     @FXML
  63.     public Label infoL;
  64.  
  65.     /**
  66.      * backToMain starts backToMain() method
  67.      */
  68.     @FXML
  69.     public Button backToMain;
  70.  
  71.     /**
  72.      * colorRight using to show correctness of answer
  73.      */
  74.     @FXML
  75.     public Label colorRight;
  76.  
  77.     /**
  78.      * colorWrong using to show incorrectness of answer
  79.      */
  80.     @FXML
  81.     public Label colorWrong;
  82.  
  83.     /**
  84.      * This method close last and open table.fxml Stage
  85.      * @param event action, which start method
  86.      */
  87.     @FXML
  88.     public void toTableButton(ActionEvent event)  {
  89.  
  90.         FXMLLoader loader = new FXMLLoader();
  91.         Parent root = null;
  92.         String path = "table.fxml";
  93.         try {
  94.             root = loader.load(getClass().getResourceAsStream(path));
  95.         } catch (IOException e) {
  96.             e.printStackTrace();
  97.         }
  98.         Stage stage = new Stage();
  99.         stage.setTitle("Table");
  100.         stage.setResizable(false);
  101.         assert root != null;
  102.         stage.setScene(new Scene(root));
  103.         stage.show();
  104.         ((Node)(event.getSource())).getScene().getWindow().hide();
  105.     }
  106.  
  107.     /**
  108.      * control object for changing FXML objects
  109.      */
  110.     public static Controller control = new Controller();
  111.  
  112.     /**
  113.      * This method close last window and open game.fxml Stage
  114.      * @param event action, which start method
  115.      * @throws IOException Exception if it can't open file
  116.      */
  117.     @FXML
  118.     public void toGameButton(ActionEvent event) throws IOException {
  119.         String logo = "", name = "";
  120.         File file = new File(Objects.requireNonNull(getClass().getResource("1.txt")).getFile());
  121.         try (BufferedReader br = new BufferedReader(new FileReader(file.getPath()))) {
  122.             logo = br.readLine();
  123.             name = br.readLine();
  124.         }catch (Exception ignored) {}
  125.         FXMLLoader loader = new FXMLLoader(getClass().getResource("game.fxml"));
  126.         Stage stage = new Stage();
  127.         stage.setTitle("Game");
  128.         stage.setResizable(false);
  129.         Parent root = loader.load();
  130.         stage.setScene(new Scene(root));
  131.         stage.show();
  132.         System.out.println(logo);
  133.         System.out.println(name);
  134.         ((Node)(event.getSource())).getScene().getWindow().hide();
  135.         control = loader.getController();
  136.     }
  137.  
  138.     /**
  139.      * This method close last window and open info.fxml Stage
  140.      * @param event action, which start method
  141.      * @throws Exception Throws Exception if it can't open file for info output
  142.      */
  143.  
  144.     @FXML
  145.     public void goToElemInfo(ActionEvent event) throws Exception  {
  146.         String logo = "", name = "", mass = "", info = "", num = "";
  147.         Button btn = (Button) event.getSource();
  148.         File file = new File(Objects.requireNonNull(getClass().getResource(btn.getId() + ".txt")).getFile());
  149.         try (BufferedReader br = new BufferedReader(new FileReader(file.getPath()))) {
  150.             logo = br.readLine();
  151.             name = br.readLine();
  152.             mass = br.readLine();
  153.             info = br.readLine();
  154.             num = btn.getId();
  155.         }catch (Exception ignored) {}
  156.  
  157.         FXMLLoader loader = new FXMLLoader(getClass().getResource("info.fxml"));
  158.         Stage stage = new Stage();
  159.         stage.setTitle("Element Info");
  160.         stage.setResizable(false);
  161.         Parent root = loader.load();
  162.         Controller controller = loader.getController();
  163.         stage.setScene(new Scene(root));
  164.         stage.show();
  165.         controller.logoL.setText(logo);
  166.         controller.nameR.setText(name);
  167.         controller.massL.setText(mass);
  168.         controller.infoL.setText(info);
  169.         controller.numL.setText(num);
  170.     }
  171.  
  172.     /**
  173.      * This method close last window and open view.fxml Stage
  174.      * @param event action, which start method
  175.      */
  176.     @FXML
  177.     public void backToMain(ActionEvent event) {
  178.         ((Node)(event.getSource())).getScene().getWindow().hide();
  179.         FXMLLoader loader = new FXMLLoader();
  180.         Parent root = null;
  181.         String path = "view.fxml";
  182.         try {
  183.             root = loader.load(getClass().getResourceAsStream(path));
  184.         } catch (IOException e) {
  185.             e.printStackTrace();
  186.         }
  187.         Stage stage = new Stage();
  188.         stage.setResizable(false);
  189.         assert root != null;
  190.         stage.setScene(new Scene(root));
  191.         stage.show();
  192.     }
  193.  
  194.     /**
  195.      * This method close last window and open view.fxml Stage
  196.      * @param event action, which start method
  197.      */
  198.  
  199.     @FXML
  200.     public void backToMain1(ActionEvent event) {
  201.         gameEnd(event);
  202.         FXMLLoader loader = new FXMLLoader();
  203.         Parent root = null;
  204.         String path = "view.fxml";
  205.         try {
  206.             root = loader.load(getClass().getResourceAsStream(path));
  207.         } catch (IOException e) {
  208.             e.printStackTrace();
  209.         }
  210.         Stage stage = new Stage();
  211.         stage.setResizable(false);
  212.         assert root != null;
  213.         stage.setScene(new Scene(root));
  214.         stage.show();
  215.     }
  216.  
  217.     /**
  218.      * This method close info window
  219.      * @param event action, which start method
  220.      */
  221.     @FXML
  222.     public void backToTable(ActionEvent event){
  223.         ((Node)(event.getSource())).getScene().getWindow().hide();
  224.     }
  225.  
  226.     /**
  227.      * points Label stores number of points in game
  228.      */
  229.     @FXML
  230.     public Label points;
  231.     /**
  232.      * health Label stores number of health in game
  233.      */
  234.     @FXML
  235.     public Label health;
  236.     /**
  237.      * logoG Label stores element's logo
  238.      */
  239.     @FXML
  240.     public Label logoG;
  241.     /**
  242.      * nameID Label is auxiliary title
  243.      */
  244.     @FXML
  245.     public Label nameID;
  246.     /**
  247.      * gameInfoLabel Label stores info about game
  248.      */
  249.     @FXML
  250.     public Label gameInfoLabel;
  251.     /**
  252.      * totalPoints Label stores points for GameOver
  253.      */
  254.     @FXML
  255.     public Label totalPoints;
  256.     /**
  257.      * The button checkAnswerButton starts the checkAnswer() method
  258.      */
  259.     @FXML
  260.     public Button checkAnswerButton;
  261.     /**
  262.      * The button easyModeButton starts the esMode() method
  263.      */
  264.     @FXML
  265.     public Button easyModeButton;
  266.     /**
  267.      * The button normalModeButton starts the normMode() method
  268.      */
  269.     @FXML
  270.     public Button normalModeButton;
  271.     /**
  272.      * The button hardModeButton starts the hardMode() method
  273.      */
  274.     @FXML
  275.     public Button hardModeButton;
  276.     /**
  277.      * usersAnswer TextField takes user's answer about element's name
  278.      */
  279.     @FXML
  280.     public TextField usersAnswer;
  281.     /**
  282.      * firstHeart ImageView is heart for 1st live
  283.      */
  284.     @FXML
  285.     public ImageView firstHeart;
  286.     /**
  287.      * secondHeart ImageView is heart for 2nd live
  288.      */
  289.     @FXML
  290.     public ImageView secondHeart;
  291.     /**
  292.      * thirdHeart ImageView is heart for 3rd live
  293.      */
  294.     @FXML
  295.     public ImageView thirdHeart;
  296.     /**
  297.      * DEGREE_OF_DIFFICULTY Label stores information about difficulty
  298.      */
  299.     public static String DEGREE_OF_DIFFICULTY= "";
  300.     /**
  301.      * GAME_NUM Label information about number of current game element
  302.      */
  303.     public static byte GAME_NUM = 0;
  304.     /**
  305.      * GAME_POINTS stores information about user's points in game
  306.      */
  307.     public static int GAME_POINTS = 0;
  308.     /**
  309.      * GAME_HEALTH stores information about user's lives in game
  310.      */
  311.     public static int GAME_HEALTH = 3;
  312.     /**
  313.      * GAME_ARR stores array of elements numbers for game
  314.      */
  315.     public static int[] GAME_ARR = new int[118];
  316.  
  317.     /**
  318.      * rnd object using for randomize elements numbers
  319.      */
  320.     public static SecureRandom rnd = new SecureRandom();
  321.  
  322.     /**
  323.      * This method find elements name from file with element's number
  324.      * @param number is int value of element's number
  325.      * @return name is element's name
  326.      */
  327.     public static String takeRightAnswer(int number){
  328.         String name = "";
  329.         String tmp = "";
  330.         File file = new File(Objects.requireNonNull(Controller.class.getResource(number + ".txt")).getFile());
  331.         try (BufferedReader br = new BufferedReader(new FileReader(file.getPath()))) {
  332.             tmp = br.readLine();
  333.             name = br.readLine();
  334.         }catch (Exception ignored) {}
  335.         System.out.println(tmp + " " + name);
  336.         return name;
  337.     }
  338.  
  339.     /**
  340.      * This method find elements logo from file with element's number
  341.      * @param number is int value of element's number
  342.      * @return logo is element's logo
  343.      */
  344.     public static String takeLogo(int number){
  345.         String logo = "";
  346.         File file = new File(Objects.requireNonNull(Controller.class.getResource(number + ".txt")).getFile());
  347.         try (BufferedReader br = new BufferedReader(new FileReader(file.getPath()))) {
  348.             logo = br.readLine();
  349.         }catch (Exception ignored) {}
  350.         return logo;
  351.     }
  352.  
  353.     /**
  354.      * This method change logoG text for element's logo using  takeLogo() method
  355.      * @param number is int value of element's number
  356.      */
  357.     public static void generateInfo(int number){
  358.         control.logoG.setText(takeLogo(number));
  359.     }
  360.  
  361.     /**
  362.      * This method increase users health in game if answer is right
  363.      */
  364.     public static void rightAnswer() {
  365.         GAME_POINTS++;
  366.         control.points.setText(String.valueOf(GAME_POINTS));
  367.         control.nameID.setTextFill(Color.GREEN);
  368.         control.points.setTextFill(Color.GREEN);
  369.     }
  370.  
  371.     /**
  372.      * This method decrease users health in game if answer is wrong
  373.      */
  374.     public static void wrongAnswer() {
  375.         GAME_HEALTH--;
  376.         control.nameID.setTextFill(Color.RED);
  377.         control.points.setTextFill(Color.RED);
  378.     }
  379.  
  380.     /**
  381.      * This method reset points and number of games in game window
  382.      */
  383.     public static void clearAll(){
  384.         GAME_HEALTH = 3;
  385.         GAME_POINTS = 0;
  386.         GAME_NUM = 0;
  387.         control.points.setText("0");
  388.         control.logoG.setText("");
  389.         control.usersAnswer.setText("");
  390.     }
  391.  
  392.     /**
  393.      * This method clear the window and offers to start the game
  394.      * @param event action, which start method
  395.      */
  396.     public static void gameEnd(ActionEvent event){
  397.         ((Node)(event.getSource())).getScene().getWindow().hide();
  398.         setGameElemsVisible(false);
  399.         clearAll();
  400.         control.checkAnswerButton.setText("Start the game");
  401.         control.health.setText("Health");
  402.     }
  403.  
  404.     /**
  405.      * This method shows the lose window to user
  406.      * @param event action, which start method
  407.      * @throws IOException Throws Exception if it can't make game over variant
  408.      */
  409.     public static void gameOver(ActionEvent event) throws IOException{
  410.         int points = GAME_POINTS;
  411.         System.out.println(points);
  412.         gameEnd(event);
  413.         FXMLLoader fxmlLoader = new FXMLLoader(Controller.class.getResource("Gameover.fxml"));
  414.         Scene scene = new Scene(fxmlLoader.load());
  415.         Controller ctrl = fxmlLoader.getController();
  416.         scene.getRoot().setStyle("-fx-font-family: 'serif';");
  417.         Stage stage = new Stage();
  418.         stage.setTitle("Game over");
  419.         stage.setScene(scene);
  420.         stage.show();
  421.         ctrl.totalPoints.setText(String.valueOf(points));
  422.     }
  423.  
  424.     /**
  425.      * This method shows the win window to user
  426.      * @param event action, which start method
  427.      * @throws IOException Trows Exception if can't make game win variant
  428.      */
  429.     public static void gameWin(ActionEvent event) throws IOException{
  430.         gameEnd(event);
  431.         FXMLLoader fxmlLoader = new FXMLLoader(Controller.class.getResource("GameWin.fxml"));
  432.         Scene scene = new Scene(fxmlLoader.load());
  433.         scene.getRoot().setStyle("-fx-font-family: 'serif';");
  434.         Stage stage = new Stage();
  435.         stage.setTitle("Game Win");
  436.         stage.setScene(scene);
  437.         stage.setResizable(false);
  438.         stage.show();
  439.     }
  440.  
  441.     /**
  442.      * This method shuffles and makes random appearance of elements in game
  443.      * @param numberOfElements uses to shuffle elements in game
  444.      */
  445.     public static int[] shuffleArray(int numberOfElements) {
  446.         int[] arr = new int[numberOfElements];
  447.         for (int i = 0; i < numberOfElements; i++) {
  448.             arr[i] = i + 1;
  449.         }
  450.         int n, current;
  451.         for (int i = numberOfElements - 1; i >= 0; i--) {
  452.             n = rnd.nextInt(i + 1);
  453.             current = arr[n];
  454.             arr[n] = arr[i];
  455.             arr[i] = current;
  456.         }
  457.         return arr;
  458.     }
  459.  
  460.     /**
  461.      * This method deletes all spaces between elements
  462.      * @param line uses to delete all spaces in line
  463.      */
  464.     public static String deleteAllSpaces (String line){
  465.         char[] arrayOfLetters = line.toCharArray();
  466.         StringBuilder answer = new StringBuilder();
  467.         for (char arrayOfLetter : arrayOfLetters) {
  468.             if (arrayOfLetter != ' ') {
  469.                 answer.append(arrayOfLetter);
  470.             }
  471.         }
  472.         return answer.toString();
  473.     }
  474.  
  475.     /**
  476.      * This method makes line elements lower
  477.      * @param line uses to make string lower
  478.      */
  479.  
  480.     public static String makeStringLower(String line){
  481.         char[] arrayOfLetters = line.toCharArray();
  482.         for (int i = 0; i < arrayOfLetters.length; i++) {
  483.             if ((arrayOfLetters[i] >= 1040) && (arrayOfLetters[i] < 1072)){
  484.                 arrayOfLetters[i] = (char) ((int) arrayOfLetters[i] + 32);
  485.             } else {
  486.                 if (arrayOfLetters[i] == 1025){
  487.                     arrayOfLetters[i] = (char) ((int) arrayOfLetters[i] + 80);
  488.                 }
  489.             }
  490.         }
  491.         StringBuilder answer = new StringBuilder();
  492.         for (char arrayOfLetter : arrayOfLetters) {
  493.             answer.append(arrayOfLetter);
  494.         }
  495.         return answer.toString();
  496.     }
  497.  
  498.     /**
  499.      *This method gets the user answer and compares it with the correct one. If there is a match, it calls the rightAnswer() method, otherwise wrongAnswer().
  500.      * @param rightAnswer correct element name
  501.      * @param usersAnswer user element name variant
  502.      * @param event action, which start method
  503.      * @throws IOException Exception if it can't get right answer from file
  504.      */
  505.  
  506.  
  507.     public static void checkAnswerFunc(String rightAnswer, String usersAnswer, ActionEvent event) throws IOException {
  508.         if ((rightAnswer.trim()).equalsIgnoreCase(usersAnswer.trim())){
  509.             rightAnswer();
  510.         }else{
  511.             wrongAnswer();
  512.         }
  513.         GAME_NUM++;
  514.         if (GAME_HEALTH == 2){
  515.             control.firstHeart.setVisible(false);
  516.         }
  517.         if (GAME_HEALTH == 1){
  518.             control.secondHeart.setVisible(false);
  519.         }
  520.         if (GAME_HEALTH == 0){
  521.             control.thirdHeart.setVisible(false);
  522.             gameOver(event);
  523.         }else if (GAME_NUM == choosingGameDifficulty(DEGREE_OF_DIFFICULTY) + 1){
  524.             gameWin(event);
  525.         } else {
  526.             generateInfo(GAME_ARR[GAME_NUM - 1]);
  527.         }
  528.         control.usersAnswer.setText("");
  529.     }
  530.  
  531.     /**
  532.      * This method makes elements visible in game window
  533.      * @param way uses to make element of game visible
  534.      */
  535.     public static void setGameElemsVisible(boolean way){
  536.         control.gameInfoLabel.setVisible(!way);
  537.         control.easyModeButton.setVisible(!way);
  538.         control.normalModeButton.setVisible(!way);
  539.         control.hardModeButton.setVisible(!way);
  540.         control.checkAnswerButton.setVisible(way);
  541.         control.checkAnswerButton.setVisible(way);
  542.         control.usersAnswer.setVisible(way);
  543.         control.points.setVisible(way);
  544.         control.nameID.setVisible(way);
  545.         control.health.setVisible(way);
  546.         control.firstHeart.setVisible(way);
  547.         control.secondHeart.setVisible(way);
  548.         control.thirdHeart.setVisible(way);
  549.     }
  550.  
  551.     /**
  552.      * This method allows user to set difficulty mode in game
  553.      * @param choice variant of user choice
  554.      */
  555.     public static int choosingGameDifficulty(String choice){
  556.         int numberOfElements = 0;
  557.         switch (choice) {
  558.             case ("Easy") -> numberOfElements = 30;
  559.             case ("Normal") -> numberOfElements = 60;
  560.             case ("Hard") -> numberOfElements = 118;
  561.         }
  562.         return numberOfElements;
  563.     }
  564.  
  565.     /**
  566.      * This method allows user to set easy mode in game
  567.      * @param event action, which start method
  568.      * @throws IOException Trows Exception if can't set easy mode
  569.      */
  570.     @FXML
  571.     public void esMode(ActionEvent event) throws IOException {
  572.         DEGREE_OF_DIFFICULTY = "Easy";
  573.         checkAnswer(event);
  574.     }
  575.  
  576.     /**
  577.      * This method allows user to set normal mode in game
  578.      * @param event action, which start method
  579.      * @throws IOException Trows Exception if can't set normal mode
  580.      */
  581.     @FXML
  582.     public void normMode(ActionEvent event) throws IOException {
  583.         DEGREE_OF_DIFFICULTY = "Normal";
  584.         checkAnswer(event);
  585.     }
  586.     /**
  587.      * This method allows user to set hard mode in game
  588.      * @param event action, which start method
  589.      * @throws IOException Trows Exception if can't set hard mode
  590.      */
  591.     @FXML
  592.     public void hardMode(ActionEvent event) throws IOException {
  593.         DEGREE_OF_DIFFICULTY = "Hard";
  594.         checkAnswer(event);
  595.     }
  596.  
  597.     /**
  598.      * checkAnswer void
  599.      * @param event action, which start method
  600.      * @throws IOException Trows Exception if can't check the answer
  601.      */
  602.  
  603.     @FXML
  604.     public void checkAnswer(ActionEvent event) throws IOException {
  605.         if (GAME_NUM == 0){
  606.             control.nameID.setTextFill(Color.BLACK);
  607.             control.points.setTextFill(Color.BLACK);
  608.             setGameElemsVisible(true);
  609.             control.checkAnswerButton.setText("Check");
  610.             clearAll();
  611.             GAME_ARR = shuffleArray(choosingGameDifficulty(DEGREE_OF_DIFFICULTY));
  612.             GAME_NUM = 1;
  613.             generateInfo(GAME_ARR[GAME_NUM - 1]);
  614.         }else{
  615.             String rAnswer = makeStringLower(deleteAllSpaces(takeRightAnswer(GAME_ARR[GAME_NUM - 1])));
  616.             String uAnswer = makeStringLower(deleteAllSpaces(usersAnswer.getText()));
  617.             if(usersAnswer.getText().equals("cheat::answer")){
  618.                 control.usersAnswer.setText(rAnswer);
  619.             }else if (usersAnswer.getText().equals("cheat::win")){
  620.                 gameWin(event);
  621.                 control.usersAnswer.setText("");
  622.             } else if (usersAnswer.getText().equals("cheat::gameover")){
  623.                 gameOver(event);
  624.                 control.usersAnswer.setText("");
  625.             } else if (usersAnswer.getText().equals("cheat::pluspoints")){
  626.                 GAME_POINTS += 5;
  627.                 control.points.setText(String.valueOf(GAME_POINTS));
  628.                 control.usersAnswer.setText("");
  629.             } else if (usersAnswer.getText().equals("cheat::heal")){
  630.                 GAME_HEALTH = 3;
  631.                 control.firstHeart.setVisible(true);
  632.                 control.secondHeart.setVisible(true);
  633.                 control.thirdHeart.setVisible(true);
  634.                 control.usersAnswer.setText("");
  635.             }else if(usersAnswer.getText().equals("cheat::godmode")){
  636.                 GAME_HEALTH = 5000;
  637.                 control.firstHeart.setVisible(false);
  638.                 control.secondHeart.setVisible(false);
  639.                 control.thirdHeart.setVisible(false);
  640.                 control.health.setText("Immortal");
  641.                 control.usersAnswer.setText("");
  642.  
  643.             } else {
  644.                 checkAnswerFunc(rAnswer, uAnswer, event);
  645.             }
  646.         }
  647.     }
  648. }
  649.  
Add Comment
Please, Sign In to add comment