Compare commits

..

15 Commits

59 changed files with 1410 additions and 1342 deletions

3
.idea/workspace.xml generated
View File

@ -5,7 +5,8 @@
</component> </component>
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="395d95f7-d752-4ca0-b261-78679f611e7e" name="Changes" comment=""> <list default="true" id="395d95f7-d752-4ca0-b261-78679f611e7e" name="Changes" comment="">
<change beforePath="$PROJECT_DIR$/list/src/main/java/de/vivi/list/Main.java" beforeDir="false" afterPath="$PROJECT_DIR$/list/src/main/java/de/vivi/list/Main.java" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/vcs.xml" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_CONFLICTS" value="true" />

3
bank/.idea/.gitignore generated vendored
View File

@ -1,3 +0,0 @@
# Default ignored files
/shelf/
/workspace.xml

View File

@ -1,13 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<annotationProcessing>
<profile name="Maven default annotation processors profile" enabled="true">
<sourceOutputDir name="target/generated-sources/annotations" />
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
<outputRelativeToContentRoot value="true" />
<module name="bank" />
</profile>
</annotationProcessing>
</component>
</project>

View File

@ -1,7 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
</component>
</project>

View File

@ -1,20 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RemoteRepositoriesConfiguration">
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Central Repository" />
<option name="url" value="https://repo.maven.apache.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Maven Central repository" />
<option name="url" value="https://repo1.maven.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="jboss.community" />
<option name="name" value="JBoss Community repository" />
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
</remote-repository>
</component>
</project>

12
bank/.idea/misc.xml generated
View File

@ -1,12 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="temurin-21" project-jdk-type="JavaSDK" />
</project>

View File

@ -1,124 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Palette2">
<group name="Swing">
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
</item>
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
</item>
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.svg" removable="false" auto-create-binding="false" can-attach-label="true">
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
</item>
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
<initial-values>
<property name="text" value="Button" />
</initial-values>
</item>
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="RadioButton" />
</initial-values>
</item>
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="CheckBox" />
</initial-values>
</item>
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
<initial-values>
<property name="text" value="Label" />
</initial-values>
</item>
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
</item>
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
</item>
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
<preferred-size width="-1" height="20" />
</default-constraints>
</item>
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
</item>
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
</item>
</group>
</component>
</project>

6
bank/.idea/vcs.xml generated
View File

@ -1,6 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
</component>
</project>

View File

View File

View File

@ -1,41 +0,0 @@
$> create Paul
Created account with id: '0' and name 'Paul'
$> create Peter
Created account with id: '1' and name 'Peter'
$> create Mic
Created account with id: '2' and name 'Mic'
$> info 1
Info for account '1': [Name: 'Peter', Balance: 0$]
$> add 1 100
Added 100$ to account '1'
$> info 1
Info for account '1': [Name: 'Paul', Balance: 0$]
$> transfer 1 0 40
Transferred 40$ from account '1' to account '0'
$> info 0
Info for account '0': [Name: 'Paul', Balance: 40$]
$> info 1
Info for account '1': [Name: 'Peter', Balance: 60$]
$> transfer 1 2 30
Transferred 30$ from account '1' to account '2'
$> transfer 2 1 10
Transferred 10$ from account '2' to account '1'
$> transactions 1
Transactions for account '1':
Send 40$ to account '0'
Send 30$ to account '2'
Received 10$ from account '2'
$> remove 0 120
Removed 120$ from account '0'

View File

@ -1,81 +0,0 @@
<?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>de.vivi</groupId>
<artifactId>bank</artifactId>
<version>1.0-SNAPSHOT</version>
<name>bank</name>
<description>A simple bank.</description>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.4.0</version>
</plugin>
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.12.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.6.1</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.3.1</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.13.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.3.0</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.4.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>3.1.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>3.1.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
<reporting>
<plugins>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
</plugin>
</plugins>
</reporting>
</project>

View File

@ -1,27 +0,0 @@
package de.vivi;
import de.vivi.bank.Bank;
import java.util.Scanner;
/**
* Hello world!
*
*/
public class App {
public static void main(String[] args) {
Bank bank = null;
Scanner scanner = new Scanner(System.in);
while (true) {
String[] values = scanner.nextLine().split(" ");
if (values.length > 0 && values[0].equals("exit")) {
return;
}
Command.exec(bank, values);
}
}
}

View File

@ -1,50 +0,0 @@
package de.vivi;
import de.vivi.bank.Bank;
import java.util.HashMap;
import java.util.Map;
import java.util.function.BiConsumer;
public class Command {
private static final Map<String, Command> COMMANDS = new HashMap<String, Command>() {{
put("create", new Command(1, (bank, args) -> bank.create(args[1])));
put("info", new Command(1, (bank, args) -> bank.info(Integer.parseInt(args[1]))));
put("add", new Command(1, (bank, args) -> bank.add(Integer.parseInt(args[1]), Integer.parseInt(args[2]))));
put("remove", new Command(1, (bank, args) -> bank.remove(Integer.parseInt(args[1]), Integer.parseInt(args[2]))));
put("transfer", new Command(1, (bank, args) -> bank.transfer(Integer.parseInt(args[1]), Integer.parseInt(args[2]), Integer.parseInt(args[3]))));
put("transactions", new Command(1, (bank, args) -> bank.transactions(Integer.parseInt(args[1]))));
}};
private final int args;
private final BiConsumer<Bank, String[]> consumer;
private Command(int args, BiConsumer<Bank, String[]> consumer) {
this.args = args;
this.consumer = consumer;
}
public static void exec(Bank bank, String[] args) {
if (args.length > 0) {
Command command = COMMANDS.get(args[0]);
if (command == null) {
System.out.println("Command not found");
return;
}
if (command.args != args.length) {
System.out.println("Invalid argument count");
return;
}
try {
command.consumer.accept(bank, args);
} catch (NumberFormatException e) {
System.out.println("Invalid argument");
}
}
}
}

View File

@ -1,50 +0,0 @@
package de.vivi.bank;
public interface Bank {
/**
* Creates an account with the given name and generates the id.
* Prints:
* Created account with id '#id' and name '#name'
*/
void create(String name);
/**
* Prints the info of the account with the given id.
* Prints:
* Account info: [Name: '#name', Balance: '#balance']
*/
void info(int id);
/**
* Adds the given amount to the account with the given id.
* Prints:
* Added #amount$ to account '#id'
*/
void add(int id, int amount);
/**
* Removes the given amount from the account with the given id.
* Prints:
* Removed #amount$ from account '#id'
*/
void remove(int id, int amount);
/**
* Transfers the given amount from the account with the given
* fromId to the account with the given toId.
* Prints:
* Transferred #amount$ from account '#fromId' to account '#toId'
*/
void transfer(int fromId, int toId, int amount);
/**
* Lists all transactions of the account with the given id.
* Prints:
* Transactions for account with id '#id':
* Send #amount$ to account '#id'
* Received #amount$ from account '#id'
* ...
*/
void transactions(int id);
}

View File

@ -1,26 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project name="bank" xmlns="http://maven.apache.org/DECORATION/1.8.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/DECORATION/1.8.0 http://maven.apache.org/xsd/decoration-1.8.0.xsd">
<bannerLeft>
<name>bank</name>
<src>https://maven.apache.org/images/apache-maven-project.png</src>
<href>https://www.apache.org/</href>
</bannerLeft>
<bannerRight>
<src>https://maven.apache.org/images/maven-logo-black-on-white.png</src>
<href>https://maven.apache.org/</href>
</bannerRight>
<skin>
<groupId>org.apache.maven.skins</groupId>
<artifactId>maven-fluido-skin</artifactId>
<version>1.7</version>
</skin>
<body>
<menu ref="parent" />
<menu ref="reports" />
</body>
</project>

View File

@ -1,38 +0,0 @@
package de.vivi;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Unit test for simple App.
*/
public class AppTest
extends TestCase
{
/**
* Create the test case
*
* @param testName name of the test case
*/
public AppTest( String testName )
{
super( testName );
}
/**
* @return the suite of tests being tested
*/
public static Test suite()
{
return new TestSuite( AppTest.class );
}
/**
* Rigourous Test :-)
*/
public void testApp()
{
assertTrue( true );
}
}

View File

@ -1,12 +1,18 @@
package de.vivi.chess; package de.vivi.chess;
import de.vivi.chess.board.Board; import de.vivi.chess.board.Board;
import de.vivi.chess.board.Field;
import de.vivi.chess.game.Game; import de.vivi.chess.game.Game;
import de.vivi.chess.pieces.Piece;
import java.util.Arrays;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
Game game = new Game(); Game game = new Game();
game.play(); game.play();
} }
} }

View File

@ -1,11 +1,11 @@
package de.vivi.chess.board; package de.vivi.chess.board;
import de.vivi.chess.pieces.Piece; import de.vivi.chess.pieces.*;
import de.vivi.chess.game.Game;
public class Board { public class Board {
private static final int SIZE = 8; private static final int SIZE = 8;
private final Piece[/* column */][/* row */] grid; private final Piece[/* column */][/* row */] grid;
public Board() { public Board() {
@ -21,8 +21,52 @@ public class Board {
* in the bottom two rows. "White queen, white field, black * in the bottom two rows. "White queen, white field, black
* queen, black field". * queen, black field".
*/ */
private void setup() {
private void setup() {
grid[0][0] = new Rook(Color.BLACK, Type.ROOK, new Field(0, 0));
grid[1][0] = new Knight(Color.BLACK, Type.KNIGHT, new Field(1, 0));
grid[2][0] = new Bishop(Color.BLACK, Type.BISHOP, new Field(2, 0));
grid[3][0] = new Queen(Color.BLACK, Type.QUEEN, new Field(3, 0));
grid[4][0] = new King(Color.BLACK, Type.KING, new Field(4, 0));
grid[5][0] = new Bishop(Color.BLACK, Type.BISHOP, new Field(5, 0));
grid[6][0] = new Knight(Color.BLACK, Type.KNIGHT, new Field(6, 0));
grid[7][0] = new Rook(Color.BLACK, Type.ROOK, new Field(7, 0));
//grid[0][0] = new Rook(Color.BLACK, Type.ROOK);
// grid[1][0] = new Knight(Color.BLACK, Type.KNIGHT);
// grid[2][0] = new Bishop(Color.BLACK, Type.BISHOP);
// grid[3][0] = new Queen(Color.BLACK, Type.QUEEN);
// grid[4][0] = new King(Color.BLACK, Type.KING);
// grid[5][0] = new Bishop(Color.BLACK, Type.BISHOP);
// grid[6][0] = new Knight(Color.BLACK, Type.KNIGHT);
// grid[7][0] = new Rook(Color.BLACK, Type.ROOK);
for (int i = 0; i < SIZE; i++) {
//grid[i][1] = new Pawn(Color.BLACK, Type.PAWN);
grid[i][1] = new Pawn(Color.BLACK, Type.PAWN, new Field(i, 0));
}
grid[0][7] = new Rook(Color.WHITE, Type.ROOK, new Field(0, 7));
grid[1][7] = new Knight(Color.WHITE, Type.KNIGHT, new Field(1, 7));
grid[2][7] = new Bishop(Color.WHITE, Type.BISHOP, new Field(2, 7));
grid[3][7] = new Queen(Color.WHITE, Type.QUEEN, new Field(3, 7));
grid[4][7] = new King(Color.WHITE, Type.KING, new Field(4, 7));
grid[5][7] = new Bishop(Color.WHITE, Type.BISHOP, new Field(5, 7));
grid[6][7] = new Knight(Color.WHITE, Type.KNIGHT, new Field(6, 7));
grid[7][7] = new Rook(Color.WHITE, Type.ROOK, new Field(7, 7));
// grid[0][7] = new Rook(Color.WHITE, Type.ROOK);
// grid[1][7] = new Knight(Color.WHITE, Type.KNIGHT);
// grid[2][7] = new Bishop(Color.WHITE, Type.BISHOP);
// grid[3][7] = new Queen(Color.WHITE, Type.QUEEN);
// grid[4][7] = new King(Color.WHITE, Type.KING);
// grid[5][7] = new Bishop(Color.WHITE, Type.BISHOP);
// grid[6][7] = new Knight(Color.WHITE, Type.KNIGHT);
// grid[7][7] = new Rook(Color.WHITE, Type.ROOK);
for (int i = 0; i < SIZE; i++) {
//grid[i][6] = new Pawn(Color.WHITE, Type.PAWN);
grid[i][6] = new Pawn(Color.WHITE, Type.PAWN, new Field(i, 6));
}
} }
/** /**
@ -34,7 +78,11 @@ public class Board {
* just getting replaced. * just getting replaced.
*/ */
public void move(Field from, Field to) { public void move(Field from, Field to) {
if (grid[from.getColumn()][from.getRow()] != null) {
grid[to.getColumn()][to.getRow()] = grid[from.getColumn()][from.getRow()];
grid[to.getColumn()][to.getRow()].setField(to);
grid[from.getColumn()][from.getRow()] = null;
}
} }
/** /**
@ -44,7 +92,22 @@ public class Board {
* present it just returns null. * present it just returns null.
*/ */
public Piece getPiece(Field field) { public Piece getPiece(Field field) {
return null; return grid[field.getColumn()][field.getRow()];
}
public Piece[][] getBoard() {
return grid;
}
public Piece getPiece(int column, int row) {
return grid[column][row];
}
public void setPiece(int column, int row, Piece piece) {
grid[column][row] = piece;
if (piece != null) {
piece.setField(new Field(column, row));
}
} }
@Override @Override

View File

@ -1,5 +1,7 @@
package de.vivi.chess.board; package de.vivi.chess.board;
import de.vivi.chess.pieces.Piece;
/** /**
* TODO: implement fields and constructor * TODO: implement fields and constructor
* <p> * <p>
@ -8,6 +10,14 @@ package de.vivi.chess.board;
*/ */
public class Field { public class Field {
private int column;
private int row;
public Field(int column, int row) {
this.column = column;
this.row = row;
}
/** /**
* TODO: implement * TODO: implement
* <p> * <p>
@ -15,7 +25,7 @@ public class Field {
* column and row indices. * column and row indices.
*/ */
public static Field from(int column, int row) { public static Field from(int column, int row) {
return null; return new Field(column, row);
} }
/** /**
@ -28,16 +38,81 @@ public class Field {
* it should throw an IllegalArgument exception. * it should throw an IllegalArgument exception.
*/ */
public static Field fromString(String value) throws IllegalArgumentException { public static Field fromString(String value) throws IllegalArgumentException {
return null; try {
char[] array = value.toCharArray();
int column_ = 0;
int row_ = 0;
switch (array[0]) {
case 'A':
column_ = 0;
break;
case 'B':
column_ = 1;
break;
case 'C':
column_ = 2;
break;
case 'D':
column_ = 3;
break;
case 'E':
column_ = 4;
break;
case 'F':
column_ = 5;
break;
case 'G':
column_ = 6;
break;
case 'H':
column_ = 7;
break;
default:
System.out.println("Wrong input");
}
switch (array[1]) {
case '1':
row_ = 0;
break;
case '2':
row_ = 1;
break;
case '3':
row_ = 2;
break;
case '4':
row_ = 3;
break;
case '5':
row_ = 4;
break;
case '6':
row_ = 5;
break;
case '7':
row_ = 6;
break;
case '8':
row_ = 7;
break;
default:
System.out.println("Wrong input");
}
return from(column_, row_);
} catch (RuntimeException e) {
throw new RuntimeException(e);
}
} }
public int getColumn() { public int getColumn() {
// TODO: implement // TODO: implement
return -1; return column;
} }
public int getRow() { public int getRow() {
// TODO: implement // TODO: implement
return -1; return row;
} }
} }

View File

@ -1,19 +1,30 @@
package de.vivi.chess.game; package de.vivi.chess.game;
import de.vivi.chess.board.Board; import de.vivi.chess.board.Board;
import de.vivi.chess.board.Field;
import de.vivi.chess.pieces.Color; import de.vivi.chess.pieces.Color;
import de.vivi.chess.pieces.King;
import de.vivi.chess.pieces.Piece;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import static de.vivi.chess.pieces.Color.BLACK;
import static de.vivi.chess.pieces.Color.WHITE;
public class Game { public class Game {
private final Board board; private Board board;
private Color player; private Color player;
private int count = 0;
private boolean whiteTurn = true;
/** /**
* TODO: implement * TODO: implement
*/ */
public Game() { public Game() {
board = null; this.board = new Board();
player = null; player = null;
} }
@ -24,6 +35,296 @@ public class Game {
* specified inside example-output.txt. * specified inside example-output.txt.
*/ */
public void play() { public void play() {
System.out.println("Welcome to Vivi's Chess Game!");
System.out.println(board);
player = ((count % 2) == 0) ? WHITE : BLACK;
System.out.println("It is white's turn: [from:to]");
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
scanner.useDelimiter(":\\s*");
String fromString = scanner.nextLine();
char[] arrayInRange = fromString.toCharArray();
if ((arrayInRange[0] == 'A' ||
arrayInRange[0] == 'B' ||
arrayInRange[0] == 'C' ||
arrayInRange[0] == 'D' ||
arrayInRange[0] == 'E' ||
arrayInRange[0] == 'F' ||
arrayInRange[0] == 'G' ||
arrayInRange[0] == 'H' ) &&
(arrayInRange[1] == '0' ||
arrayInRange[1] == '1' ||
arrayInRange[1] == '2' ||
arrayInRange[1] == '3' ||
arrayInRange[1] == '4' ||
arrayInRange[1] == '5' ||
arrayInRange[1] == '6' ||
arrayInRange[1] == '7' ||
arrayInRange[1] == '8' )) {
StringBuilder sb=new StringBuilder(fromString);
int l=sb.length();
if (l == 2) {
String toString = scanner.nextLine();
Field from = Field.fromString(fromString);
Field to = Field.fromString(toString);
System.out.println("player == WHITE && board.getPiece(from).getColor() == WHITE " +
(player == WHITE && board.getPiece(from).getColor() == WHITE));
System.out.println("board.getPiece(from).isValidMove(board, from,to) " +
(board.getPiece(from).isValidMove(board, from,to)));
if (player == WHITE && board.getPiece(from).getColor() == WHITE) {
if (board.getPiece(from).isValidMove(board, from,to)) {
count++;
board.move(from, to);
System.out.println("Moving white " + board.getPiece(to).getType()
+ " from " + fromString + " to " + toString);
}
System.out.println(board);
} else if (player == BLACK && board.getPiece(from).getColor() == BLACK) {
if (board.getPiece(from).isValidMove(board, from,to)) {
count++;
board.move(from, to);
System.out.println("Moving black " + board.getPiece(to).getType()
+ " from " + fromString + " to " + toString);
}
System.out.println(board);
} else {
System.err.println("Illegal move, try again: ");
}
player = ((count % 2) == 0) ? WHITE : BLACK;
if (player == BLACK) {
System.out.println("It is black's turn: [from:to]");
} else {
System.out.println("It is white's turn: [from:to]");
}
} else {
System.err.println("Illegal move, try again: ");
}
} else
System.err.println("Illegal move, try again: ");
}
}
public Board getBoard() {
return this.board;
}
public void resetGame() {
this.board = new Board();
this.whiteTurn = true;
}
public Color getCurrentPlayerColor() {
return whiteTurn ? Color.WHITE : Color.BLACK;
}
private Field selectedField;
public boolean isPieceSelected() {
return selectedField != null;
}
public boolean handleSquareSelection(int row, int col) {
if (selectedField == null) {
Piece selectedPiece = board.getPiece(row, col);
if (selectedPiece != null
&& selectedPiece.getColor() == (whiteTurn ? Color.WHITE : Color.BLACK)) {
selectedField = new Field(row, col);
return false;
}
} else {
boolean moveMade = makeMove(selectedField, new Field(row, col));
selectedField = null;
return moveMade;
}
return false;
}
public boolean makeMove(Field from, Field to) {
Piece movingPiece = board.getPiece(Field.from(from.getRow(), from.getColumn()));
if (movingPiece == null || movingPiece.getColor() != (whiteTurn ? WHITE : BLACK)) {
return false;
}
if (movingPiece.isValidMove(board, movingPiece.getField(), to)) {
board.move(from, to);
whiteTurn = !whiteTurn;
return true;
}
return false;
}
public boolean isInCheck(Color kingColor) {
Field kingPosition = findKingPosition(kingColor);
for (int row = 0; row < 8; row++) {
for (int col = 0; col < 8; col++) {
Piece piece = board.getPiece(Field.from(row, col));
if (piece != null && piece.getColor() != kingColor) {
if (piece.isValidMove(board, piece.getField(), kingPosition)) {
return true;
}
}
}
}
return false;
} }
private Field findKingPosition(Color color) {
for (int row = 0; row < 8; row++) {
for (int col = 0; col < 8; col++) {
Piece piece = board.getPiece(Field.from(row, col));
if (piece instanceof King && piece.getColor() == color) {
return new Field(row, col);
}
}
}
throw new RuntimeException("King not found, which should never happen.");
}
public boolean isCheckmate(Color kingColor) {
if (!isInCheck(kingColor)) {
return false;
}
Field kingPosition = findKingPosition(kingColor);
King king = (King) board.getPiece(Field.from(kingPosition.getRow(), kingPosition.getColumn()));
for (int rowOffset = -1; rowOffset <= 1; rowOffset++) {
for (int colOffset = -1; colOffset <= 1; colOffset++) {
if (rowOffset == 0 && colOffset == 0) {
continue;
}
Field newPosition = new Field(kingPosition.getRow() + rowOffset,
kingPosition.getColumn() + colOffset);
if (
isPositionOnBoard(newPosition) &&
king.isValidMove(board, king.getField() , newPosition)
&& !wouldBeInCheckAfterMove(kingColor, kingPosition, newPosition)) {
return false;
}
}
}
return true;
}
private boolean isPositionOnBoard(Field field) {
return field.getRow() >= 0 && field.getRow() < 8 &&
field.getColumn() >= 0 && field.getColumn() < 8;
}
private boolean wouldBeInCheckAfterMove(Color kingColor, Field from, Field to) {
Piece temp = board.getPiece(Field.from(to.getRow(), to.getColumn()));
board.setPiece(to.getRow(), to.getColumn(), board.getPiece(to));
board.setPiece(from.getRow(), from.getColumn(), null);
boolean inCheck = isInCheck(kingColor);
board.setPiece(from.getRow(), from.getColumn(), board.getPiece(to.getRow(), to.getColumn()));
board.setPiece(to.getRow(), to.getColumn(), temp);
return inCheck;
}
public List<Field> getLegalMovesForPieceAt(Field field) {
Piece selectedPiece = board.getPiece(field.getRow(), field.getColumn());
if (selectedPiece == null)
return new ArrayList<>();
List<Field> legalMoves = new ArrayList<>();
switch (selectedPiece.getClass().getSimpleName()) {
case "Pawn":
addPawnMoves(field, selectedPiece.getColor(), legalMoves);
break;
case "Rook":
addLineMoves(field, new int[][] { { 1, 0 }, { -1, 0 }, { 0, 1 }, { 0, -1 } }, legalMoves);
break;
case "Knight":
addSingleMoves(field, new int[][] { { 2, 1 }, { 2, -1 }, { -2, 1 }, { -2, -1 }, { 1, 2 }, { -1, 2 },
{ 1, -2 }, { -1, -2 } }, legalMoves);
break;
case "Bishop":
addLineMoves(field, new int[][] { { 1, 1 }, { -1, -1 }, { 1, -1 }, { -1, 1 } }, legalMoves);
break;
case "Queen":
addLineMoves(field, new int[][] { { 1, 0 }, { -1, 0 }, { 0, 1 }, { 0, -1 }, { 1, 1 }, { -1, -1 },
{ 1, -1 }, { -1, 1 } }, legalMoves);
break;
case "King":
addSingleMoves(field, new int[][] { { 1, 0 }, { -1, 0 }, { 0, 1 }, { 0, -1 }, { 1, 1 }, { -1, -1 },
{ 1, -1 }, { -1, 1 } }, legalMoves);
break;
}
return legalMoves;
}
private void addLineMoves(Field field, int[][] directions, List<Field> legalMoves) {
for (int[] d : directions) {
Field newPos = new Field(field.getRow() + d[0], field.getColumn() + d[1]);
while (isPositionOnBoard(newPos)) {
if (board.getPiece(newPos.getRow(), newPos.getColumn()) == null) {
legalMoves.add(new Field(newPos.getRow(), newPos.getColumn()));
newPos = new Field(newPos.getRow() + d[0], newPos.getColumn() + d[1]);
} else {
if (board.getPiece(newPos.getRow(), newPos.getColumn()).getColor() != board
.getPiece(field.getRow(), field.getColumn()).getColor()) {
legalMoves.add(newPos);
}
break;
}
}
}
}
private void addSingleMoves(Field field, int[][] moves, List<Field> legalMoves) {
for (int[] move : moves) {
Field newPos = new Field(field.getRow() + move[0], field.getColumn() + move[1]);
if (isPositionOnBoard(newPos) && (board.getPiece(newPos.getRow(), newPos.getColumn()) == null ||
board.getPiece(newPos.getRow(), newPos.getColumn()).getColor() != board
.getPiece(field.getRow(), field.getColumn()).getColor())) {
legalMoves.add(newPos);
}
}
}
private void addPawnMoves(Field field, Color color, List<Field> legalMoves) {
int direction = color == WHITE ? -1 : 1;
Field newPos = new Field(field.getRow() + direction, field.getColumn());
if (isPositionOnBoard(newPos) && board.getPiece(newPos.getRow(), newPos.getColumn()) == null) {
legalMoves.add(newPos);
}
if ((color == Color.WHITE && field.getRow() == 6)
|| (color == Color.BLACK && field.getRow() == 1)) {
newPos = new Field(field.getRow() + 2 * direction, field.getColumn());
Field intermediatePos = new Field(field.getRow() + direction, field.getColumn());
if (isPositionOnBoard(newPos) && board.getPiece(newPos.getRow(), newPos.getColumn()) == null
&& board.getPiece(intermediatePos.getRow(), intermediatePos.getColumn()) == null) {
legalMoves.add(newPos);
}
}
int[] captureCols = { field.getColumn() - 1, field.getColumn() + 1 };
for (int col : captureCols) {
newPos = new Field(field.getRow() + direction, col);
if (isPositionOnBoard(newPos) && board.getPiece(newPos.getRow(), newPos.getColumn()) != null &&
board.getPiece(newPos.getRow(), newPos.getColumn()).getColor() != color) {
legalMoves.add(newPos);
}
}
}
} }

View File

@ -0,0 +1,59 @@
package de.vivi.chess.pieces;
import de.vivi.chess.board.Board;
import de.vivi.chess.board.Field;
public class Bishop extends Piece {
public Bishop(Color color, Type type) {
super(color, type);
}
public Bishop(Color color, Type type, Field field) {
super(color, type, field);
}
@Override
public char getSymbol() {
if (getColor() == Color.WHITE) {
if (getType() == Type.BISHOP) {
symbol = '\u265D';
}
} else if (getColor() == Color.BLACK) {
if (getType() == Type.BISHOP) {
symbol = '\u2657';
}
}
return symbol;
}
@Override
public boolean isValidMove(Board board, Field from, Field to) {
int rowDiff = Math.abs(from.getRow() - to.getRow());
int colDiff = Math.abs(from.getColumn() - to.getColumn());
if (rowDiff != colDiff) {
return false;
}
int rowStep = to.getRow() > from.getRow() ? 1 : -1;
int colStep = to.getColumn() > from.getColumn() ? 1 : -1;
int steps = rowDiff - 1;
for (int i = 1; i <= steps; i++) {
if (board.getPiece(Field.from(from.getColumn() + i * rowStep,
from.getRow() + i * colStep)) != null) {
return false;
}
}
Piece destinationPiece = board.getPiece(Field.from(to.getColumn(), to.getRow()));
if (destinationPiece == null) {
return true;
} else if (destinationPiece.getColor() != this.getColor()) {
return true;
}
return false;
}
}

View File

@ -4,4 +4,6 @@ package de.vivi.chess.pieces;
* TODO: add colors (white and black) * TODO: add colors (white and black)
*/ */
public enum Color { public enum Color {
WHITE,
BLACK
} }

View File

@ -0,0 +1,44 @@
package de.vivi.chess.pieces;
import de.vivi.chess.board.Board;
import de.vivi.chess.board.Field;
public class King extends Piece {
public King(Color color, Type type) {
super(color, type);
}
public King(Color color, Type type, Field field) {
super(color, type, field);
}
@Override
public char getSymbol() {
if (getColor() == Color.WHITE) {
if (getType() == Type.KING) {
symbol = '\u265A';
}
} else if (getColor() == Color.BLACK) {
if (getType() == Type.KING) {
symbol = '\u2654';
}
}
return symbol;
}
@Override
public boolean isValidMove(Board board, Field from, Field to) {
int rowDiff = Math.abs(from.getRow() - to.getRow());
int colDiff = Math.abs(from.getColumn() - to.getColumn());
boolean isOneSquareMove = rowDiff <= 1 && colDiff <= 1 && !(rowDiff == 0 && colDiff == 0);
if (!isOneSquareMove) {
return false;
}
Piece destinationPiece = board.getPiece(Field.from(to.getColumn(), to.getRow()));
return destinationPiece == null || destinationPiece.getColor() != this.getColor();
}
}

View File

@ -0,0 +1,53 @@
package de.vivi.chess.pieces;
import de.vivi.chess.board.Board;
import de.vivi.chess.board.Field;
public class Knight extends Piece {
public Knight(Color color, Type type) {
super(color, type);
}
public Knight(Color color, Type type, Field field) {
super(color, type, field);
}
@Override
public char getSymbol() {
if (getColor() == Color.WHITE) {
if (getType() == Type.KNIGHT) {
symbol = '\u265E';
}
} else if (getColor() == Color.BLACK) {
if (getType() == Type.KNIGHT) {
symbol = '\u2658';
}
}
return symbol;
}
@Override
public boolean isValidMove(Board board, Field from, Field to) {
if (to.equals(from)) {
return false;
}
int rowDiff = Math.abs(from.getRow() - to.getRow());
int colDiff = Math.abs(from.getColumn() - to.getColumn());
boolean isValidLMove = (rowDiff == 2 && colDiff == 1) || (rowDiff == 1 && colDiff == 2);
if (!isValidLMove) {
return false;
}
Piece targetPiece = board.getPiece(to);
if (targetPiece == null) {
return true;
} else {
return targetPiece.getColor() != this.getColor();
}
}
}

View File

@ -0,0 +1,73 @@
package de.vivi.chess.pieces;
import de.vivi.chess.board.Board;
import de.vivi.chess.board.Field;
public class Pawn extends Piece {
public Pawn(Color color, Type type) {
super(color, type);
}
public Pawn(Color color, Type type, Field field) {
super(color, type, field);
}
@Override
public char getSymbol() {
if (getColor() == Color.WHITE) {
if (getType() == Type.PAWN) {
symbol = '\u265F';
}
} else if (getColor() == Color.BLACK) {
if (getType() == Type.PAWN) {
symbol = '\u2659';
}
}
return symbol;
}
@Override
public boolean isValidMove(Board board, Field from, Field to) {
int forwardDirection = getColor() == Color.WHITE ? -1 : 1;
System.out.println("forwardDirection " + forwardDirection);
System.out.println("to.getRow() " + to.getRow());
System.out.println("from.getRow() " + from.getRow());
int rowDiff = (to.getRow() - from.getRow()) * forwardDirection;
System.out.println("rowDiff " + rowDiff);
int colDiff = to.getColumn() - from.getColumn();
System.out.println("colDiff " + colDiff);
System.out.println("black pawn links " + board.getPiece(0,1));
if (colDiff == 0 && rowDiff == 1 && board.getPiece(to) == null) {
return true;
}
boolean isStartingField = (getColor() == Color.WHITE && from.getRow() == 6) ||
(getColor() == Color.BLACK && from.getRow() == 1);
System.out.println("getColor() " + getColor());
System.out.println("from.getRow() " + from.getRow());
System.out.println("isStartingField " + isStartingField);
System.out.println("board.getPiece(to) " + board.getPiece(to));
if (colDiff == 0 && rowDiff == 2 && isStartingField &&
board.getPiece(to) == null) {
int middleRow = from.getRow() + forwardDirection;
System.out.println("from.getRow() " +from.getRow());
System.out.println("middleRow " + middleRow);
System.out.println("from.getColumn() " + from.getColumn());
if (board.getPiece(Field.from(from.getColumn(), middleRow)) == null) {
return true;
}
}
if (Math.abs(colDiff) == 1 && rowDiff == 1 &&
board.getPiece(to) != null &&
board.getPiece(to).getColor() != this.getColor()) {
return true;
}
return false;
}
}

View File

@ -5,6 +5,28 @@ import de.vivi.chess.board.Field;
public abstract class Piece { public abstract class Piece {
Color color;
private Type type;
protected char symbol;
protected Field field;
protected Piece(Color color, Type type) {
this.color = color;
this.type = type;
symbol = '\u0000';
}
protected Piece(Color color, Field field) {
this.color = color;
this.field = field;
}
protected Piece(Color color, Type type, Field field) {
this.color = color;
this.type = type;
this.field = field;
}
/** /**
* TODO: implement * TODO: implement
* <p> * <p>
@ -16,14 +38,15 @@ public abstract class Piece {
*/ */
public abstract boolean isValidMove(Board board, Field from, Field to); public abstract boolean isValidMove(Board board, Field from, Field to);
public Color getColor() { public Color getColor() {
// TODO: implement // TODO: implement
return null; return color;
} }
public Type getType() { public Type getType() {
// TODO: implement // TODO: implement
return null; return type;
} }
/** /**
@ -34,6 +57,14 @@ public abstract class Piece {
* <a href="https://en.wikipedia.org/wiki/Chess_symbols_in_Unicode">Unicode chess pieces</a> * <a href="https://en.wikipedia.org/wiki/Chess_symbols_in_Unicode">Unicode chess pieces</a>
*/ */
public char getSymbol() { public char getSymbol() {
return 0; return symbol;
}
public Field getField() {
return field;
}
public void setField(Field field) {
this.field = field;
} }
} }

View File

@ -0,0 +1,64 @@
package de.vivi.chess.pieces;
import de.vivi.chess.board.Board;
import de.vivi.chess.board.Field;
public class Queen extends Piece {
public Queen(Color color, Type type) {
super(color, type);
}
public Queen(Color color, Type type, Field field) {
super(color, type, field);
}
@Override
public char getSymbol() {
if (getColor() == Color.WHITE) {
if (getType() == Type.QUEEN) {
symbol = '\u265B';
}
} else if (getColor() == Color.BLACK) {
if (getType() == Type.QUEEN) {
symbol = '\u2655';
}
}
return symbol;
}
@Override
public boolean isValidMove(Board board, Field from, Field to) {
if (to.equals(from)) {
return false;
}
int rowDiff = Math.abs(to.getRow() - from.getRow());
int colDiff = Math.abs(to.getColumn() - from.getColumn());
boolean straightLine = from.getRow() == to.getRow()
|| from.getColumn() == to.getColumn();
boolean diagonal = rowDiff == colDiff;
if (!straightLine && !diagonal) {
return false;
}
int rowDirection = Integer.compare(to.getRow(), from.getRow());
int colDirection = Integer.compare(to.getColumn(), from.getColumn());
int currentRow = from.getRow() + rowDirection;
int currentCol = from.getColumn() + colDirection;
while (currentRow != to.getRow() || currentCol != to.getColumn()) {
if (board.getPiece(Field.from(currentCol, currentRow)) != null) {
return false;
}
currentRow += rowDirection;
currentCol += colDirection;
}
Piece destinationPiece = board.getPiece(Field.from(to.getColumn(), to.getRow()));
return destinationPiece == null || destinationPiece.getColor() != this.getColor();
}
}

View File

@ -0,0 +1,68 @@
package de.vivi.chess.pieces;
import de.vivi.chess.board.Board;
import de.vivi.chess.board.Field;
public class Rook extends Piece {
private boolean isValid = false;
public Rook(Color color, Type type) {
super(color, type);
}
public Rook(Color color, Field field) {
super(color, field);
}
public Rook(Color color, Type type, Field field) {
super(color, type, field);
}
@Override
public char getSymbol() {
if (getColor() == Color.WHITE) {
if (getType() == Type.ROOK) {
symbol = '\u265C';
}
} else if (getColor() == Color.BLACK) {
if (getType() == Type.ROOK) {
symbol = '\u2656';
}
}
return symbol;
}
@Override
public boolean isValidMove(Board board, Field from, Field to) {
if (from.getRow() == to.getRow()) {
int columnStart = Math.min(from.getColumn(), to.getColumn()) + 1;
int columnEnd = Math.max(from.getColumn(), to.getColumn());
for (int column = columnStart; column < columnEnd; column++) {
if (board.getPiece(Field.from(column, from.getRow())) != null) {
return false;
}
}
} else if (from.getColumn() == to.getColumn()) {
int rowStart = Math.min(from.getRow(), to.getRow()) + 1;
int rowEnd = Math.max(from.getRow(), to.getRow());
for (int row = rowStart; row < rowEnd; row++) {
if (board.getPiece(Field.from(from.getColumn(), row)) != null) {
return false;
}
}
} else {
return false;
}
Piece destinationPiece = board.getPiece(to);
if (destinationPiece == null) {
return true;
} else if (destinationPiece.getColor() != this.getColor()) {
return true;
}
return false;
}
}

View File

@ -14,4 +14,10 @@ package de.vivi.chess.pieces;
* - pawn (Bauer) * - pawn (Bauer)
*/ */
public enum Type { public enum Type {
KING,
QUEEN,
BISHOP,
KNIGHT,
ROOK,
PAWN
} }

38
jmarket/.gitignore vendored
View File

@ -1,38 +0,0 @@
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
### IntelliJ IDEA ###
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
*.iws
*.iml
*.ipr
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store

3
jmarket/.idea/.gitignore generated vendored
View File

@ -1,3 +0,0 @@
# Default ignored files
/shelf/
/workspace.xml

View File

@ -1,7 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
</component>
</project>

14
jmarket/.idea/misc.xml generated
View File

@ -1,14 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" project-jdk-name="temurin-17" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

View File

@ -1,124 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Palette2">
<group name="Swing">
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
</item>
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
</item>
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.svg" removable="false" auto-create-binding="false" can-attach-label="true">
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
</item>
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
<initial-values>
<property name="text" value="Button" />
</initial-values>
</item>
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="RadioButton" />
</initial-values>
</item>
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="CheckBox" />
</initial-values>
</item>
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
<initial-values>
<property name="text" value="Label" />
</initial-values>
</item>
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
</item>
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
</item>
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
<preferred-size width="-1" height="20" />
</default-constraints>
</item>
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
</item>
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
</item>
</group>
</component>
</project>

7
jmarket/.idea/vcs.xml generated
View File

@ -1,7 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

View File

@ -1,16 +0,0 @@
<?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>org.example</groupId>
<artifactId>jmarket-09-10</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>

View File

@ -1,28 +0,0 @@
package de.vimo.jmarket;
import de.vimo.jmarket.api.ProductDatabase;
import java.time.LocalDate;
public class Main {
public static void main(String[] args) {
}
/*
private static void usage() {
ProductDatabase db = null;
db.addProduct(new PaulMarketProduct("banana", LocalDate.of(2025, 8, 1), 10.4, 4.5));
db.addProduct(new PaulMarketProduct("banana", LocalDate.of(2025, 4, 3), 6.4, 2.5));
db.addProduct(new PaulMarketProduct("banana", LocalDate.of(2026, 8, 1), 12.7, 6.5));
db.addProduct(new PaulMarketProduct("orange", LocalDate.of(2024, 1, 14), 2.4, 1.5));
db.addProduct(new PaulMarketProduct("orange", LocalDate.of(2023, 3, 20), 3.3, 2.5));
db.addProduct(new PaulMarketProduct("candy", LocalDate.of(2028, 8, 1), 30.4, 10.4));
db.addProduct(new PaulMarketProduct("candy", LocalDate.of(2026, 8, 23), 24.5, 12.6));
db.addProduct(new PaulMarketProduct("beans", LocalDate.of(2025, 6, 12), 2.3, 120.5));
db.addProduct(new PaulMarketProduct("doughnut", LocalDate.of(2020, 3, 1), 1.4, 6.5));
}
*/
}

View File

@ -1,17 +0,0 @@
package de.vimo.jmarket.api;
import java.time.LocalDate;
/**
* TODO: implement all methods
*/
public interface Product {
String getName();
LocalDate getExpirationDate();
double getWeight();
double getPrice();
}

View File

@ -1,93 +0,0 @@
package de.vimo.jmarket.api;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;
import java.util.stream.Stream;
/**
* TODO: implement all methods
*/
public interface ProductDatabase {
/**
* Adds a product to the database and generates a random id for it.
*
* @param product The product to add
* @return The randomly generated id
*/
int addProduct(Product product);
/**
* Returns the product with the specified id. If there is no
* such product, it returns null.
*
* @param id The id of the product
* @return The product with the specified id
*/
Product getProduct(int id);
/**
* Returns all products with the specified name.
*
* @param name The name to search for
* @return The products that have the specified name
*/
List<Product> getProductsByName(String name);
/**
* Gets all products that match the given predicate.
*
* @param predicate The condition the products have to fulfill
* @return The filtered products
*/
List<Product> getProducts(Predicate<Product> predicate);
/**
* Returns all products that are expired at or after the specified
* date.
*
* @param date The date to check for
* @return The products that are expired at or after the date
*/
List<Product> getExpiredProducts(LocalDate date);
/**
* Returns a list of all distinct product names inside the database.
*
* @return The product names.
*/
List<String> getDistinctProductNames();
/**
* Returns a map where the key is the product name and the
* value is the total weight of all products with that name.
*
* @return The map with product names and weights
*/
Map<String, Double> getTotalProductWeights();
/**
* Returns the count of products with the specified name.
*
* @param name The name of the product to count
* @return The count of products
*/
int getProductCount(String name);
/**
* Returns the total price of all products with the specified name.
*
* @param name The name of the product
* @return The total price
*/
double getTotalProductPrice(String name);
/**
* Adds all values from the specified databases to this database.
*
* @param databases The databases to add
*/
void addAll(Stream<Stream<ProductDatabase>> databases);
}

View File

@ -1,34 +0,0 @@
package de.vimo.jmarket.api;
import java.util.function.BiConsumer;
/**
* TODO: implement all methods
*/
public interface TreeList {
/**
* Adds the product with the id into the list.
*
* @param id The unique identifier
* @param product The product
*/
void add(int id, Product product);
/**
* Returns the product with the specified id or null if
* the id can't be found.
*
* @param id The unique identifier
* @return The product
*/
Product get(int id);
/**
* Calls the accept method for each entry inside the tree
* for the provided consumer.
*
* @param consumer The consumer to consume all entries in the list
*/
void forEach(BiConsumer<Integer, Product> consumer);
}

View File

@ -1,124 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Palette2">
<group name="Swing">
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
</item>
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
</item>
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.svg" removable="false" auto-create-binding="false" can-attach-label="true">
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
</item>
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
<initial-values>
<property name="text" value="Button" />
</initial-values>
</item>
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="RadioButton" />
</initial-values>
</item>
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="CheckBox" />
</initial-values>
</item>
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
<initial-values>
<property name="text" value="Label" />
</initial-values>
</item>
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
</item>
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
</item>
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
<preferred-size width="-1" height="20" />
</default-constraints>
</item>
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
</item>
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
</item>
</group>
</component>
</project>

View File

@ -1,10 +1,10 @@
package de.vivi.list.list; package de.vivi.list;
import java.util.Arrays; import java.util.*;
import java.util.Iterator;
public class ArrayByteList implements ByteList { public class ArrayByteList implements ByteList {
private int size = 0;
private byte[] values; private byte[] values;
public ArrayByteList() { public ArrayByteList() {
@ -37,19 +37,14 @@ public class ArrayByteList implements ByteList {
values = newValues; values = newValues;
} }
@Override
public byte get(int index) {
return values[index];
}
@Override @Override
public void set(int index, byte value) { public void set(int index, byte value) {
values[index] = value; values[index] = value;
} }
@Override @Override
public void clear() { public byte get(int index) {
values = new byte[0]; return values[index];
} }
@Override @Override
@ -58,12 +53,36 @@ public class ArrayByteList implements ByteList {
} }
@Override @Override
public Iterator<Byte> iterator() { public boolean contains(int value) {
return new ByteListIterator(this); boolean isIndexInArray = false;
for (int i = 0; i < values.length; i++) {
if (values[i] == value) {
isIndexInArray = true;
}
}
return isIndexInArray;
} }
@Override @Override
public String toString() { public String toString() {
return Arrays.toString(values); return "ArrayByteList " + Arrays.toString(values);
}
@Override
public ByteList copy() {
ArrayByteList newArrayList = new ArrayByteList();
newArrayList.values = Arrays.copyOf(values, values.length);
return newArrayList;
}
@Override
public void clear() {
values = new byte[0];
}
@Override
public ArrayByteListIterator iterator() {
ArrayByteListIterator it = new ArrayByteListIterator(this);
return it;
} }
} }

View File

@ -0,0 +1,24 @@
package de.vivi.list;
import java.util.Iterator;
import java.util.function.Consumer;
public class ArrayByteListIterator implements Iterator<Byte> {
private ArrayByteList array;
private int currentIndex = 0;
public ArrayByteListIterator(ArrayByteList array) {
this.array = array;
}
@Override
public boolean hasNext() {
return currentIndex < array.size();
}
@Override
public Byte next() {
return array.get(currentIndex++);
}
}

View File

@ -0,0 +1,35 @@
package de.vivi.list;
import java.util.Random;
public class BogoSort implements ByteSort {
@Override
public ByteList sort(ByteList list) {
bogoSort(list);
return list;
}
private void bogoSort(ByteList list) {
while (isSorted(list) == false) {
shuffleList(list);
}
}
private void shuffleList(ByteList list) {
Random r = new Random();
for (int i = list.size() - 1; i > 0; i--) {
int j = r.nextInt(i);
byte temp = list.get(i);
list.set(i, list.get(j));
list.set(j, temp);
}
}
private boolean isSorted(ByteList list) {
for (int i = 1; i < list.size(); i++)
if (list.get(i) < list.get(i - 1))
return false;
return true;
}
}

View File

@ -0,0 +1,19 @@
package de.vivi.list;
public class BubbleSort implements ByteSort {
@Override
public ByteList sort(ByteList list) {
int n = list.size();
for (int i = 0; i < n - 1; i++)
for (int j = 0; j < n - i - 1; j++)
if (list.get(j) > list.get(j + 1)) {
byte temp = list.get(j);
list.set(j, list.get(j + 1));
list.set(j + 1, temp);
}
return list;
}
}

View File

@ -1,4 +1,4 @@
package de.vivi.list.list; package de.vivi.list;
public interface ByteList extends Iterable<Byte> { public interface ByteList extends Iterable<Byte> {
@ -8,11 +8,17 @@ public interface ByteList extends Iterable<Byte> {
void remove(int index); void remove(int index);
byte get(int index);
void set(int index, byte value); void set(int index, byte value);
void clear(); byte get(int index);
int size(); int size();
boolean contains(int value);
String toString();
ByteList copy();
void clear();
} }

View File

@ -0,0 +1,6 @@
package de.vivi.list;
public interface ByteSort {
ByteList sort(ByteList list);
}

View File

@ -0,0 +1,59 @@
package de.vivi.list;
import java.util.Iterator;
public class CombinedByteList implements ByteList {
public CombinedByteList(int i) {
}
@Override
public void add(byte value) {
}
@Override
public void add(int index, byte value) {
}
@Override
public void remove(int index) {
}
@Override
public void set(int index, byte value) {
}
@Override
public byte get(int index) {
return 0;
}
@Override
public int size() {
return 0;
}
@Override
public boolean contains(int value) {
return false;
}
@Override
public ArrayByteList copy() {
return null;
}
@Override
public void clear() {
}
@Override
public Iterator<Byte> iterator() {
return null;
}
}

View File

@ -0,0 +1,158 @@
package de.vivi.list;
public class LinkedByteList implements ByteList {
private NodeLinked currNode;
private int size;
private NodeLinked last;
private NodeLinked first;
@Override
public void add(byte value) {
NodeLinked newNode = new NodeLinked(value);
if (first == null) {
first = newNode;
} else {
NodeLinked last = first;
while (last.next != null) {
last = last.next;
}
last.next = newNode;
}
size++;
}
@Override
public void add(int index, byte value) {
if (index == 0) {
NodeLinked newNode = new NodeLinked(value);
if (size == 0) {
last = newNode;
} else {
newNode.next = first;
}
first = newNode;
size++;
} else if (index == size) {
NodeLinked newNode = new NodeLinked(value);
last.next = newNode;
last = newNode;
size++;
} else {
NodeLinked newNode = new NodeLinked(value);
NodeLinked prev = getCurrNode(index - 1);
newNode.next = prev.next;
prev.next = newNode;
size++;
}
}
@Override
public void remove(int index) {
NodeLinked current = first;
if (index == 0) {
size--;
first = current.next;
}
NodeLinked temp = null;
for (int i = 1; i <= index; i++) {
temp = current;
current = current.next;
}
temp.next = current.next;
size--;
}
@Override
public void set(int index, byte value) {
NodeLinked x = node(index);
x.item = value;
}
NodeLinked node(int index) {
NodeLinked current = first;
for (int i = 0; i < index; i++) {
current = current.next;
}
return current;
}
@Override
public byte get(int index) {
return node(index).item;
}
public NodeLinked getCurrNode(int index) {
if (index < 0 || index >= size) {
return null;
}
NodeLinked current = first;
for (int i = 0; i < index; i++) {
current = current.next;
}
return current;
}
@Override
public int size() {
return size;
}
@Override
public boolean contains(int value) {
byte byteValue = (byte) value;
boolean isEqual = false;
for (int i = 0; i < size; i++) {
if (get(i) == byteValue) {
isEqual = true;
break;
} else {
isEqual = false;
}
}
return isEqual;
}
@Override
public ByteList copy() {
LinkedByteList clone = new LinkedByteList();
clone.first = clone.last = null;
clone.size = 0;
for (NodeLinked x = first; x != null; x = x.next) {
clone.add(x.item);
}
return clone;
}
@Override
public void clear() {
for (NodeLinked x = first; x != null;) {
NodeLinked next = x.next;
x.next = null;
x = next;
}
first = null;
size = 0;
}
@Override
public LinkedByteListIterator iterator() {
LinkedByteListIterator it = new LinkedByteListIterator(this);
return it;
}
@Override
public String toString() {
System.out.println();
System.out.println("LinkedList: ");
currNode = first;
while (currNode != null) {
System.out.print(currNode.item + " ");
currNode = currNode.next;
}
return String.valueOf(currNode).replace("null", "");
}
}

View File

@ -0,0 +1,23 @@
package de.vivi.list;
import java.util.Iterator;
import java.util.function.Consumer;
public class LinkedByteListIterator implements Iterator<Byte> {
private LinkedByteList linkedList;
private int currentIndex;
public LinkedByteListIterator(LinkedByteList linkedList) {
this.linkedList = linkedList;
}
@Override
public boolean hasNext() {
return currentIndex < linkedList.size();
}
@Override
public Byte next() {
return linkedList.get(currentIndex++);
}
}

View File

@ -1,39 +1,72 @@
package de.vivi.list; package de.vivi.list;
import de.vivi.list.list.ArrayByteList;
import de.vivi.list.list.ByteList;
import de.vivi.list.list.LinkedByteList;
import de.vivi.list.sort.BubbleSort;
import de.vivi.list.sort.ByteSort;
import de.vivi.list.sort.MergeSort;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
ByteList array = new ArrayByteList(); ByteList array = new ArrayByteList();
ByteList linked = new LinkedByteList(); ByteList linked = new LinkedByteList();
// ByteList combined = new CombinedByteList(16); // ByteList combined = new CombinedByteList(16); // Paul meinte das kann weg gelassen werden
// testListVivi(array);
// testList(array); // testListVivi(linked);
// testSortVivi(array);
testList(array);
testList(linked); testList(linked);
// testList(combined);
// testSort(array); // testSortVivi(array);
// testSortVivi(linked);
// testList(combined);
testSort(array);
testSort(linked); testSort(linked);
// testSort(combined); // testSort(combined);
} }
private static void testListVivi(ByteList list) {
list.add((byte) 10);
list.add((byte) 5);
list.add((byte) -86);
list.add((byte) 3);
list.add(3, (byte) 99);
list.add(2, (byte) -40);
list.remove(2);
list.remove(3);
list.set(1, (byte) 43);
list.set(0, (byte) 8);
list.get(2);
list.get(1);
list.size();
list.contains(43);
list.contains(4);
String s = list.toString();
System.out.println(s);
list.copy();
System.out.println(list.copy());
// list.clear();
// System.out.println(list);
for (byte value : list) {
System.out.print(value + " ");
}
}
private static void testList(ByteList list) { private static void testList(ByteList list) {
// add the element at the end // add the element at the end
list.add((byte) 10); list.add((byte) 10);
list.add((byte) 5); list.add((byte) 5);
list.add((byte) -86); list.add((byte) -86);
list.add((byte) 3); list.add((byte) 3);
list.add((byte) 100);
list.add((byte) 20);
list.add((byte) 40);
list.add((byte) -100);
list.add((byte) 40);
// list: [10, 5, -86, 3] // list: [10, 5, -86, 3]
@ -63,35 +96,69 @@ public class Main {
list.size(); // returns: 4 list.size(); // returns: 4
// does the list contain the specified element? // does the list contain the specified element?
// list.contains(43); // returns: true list.contains(43); // returns: true
// list.contains(4); // returns: false list.contains(4); // returns: false
//
// // return a string of the form: [1, 84, 53] // return a string of the form: [1, 84, 53]
// list.toString(); // returns: "[8, 43, -86, 3]" list.toString(); // returns: "[8, 43, -86, 3]"
//
// // return a copy of the list // return a copy of the list
// list.copy(); // returns: [8, 43, -86, 3] list.copy(); // returns: [8, 43, -86, 3]
//
//
// // remove all elements inside the list // remove all elements inside the list
// list.clear(); // list.clear();
//
// // list: [] // list: []
//
// // allow to use for each loop on list // allow to use for each loop on list
// for (byte value : list) { // for (byte value : list) {
// // do some stuff with value... // // do some stuff with value...
// } // }
System.out.println();
System.out.println("List ");
for (byte value : list) {
System.out.print(value + " ");
}
} }
private static void testSort(ByteList list) { private static void testSort(ByteList list) {
ByteSort bubble = new BubbleSort(); ByteSort bubble = new BubbleSort();
ByteSort merge = new MergeSort(); ByteSort merge = new MergeSort();
// ByteSort bogo = new BogoSort(); ByteSort bogo = new BogoSort();
//
// // sort the lists with the sorting algorithm // sort the lists with the sorting algorithm
bubble.sort(list); System.out.println();
merge.sort(list); bubble.sort(list.copy());
// bogo.sort(list.copy()); System.out.println("bubble ");
System.out.println(bubble.sort(list.copy()));
merge.sort(list.copy());
System.out.println("merge ");
System.out.println(merge.sort(list.copy()));
bogo.sort(list.copy());
System.out.println("bogo ");
System.out.println(bogo.sort(list.copy()));
} }
private static void testSortVivi(ByteList list) {
ByteSort bubble = new BubbleSort();
ByteSort merge = new MergeSort();
ByteSort bogo = new BogoSort();
// sort the lists with the sorting algorithm
bubble.sort(list.copy());
System.out.println();
System.out.println();
System.out.println("bubblesort " + bubble.sort(list.copy()));
System.out.println(merge.sort(list.copy()));
merge.sort(list.copy());
bogo.sort(list.copy());
System.out.println(bogo.sort(list.copy()));
}
} }

View File

@ -0,0 +1,53 @@
package de.vivi.list;
public class MergeSort implements ByteSort {
@Override
public ByteList sort(ByteList list) {
int mid = list.size() / 2;
ByteList leftList = createSubList(list, 0, mid);
ByteList rightList = createSubList(list, mid, list.size());
ByteList mergedList = mergeList(leftList, rightList, mid);
sortMergedList(mergedList);
return mergedList;
}
private void sortMergedList(ByteList mergedList) {
for (int i = 0; i < (mergedList.size() - 1); i++)
for (int j = 0; j < mergedList.size() - i - 1; j++)
if (mergedList.get(j) > mergedList.get(j + 1)) {
byte temp = mergedList.get(j);
mergedList.set(j, mergedList.get(j + 1));
mergedList.set(j + 1, temp);
}
}
private ByteList mergeList(ByteList leftList, ByteList rightList, int mid) {
ByteList mergedList = new ArrayByteList();
for (int i = 0; i < mid; i++) {
mergedList.add(leftList.get(i));
mergedList.add(rightList.get(i));
}
return mergedList;
}
public ByteList createSubList(ByteList list, int start, int end) {
ByteList subList = new ArrayByteList();
for (int i = start; i < end; i++) {
subList.add(list.get(i));
}
return subList;
}
public ByteList sortSubList(ByteList list, int mid) {
for (int i = 0; i < mid - 1; i++)
for (int j = 0; j < mid - i - 1; j++)
if (list.get(j) > list.get(j + 1)) {
byte temp = list.get(j);
list.set(j, list.get(j + 1));
list.set(j + 1, temp);
}
return list;
}
}

View File

@ -0,0 +1,23 @@
package de.vivi.list;
public class NodeLinked {
byte item;
NodeLinked next;
NodeLinked prev;
public NodeLinked(NodeLinked prev, byte element, NodeLinked next) {
this.item = element;
this.next = null;
this.prev = null;
}
public NodeLinked(byte element) {
this.item = element;
}
public NodeLinked(byte element, NodeLinked next) {
this.item = element;
this.next = null;
}
}

View File

@ -1,25 +0,0 @@
package de.vivi.list.list;
import java.util.Iterator;
public class ByteListIterator implements Iterator<Byte> {
private int index;
private final ByteList list;
public ByteListIterator(ByteList list) {
this.index = 0;
this.list = list;
}
@Override
public boolean hasNext() {
return index < list.size();
}
@Override
public Byte next() {
return list.get(index++);
}
}

View File

@ -1,149 +0,0 @@
package de.vivi.list.list;
import java.util.Iterator;
public class LinkedByteList implements ByteList {
private static class Entry {
public byte value;
public Entry next;
}
private Entry root;
private int size;
public LinkedByteList() {
root = null;
size = 0;
}
@Override
public void add(byte value) {
add(size, value);
}
@Override
public void add(int index, byte value) {
if (index < 0) {
throw new IndexOutOfBoundsException();
}
size += 1;
// create the new entry
Entry entry = new Entry();
entry.value = value;
// check if we need to set the root
if (index == 0) {
entry.next = root;
root = entry;
return;
}
// the entry before the new entry
Entry prev = root;
// find the spot to insert the entry
while (--index > 0) {
prev = prev.next;
}
// insert the entry
entry.next = prev.next;
prev.next = entry;
}
@Override
public void remove(int index) {
if (index < 0) {
throw new IndexOutOfBoundsException();
}
size -= 1;
// check if we need to set the root
if (index == 0) {
root = root.next;
return;
}
// the entry before the removed entry
Entry prev = root;
// find the spot to remove the entry
while (--index > 0) {
prev = prev.next;
}
// remove the entry
prev.next = prev.next.next;
}
@Override
public byte get(int index) {
if (index < 0) {
throw new IndexOutOfBoundsException();
}
Entry entry = root;
while (index-- > 0) {
entry = entry.next;
}
return entry.value;
}
@Override
public void set(int index, byte value) {
if (index < 0) {
throw new IndexOutOfBoundsException();
}
Entry entry = root;
while (index-- > 0) {
entry = entry.next;
}
entry.value = value;
}
@Override
public void clear() {
root = null;
size = 0;
}
@Override
public int size() {
return size;
}
@Override
public Iterator<Byte> iterator() {
return new ByteListIterator(this);
}
@Override
public String toString() {
StringBuilder result = new StringBuilder("[");
Entry entry = root;
while (entry != null) {
if (entry != root) {
result.append(", ");
}
result.append(entry.value);
entry = entry.next;
}
return result.append("]").toString();
}
}

View File

@ -1,25 +0,0 @@
package de.vivi.list.sort;
import de.vivi.list.list.ByteList;
public class BubbleSort implements ByteSort {
@Override
public void sort(ByteList list) {
boolean swapped = true;
while (swapped) {
swapped = false;
for (int i = 0; i < list.size() - 1; i++) {
if (list.get(i) > list.get(i + 1)) {
swapped = true;
byte temp = list.get(i);
list.set(i, list.get(i + 1));
list.set(i + 1, temp);
}
}
}
}
}

View File

@ -1,8 +0,0 @@
package de.vivi.list.sort;
import de.vivi.list.list.ByteList;
public interface ByteSort {
void sort(ByteList list);
}

View File

@ -1,60 +0,0 @@
package de.vivi.list.sort;
import de.vivi.list.list.ArrayByteList;
import de.vivi.list.list.ByteList;
public class MergeSort implements ByteSort {
@Override
public void sort(ByteList list) {
if (list.size() <= 1) {
return;
}
ByteList left = new ArrayByteList();
ByteList right = new ArrayByteList();
split(list, left, right);
if (left.size() > 1) {
sort(left);
}
if (right.size() > 1) {
sort(right);
}
int leftIndex = 0;
int rightIndex = 0;
while (leftIndex < left.size() && rightIndex < right.size()) {
if (left.get(leftIndex) < right.get(rightIndex)) {
list.add(left.get(leftIndex++));
} else {
list.add(right.get(rightIndex++));
}
}
while (leftIndex < left.size()) {
list.add(left.get(leftIndex++));
}
while (rightIndex < right.size()) {
list.add(right.get(rightIndex++));
}
}
private void split(ByteList list, ByteList left, ByteList right) {
int half = list.size() >> 1; // divide by two
for (int i = 0; i < list.size(); i++) {
if (i < half) {
left.add(list.get(i));
} else {
right.add(list.get(i));
}
}
list.clear();
}
}