Advertisement
Ansaid

Controller

Nov 7th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.68 KB | None | 0 0
  1. package sample;
  2.  
  3. import javafx.event.ActionEvent;
  4. import javafx.fxml.FXML;
  5. import javafx.scene.control.Button;
  6. import javafx.scene.layout.GridPane;
  7.  
  8. import java.util.ArrayList;
  9.  
  10. public class Controller {
  11.     private Logic logic = new Logic();
  12.  
  13.     private Button[][] buttonsGameField = new Button[9][9];
  14.     private Button bufferNumberForSelection;
  15.     private int indexRow;
  16.     private int indexColumn;
  17. //    private ArrayList <Integer> errorIndexRow = new ArrayList<>();
  18. //    private ArrayList <Integer> errorIndexColumn = new ArrayList<>();
  19. //    private int errorSize = 0;
  20.     private boolean flagHelp = false;
  21.  
  22.     @FXML
  23.     private Button numberForSelection1;
  24.  
  25.     @FXML
  26.     private Button buttonHelp;
  27.  
  28.     @FXML
  29.     private void clickedButtonHelp() {
  30.  
  31.         backgroundUpdateHelp();
  32.  
  33.         int result = logic.clickedButtonHelp();
  34.         indexRow = result / 10;
  35.         indexColumn = result % 10;
  36.  
  37.         buttonsGameField[indexRow][indexColumn].setText(String.valueOf(logic.getGameFieldElement(indexRow, indexColumn)));
  38.         flagHelp = true;
  39.  
  40.         buttonsGameField[indexRow][indexColumn].setStyle("-fx-background-color: yellow; -fx-font-weight:bold; -fx-font-size: 24; -fx-font-family: \"System\"");
  41.     }
  42.  
  43.     private void backgroundUpdateHelp() {
  44.         if (flagHelp) {
  45.             buttonsGameField[indexRow][indexColumn].setStyle("-fx-font-weight:bold; -fx-font-size: 24; -fx-font-family: \"System\"");
  46.             flagHelp = false;
  47.         }
  48.     }
  49.  
  50.     public Controller() {
  51.         for (int row = 0; row < 9; row++) {
  52.             for (int column = 0; column < 9; column++) {
  53.                 buttonsGameField[row][column] = new Button("");
  54.                 buttonsGameField[row][column].setMinSize(50, 50);
  55.                 buttonsGameField[row][column].setStyle("-fx-font-weight:bold; -fx-font-size: 24; -fx-font-family: \"System\" ");
  56.             }
  57.         }
  58.         fillingInTheField();
  59.     }
  60.  
  61.     private void fillingInTheField() {
  62.         logic.fillingInTheField();
  63.         for (int i = 0; i < 9; i++) {
  64.             for (int j = 0; j < 9; j++) {
  65.                 if (logic.getGameFieldElement(i,j) != 0) {
  66.                     buttonsGameField[i][j].setText(String.valueOf(logic.getGameFieldElement(i, j)));
  67.                 }
  68.             }
  69.         }
  70.     }
  71.  
  72.     private void onHighlightingSelectedNumbers() { //для подсказок//
  73.         for (int i = 0; i < 9; i++) {
  74.             for (int j = 0; j < 9; j++) {
  75.                 if (buttonsGameField[i][j].getText().equals(bufferNumberForSelection.getText())) {
  76.                     buttonsGameField[i][j].setStyle("-fx-background-color: green; -fx-font-weight:bold; -fx-font-size: 24; -fx-font-family: \"System\" ");
  77.                 }
  78.             }
  79.         }
  80.         for (int i = 0; i < errorSize; i++) {
  81.             buttonsGameField[errorIndexRow.get(i)][errorIndexColumn.get(i)].setStyle("-fx-background-color: red; -fx-font-weight:bold; -fx-font-size: 24; -fx-font-family: \"System\" ");;
  82.         }
  83.     }
  84.  
  85.     private void offHighlightingSelectedNumbers() { //для подсказок//
  86.         backgroundUpdateHelp();
  87.  
  88.         for (int i = 0; i < 9; i++) {
  89.             for (int j = 0; j < 9; j++) {
  90.                 if (buttonsGameField[i][j].getText().equals(bufferNumberForSelection.getText())) {
  91.                     //buttonsGameField[i][j].setStyle(null);
  92.                     buttonsGameField[i][j].setStyle("-fx-font-weight:bold; -fx-font-size: 24; -fx-font-family: \"System\" ");
  93.                 }
  94.             }
  95.         }
  96.     }
  97.  
  98.     @FXML
  99.     private GridPane gridMap;
  100.  
  101.     @FXML
  102.     private void clickedNumberForSelection(ActionEvent actionEvent) {
  103.         bufferNumberForSelection.setStyle(null);
  104.         offHighlightingSelectedNumbers();
  105.  
  106.         bufferNumberForSelection = (Button) actionEvent.getSource();
  107.         logic.setSelectedNumber(Integer.parseInt(bufferNumberForSelection.getText()));
  108.         bufferNumberForSelection.setStyle("-fx-background-color: green");
  109.  
  110.         onHighlightingSelectedNumbers();
  111.     }
  112.  
  113.     @FXML
  114.     public void initialize() {
  115.         bufferNumberForSelection = numberForSelection1;
  116.         numberForSelection1.setStyle("-fx-background-color: green");;
  117.  
  118.         for (int row = 0; row < 9; row++) {
  119.             for (int column = 0; column < 9; column++) {
  120.                 gridMap.add(buttonsGameField[row][column], row, column);
  121.  
  122.                 buttonsGameField[row][column].setOnAction(this::clickedButtonGameField);
  123.             }
  124.         }
  125.     }
  126.  
  127.     public void searchButtonGameField(Button buffer) {
  128.         for (int i = 0; i < 9; i++) {
  129.             for (int j = 0; j < 9; j++) {
  130.                 if(buffer == buttonsGameField[i][j]) {
  131.                     logic.setGameFieldElement(i, j);
  132.                     indexRow = i;
  133.                     indexColumn = j;
  134.                     return;
  135.                 }
  136.             }
  137.         }
  138.     }
  139.  
  140.     public void clickedButtonGameField(ActionEvent actionEvent) {
  141.         backgroundUpdateHelp();
  142.  
  143.         Button buffer = (Button) actionEvent.getSource();
  144.         buffer.setText(String.valueOf(logic.getSelectedNumber()));
  145.  
  146.         searchButtonGameField(buffer);
  147.  
  148.         if (logic.checkGameField(indexRow, indexColumn)) { // для жизней //
  149.             buffer.setStyle("-fx-background-color: green; -fx-font-weight:bold; -fx-font-size: 24; -fx-font-family: \"System\" "); //для подсказок//
  150.             correctionError();
  151.         } else {
  152.             addError();
  153.             buffer.setStyle("-fx-background-color: red; -fx-font-weight:bold; -fx-font-size: 24; -fx-font-family: \"System\" "); //для жизней//
  154.         }
  155.         System.out.println("doClick");
  156.     }
  157.  
  158.     private void addError() {
  159.  
  160.         boolean repetitionFound = false;
  161.  
  162.         for (int i = 0; i < errorSize; i++) { // добавление ошибки
  163.             if (indexRow == errorIndexRow.get(i) && indexColumn == errorIndexColumn.get(i)) {
  164.                 repetitionFound = true;
  165.                 break;
  166.             }
  167.         }
  168.  
  169.         if (!repetitionFound) {
  170.             errorIndexRow.add(indexRow);
  171.             errorIndexColumn.add(indexColumn);
  172.             errorSize++;
  173.         }
  174.         System.out.println(errorIndexColumn.size());
  175.     }
  176.  
  177.     private void correctionError() {
  178.  
  179.         for (int i = 0; i < errorSize; i++) { // добавление ошибки
  180.             if (indexRow == errorIndexRow.get(i) && indexColumn == errorIndexColumn.get(i)) {
  181.                 errorIndexRow.remove(i);
  182.                 errorIndexColumn.remove(i);
  183.                 errorSize--;
  184.                 break;
  185.             }
  186.         }
  187.         System.out.println(errorIndexColumn.size());
  188.     }
  189.  
  190.  
  191.  
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement