Commit 637e5b00 authored by Jens Ehlers's avatar Jens Ehlers
Browse files

Initial version of source codes

parent cbc34c5e
Pipeline #3834 passed with stage
in 33 seconds
.idea/
*.iml
target/
out/
bin/
*.class
.settings/
.project
.classpath
.factorypath
\ No newline at end of file
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>de.oncampus.patterns</groupId>
<artifactId>jpms</artifactId>
<version>1</version>
<packaging>pom</packaging>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<modules>
<module>regression-client</module>
<module>regression-provider</module>
</modules>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
\ No newline at end of file
<project>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>de.oncampus.patterns</groupId>
<artifactId>jpms</artifactId>
<version>1</version>
</parent>
<artifactId>jpms-regression-client</artifactId>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>de.oncampus.patterns</groupId>
<artifactId>jpms-regression-provider</artifactId>
<version>1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<configuration>
<mainClass>client.Client</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
\ No newline at end of file
package client;
import regression.SimpleRegressionModel;
class Client {
public static void main(String[] args) {
String json = "[{'x': 2, 'y': 4}, {'x': 4, 'y': 3}, {'x': 3, 'y': 6}, {'x': 9, 'y': 7}, {'x': 7, 'y': 8}]";
SimpleRegressionModel model = new SimpleRegressionModel(json);
System.out.println("Regression function: y = f(x) = " + model.getSlope() + "x + " + model.getIntercept());
System.out.println("R² = " + model.getDeterminationCoefficient());
double x = 5;
System.out.printf("Prediction: y = f(%.1f) = %.1f", x, model.predict(x));
}
}
module client {
requires simpleRegression;
}
\ No newline at end of file
<project>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>de.oncampus.patterns</groupId>
<artifactId>jpms</artifactId>
<version>1</version>
</parent>
<artifactId>jpms-regression-provider</artifactId>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-math3</artifactId>
<version>3.6.1</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<configuration>
<mainClass>regression.SimpleRegressionModel</mainClass>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
\ No newline at end of file
module simpleRegression {
requires gson;
requires commons.math3;
requires java.desktop;
requires transitive java.sql; // gson depends on java.sql
exports regression; // exports package
}
\ No newline at end of file
package regression;
import com.google.gson.Gson;
import org.apache.commons.math3.stat.regression.SimpleRegression;
import java.awt.Point;
public class SimpleRegressionModel {
Gson gson = new Gson(); // dependency to Google GSON
SimpleRegression model = new SimpleRegression(); // dependency to Apache Commons Math
public SimpleRegressionModel(String json) {
Point[] points = gson.fromJson(json, Point[].class); // dependency to Java Desktop (Point class)
for (Point p : points) model.addData(p.x, p.y);
}
public double getIntercept() {
return model.getIntercept();
}
public double getSlope() {
return model.getSlope();
}
public double getDeterminationCoefficient() {
return model.getRSquare();
}
public double predict(double x) {
return model.predict(x);
}
}
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>de.oncampus.patterns</groupId>
<artifactId>mvc</artifactId>
<version>1</version>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>11.0.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<configuration>
<mainClass>TicTacToeApplicationMVC</mainClass>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
</plugins>
</build>
</project>
\ No newline at end of file
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.IOException;
public class TicTacToeApplicationMVC extends Application {
@Override
public void start(Stage stage) throws IOException {
Parent view = FXMLLoader.load(getClass().getResource("view/tictactoe.fxml"));
Scene scene = new Scene(view);
stage.setScene(scene);
stage.setTitle("Tic Tac Toe");
stage.setResizable(false);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
package controller;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import model.Board;
import model.Player;
public class TicTacToeController {
private Board model; // controller is connected to model here
@FXML
private GridPane board;
@FXML
private Label winner;
@FXML
protected void initialize() {
model = new Board();
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
Button cell = new Button();
cell.getStyleClass().add("cell");
cell.setOnAction(event -> selectCell(event));
board.add(cell, col, row);
}
}
}
public void selectCell(ActionEvent event) {
Button cell = (Button) event.getSource();
int row = GridPane.getRowIndex(cell);
int col = GridPane.getColumnIndex(cell);
Player player = model.markCell(row, col);
if (player != null) {
cell.setText(player.toString());
if (model.getWinner() != null) {
winner.setText("Winner is " + player.toString());
}
}
}
public void reset(ActionEvent event) {
model.restart();
winner.setText("");
board.getChildren().forEach(node -> ((Button) node).setText(""));
}
}
\ No newline at end of file
package model;
public class Board {
Cell[][] cells = new Cell[3][3];
Player currentTurn;
Player winner;
public Board() {
restart();
}
/**
* Start a new game, i.e. clear the board and the game status.
*/
public void restart() {
clearCells();
winner = null;
currentTurn = Player.X;
}
/**
* Mark a cell for the player in turn.
* Nothing is done, if row or col are out of range, or the cell is already marked, or the game is finished.
*
* @param row 0..2
* @param col 0..2
* @return player who made the turn
*/
public Player markCell(int row, int col) {
if (isValid(row, col)) {
Player player = currentTurn;
cells[row][col].value = currentTurn;
if (isWinningMoveByPlayer(currentTurn, row, col)) { // check if game is won
winner = currentTurn;
} else {
currentTurn = currentTurn == Player.X ? Player.O : Player.X; // flip current turn
}
return player;
}
return null;
}
public Player getWinner() {
return winner;
}
void clearCells() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
cells[i][j] = new Cell();
}
}
}
boolean isValid(int row, int col) {
return !(winner != null || isOutOfBounds(row) || isOutOfBounds(col) || isCellValueAlreadySet(row, col));
}
boolean isOutOfBounds(int i) {
return i < 0 || i > 2;
}
boolean isCellValueAlreadySet(int row, int col) {
return cells[row][col].value != null;
}
boolean isWinningMoveByPlayer(Player player, int row, int col) {
return cells[row][0].value == player && cells[row][1].value == player && cells[row][2].value == player // 3 in a row
|| cells[0][col].value == player && cells[1][col].value == player && cells[2][col].value == player // 3 in a col
|| cells[0][0].value == player && cells[1][1].value == player && cells[2][2].value == player // 3 in a diagonal
|| cells[0][2].value == player && cells[1][1].value == player && cells[2][0].value == player; // 3 in other diagonal
}
}
package model;
class Cell {
Player value;
}
package model;
public enum Player {X, O}
#content {
-fx-padding: 20;
}
.cell {
-fx-background-color: #fff;
-fx-pref-width: 80;
-fx-pref-height: 80;
-fx-font-size: 24;
-fx-font-weight: bold;
-fx-effect: dropshadow(three-pass-box, #ccc, 5, 0, 2, 2);
}
.cell:hover {
-fx-background-color: #eee;
-fx-opacity: 0.75;
}
.cell:hover:pressed {
-fx-background-color: #eee;
-fx-opacity: 0.5;
}
.winnerMessage {
-fx-padding: 10 0 10 0;
-fx-font-size: 14;
-fx-text-fill: #cc0000;
}
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<?import java.net.URL?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.layout.GridPane?>
<StackPane xmlns="http://javafx.com/javafx/8.0.121" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.TicTacToeController">
<stylesheets>
<URL value="@tictactoe.css"/>
</stylesheets>
<children>
<VBox fx:id="content">
<GridPane fx:id="board" hgap="10" vgap="10"/>
<Label fx:id="winner" styleClass="winnerMessage"/>
<HBox alignment="CENTER_RIGHT">
<Button onAction="#reset" text="Reset"/>
</HBox>
</VBox>
</children>
</StackPane>
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>de.oncampus.patterns</groupId>
<artifactId>mvvm</artifactId>
<version>1</version>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>11.0.2</version>
</dependency>
<dependency>
<groupId>de.saxsys</groupId>
<artifactId>mvvmfx</artifactId>
<version>1.7.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<configuration>
<mainClass>TicTacToeApplicationMVVM</mainClass>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
</plugins>
</build>
</project>
\ No newline at end of file
import de.saxsys.mvvmfx.FluentViewLoader;
import de.saxsys.mvvmfx.ViewTuple;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import view.TicTacToeView;
import viewmodel.TicTacToeViewModel;
public class TicTacToeApplicationMVVM extends Application {
@Override
public void start(Stage stage) {
ViewTuple<TicTacToeView, TicTacToeViewModel> viewTuple = FluentViewLoader.fxmlView(TicTacToeView.class).load();
Scene scene = new Scene(viewTuple.getView());
stage.setScene(scene);
stage.setTitle("Tic Tac Toe");
stage.setResizable(false);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
package model;
public class Board {
Cell[][] cells = new Cell[3][3];
Player currentTurn;
Player winner;
public Board() {
restart();
}
/**
* Start a new game, i.e. clear the board and the game status.
*/
public void restart() {
clearCells();
winner = null;
currentTurn = Player.X;
}
/**
* Mark a cell for the player in turn.
* Nothing is done, if row or col are out of range, or the cell is already marked, or the game is finished.
*
* @param row 0..2
* @param col 0..2
* @return player who made the turn
*/
public Player markCell(int row, int col) {
if (isValid(row, col)) {
Player player = currentTurn;
cells[row][col].value = currentTurn;
if (isWinningMoveByPlayer(currentTurn, row, col)) { // check if game is won
winner = currentTurn;
} else {
currentTurn = currentTurn == Player.X ? Player.O : Player.X; // flip current turn
}
return player;
}
return null;
}
public Player getWinner() {
return winner;
}
void clearCells() {
for (int i = 0; i < 3; i++) {
for