From 9f45ee8c1932b985312b688cdabeef02cf2cb32f Mon Sep 17 00:00:00 2001 From: alyokhina-olya Date: Fri, 23 Mar 2018 03:02:44 +0300 Subject: [PATCH 1/2] first commit --- .travis.yml | 7 + 02.TicTacToe/pom.xml | 36 ++++ .../alyokhina/TicTacToe/CreateElements.java | 198 ++++++++++++++++++ .../spbau/mit/alyokhina/TicTacToe/Game.java | 120 +++++++++++ .../mit/alyokhina/TicTacToe/GameWithComp.java | 125 +++++++++++ .../alyokhina/TicTacToe/GameWithFriend.java | 51 +++++ .../spbau/mit/alyokhina/TicTacToe/Main.java | 23 ++ .../mit/alyokhina/TicTacToe/Statistics.java | 30 +++ .../mit/alyokhina/TicTacToe/GameTest.java | 109 ++++++++++ .../alyokhina/TicTacToe/GameWithCompTest.java | 51 +++++ buildscript.sh | 9 + 11 files changed, 759 insertions(+) create mode 100644 .travis.yml create mode 100644 02.TicTacToe/pom.xml create mode 100644 02.TicTacToe/src/main/java/ru/spbau/mit/alyokhina/TicTacToe/CreateElements.java create mode 100644 02.TicTacToe/src/main/java/ru/spbau/mit/alyokhina/TicTacToe/Game.java create mode 100644 02.TicTacToe/src/main/java/ru/spbau/mit/alyokhina/TicTacToe/GameWithComp.java create mode 100644 02.TicTacToe/src/main/java/ru/spbau/mit/alyokhina/TicTacToe/GameWithFriend.java create mode 100644 02.TicTacToe/src/main/java/ru/spbau/mit/alyokhina/TicTacToe/Main.java create mode 100644 02.TicTacToe/src/main/java/ru/spbau/mit/alyokhina/TicTacToe/Statistics.java create mode 100644 02.TicTacToe/src/test/java/ru/spbau/mit/alyokhina/TicTacToe/GameTest.java create mode 100644 02.TicTacToe/src/test/java/ru/spbau/mit/alyokhina/TicTacToe/GameWithCompTest.java create mode 100755 buildscript.sh 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.TicTacToe/pom.xml b/02.TicTacToe/pom.xml new file mode 100644 index 0000000..ead7aac --- /dev/null +++ b/02.TicTacToe/pom.xml @@ -0,0 +1,36 @@ + + + 4.0.0 + + 02 + TicTacToe + 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.TicTacToe/src/main/java/ru/spbau/mit/alyokhina/TicTacToe/CreateElements.java b/02.TicTacToe/src/main/java/ru/spbau/mit/alyokhina/TicTacToe/CreateElements.java new file mode 100644 index 0000000..b22845b --- /dev/null +++ b/02.TicTacToe/src/main/java/ru/spbau/mit/alyokhina/TicTacToe/CreateElements.java @@ -0,0 +1,198 @@ +package ru.spbau.mit.alyokhina.TicTacToe; + + +import javafx.application.Platform; +import javafx.scene.control.Button; +import javafx.scene.control.TextArea; +import javafx.scene.layout.Pane; + +import java.util.function.Consumer; + +/** Create UI elements */ +public class CreateElements { + + /** + * Create 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(Pane pane, double height, double width, double x, double y, String text) { + Button button = new Button(); + pane.getChildren().add(button); + button.setText(text); + button.setPrefHeight(height); + button.setPrefWidth(width); + button.setLayoutX(x); + button.setLayoutY(y); + return button; + } + + + /** + * Create board for TicTacToe. Every cell is button + * + * @return array of buttons + */ + public static Button[] createTableView(Pane pane) { + Button[] buttons = new Button[9]; + for (int i = 0; i < 9; i++) { + buttons[i] = createButton(pane, 60, 60, 210 + 60 * (i % 3), 110 + 60 * (i / 3), ""); + } + return buttons; + } + + /**Create Menu for Main Activity */ + public static void createMainActivity(Pane pane) { + Button buttonPlayWithComp = CreateElements.createButton(pane, 50, 200, 200, 70, "Играть с компьютером"); + Button buttonPlayWithFriend = CreateElements.createButton(pane, 50, 200, 200, 170, "Играть с другом"); + Button buttonExit = CreateElements.createButton(pane, 50, 200, 200, 270, "Выход"); + buttonPlayWithComp.setOnAction(actionEvent -> { + GameWithComp gameWithComp = new GameWithComp(pane, new Statistics()); + gameWithComp.menu(); + }); + + buttonPlayWithFriend.setOnAction(actionEvent -> { + GameWithFriend gameWithFriend = new GameWithFriend(pane, new Statistics()); + gameWithFriend.menu(); + }); + buttonExit.setOnAction(value -> Platform.exit()); + } + + /** + * Create button for new game + * + * @param statistics is parameter for consumer + * @param consumer will be accepted after clicking on the button + */ + public static void createButtonReplay(Pane pane, Statistics statistics, Consumer consumer) { + Button rePlay = CreateElements.createButton(pane, 50, 200, 400, 20, "Начать новую партию"); + rePlay.setOnAction(actionEvent -> { + pane.getChildren().clear(); + consumer.accept(statistics); + }); + } + + + /** Create button to go to the main activity */ + public static void createButtonToMainActivity(Pane pane) { + Button buttonToMainActivity = CreateElements.createButton(pane, 50, 200, 400, 350, "В главное меню"); + buttonToMainActivity.setOnAction(actionEvent -> { + pane.getChildren().clear(); + createMainActivity(pane); + }); + } + + + /** + * Create button to go to statistics for hot seat. After clicking go to new activity + * + * @param go will be accepted after clicking button back + */ + public static void createButtonGetStatisticsForOnePlayers(Pane pane, Statistics statistics, Consumer go) { + Button getStatistics = CreateElements.createButton(pane, 50, 200, 200, 270, "Статистика"); + getStatistics.setOnAction(actionEvent -> { + pane.getChildren().clear(); + CreateElements.createButtonToMainActivity(pane); + Button buttonBack = CreateElements.createButton(pane, 50, 200, 400, 20, "Назад"); + buttonBack.setOnAction(actionEvent1 -> { + pane.getChildren().clear(); + go.accept(statistics); + }); + TextArea textArea = new TextArea(); + textArea.setLayoutY(0); + textArea.setLayoutX(0); + textArea.setPrefHeight(600); + textArea.setPrefWidth(200); + pane.getChildren().add(textArea); + textArea.setText("количество побед: " + statistics.getCountWins().toString() + + "\nколичество поражений: " + statistics.getCountLose().toString() + + "\nколичество ничьих: " + statistics.getCountDraw().toString()); + + }); + } + + + /** + * Create button to go to statistics for two players. After clicking go to new activity + * + * @param go will be accepted after clicking button back + */ + public static void createButtonGetStatisticsForTwoPlayers(Pane pane, Statistics statistics, Consumer go) { + Button getStatistics = CreateElements.createButton(pane, 50, 200, 200, 240, "Статистика"); + getStatistics.setOnAction(actionEvent -> { + pane.getChildren().clear(); + CreateElements.createButtonToMainActivity(pane); + Button button = CreateElements.createButton(pane, 50, 200, 400, 20, "Назад"); + button.setOnAction(actionEvent1 -> { + pane.getChildren().clear(); + go.accept(statistics); + }); + TextArea textAreaForFirst = new TextArea(); + textAreaForFirst.setLayoutY(0); + textAreaForFirst.setLayoutX(0); + textAreaForFirst.setPrefHeight(600); + textAreaForFirst.setPrefWidth(200); + pane.getChildren().add(textAreaForFirst); + textAreaForFirst.setText("Х \nколичество побед: " + statistics.getCountLose().toString() + + "\nколичество поражений: " + statistics.getCountWins().toString() + + "\nколичество ничьих: " + statistics.getCountDraw().toString()); + + + TextArea textAreaForSecond = new TextArea(); + textAreaForSecond.setLayoutY(0); + textAreaForSecond.setLayoutX(200); + textAreaForSecond.setPrefHeight(600); + textAreaForSecond.setPrefWidth(200); + pane.getChildren().add(textAreaForSecond); + textAreaForSecond.setText("О \nколичество побед: " + statistics.getCountWins().toString() + + "\nколичество поражений: " + statistics.getCountLose().toString() + + "\nколичество ничьих: " + statistics.getCountDraw().toString()); + }); + } + + /** Print result on screen and update statistics */ + public static void setResult(Pane pane, Statistics statistics, String win) { + pane.getChildren().clear(); + CreateElements.createButtonToMainActivity(pane); + switch (win) { + case "draw": { + TextArea textArea = new TextArea(); + textArea.setLayoutY(150); + textArea.setLayoutX(250); + textArea.setPrefHeight(50); + textArea.setPrefWidth(100); + textArea.setText("draw"); + pane.getChildren().add(textArea); + statistics.incDraw(); + break; + } + case "X": { + TextArea textArea = new TextArea(); + textArea.setLayoutY(150); + textArea.setLayoutX(250); + textArea.setPrefHeight(50); + textArea.setPrefWidth(100); + textArea.setText("X wins"); + pane.getChildren().add(textArea); + statistics.incLose(); + break; + } + default: { + TextArea textArea = new TextArea(); + textArea.setLayoutY(150); + textArea.setLayoutX(250); + textArea.setPrefHeight(50); + textArea.setPrefWidth(100); + pane.getChildren().add(textArea); + textArea.setText("O wins"); + statistics.incWins(); + break; + } + } + + } + +} diff --git a/02.TicTacToe/src/main/java/ru/spbau/mit/alyokhina/TicTacToe/Game.java b/02.TicTacToe/src/main/java/ru/spbau/mit/alyokhina/TicTacToe/Game.java new file mode 100644 index 0000000..8c6284c --- /dev/null +++ b/02.TicTacToe/src/main/java/ru/spbau/mit/alyokhina/TicTacToe/Game.java @@ -0,0 +1,120 @@ +package ru.spbau.mit.alyokhina.TicTacToe; + +import javafx.scene.control.Button; +import javafx.scene.layout.Pane; + +/** Class for key actions of game */ +public class Game { + /** + * Pane for current game + */ + protected Pane pane; + /** Number playing */ + protected int numberGames = 0; + /** Statistics for current game */ + protected Statistics statistics; + + /** Constructor */ + public Game(Pane pane, Statistics statistics) { + this.pane = pane; + this.statistics = statistics; + pane.getChildren().clear(); + CreateElements.createButtonToMainActivity(pane); + } + + /** Translated text from buttons array in string array */ + protected String[] toStringArray(Button[] table) { + String[] tableString = new String[table.length]; + for (int i = 0; i < table.length; i++) { + tableString[i] = table[i].getText(); + } + return tableString; + } + + /** + * Check the state of the game + * + * @param tableString state board, "" - if cell is empty + * @return "X" - if X wins , "O" - if O wins, "" - if state is unknown, else - draw + */ + public static String check(String[] tableString) { + + boolean existEmpty = false; + for (int i = 0; i < tableString.length; i++) { + if (tableString[i].equals("")) { + existEmpty = true; + } + } + + for (int i = 0; i < 9; i += 3) { + if (!tableString[i].equals("") && tableString[i].equals(tableString[i + 1]) && tableString[i + 1].equals(tableString[i + 2])) { + return tableString[i]; + } + } + + for (int i = 0; i < 3; i++) { + if (!tableString[i].equals("") && tableString[i].equals(tableString[i + 3]) && tableString[i + 3].equals(tableString[i + 6])) { + return tableString[i]; + } + } + + if (!tableString[0].equals("") && tableString[0].equals(tableString[4]) && tableString[4].equals(tableString[8])) { + return tableString[0]; + } + + if (!tableString[2].equals("") && tableString[2].equals(tableString[4]) && tableString[4].equals(tableString[6])) { + + return tableString[2]; + } + if (existEmpty) { + return ""; + } else { + return "draw"; + } + } + + /** + * /** + * Check the state of the game + * + * @param tableString state board, "" - if cell is empty + * @param isPreliminaryCheck, true - if real state, false - if we want check move + * @return "X" - if X wins , "O" - if O wins, "" - if state is unknown, else - draw + */ + protected String check(String[] tableString, boolean isPreliminaryCheck) { + String result = check(tableString); + if (!isPreliminaryCheck && !result.equals("")) { + setResult(result); + } + return result; + } + + /** Print result games */ + private void setResult(String win) { + numberGames++; + CreateElements.setResult(pane, statistics, win); + CreateElements.createButtonReplay(pane, statistics, this::goBack); + } + + /** For return in this activity */ + protected void goBack(Statistics statistics) { + this.statistics = statistics; + pane.getChildren().clear(); + CreateElements.createButtonToMainActivity(pane); + menu(); + } + + /** Symbol to which the player move */ + + public String getCompSymb(int player) { + return (numberGames % 2 == player) ? "X" : "O"; + } + + + /** Before games we should choose characteristics game */ + public void menu() { + CreateElements.createButton(pane, 50, 200, 200, 70, "Легкий"); + CreateElements.createButton(pane, 50, 200, 200, 170, "Сложный"); + } + +} diff --git a/02.TicTacToe/src/main/java/ru/spbau/mit/alyokhina/TicTacToe/GameWithComp.java b/02.TicTacToe/src/main/java/ru/spbau/mit/alyokhina/TicTacToe/GameWithComp.java new file mode 100644 index 0000000..49555ba --- /dev/null +++ b/02.TicTacToe/src/main/java/ru/spbau/mit/alyokhina/TicTacToe/GameWithComp.java @@ -0,0 +1,125 @@ +package ru.spbau.mit.alyokhina.TicTacToe; + + +import javafx.scene.control.Button; +import javafx.scene.layout.Pane; + +import java.util.Random; +import java.util.function.Consumer; + +public class GameWithComp extends Game { + /** + * Constructor + * + * @param statistics for collection information + */ + public GameWithComp(Pane pane, Statistics statistics) { + super(pane, statistics); + } + + + /** + * Menu. + * Choose level or get statistics + */ + @Override + public void menu() { + CreateElements.createButtonGetStatisticsForOnePlayers(pane, statistics, value -> menu()); + Button buttonEasyLevel = CreateElements.createButton(pane, 50, 200, 200, 70, "Легкий"); + Button buttonHardLevel = CreateElements.createButton(pane, 50, 200, 200, 170, "Сложный"); + buttonEasyLevel.setOnAction(value -> { + pane.getChildren().clear(); + CreateElements.createButtonToMainActivity(pane); + playGame(this::moveCompEasyLevel); + }); + buttonHardLevel.setOnAction(value -> { + pane.getChildren().clear(); + CreateElements.createButtonToMainActivity(pane); + playGame(this::moveCompHardLevel); + }); + } + + /** + * Start game + * + * @param moveComp move computer + */ + private void playGame(Consumer moveComp) { + CreateElements.createButtonReplay(pane, statistics, this::goBack); + Button[] table = CreateElements.createTableView(pane); + if (numberGames % 2 == 0) { + moveComp.accept(table); + } + for (Button button : table) { + button.setOnAction(actionEvent -> { + button.setText(getCompSymb(1)); + button.setDisable(true); + moveComp.accept(table); + }); + } + + + } + + /** Move will be get with help random */ + private void moveCompEasyLevel(Button[] table) { + Random random = new Random(); + if (check(toStringArray(table), false).equals("")) { + while (true) { + int x = random.nextInt(table.length); + if (table[x].getText().equals("")) { + table[x].setText(getCompSymb(0)); + table[x].setDisable(true); + break; + } + } + check(toStringArray(table), false); + } + } + + /** + * Analyze board. + * It's guaranteed if there is a possibility of winning in one step, then this step will be taken + */ + private void moveCompHardLevel(Button[] table) { + int move = goodMove(toStringArray(table), getCompSymb(0), getCompSymb(1)); + if (move == -1) { + moveCompEasyLevel(table); + } else { + table[move].setText(getCompSymb(0)); + table[move].setDisable(true); + check(toStringArray(table), false); + } + } + + /** + * Check, if there is a possibility of winning in one step, then return index of cell + * And interfere enemy win in the next move + * + * @return number cell + */ + public static int goodMove(String[] tableString, String symbolPlayer1, String symbolPlayer2) { + + for (int i = 0; i < tableString.length; i++) { + if (tableString[i].equals("")) { + tableString[i] = symbolPlayer1; + String whoWin = Game.check(tableString); + if (whoWin.equals(symbolPlayer1)) { + return i; + } + tableString[i] = ""; + } + } + for (int i = 0; i < tableString.length; i++) { + if (tableString[i].equals("")) { + tableString[i] = symbolPlayer2; + String whoWin = check(tableString); + if (whoWin.equals(symbolPlayer2)) { + return i; + } + tableString[i] = ""; + } + } + return -1; + } +} diff --git a/02.TicTacToe/src/main/java/ru/spbau/mit/alyokhina/TicTacToe/GameWithFriend.java b/02.TicTacToe/src/main/java/ru/spbau/mit/alyokhina/TicTacToe/GameWithFriend.java new file mode 100644 index 0000000..0370136 --- /dev/null +++ b/02.TicTacToe/src/main/java/ru/spbau/mit/alyokhina/TicTacToe/GameWithFriend.java @@ -0,0 +1,51 @@ +package ru.spbau.mit.alyokhina.TicTacToe; + +import javafx.scene.control.Button; +import javafx.scene.layout.Pane; + + +public class GameWithFriend extends Game { + + /** + * Constructor + * + * @param statistics for collection information + */ + public GameWithFriend(Pane pane, Statistics statistics) { + super(pane, statistics); + } + + /** Menu. Two categories : play and get statistics */ + @Override + public void menu() { + Button buttonPlay = CreateElements.createButton(pane, 50, 200, 200, 110, "Играть"); + buttonPlay.setOnAction(actionEvent -> { + pane.getChildren().clear(); + CreateElements.createButtonReplay(this.pane, statistics, this::goBack); + playGame(); + }); + CreateElements.createButtonGetStatisticsForTwoPlayers(pane, statistics, value -> menu()); + } + + /** Start play */ + private void playGame() { + Button[] table = CreateElements.createTableView(pane); + changeMove(table, 0); + } + + /** Change players move */ + private void changeMove(Button[] table, int x) { + String c = getCompSymb(x); + for (Button button : table) { + button.setOnAction(actionEvent -> { + button.setText(c); + button.setDisable(true); + check(toStringArray(table), false); + changeMove(table, 1 - x); + }); + } + + } + + +} diff --git a/02.TicTacToe/src/main/java/ru/spbau/mit/alyokhina/TicTacToe/Main.java b/02.TicTacToe/src/main/java/ru/spbau/mit/alyokhina/TicTacToe/Main.java new file mode 100644 index 0000000..495bd74 --- /dev/null +++ b/02.TicTacToe/src/main/java/ru/spbau/mit/alyokhina/TicTacToe/Main.java @@ -0,0 +1,23 @@ +package ru.spbau.mit.alyokhina.TicTacToe; + +import javafx.application.Application; + +import javafx.scene.Scene; +import javafx.scene.layout.Pane; +import javafx.stage.Stage; + +public class Main extends Application { + + @Override + public void start(Stage primaryStage) throws Exception { + primaryStage.setTitle("Крестики - Нолики"); + Pane root = new Pane(); + primaryStage.setScene(new Scene(root, 600, 400)); + primaryStage.show(); + CreateElements.createMainActivity(root); + } + + public static void main(String[] args) { + launch(args); + } +} diff --git a/02.TicTacToe/src/main/java/ru/spbau/mit/alyokhina/TicTacToe/Statistics.java b/02.TicTacToe/src/main/java/ru/spbau/mit/alyokhina/TicTacToe/Statistics.java new file mode 100644 index 0000000..7ca02a9 --- /dev/null +++ b/02.TicTacToe/src/main/java/ru/spbau/mit/alyokhina/TicTacToe/Statistics.java @@ -0,0 +1,30 @@ +package ru.spbau.mit.alyokhina.TicTacToe; + +/** Class for collection information about games */ +public class Statistics { + private int countWins = 0; + private int countLose = 0; + private int countDraw = 0; + + public void incWins() { + countWins++; + } + + public void incLose() { + countLose++; + } + public void incDraw() { + countDraw++; + } + public Integer getCountWins() { + return countWins; + } + + public Integer getCountLose() { + return countLose; + } + + public Integer getCountDraw() { + return countDraw; + } +} diff --git a/02.TicTacToe/src/test/java/ru/spbau/mit/alyokhina/TicTacToe/GameTest.java b/02.TicTacToe/src/test/java/ru/spbau/mit/alyokhina/TicTacToe/GameTest.java new file mode 100644 index 0000000..1c99265 --- /dev/null +++ b/02.TicTacToe/src/test/java/ru/spbau/mit/alyokhina/TicTacToe/GameTest.java @@ -0,0 +1,109 @@ +package ru.spbau.mit.alyokhina.TicTacToe; + +import org.junit.Test; + +import static org.junit.Assert.*; + +public class GameTest { + @Test + public void checkIfNoResult() { + String board[] = new String[]{ + "", "", "", + "", "", "", + "", "", "" + }; + + assertEquals("", Game.check(board)); + } + + @Test + public void checkIfXWinInDiagonal() { + String board[] = new String[]{ + "X", "O", "O", + "", "X", "", + "", "", "X" + }; + + assertEquals("X", Game.check(board)); + } + + @Test + public void checkIfXWinInVertical() { + String board[] = new String[]{ + "X", "O", "", + "X", "", "O", + "X", "", "" + }; + + assertEquals("X", Game.check(board)); + } + + @Test + public void checkIfXWinInHorizontal() { + String board[] = new String[]{ + "", "O", "", + "X", "X", "X", + "", "O", "" + }; + + assertEquals("X", Game.check(board)); + } + + @Test + public void checkIfOWinInDiagonal() { + String board[] = new String[]{ + "O", "X", "X", + "", "O", "", + "", "", "O" + }; + + assertEquals("O", Game.check(board)); + } + + @Test + public void checkIfOWinInVertical() { + String board[] = new String[]{ + "O", "X", "", + "O", "", "X", + "O", "", "" + }; + + assertEquals("O", Game.check(board)); + } + + @Test + public void checkIfOWinInHorizontal() { + String board[] = new String[]{ + "", "X", "", + "O", "O", "O", + "", "X", "" + }; + + assertEquals("O", Game.check(board)); + } + + + @Test + public void checkIfDraw() { + String board[] = new String[]{ + "X", "O", "X", + "O", "X", "X", + "O", "X", "O" + }; + + assertEquals("draw", Game.check(board)); + } + + @Test + public void checkIfExistMove() { + String board[] = new String[]{ + "X", "O", "", + "O", "X", "X", + "O", "X", "O" + }; + + assertEquals("", Game.check(board)); + } + + +} \ No newline at end of file diff --git a/02.TicTacToe/src/test/java/ru/spbau/mit/alyokhina/TicTacToe/GameWithCompTest.java b/02.TicTacToe/src/test/java/ru/spbau/mit/alyokhina/TicTacToe/GameWithCompTest.java new file mode 100644 index 0000000..f00d64a --- /dev/null +++ b/02.TicTacToe/src/test/java/ru/spbau/mit/alyokhina/TicTacToe/GameWithCompTest.java @@ -0,0 +1,51 @@ +package ru.spbau.mit.alyokhina.TicTacToe; + +import org.junit.Test; + +import static org.junit.Assert.*; + +public class GameWithCompTest { + @Test + public void goodMoveEmptyBoard() { + String board[] = new String[]{ + "", "", "", + "", "", "", + "", "", "" + }; + + assertEquals(-1, GameWithComp.goodMove(board, "X", "O")); + } + + @Test + public void goodMoveifCanBeMoveOnDiagonal() { + String board[] = new String[]{ + "X", "", "", + "", "", "", + "O", "", "X" + }; + + assertEquals(4, GameWithComp.goodMove(board, "X", "O")); + } + + @Test + public void goodMoveIfCanBeMoveOnVertical() { + String board[] = new String[]{ + "X", "X", "", + "", "", "", + "O", "", "O" + }; + + assertEquals(2, GameWithComp.goodMove(board, "X", "O")); + } + + @Test + public void goodMoveIfCanBeMoveOnHorizontal() { + String board[] = new String[]{ + "X", "", "", + "", "X", "X", + "O", "", "O" + }; + + assertEquals(3, GameWithComp.goodMove(board, "X", "O")); + } +} \ 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 + From 4ac1328b55d7f564565476739b2bfa51248e1086 Mon Sep 17 00:00:00 2001 From: alyokhina-olya Date: Mon, 9 Apr 2018 03:57:56 +0300 Subject: [PATCH 2/2] add GridPane --- .../alyokhina/TicTacToe/CreateElements.java | 83 ++++++++++--------- .../spbau/mit/alyokhina/TicTacToe/Game.java | 62 +++++++++----- .../mit/alyokhina/TicTacToe/GameWithComp.java | 24 +++--- .../alyokhina/TicTacToe/GameWithFriend.java | 15 ++-- .../spbau/mit/alyokhina/TicTacToe/Main.java | 14 +++- 5 files changed, 114 insertions(+), 84 deletions(-) diff --git a/02.TicTacToe/src/main/java/ru/spbau/mit/alyokhina/TicTacToe/CreateElements.java b/02.TicTacToe/src/main/java/ru/spbau/mit/alyokhina/TicTacToe/CreateElements.java index b22845b..b788eb6 100644 --- a/02.TicTacToe/src/main/java/ru/spbau/mit/alyokhina/TicTacToe/CreateElements.java +++ b/02.TicTacToe/src/main/java/ru/spbau/mit/alyokhina/TicTacToe/CreateElements.java @@ -2,8 +2,11 @@ import javafx.application.Platform; +import javafx.geometry.Insets; import javafx.scene.control.Button; import javafx.scene.control.TextArea; +import javafx.scene.layout.GridPane; +import javafx.scene.layout.HBox; import javafx.scene.layout.Pane; import java.util.function.Consumer; @@ -19,9 +22,9 @@ public class CreateElements { * @param text this text will be on the button * @return new button */ - public static Button createButton(Pane pane, double height, double width, double x, double y, String text) { + public static Button createButton(GridPane gridPane, double height, double width, int x, int y, String text) { Button button = new Button(); - pane.getChildren().add(button); + gridPane.add(button, x, y); button.setText(text); button.setPrefHeight(height); button.setPrefWidth(width); @@ -36,26 +39,26 @@ public static Button createButton(Pane pane, double height, double width, double * * @return array of buttons */ - public static Button[] createTableView(Pane pane) { + public static Button[] createTableView(GridPane gridPane) { Button[] buttons = new Button[9]; for (int i = 0; i < 9; i++) { - buttons[i] = createButton(pane, 60, 60, 210 + 60 * (i % 3), 110 + 60 * (i / 3), ""); + buttons[i] = createButton(gridPane, 60, 60, 210 + 60 * (i % 3), 110 + 60 * (i / 3), ""); } return buttons; } /**Create Menu for Main Activity */ - public static void createMainActivity(Pane pane) { - Button buttonPlayWithComp = CreateElements.createButton(pane, 50, 200, 200, 70, "Играть с компьютером"); - Button buttonPlayWithFriend = CreateElements.createButton(pane, 50, 200, 200, 170, "Играть с другом"); - Button buttonExit = CreateElements.createButton(pane, 50, 200, 200, 270, "Выход"); + public static void createMainActivity(GridPane gridPane) { + Button buttonPlayWithComp = CreateElements.createButton(gridPane, 50, 200, 0, 0, "Играть с компьютером"); + Button buttonPlayWithFriend = CreateElements.createButton(gridPane, 50, 200, 0, 1, "Играть с другом"); + Button buttonExit = CreateElements.createButton(gridPane, 50, 200, 0, 2, "Выход"); buttonPlayWithComp.setOnAction(actionEvent -> { - GameWithComp gameWithComp = new GameWithComp(pane, new Statistics()); + GameWithComp gameWithComp = new GameWithComp(gridPane, new Statistics()); gameWithComp.menu(); }); buttonPlayWithFriend.setOnAction(actionEvent -> { - GameWithFriend gameWithFriend = new GameWithFriend(pane, new Statistics()); + GameWithFriend gameWithFriend = new GameWithFriend(gridPane, new Statistics()); gameWithFriend.menu(); }); buttonExit.setOnAction(value -> Platform.exit()); @@ -67,21 +70,21 @@ public static void createMainActivity(Pane pane) { * @param statistics is parameter for consumer * @param consumer will be accepted after clicking on the button */ - public static void createButtonReplay(Pane pane, Statistics statistics, Consumer consumer) { - Button rePlay = CreateElements.createButton(pane, 50, 200, 400, 20, "Начать новую партию"); + public static void createButtonReplay(GridPane gridPane, Statistics statistics, Consumer consumer) { + Button rePlay = CreateElements.createButton(gridPane, 50, 200, 400, 20, "Начать новую партию"); rePlay.setOnAction(actionEvent -> { - pane.getChildren().clear(); + gridPane.getChildren().clear(); consumer.accept(statistics); }); } /** Create button to go to the main activity */ - public static void createButtonToMainActivity(Pane pane) { - Button buttonToMainActivity = CreateElements.createButton(pane, 50, 200, 400, 350, "В главное меню"); + public static void createButtonToMainActivity(GridPane gridPane) { + Button buttonToMainActivity = CreateElements.createButton(gridPane, 50, 200, 400, 350, "В главное меню"); buttonToMainActivity.setOnAction(actionEvent -> { - pane.getChildren().clear(); - createMainActivity(pane); + gridPane.getChildren().clear(); + createMainActivity(gridPane); }); } @@ -91,14 +94,14 @@ public static void createButtonToMainActivity(Pane pane) { * * @param go will be accepted after clicking button back */ - public static void createButtonGetStatisticsForOnePlayers(Pane pane, Statistics statistics, Consumer go) { - Button getStatistics = CreateElements.createButton(pane, 50, 200, 200, 270, "Статистика"); + public static void createButtonGetStatisticsForOnePlayers(GridPane gridPane, Statistics statistics, Consumer go) { + Button getStatistics = CreateElements.createButton(gridPane, 50, 200, 200, 270, "Статистика"); getStatistics.setOnAction(actionEvent -> { - pane.getChildren().clear(); - CreateElements.createButtonToMainActivity(pane); - Button buttonBack = CreateElements.createButton(pane, 50, 200, 400, 20, "Назад"); + gridPane.getChildren().clear(); + CreateElements.createButtonToMainActivity(gridPane); + Button buttonBack = CreateElements.createButton(gridPane, 50, 200, 400, 20, "Назад"); buttonBack.setOnAction(actionEvent1 -> { - pane.getChildren().clear(); + gridPane.getChildren().clear(); go.accept(statistics); }); TextArea textArea = new TextArea(); @@ -106,7 +109,7 @@ public static void createButtonGetStatisticsForOnePlayers(Pane pane, Statistics textArea.setLayoutX(0); textArea.setPrefHeight(600); textArea.setPrefWidth(200); - pane.getChildren().add(textArea); + gridPane.getChildren().add(textArea); textArea.setText("количество побед: " + statistics.getCountWins().toString() + "\nколичество поражений: " + statistics.getCountLose().toString() + "\nколичество ничьих: " + statistics.getCountDraw().toString()); @@ -120,22 +123,22 @@ public static void createButtonGetStatisticsForOnePlayers(Pane pane, Statistics * * @param go will be accepted after clicking button back */ - public static void createButtonGetStatisticsForTwoPlayers(Pane pane, Statistics statistics, Consumer go) { - Button getStatistics = CreateElements.createButton(pane, 50, 200, 200, 240, "Статистика"); + public static void createButtonGetStatisticsForTwoPlayers(GridPane gridPane, Statistics statistics, Consumer go) { + Button getStatistics = CreateElements.createButton(gridPane, 50, 200, 200, 240, "Статистика"); getStatistics.setOnAction(actionEvent -> { - pane.getChildren().clear(); - CreateElements.createButtonToMainActivity(pane); - Button button = CreateElements.createButton(pane, 50, 200, 400, 20, "Назад"); + gridPane.getChildren().clear(); + CreateElements.createButtonToMainActivity(gridPane); + Button button = CreateElements.createButton(gridPane, 50, 200, 400, 20, "Назад"); button.setOnAction(actionEvent1 -> { - pane.getChildren().clear(); + gridPane.getChildren().clear(); go.accept(statistics); }); TextArea textAreaForFirst = new TextArea(); - textAreaForFirst.setLayoutY(0); - textAreaForFirst.setLayoutX(0); + //textAreaForFirst.setLayoutY(0); + //textAreaForFirst.setLayoutX(0); textAreaForFirst.setPrefHeight(600); textAreaForFirst.setPrefWidth(200); - pane.getChildren().add(textAreaForFirst); + gridPane.add(textAreaForFirst, 0, 0); textAreaForFirst.setText("Х \nколичество побед: " + statistics.getCountLose().toString() + "\nколичество поражений: " + statistics.getCountWins().toString() + "\nколичество ничьих: " + statistics.getCountDraw().toString()); @@ -146,7 +149,7 @@ public static void createButtonGetStatisticsForTwoPlayers(Pane pane, Statistics textAreaForSecond.setLayoutX(200); textAreaForSecond.setPrefHeight(600); textAreaForSecond.setPrefWidth(200); - pane.getChildren().add(textAreaForSecond); + gridPane.add(textAreaForSecond, 5, 0); textAreaForSecond.setText("О \nколичество побед: " + statistics.getCountWins().toString() + "\nколичество поражений: " + statistics.getCountLose().toString() + "\nколичество ничьих: " + statistics.getCountDraw().toString()); @@ -154,9 +157,9 @@ public static void createButtonGetStatisticsForTwoPlayers(Pane pane, Statistics } /** Print result on screen and update statistics */ - public static void setResult(Pane pane, Statistics statistics, String win) { - pane.getChildren().clear(); - CreateElements.createButtonToMainActivity(pane); + public static void setResult(GridPane gridPane, Statistics statistics, String win) { + gridPane.getChildren().clear(); + CreateElements.createButtonToMainActivity(gridPane); switch (win) { case "draw": { TextArea textArea = new TextArea(); @@ -165,7 +168,7 @@ public static void setResult(Pane pane, Statistics statistics, String win) { textArea.setPrefHeight(50); textArea.setPrefWidth(100); textArea.setText("draw"); - pane.getChildren().add(textArea); + gridPane.getChildren().add(textArea); statistics.incDraw(); break; } @@ -176,7 +179,7 @@ public static void setResult(Pane pane, Statistics statistics, String win) { textArea.setPrefHeight(50); textArea.setPrefWidth(100); textArea.setText("X wins"); - pane.getChildren().add(textArea); + gridPane.getChildren().add(textArea); statistics.incLose(); break; } @@ -186,7 +189,7 @@ public static void setResult(Pane pane, Statistics statistics, String win) { textArea.setLayoutX(250); textArea.setPrefHeight(50); textArea.setPrefWidth(100); - pane.getChildren().add(textArea); + gridPane.getChildren().add(textArea); textArea.setText("O wins"); statistics.incWins(); break; diff --git a/02.TicTacToe/src/main/java/ru/spbau/mit/alyokhina/TicTacToe/Game.java b/02.TicTacToe/src/main/java/ru/spbau/mit/alyokhina/TicTacToe/Game.java index 8c6284c..bedd786 100644 --- a/02.TicTacToe/src/main/java/ru/spbau/mit/alyokhina/TicTacToe/Game.java +++ b/02.TicTacToe/src/main/java/ru/spbau/mit/alyokhina/TicTacToe/Game.java @@ -1,28 +1,38 @@ package ru.spbau.mit.alyokhina.TicTacToe; import javafx.scene.control.Button; -import javafx.scene.layout.Pane; +import javafx.scene.layout.GridPane; -/** Class for key actions of game */ +/** + * Class for key actions of game + */ public class Game { /** - * Pane for current game + * GridPane for current game + */ + protected GridPane gridPane; + /** + * Number playing */ - protected Pane pane; - /** Number playing */ protected int numberGames = 0; - /** Statistics for current game */ + /** + * Statistics for current game + */ protected Statistics statistics; - /** Constructor */ - public Game(Pane pane, Statistics statistics) { - this.pane = pane; + /** + * Constructor + */ + public Game(GridPane gridPane, Statistics statistics) { + this.gridPane = gridPane; this.statistics = statistics; - pane.getChildren().clear(); - CreateElements.createButtonToMainActivity(pane); + gridPane.getChildren().clear(); + CreateElements.createButtonToMainActivity(gridPane); } - /** Translated text from buttons array in string array */ + /** + * Translated text from buttons array in string array + */ protected String[] toStringArray(Button[] table) { String[] tableString = new String[table.length]; for (int i = 0; i < table.length; i++) { @@ -89,32 +99,40 @@ protected String check(String[] tableString, boolean isPreliminaryCheck) { return result; } - /** Print result games */ + /** + * Print result games + */ private void setResult(String win) { numberGames++; - CreateElements.setResult(pane, statistics, win); - CreateElements.createButtonReplay(pane, statistics, this::goBack); + CreateElements.setResult(gridPane, statistics, win); + CreateElements.createButtonReplay(gridPane, statistics, this::goBack); } - /** For return in this activity */ + /** + * For return in this activity + */ protected void goBack(Statistics statistics) { this.statistics = statistics; - pane.getChildren().clear(); - CreateElements.createButtonToMainActivity(pane); + gridPane.getChildren().clear(); + CreateElements.createButtonToMainActivity(gridPane); menu(); } - /** Symbol to which the player move */ + /** + * Symbol to which the player move + */ public String getCompSymb(int player) { return (numberGames % 2 == player) ? "X" : "O"; } - /** Before games we should choose characteristics game */ + /** + * Before games we should choose characteristics game + */ public void menu() { - CreateElements.createButton(pane, 50, 200, 200, 70, "Легкий"); - CreateElements.createButton(pane, 50, 200, 200, 170, "Сложный"); + CreateElements.createButton(gridPane, 50, 200, 200, 70, "Легкий"); + CreateElements.createButton(gridPane, 50, 200, 200, 170, "Сложный"); } } diff --git a/02.TicTacToe/src/main/java/ru/spbau/mit/alyokhina/TicTacToe/GameWithComp.java b/02.TicTacToe/src/main/java/ru/spbau/mit/alyokhina/TicTacToe/GameWithComp.java index 49555ba..ea5fd07 100644 --- a/02.TicTacToe/src/main/java/ru/spbau/mit/alyokhina/TicTacToe/GameWithComp.java +++ b/02.TicTacToe/src/main/java/ru/spbau/mit/alyokhina/TicTacToe/GameWithComp.java @@ -2,7 +2,7 @@ import javafx.scene.control.Button; -import javafx.scene.layout.Pane; +import javafx.scene.layout.GridPane; import java.util.Random; import java.util.function.Consumer; @@ -13,8 +13,8 @@ public class GameWithComp extends Game { * * @param statistics for collection information */ - public GameWithComp(Pane pane, Statistics statistics) { - super(pane, statistics); + public GameWithComp(GridPane gridPane, Statistics statistics) { + super(gridPane, statistics); } @@ -24,17 +24,17 @@ public GameWithComp(Pane pane, Statistics statistics) { */ @Override public void menu() { - CreateElements.createButtonGetStatisticsForOnePlayers(pane, statistics, value -> menu()); - Button buttonEasyLevel = CreateElements.createButton(pane, 50, 200, 200, 70, "Легкий"); - Button buttonHardLevel = CreateElements.createButton(pane, 50, 200, 200, 170, "Сложный"); + CreateElements.createButtonGetStatisticsForOnePlayers(gridPane, statistics, value -> menu()); + Button buttonEasyLevel = CreateElements.createButton(gridPane, 50, 200, 200, 70, "Легкий"); + Button buttonHardLevel = CreateElements.createButton(gridPane, 50, 200, 200, 170, "Сложный"); buttonEasyLevel.setOnAction(value -> { - pane.getChildren().clear(); - CreateElements.createButtonToMainActivity(pane); + gridPane.getChildren().clear(); + CreateElements.createButtonToMainActivity(gridPane); playGame(this::moveCompEasyLevel); }); buttonHardLevel.setOnAction(value -> { - pane.getChildren().clear(); - CreateElements.createButtonToMainActivity(pane); + gridPane.getChildren().clear(); + CreateElements.createButtonToMainActivity(gridPane); playGame(this::moveCompHardLevel); }); } @@ -45,8 +45,8 @@ public void menu() { * @param moveComp move computer */ private void playGame(Consumer moveComp) { - CreateElements.createButtonReplay(pane, statistics, this::goBack); - Button[] table = CreateElements.createTableView(pane); + CreateElements.createButtonReplay(gridPane, statistics, this::goBack); + Button[] table = CreateElements.createTableView(gridPane); if (numberGames % 2 == 0) { moveComp.accept(table); } diff --git a/02.TicTacToe/src/main/java/ru/spbau/mit/alyokhina/TicTacToe/GameWithFriend.java b/02.TicTacToe/src/main/java/ru/spbau/mit/alyokhina/TicTacToe/GameWithFriend.java index 0370136..1e7ae0b 100644 --- a/02.TicTacToe/src/main/java/ru/spbau/mit/alyokhina/TicTacToe/GameWithFriend.java +++ b/02.TicTacToe/src/main/java/ru/spbau/mit/alyokhina/TicTacToe/GameWithFriend.java @@ -1,6 +1,7 @@ package ru.spbau.mit.alyokhina.TicTacToe; import javafx.scene.control.Button; +import javafx.scene.layout.GridPane; import javafx.scene.layout.Pane; @@ -11,25 +12,25 @@ public class GameWithFriend extends Game { * * @param statistics for collection information */ - public GameWithFriend(Pane pane, Statistics statistics) { - super(pane, statistics); + public GameWithFriend(GridPane gridPane, Statistics statistics) { + super(gridPane, statistics); } /** Menu. Two categories : play and get statistics */ @Override public void menu() { - Button buttonPlay = CreateElements.createButton(pane, 50, 200, 200, 110, "Играть"); + Button buttonPlay = CreateElements.createButton(gridPane, 50, 200, 200, 110, "Играть"); buttonPlay.setOnAction(actionEvent -> { - pane.getChildren().clear(); - CreateElements.createButtonReplay(this.pane, statistics, this::goBack); + gridPane.getChildren().clear(); + CreateElements.createButtonReplay(this.gridPane, statistics, this::goBack); playGame(); }); - CreateElements.createButtonGetStatisticsForTwoPlayers(pane, statistics, value -> menu()); + CreateElements.createButtonGetStatisticsForTwoPlayers(gridPane, statistics, value -> menu()); } /** Start play */ private void playGame() { - Button[] table = CreateElements.createTableView(pane); + Button[] table = CreateElements.createTableView(gridPane); changeMove(table, 0); } diff --git a/02.TicTacToe/src/main/java/ru/spbau/mit/alyokhina/TicTacToe/Main.java b/02.TicTacToe/src/main/java/ru/spbau/mit/alyokhina/TicTacToe/Main.java index 495bd74..29ddc41 100644 --- a/02.TicTacToe/src/main/java/ru/spbau/mit/alyokhina/TicTacToe/Main.java +++ b/02.TicTacToe/src/main/java/ru/spbau/mit/alyokhina/TicTacToe/Main.java @@ -2,8 +2,14 @@ import javafx.application.Application; +import javafx.geometry.Insets; +import javafx.geometry.Pos; import javafx.scene.Scene; +import javafx.scene.control.Button; +import javafx.scene.layout.GridPane; +import javafx.scene.layout.HBox; import javafx.scene.layout.Pane; +import javafx.scene.layout.VBox; import javafx.stage.Stage; public class Main extends Application { @@ -11,10 +17,12 @@ public class Main extends Application { @Override public void start(Stage primaryStage) throws Exception { primaryStage.setTitle("Крестики - Нолики"); - Pane root = new Pane(); - primaryStage.setScene(new Scene(root, 600, 400)); + GridPane gridPane = new GridPane(); + gridPane.setAlignment(Pos.CENTER); + CreateElements.createMainActivity(gridPane); + primaryStage.setScene(new Scene(gridPane, 600, 400)); primaryStage.show(); - CreateElements.createMainActivity(root); + } public static void main(String[] args) {