/
DmZh
/
BoolCows
Обзор
Документация
Войти
/
DmZh
/
BoolCows
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
src/main/java/com/example/myjfx/HelloController.java
145 строк
4 KB
unknown
first_commit
16 окт 2024, 14:47
16 окт 2024, 14:47
118e6c2
Код
Авторство
О чём код?
package com.example.myjfx; import javafx.application.Platform; import javafx.beans.Observable; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.fxml.FXML; import javafx.scene.control.*; import java.util.*; public class HelloController { ObservableList<String> countDigitsList = FXCollections. observableArrayList("1", "2", "3", "4", "5", "6", "7", "8", "9", "10"); @FXML private Label welcomeText; @FXML private ComboBox countDigitsBox; @FXML private Button startButton; @FXML private TextField attemptNumber; @FXML private Button checkButton; @FXML private Label errorLabel; @FXML private SplitPane answerPane; @FXML private TextArea answerArea; @FXML private Button endButton; @FXML private Label debugLabel; @FXML private void initialize() { debugLabel.setText(""); countDigitsBox.setValue("4"); countDigitsBox.setItems(countDigitsList); answerArea.setText(""); errorLabel.setText(""); attemptNumber.setVisible(false); checkButton.setVisible(false); } @FXML protected void onCheckButtonClick() { errorLabel.setText(""); int n = Integer.parseInt(countDigitsBox.getValue().toString()); if (ShowMustGoOn(n, debugLabel.getText()) == n) { // WIN answerArea.appendText("YOU WIN!!!" + "\n"); attemptNumber.setVisible(false); checkButton.setVisible(false); } attemptNumber.setText(""); } @FXML protected void onExitButtonClick() { Platform.exit(); } @FXML protected void onStartButtonClick() { answerArea.clear(); errorLabel.setText(""); attemptNumber.setVisible(true); checkButton.setVisible(true); //debugLabel.setText(generateNum(Integer.parseInt(countDigitsBox.getValue().toString()))); } public String generateNum(int n) { String mysteryStr = ""; Set<Integer> setNumber = new TreeSet<Integer>(); //это придумал Дима, чтобы я познакомился с множествами какими то :) int tmpInt; Random rand = new Random(); // загадывать будем случайное число //генерируем число из n цифр без повторов for (int i = 0; i < n; i++) { tmpInt = rand.nextInt(10); if (setNumber.contains(tmpInt)) { i--; } else { setNumber.add(tmpInt); mysteryStr += String.valueOf(tmpInt); } } return mysteryStr; } public int ShowMustGoOn(int n, String firstStr) { int bools = 0; // быки, цифра на месте int cows = 0; // коровы, цифра не на месте String secondStr = attemptNumber.getText().toString(); //ну и наконец то считаем быков коров if (!checkUnique(n, secondStr)) { for (int i = 0; i < firstStr.length(); i++) { for (int j = 0; j < secondStr.length(); j++) { if (firstStr.charAt(i) == secondStr.charAt(j)) { if (i == j) { bools++; } else { cows++; } } } } answerArea.appendText(secondStr + " bools: " + bools + " cows: " + cows + "\n"); } return bools; } public boolean checkUnique(int n, String userStr) { boolean flagWrong = false; if (userStr.length() != n) { errorLabel.setText("The number is the wrong length!"); flagWrong = true; } else { for (int i = 0; i < (userStr.length() - 1); i++) { if (userStr.indexOf(userStr.charAt(i)) != userStr.lastIndexOf(userStr.charAt(i))) { errorLabel.setText("The digits should not be repeated!"); flagWrong = true; i = userStr.length(); } } } return flagWrong; } }