diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..6b8dde0 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,7 @@ +language: java +jdk: + - oraclejdk8 +os: + - linux +script: + - chmod +x buildscript.sh && ./buildscript.sh diff --git a/02.Test/pom.xml b/02.Test/pom.xml new file mode 100644 index 0000000..6cefa9b --- /dev/null +++ b/02.Test/pom.xml @@ -0,0 +1,36 @@ + + + 4.0.0 + + Test2 + Test2 + 1.0-SNAPSHOT + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + + + + + + + junit + junit + 4.12 + + + junit + junit + 4.12 + + + + + \ No newline at end of file diff --git a/02.Test/src/main/java/ru/spbau/mit/alyokhina/CreateElements.java b/02.Test/src/main/java/ru/spbau/mit/alyokhina/CreateElements.java new file mode 100644 index 0000000..e0a1d73 --- /dev/null +++ b/02.Test/src/main/java/ru/spbau/mit/alyokhina/CreateElements.java @@ -0,0 +1,82 @@ +package ru.spbau.mit.alyokhina; + + +import javafx.application.Platform; +import javafx.scene.control.Button; +import javafx.scene.control.TextField; +import javafx.scene.layout.GridPane; + +/** + * Create UI elements + */ +public class CreateElements { + + /** + * Create TextField for write size of table + * + * @param gridPane for placing textField + * @return textField which was created + */ + public static TextField CreateTextField(GridPane gridPane) { + TextField textField = new TextField(); + textField.setPrefHeight(200); + textField.setPrefWidth(200); + gridPane.add(textField, 0, 0); + return textField; + } + + /** + * Create button + * + * @param gridPane for placing button + * @param x abscissa coordinate + * @param y ordinate coordinate + * @param text this text will be on the button + * @return new button + */ + public static Button createButton(GridPane gridPane, double height, double width, int x, int y, String text) { + Button button = new Button(); + gridPane.add(button, x, y); + button.setText(text); + button.setPrefHeight(height); + button.setPrefWidth(width); + button.setLayoutX(x); + button.setLayoutY(y); + return button; + } + + /** + * Create board for Pair. Every cell is button + * + * @param gridPane for placing buttons + * @param n size of board + * @return array of buttons + */ + public static Button[] createField(GridPane gridPane, Integer n) { + Button[] buttons = new Button[n * n]; + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + buttons[i * n + j] = createButton(gridPane, 60, 60, 210 + 60 * j, 110 + 60 * i, ""); + } + } + return buttons; + } + + /** + * Create Result Activity with Text "WIN" + * @param gridPane for placing textField + */ + public static void createResultActivity(GridPane gridPane) { + gridPane.getChildren().clear(); + TextField textField = new TextField(); + textField.setPrefHeight(200); + textField.setPrefWidth(200); + gridPane.add(textField, 0, 0); + textField.setText("You win =)"); + Button ok = CreateElements.createButton(gridPane, 100, 100, 2, 6, "Exit"); + ok.setOnAction(actionEvent -> { + Platform.exit(); + }); + } + +} diff --git a/02.Test/src/main/java/ru/spbau/mit/alyokhina/Game.java b/02.Test/src/main/java/ru/spbau/mit/alyokhina/Game.java new file mode 100644 index 0000000..dd5d329 --- /dev/null +++ b/02.Test/src/main/java/ru/spbau/mit/alyokhina/Game.java @@ -0,0 +1,168 @@ +package ru.spbau.mit.alyokhina; + +import javafx.animation.PauseTransition; +import javafx.scene.control.Button; +import javafx.scene.layout.GridPane; +import javafx.util.Duration; + +import java.util.Random; + + +/** + * Class for the Game Pair + */ +public class Game { + /** + * Board size + */ + private Integer n = 0; + /** + * For placing UI elements + */ + private GridPane gridPane; + /** + * Random for value on the table + */ + final Random random = new Random(); + /** + * Cells values + */ + private int[] values; + /** + * Index opened cell on the table + */ + private int openField = -1; + /** + * Button which corresponds open cell + */ + private Button openButton; + /** + * Text on th open cell + */ + private String openString; + + /** + * Constructor + */ + public Game(GridPane gridPane) { + this.gridPane = gridPane; + } + + /** + * Create board + * + * @param str size board + * @return Cells values + * @throws Exception if size is even + */ + public int[] CreateBoard(String str) throws Exception { + n = Integer.parseInt(str); + if ((n % 2) != 0) { + throw new Exception("Number is odd"); + } + values = new int[n * n]; + for (int i = 0; i < n * n; i++) { + values[i] = -1; + } + return addValues(); + } + + /** + * Create values on the board + * + * @return Cells values + */ + private int[] addValues() { + for (int i = 0; i < (n * n) / 2; i++) { + int rand1 = random.nextInt(n * n); + + while (values[rand1] != -1) { + rand1 = random.nextInt(n * n); + } + values[rand1] = i; + + int rand2 = random.nextInt(n * n); + while (values[rand2] != -1) { + rand2 = random.nextInt(n * n); + } + values[rand2] = i; + } + return values; + } + + + /** Check the presence of closed cells + * @param buttons values on the table + * @return true - if exist close cells, else - false + */ + public boolean isEmptyButtons(String[] buttons) { + for (String button : buttons) { + if (button.equals("")) { + return true; + } + } + return false; + } + + /** + * Move player + * @param i index cell, which player wants open + * @param button cell, which player wants open + */ + private void move(int i, Button button) { + if (openField == -1) { + openField = i; + openButton = button; + openString = ((Integer) values[i]).toString(); + openButton.setDisable(true); + } else { + openButton.setText(openString); + button.setText(((Integer) values[i]).toString()); + + String str2 = ((Integer) values[i]).toString(); + + if (openString.equals(str2)) { + button.setDisable(true); + openButton.setDisable(true); + openButton = null; + openField = -1; + openString = ""; + } else { + button.setDisable(true); + PauseTransition delay = new PauseTransition(Duration.seconds(0.5)); + delay.setOnFinished(event1 -> { + button.setText(""); + openButton.setText(""); + button.setDisable(false); + openButton.setDisable(false); + + openButton = null; + openField = -1; + openString = ""; + }); + delay.play(); + } + } + } + + /** Start Game */ + public void startGame() { + gridPane.getChildren().clear(); + Button[] table = CreateElements.createField(gridPane, n); + + for (int i = 0; i < table.length; i++) { + final int j = i; + table[i].setOnAction(actionEvent -> { + move(j, table[j]); + + String[] valuesOnTheButtons = new String[table.length]; + for (int k = 0; k < table.length; k++) { + valuesOnTheButtons[k] = table[k].getText(); + } + if (!isEmptyButtons(valuesOnTheButtons)) { + CreateElements.createResultActivity(gridPane); + } + }); + } + } +} diff --git a/02.Test/src/main/java/ru/spbau/mit/alyokhina/Main.java b/02.Test/src/main/java/ru/spbau/mit/alyokhina/Main.java new file mode 100644 index 0000000..71d12c7 --- /dev/null +++ b/02.Test/src/main/java/ru/spbau/mit/alyokhina/Main.java @@ -0,0 +1,42 @@ +package ru.spbau.mit.alyokhina; + +import javafx.application.Application; +import javafx.application.Platform; +import javafx.geometry.Pos; +import javafx.scene.Scene; +import javafx.scene.control.Button; +import javafx.scene.control.TextField; +import javafx.scene.layout.GridPane; +import javafx.stage.Stage; + +/** + * Game Pair. Find equals numbers on the board. Board size must be even. + */ +public class Main extends Application { + @Override + public void start(Stage primaryStage) throws Exception { + primaryStage.setTitle("Пары"); + GridPane gridPane = new GridPane(); + gridPane.setAlignment(Pos.CENTER); + final TextField textField = CreateElements.CreateTextField(gridPane); + Button ok = CreateElements.createButton(gridPane, 50, 50, 4, 6, "OK"); + Game game = new Game(gridPane); + ok.setOnAction(actionEvent -> { + try { + game.CreateBoard(textField.getText()); + game.startGame(); + } catch (Exception e) { + Platform.exit(); + } + + + }); + primaryStage.setScene(new Scene(gridPane, 600, 400)); + primaryStage.show(); + + } + + public static void main(String[] args) { + launch(args); + } +} diff --git a/02.Test/src/test/java/ru/spbau/mit/alyokhina/GameTest.java b/02.Test/src/test/java/ru/spbau/mit/alyokhina/GameTest.java new file mode 100644 index 0000000..541f25d --- /dev/null +++ b/02.Test/src/test/java/ru/spbau/mit/alyokhina/GameTest.java @@ -0,0 +1,39 @@ +package ru.spbau.mit.alyokhina; + +import javafx.scene.layout.GridPane; +import org.junit.Test; + +import static org.junit.Assert.*; + +public class GameTest { + @Test + public void testCreateValueForTable() throws Exception { + Game game = new Game( new GridPane()); + int[] value = game.CreateBoard("4"); + + for (int i = 0; i < 8; i ++) { + int count = 0; + for (int j = 0; j < value.length; j++) { + if (value[j] == i) { + count++; + } + } + assertEquals(2, count); + } + + } + + @Test + public void testIsEmptyButtonsIfTrue() { + Game game = new Game( new GridPane()); + String[] values = {"1", "2", "", ""}; + assertEquals(true, game.isEmptyButtons(values)); + } + @Test + public void testIsEmptyButtonsIfFalse() { + Game game = new Game( new GridPane()); + String[] values = {"1", "2", "3", "4"}; + assertEquals(false, game.isEmptyButtons(values)); + } + +} \ No newline at end of file diff --git a/buildscript.sh b/buildscript.sh new file mode 100755 index 0000000..1cc07e7 --- /dev/null +++ b/buildscript.sh @@ -0,0 +1,9 @@ +#!/bin/bash +files=$(find . -maxdepth 1 -type d | grep "./0.*") +for file in $files +do + cd $file + mvn test -B + cd ../ +done +