diff --git a/test_05/.gitignore b/test_05/.gitignore new file mode 100644 index 0000000..efb8c7d --- /dev/null +++ b/test_05/.gitignore @@ -0,0 +1,6 @@ +/out/* +/.idea/uiDesigner.xml +/.idea/workspace.xml +/.idea/description.xml +/.gradle/* +/build/* diff --git a/test_05/.idea/compiler.xml b/test_05/.idea/compiler.xml new file mode 100644 index 0000000..5d075a2 --- /dev/null +++ b/test_05/.idea/compiler.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/test_05/.idea/gradle.xml b/test_05/.idea/gradle.xml new file mode 100644 index 0000000..05e5219 --- /dev/null +++ b/test_05/.idea/gradle.xml @@ -0,0 +1,18 @@ + + + + + + \ No newline at end of file diff --git a/test_05/.idea/libraries/Gradle__junit_junit_4_12.xml b/test_05/.idea/libraries/Gradle__junit_junit_4_12.xml new file mode 100644 index 0000000..04c10dd --- /dev/null +++ b/test_05/.idea/libraries/Gradle__junit_junit_4_12.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/test_05/.idea/libraries/Gradle__org_hamcrest_hamcrest_core_1_3.xml b/test_05/.idea/libraries/Gradle__org_hamcrest_hamcrest_core_1_3.xml new file mode 100644 index 0000000..8262f72 --- /dev/null +++ b/test_05/.idea/libraries/Gradle__org_hamcrest_hamcrest_core_1_3.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/test_05/.idea/libraries/Gradle__org_jetbrains_annotations_15_0.xml b/test_05/.idea/libraries/Gradle__org_jetbrains_annotations_15_0.xml new file mode 100644 index 0000000..ca78bcd --- /dev/null +++ b/test_05/.idea/libraries/Gradle__org_jetbrains_annotations_15_0.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/test_05/.idea/misc.xml b/test_05/.idea/misc.xml new file mode 100644 index 0000000..e208459 --- /dev/null +++ b/test_05/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/test_05/.idea/modules.xml b/test_05/.idea/modules.xml new file mode 100644 index 0000000..4084ca4 --- /dev/null +++ b/test_05/.idea/modules.xml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/test_05/.idea/modules/test_05.iml b/test_05/.idea/modules/test_05.iml new file mode 100644 index 0000000..cc7ddcf --- /dev/null +++ b/test_05/.idea/modules/test_05.iml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/test_05/.idea/modules/test_05_main.iml b/test_05/.idea/modules/test_05_main.iml new file mode 100644 index 0000000..6a265fc --- /dev/null +++ b/test_05/.idea/modules/test_05_main.iml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/test_05/.idea/modules/test_05_test.iml b/test_05/.idea/modules/test_05_test.iml new file mode 100644 index 0000000..807037f --- /dev/null +++ b/test_05/.idea/modules/test_05_test.iml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/test_05/build.gradle b/test_05/build.gradle new file mode 100644 index 0000000..613c0e6 --- /dev/null +++ b/test_05/build.gradle @@ -0,0 +1,18 @@ +group 'test_05' +version '1.0-SNAPSHOT' + +apply plugin: 'java' + +sourceCompatibility = 1.8 + +repositories { + mavenCentral() +} + +dependencies { + testCompile group: 'junit', name: 'junit', version: '4.12' + + + // https://mvnrepository.com/artifact/org.jetbrains/annotations + compile group: 'org.jetbrains', name: 'annotations', version: '15.0' +} diff --git a/test_05/gradle/wrapper/gradle-wrapper.jar b/test_05/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..f278b42 Binary files /dev/null and b/test_05/gradle/wrapper/gradle-wrapper.jar differ diff --git a/test_05/gradle/wrapper/gradle-wrapper.properties b/test_05/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..803ef38 --- /dev/null +++ b/test_05/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,6 @@ +#Fri May 04 16:26:35 MSK 2018 +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-3.5-rc-2-bin.zip diff --git a/test_05/gradlew b/test_05/gradlew new file mode 100755 index 0000000..4453cce --- /dev/null +++ b/test_05/gradlew @@ -0,0 +1,172 @@ +#!/usr/bin/env sh + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >/dev/null +APP_HOME="`pwd -P`" +cd "$SAVED" >/dev/null + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS="" + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn ( ) { + echo "$*" +} + +die ( ) { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MINGW* ) + msys=true + ;; + NONSTOP* ) + nonstop=true + ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin, switch paths to Windows format before running java +if $cygwin ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + JAVACMD=`cygpath --unix "$JAVACMD"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=$((i+1)) + done + case $i in + (0) set -- ;; + (1) set -- "$args0" ;; + (2) set -- "$args0" "$args1" ;; + (3) set -- "$args0" "$args1" "$args2" ;; + (4) set -- "$args0" "$args1" "$args2" "$args3" ;; + (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Escape application args +save ( ) { + for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done + echo " " +} +APP_ARGS=$(save "$@") + +# Collect all arguments for the java command, following the shell quoting and substitution rules +eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" + +# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong +if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then + cd "$(dirname "$0")" +fi + +exec "$JAVACMD" "$@" diff --git a/test_05/gradlew.bat b/test_05/gradlew.bat new file mode 100644 index 0000000..e95643d --- /dev/null +++ b/test_05/gradlew.bat @@ -0,0 +1,84 @@ +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS= + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto init + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto init + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:init +@rem Get command-line arguments, handling Windows variants + +if not "%OS%" == "Windows_NT" goto win9xME_args + +:win9xME_args +@rem Slurp the command line arguments. +set CMD_LINE_ARGS= +set _SKIP=2 + +:win9xME_args_slurp +if "x%~1" == "x" goto execute + +set CMD_LINE_ARGS=%* + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/test_05/settings.gradle b/test_05/settings.gradle new file mode 100644 index 0000000..1abe8ff --- /dev/null +++ b/test_05/settings.gradle @@ -0,0 +1,2 @@ +rootProject.name = 'test_05' + diff --git a/test_05/src/main/java/mit/mikhail/pravilov/com/FindPairApplication.java b/test_05/src/main/java/mit/mikhail/pravilov/com/FindPairApplication.java new file mode 100644 index 0000000..306252f --- /dev/null +++ b/test_05/src/main/java/mit/mikhail/pravilov/com/FindPairApplication.java @@ -0,0 +1,35 @@ +package mit.mikhail.pravilov.com; + +import javafx.application.Application; +import javafx.stage.Stage; +import mit.mikhail.pravilov.com.view.FindPairSceneSupplier; + +import static java.lang.System.exit; + +public class FindPairApplication extends Application { + private static int size; + + @Override + public void start(Stage primaryStage) throws Exception { + Stage stage = new Stage(); + stage.setScene(new FindPairSceneSupplier().getScene(size)); + stage.show(); + } + + /** + * Main method of program. Runs tictactoe application. + * @param args command line arguments. + */ + public static void main(String[] args) { + if (args.length != 1) { + System.err.println("Arguments: field size"); + exit(0); + } + size = Integer.parseInt(args[0]); + if (size % 2 != 0 || size <= 0 || size > 15) { + System.err.println("Field size is incorrect!"); + exit(0); + } + Application.launch(args); + } +} diff --git a/test_05/src/main/java/mit/mikhail/pravilov/com/controller/GameController.java b/test_05/src/main/java/mit/mikhail/pravilov/com/controller/GameController.java new file mode 100644 index 0000000..e0d1ed3 --- /dev/null +++ b/test_05/src/main/java/mit/mikhail/pravilov/com/controller/GameController.java @@ -0,0 +1,154 @@ +package mit.mikhail.pravilov.com.controller; + +import javafx.concurrent.Task; +import javafx.scene.control.Alert; +import javafx.scene.control.Button; + +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; +import java.util.concurrent.ThreadLocalRandom; + +/** + * Class that controls game flow and creates buttons for user interaction. + */ +public class GameController { + /** + * Size of the field. + */ + private int size; + /** + * Values on the field. + */ + private int[][] field; + /** + * Previous pressed button. + */ + private Button prevButton; + /** + * Field of buttons for each cell. + */ + private Button[][] buttons; + /** + * How much cells are not discovered yet. + */ + private int leftToFind; + + /** + * Initializes all values: integer values, buttons. + * @param size of the field. + */ + public GameController(int size) { + this.size = size; + buttons = new Button[size][size]; + leftToFind = size * size; + initButtons(); + } + + /** + * Initializes buttons: set ups on click listeners. + */ + private void initButtons() { + List randomIntegers = getRandomIntegers(); + setUpField(randomIntegers); + for (int i = 0; i < size; i++) { + buttons[i] = new Button[size]; + for (int j = 0; j < size; j++) { + Button button = new Button(); + button.setMinSize(500 /size, 500 / size); + buttons[i][j] = button; + int finalI = i; + int finalJ = j; + button.setOnMouseClicked(e -> { + button.setDisable(true); + button.setText(String.valueOf(field[finalI][finalJ])); + if (prevButton == null) { + prevButton = button; + } + else { + if (prevButton.getText().equals(button.getText())) { + leftToFind -= 2; + if (leftToFind == 0) { + Alert winAlert = new Alert(Alert.AlertType.INFORMATION); + winAlert.setTitle("Congrats"); + winAlert.setHeaderText("WiN!"); + winAlert.showAndWait(); + } + } + else { + Button savePrevButton = prevButton; + Task sleeper = new Task() { + @Override + protected Void call() throws Exception { + try { + Thread.sleep(1000); + } catch (InterruptedException ignored) { + } + return null; + } + }; + sleeper.setOnSucceeded(event -> { + savePrevButton.setDisable(false); + button.setDisable(false); + savePrevButton.setText(""); + button.setText(""); + }); + new Thread(sleeper).start(); + } + prevButton = null; + } + }); + } + } + } + + /** + * Sets up field and puts passed values in cells. + * @param randomIntegers integers to put. + */ + private void setUpField(List randomIntegers) { + field = new int[size][size]; + for (int i = 0; i < size; i++) { + field[i] = new int[size]; + for (int j = 0; j < size; j++) { + field[i][j] = randomIntegers.get(i * size + j); + } + } + } + + /** + * Creates integers with pairs and then shuffles them. + * @return shuffled paired integers. + */ + private List getRandomIntegers() { + List randomIntegers = new LinkedList<>(); + for (int i = 0; i < size * size;) { + int random = ThreadLocalRandom.current().nextInt(0, size * size / 2); + randomIntegers.add(random); + randomIntegers.add(random); + i += 2; + } + Collections.shuffle(randomIntegers); + return randomIntegers; + } + + /** + * Returns button corresponding the cell. + * @param i x coordinate of cell. + * @param j y coordinate of cell. + * @return button for given cell. + */ + public Button getButton(int i, int j) { + return buttons[i][j]; + } + + /** + * Get value in cell. + * @param i x coordinate of cell. + * @param j y coordinate of cell. + * @return integer value in cell. + */ + int getValue(int i, int j) { + return field[i][j]; + } +} diff --git a/test_05/src/main/java/mit/mikhail/pravilov/com/view/FindPairSceneSupplier.java b/test_05/src/main/java/mit/mikhail/pravilov/com/view/FindPairSceneSupplier.java new file mode 100644 index 0000000..bbeb8b3 --- /dev/null +++ b/test_05/src/main/java/mit/mikhail/pravilov/com/view/FindPairSceneSupplier.java @@ -0,0 +1,36 @@ +package mit.mikhail.pravilov.com.view; + +import javafx.scene.Scene; +import javafx.scene.layout.GridPane; +import mit.mikhail.pravilov.com.controller.GameController; +import org.jetbrains.annotations.NotNull; + +import java.io.IOException; + +public class FindPairSceneSupplier { + private GridPane field; + + /** + * Game scene getter. + * @return main scene of the game process. + * @throws IOException if fxml of scene failed to load. + */ + @NotNull + private Scene getGameScene(int n) throws IOException { + setUpField(n); + return new Scene(field); + } + + private void setUpField(int n) { + GameController gameController = new GameController(n); + field = new GridPane(); + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + field.add(gameController.getButton(i, j), i, j); + } + } + } + public Scene getScene(int n) throws IOException { + return getGameScene(n); + } +} diff --git a/test_05/src/test/java/mit/mikhail/pravilov/com/controller/GameControllerTest.java b/test_05/src/test/java/mit/mikhail/pravilov/com/controller/GameControllerTest.java new file mode 100644 index 0000000..698e0c9 --- /dev/null +++ b/test_05/src/test/java/mit/mikhail/pravilov/com/controller/GameControllerTest.java @@ -0,0 +1,66 @@ +package mit.mikhail.pravilov.com.controller; + +import javafx.application.Application; +import javafx.stage.Stage; +import org.junit.BeforeClass; +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + +import static org.junit.Assert.*; + +public class GameControllerTest { + public static class AsNonApp extends Application { + @Override + public void start(Stage primaryStage) throws Exception { + // noop + } + } + + @BeforeClass + public static void initJFX() { + Thread t = new Thread("JavaFX Init Thread") { + public void run() { + Application.launch(AsNonApp.class); + } + }; + t.setDaemon(true); + t.start(); + } + + @Test + public void testFieldWithSize2() throws Exception { + int size = 2; + int minValue = 0; + int maxValue = 2 * 2 / 2; + Map timesValueWasMet = new HashMap<>(); + GameController gameController = new GameController(size); + checkTimesMet(timesValueWasMet, gameController, size, minValue, maxValue); + } + + @Test + public void testFieldWithSize14() throws Exception { + int size = 14; + int minValue = 0; + int maxValue = 14 * 14 / 2; + Map timesValueWasMet = new HashMap<>(); + GameController gameController = new GameController(size); + checkTimesMet(timesValueWasMet, gameController, size, minValue, maxValue); + } + + private void checkTimesMet(Map timesValueWasMet, GameController gameController, int size, int minValue, int maxValue) { + for (int i = 0; i < size; i++) { + for (int j = 0; j < size; j++) { + assertEquals(gameController.getButton(i, j).getText(), ""); + int value = gameController.getValue(i, j); + assertTrue(value >= minValue); + assertTrue(value < maxValue); + timesValueWasMet.put(value, 1 + timesValueWasMet.getOrDefault(value, 0)); + } + } + for (Integer times : timesValueWasMet.values()) { + assertTrue(times % 2 == 0); + } + } +} \ No newline at end of file