Compare commits

..

10 Commits

Author SHA1 Message Date
POGROTH THE HYPED
dbf180215d Add GitHub Actions workflow for Java CI with Maven
Some checks failed
Java CI with Maven / build (push) Failing after 5m34s
2024-06-22 12:40:24 +02:00
POGROTH THE HYPED
5c66f61bc4 Updated project version to 1.4.2 in pom.xml 2024-06-22 12:26:15 +02:00
POGROTH THE HYPED
562b196912 Fix tile swapping issue
This commit fixes incorrect tile swaps at puzzle grid edges.
2024-06-22 12:09:17 +02:00
POGROTH THE HYPED
a1733ef25d Rename artifactId in pom.xml 2024-06-22 11:56:09 +02:00
POGROTH THE HYPED
e6e781c216 Update pom.xml for JAR configuration 2024-06-22 09:10:35 +02:00
POGROTH THE HYPED
3d3675118b Add Manifest file 2024-06-22 09:10:15 +02:00
POGROTH THE HYPED
1c964ecfe3 Integrate MVC components in Main 2024-06-22 09:05:39 +02:00
POGROTH THE HYPED
32d4698421 Implement PuzzleController class 2024-06-22 08:58:36 +02:00
POGROTH THE HYPED
324fbd5da4 Implement PuzzleView class 2024-06-22 08:58:08 +02:00
POGROTH THE HYPED
f76e948f38 Implement PuzzleModel class 2024-06-22 08:57:43 +02:00
7 changed files with 218 additions and 5 deletions

31
.github/workflows/maven.yml vendored Normal file
View File

@@ -0,0 +1,31 @@
# This workflow will build a Java project with Maven, and cache/restore any dependencies to improve the workflow execution time
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-java-with-maven
# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.
name: Java CI with Maven
on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up JDK 21
uses: actions/setup-java@v3
with:
java-version: '21'
distribution: 'temurin'
cache: maven
- name: Build with Maven
run: mvn -B package --file pom.xml

18
pom.xml
View File

@@ -5,8 +5,8 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>me.taako.swt.prog2</groupId> <groupId>me.taako.swt.prog2</groupId>
<artifactId>puzzle</artifactId> <artifactId>mvc-puzzle</artifactId>
<version>1.0-SNAPSHOT</version> <version>1.4.2</version>
<properties> <properties>
<maven.compiler.source>21</maven.compiler.source> <maven.compiler.source>21</maven.compiler.source>
@@ -14,4 +14,18 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> </properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifestFile>src/main/resources/META-INF/MANIFEST.MF</manifestFile>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project> </project>

View File

@@ -1,7 +1,14 @@
package me.taako.swt.prog2; package me.taako.swt.prog2;
import me.taako.swt.prog2.controller.PuzzleController;
import me.taako.swt.prog2.model.PuzzleModel;
import me.taako.swt.prog2.view.PuzzleView;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
System.out.println("Hello world!"); PuzzleModel model = new PuzzleModel(4, 4);
PuzzleView view = new PuzzleView(model);
PuzzleController controller = new PuzzleController(model, view);
controller.initController();
} }
} }

View File

@@ -1,4 +1,47 @@
package me.taako.swt.prog2.controller; package me.taako.swt.prog2.controller;
import me.taako.swt.prog2.model.PuzzleModel;
import me.taako.swt.prog2.view.PuzzleView;
import javax.swing.*;
import java.util.List;
import java.util.stream.IntStream;
public class PuzzleController { public class PuzzleController {
} private final PuzzleModel model;
private final PuzzleView view;
public PuzzleController(PuzzleModel model, PuzzleView view) {
this.model = model;
this.view = view;
}
public void initController() {
List<JButton> buttons = view.getButtons();
IntStream.range(0, buttons.size())
.forEach(i -> buttons.get(i)
.addActionListener(e -> handleButtonClick(i)));
}
private void handleButtonClick(int index) {
int size = model.getSize();
int sqrtSize = (int) Math.sqrt(size);
int emptyTile = size - 1;
if (index % sqrtSize != 0 && model.getTile(index - 1) == emptyTile) {
model.swapTiles(index, index - 1);
} else if ((index + 1) % sqrtSize != 0 && model.getTile(index + 1) == emptyTile) {
model.swapTiles(index, index + 1);
} else if (index >= sqrtSize && model.getTile(index - sqrtSize) == emptyTile) {
model.swapTiles(index, index - sqrtSize);
} else if (index < size - sqrtSize && model.getTile(index + sqrtSize) == emptyTile) {
model.swapTiles(index, index + sqrtSize);
}
view.update();
if (model.isSolved()) {
JOptionPane.showMessageDialog(view, "Puzzle gelöst!");
}
}
}

View File

@@ -1,4 +1,48 @@
package me.taako.swt.prog2.model; package me.taako.swt.prog2.model;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.IntStream;
public class PuzzleModel { public class PuzzleModel {
private final int size;
private final List<Integer> tiles;
public PuzzleModel(int rows, int cols) {
this.size = rows * cols;
this.tiles = new ArrayList<>(size);
initTiles();
shuffleTiles();
}
private void initTiles() {
IntStream.range(0, size)
.forEach(tiles::add);
}
private void shuffleTiles() {
Collections.shuffle(tiles);
}
public int getTile(int index) {
return tiles.get(index);
}
public int getSize() {
return size;
}
public boolean isTileCorrect(int index) {
return tiles.get(index) == index;
}
public boolean isSolved() {
return IntStream.range(0, size)
.allMatch(this::isTileCorrect);
}
public void swapTiles(int index1, int index2) {
Collections.swap(tiles, index1, index2);
}
} }

View File

@@ -1,4 +1,75 @@
package me.taako.swt.prog2.view; package me.taako.swt.prog2.view;
public class PuzzleView { import me.taako.swt.prog2.model.PuzzleModel;
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.IntStream;
public class PuzzleView extends JFrame {
private final List<JButton> buttons = new ArrayList<>();
private final PuzzleModel model;
public PuzzleView(PuzzleModel model) {
this.model = model;
int gridSize = (int) Math.sqrt(model.getSize());
setLayout(new GridLayout(gridSize, gridSize, 5, 5));
initButtons();
setupWindow();
}
private void setupWindow() {
setTitle("Schiebe-Puzzle");
setSize(400, 400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
private void initButtons() {
int size = model.getSize();
IntStream.range(0, size)
.forEach(i -> {
JButton button = createButton();
buttons.add(button);
add(button);
});
update();
}
private JButton createButton() {
JButton button = new JButton();
button.setOpaque(true);
button.setBorderPainted(false);
return button;
}
public List<JButton> getButtons() {
return buttons;
}
public void update() {
int size = model.getSize();
Color correctColor = Color.GREEN;
Color incorrectColor = Color.ORANGE;
IntStream.range(0, size)
.forEach(i -> {
JButton button = buttons.get(i);
int tile = model.getTile(i);
if (tile != size - 1) {
button.setText(String.valueOf(tile + 1));
button.setBackground(model.isTileCorrect(i)
? correctColor
: incorrectColor);
} else {
button.setText("");
button.setBackground(null);
}
});
}
} }

View File

@@ -0,0 +1,3 @@
Manifest-Version: 1.0
Main-Class: me.taako.swt.prog2.Main