Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
language: java
jdk:
- oraclejdk8
os:
- linux
script:
- chmod +x buildscript.sh && ./buildscript.sh
36 changes: 36 additions & 0 deletions 02.Test/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>Test2</groupId>
<artifactId>Test2</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>


</project>
82 changes: 82 additions & 0 deletions 02.Test/src/main/java/ru/spbau/mit/alyokhina/CreateElements.java
Original file line number Diff line number Diff line change
@@ -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();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

было бы неплохо что-то говорить пользователю всё же. а то вот ввёл я нечётное N, а оно просто закрылось молча

});
}

}
168 changes: 168 additions & 0 deletions 02.Test/src/main/java/ru/spbau/mit/alyokhina/Game.java
Original file line number Diff line number Diff line change
@@ -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) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

это можно было бы красиво записать одной строчкой через стрим

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());
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ну вообще предполагалось, что текст на кнопке сразу будет виден, как только на кнопку нажали. а то сейчас надо нажать обе, и только потом мелькнут значения


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);
}
});
}
}
}
42 changes: 42 additions & 0 deletions 02.Test/src/main/java/ru/spbau/mit/alyokhina/Main.java
Original file line number Diff line number Diff line change
@@ -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));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

минимальный размер формы тоже стоит задавать, а то получается вот так:
image

primaryStage.show();

}

public static void main(String[] args) {
launch(args);
}
}
39 changes: 39 additions & 0 deletions 02.Test/src/test/java/ru/spbau/mit/alyokhina/GameTest.java
Original file line number Diff line number Diff line change
@@ -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));
}

}
9 changes: 9 additions & 0 deletions buildscript.sh
Original file line number Diff line number Diff line change
@@ -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