Compare commits
14 Commits
1930585434
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
85ba9a77b9
|
|||
|
337c673c4d
|
|||
|
15df500d82
|
|||
|
be4e036e46
|
|||
|
|
dbf180215d | ||
|
|
5c66f61bc4 | ||
|
|
562b196912 | ||
|
|
a1733ef25d | ||
|
|
e6e781c216 | ||
|
|
3d3675118b | ||
|
|
1c964ecfe3 | ||
|
|
32d4698421 | ||
|
|
324fbd5da4 | ||
|
|
f76e948f38 |
19
.github/workflows/maven.yml
vendored
Normal file
19
.github/workflows/maven.yml
vendored
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
name: Java CI with Maven
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches: [ "master" ]
|
||||||
|
pull_request:
|
||||||
|
branches: [ "master" ]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
container:
|
||||||
|
image: nektos/act-environments-ubuntu:18.04
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v6
|
||||||
|
|
||||||
|
- name: Build with Maven
|
||||||
|
run: mvn -B package --file pom.xml
|
||||||
18
pom.xml
18
pom.xml
@@ -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>
|
||||||
@@ -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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
3
src/main/resources/META-INF/MANIFEST.MF
Normal file
3
src/main/resources/META-INF/MANIFEST.MF
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
Manifest-Version: 1.0
|
||||||
|
Main-Class: me.taako.swt.prog2.Main
|
||||||
|
|
||||||
Reference in New Issue
Block a user