Compare commits

..

5 Commits

Author SHA1 Message Date
b2eb22a6cc uebung09 small adjustments 2025-06-12 12:10:00 +02:00
57aae3eda6 uebung09 2025-06-12 11:59:41 +02:00
f42c1b23ab angabe ue09 2025-06-10 13:33:42 +02:00
9e5e9eb91a ue6 2025-06-06 14:30:41 +02:00
4b73c1b5e3 correction ue7 2025-06-06 14:21:53 +02:00
29 changed files with 1795 additions and 77 deletions

View File

@@ -0,0 +1,123 @@
package sudoku;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
import java.util.Set;
import static java.util.Collections.emptySet;
/**
* A field of a Sudoku board.
*/
public class Field {
private static final int DEP_LENGTH = 2 * Sudoku.SIZE + 2;
public final Sudoku sudoku;
public final int x;
public final int y;
private Value value;
private List<Field> dependents;
private final Set<Value> domain = EnumSet.allOf(Value.class);
/**
* Creates a field of the specified Sudoku board at the specified coordinates.
*
* @param sudoku the Sudoku board
* @param x x coordinate (0 <= x < 9)
* @param y y coordinate (0 <= y < 9)
*/
public Field(Sudoku sudoku, int x, int y) {
if (x < 0 || x >= Sudoku.SIZE || y < 0 || y >= Sudoku.SIZE)
throw new IllegalArgumentException("Illegal coordinates " + x + ", " + y);
this.sudoku = sudoku;
this.x = x;
this.y = y;
}
/**
* Returns the value of this field, or null if it is empty.
*
* @return the value of this field, or null if it is empty.
*/
public Value getValue() {
return value;
}
/**
* Sets the value of this field
*
* @param v the value, or null if this field shall be empty.
*/
public void setValue(Value v) {
value = v;
}
/**
* Removes the value of this field so that it is empty.
*/
public void clearValue() {
setValue(null);
}
/**
* Checks whether this field is empty.
*
* @return true iff this field is empty.
*/
public boolean isEmpty() {
return value == null;
}
/**
* Returns a string representation of this field. As a matter of fact,
* its value is returned, or "." if it is empty.
*
* @return a string representation of this field.
*/
public String toString() {
return isEmpty() ? "." : value.toString();
}
/**
* Returns the set of all values possible for this field that do not violate the Sudoku constraints,
* if the field is empty. Otherwise, it returns the empty set. The returned set may be modified if
* the field is empty.
*
* @return the domain of this field.
*/
public Set<Value> getDomain() {
return isEmpty() ? domain : emptySet();
}
/**
* Returns the list of all other fields of this Sudoku field whose values are constrained by the
* value of this field.
*
* @return the list of all dependent fields.
*/
List<Field> getDependents() {
if (dependents == null) {
dependents = new ArrayList<>(DEP_LENGTH);
// fill the list with all fields in the given row and column
for (int i = 0; i < Sudoku.SIZE; i++) {
if (i != x)
dependents.add(sudoku.field(i, y));
if (i != y)
dependents.add(sudoku.field(x, i));
}
// determine the coordinates of the upper left corner of this field's block
final int x0 = 3 * (x / 3);
final int y0 = 3 * (y / 3);
// fill the list with the whole block
for (int x1 = x0; x1 < x0 + 3; x1++)
if (x1 != x)
for (int y1 = y0; y1 < y0 + 3; y1++)
if (y1 != y)
dependents.add(sudoku.field(x1, y1));
}
return dependents;
}
}

View File

@@ -0,0 +1,206 @@
package sudoku;
import java.util.ArrayList;
import java.util.List;
/**
* A Sudoku board as a matrix of {@linkplain sudoku.Field} instances.
* <p>
* Call {@linkplain #solve()} to solve it.
*/
public class Sudoku {
/**
* Edge size of the Sudoku board.
*/
public static final int SIZE = 9;
private static final String LS = System.lineSeparator();
private final Field[] board = new Field[SIZE * SIZE];
/**
* Creates a Sudoku board with all of its fields being empty.
*/
public Sudoku() {
for (int x = 0; x < SIZE; x++)
for (int y = 0; y < SIZE; y++)
board[SIZE * y + x] = new Field(this, x, y);
}
/**
* Returns the field at the specified coordinates
*
* @param x x coordinate (0 <= x < 9)
* @param y y coordinate (0 <= x < 9)
* @return the field at the specified coordinates
*/
public Field field(int x, int y) {
return board[SIZE * y + x];
}
/**
* Bulk operation to set values of the fields.
*
* @param values a sequence of integers where 0 means empty
*/
public void initialize(int... values) {
if (values.length != board.length)
throw new IllegalArgumentException("Wrong Sudoku board with " + values.length + " values");
for (int i = 0; i < values.length; i++)
if (values[i] != 0)
setValue(board[i], Value.of(values[i]));
}
/**
* Sets the specified value of the specified empty field
*
* @param field the empty field whose value is set
* @param value the value of the field
* @throws IllegalStateException if the field is not empty
* @throws IllegalArgumentException if setting this value would violate the Sudoku constraints
* @throws NullPointerException if the value is null
*/
public void setValue(Field field, Value value) {
if (value == null)
throw new NullPointerException("value is null");
if (!field.isEmpty())
throw new IllegalStateException("Value already set: " + field);
if (!field.getDomain().contains(value))
throw new IllegalArgumentException(value + " is not permitted");
for (Field dependent : field.getDependents())
dependent.getDomain().remove(value);
field.setValue(value);
}
/**
* Returns a string representation of this board.
*
* @return a string representation of this board
*/
public String toString() {
StringBuilder sb = new StringBuilder();
for (int y = 0; y < SIZE; y++) {
if (y == 3 || y == 6) sb.append("---------+---------+---------").append(LS);
for (int x = 0; x < SIZE; x++) {
if (x == 3 || x == 6) sb.append("|");
sb.append(" ").append(field(x, y)).append(" ");
}
sb.append(LS);
}
return sb.toString();
}
/**
* Looks for an empty field with minimal domain.
*
* @return a field with minimal domain, or null no field is empty
*/
private Field findBestCandidate() {
Field best = null;
int min = Integer.MAX_VALUE;
for (Field f : board)
if (f.isEmpty() && f.getDomain().size() < min) {
best = f;
min = best.getDomain().size();
}
return best;
}
/**
* Tries to solve the current board. The board is in the solved state if a solution is found,
* and unmodified if no solution is found.
*
* @return true iff a solution has been found.
*/
public boolean solve() {
final Field field = findBestCandidate();
if (field == null)
// A solution has been found
return true;
// There are empty fields. Try all values of its domain
final List<Field> revoke = new ArrayList<>();
for (Value v : field.getDomain()) {
field.setValue(v);
boolean consistent = true;
for (Field dependent : field.getDependents())
if (dependent.getDomain().contains(v)) {
if (dependent.getDomain().size() == 1) {
// This is a dead end because field 'dependent'
// would have an empty domain
consistent = false;
break;
}
revoke.add(dependent);
dependent.getDomain().remove(v);
}
if (consistent && solve())
return true;
// Choosing v as a value lead to a dead end.
// Revoke all modifications
field.clearValue();
for (Field f : revoke)
f.getDomain().add(v);
revoke.clear();
}
return false;
}
public List<Sudoku> solveAll() {
List<Sudoku> solutions = new ArrayList<>();
solveAll(solutions);
return solutions;
}
private void solveAll(List<Sudoku> solutions) {
final Field field = findBestCandidate();
if (field == null) {
// A solution has been found
Sudoku copy = new Sudoku();
copy.initialize(this.export());
solutions.add(copy);
return;
}
// There are empty fields. Try all values of its domain
final List<Field> revoke = new ArrayList<>();
for (Value v : field.getDomain()) {
field.setValue(v);
boolean consistent = true;
for (Field dependent : field.getDependents())
if (dependent.getDomain().contains(v)) {
if (dependent.getDomain().size() == 1) {
// This is a dead end because field 'dependent'
// would have an empty domain
consistent = false;
break;
}
revoke.add(dependent);
dependent.getDomain().remove(v);
}
if (consistent) solveAll(solutions);
// Revoke all modifications
field.clearValue();
for (Field f : revoke)
f.getDomain().add(v);
revoke.clear();
}
}
private int[] export() {
int[] values = new int[SIZE * SIZE];
int idx = 0;
while (idx < values.length) {
Field f = board[idx];
if (f != null) {
Value value = f.getValue();
if (value != null) values[idx] = value.getId();
else values[idx] = 0;
}
else throw new IllegalStateException("at least one field is not set");
++idx;
}
return values;
}
}

View File

@@ -0,0 +1,68 @@
package sudoku;
import java.util.List;
public class SudokuApp {
public static void main(String[] args) {
System.out.println("Sudoku Aufgabenblatt:");
System.out.println();
final Sudoku boardBlatt = new Sudoku();
boardBlatt.initialize(4, 5, 0, 0, 0, 0, 2, 0, 0,
6, 0, 0, 0, 2, 4, 8, 0, 0,
8, 0, 0, 0, 6, 1, 3, 0, 0,
0, 9, 0, 4, 0, 0, 0, 5, 0,
0, 1, 0, 2, 0, 8, 0, 7, 0,
0, 3, 0, 0, 0, 9, 0, 8, 0,
0, 0, 7, 1, 4, 0, 0, 0, 8,
0, 0, 2, 7, 9, 0, 0, 0, 6,
0, 0, 5, 0, 0, 0, 0, 2, 1);
printSolution(boardBlatt);
System.out.println("Sudoku Handzettel:");
System.out.println();
final Sudoku boardHandzettel = new Sudoku();
boardHandzettel.initialize(7, 0, 0, 0, 2, 5, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 9, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0,
4, 0, 9, 1, 0, 0, 0, 0, 0,
0, 0, 0, 6, 0, 0, 2, 0, 0,
0, 0, 0, 0, 0, 0, 0, 3, 0,
8, 2, 0, 0, 0, 0, 5, 0, 0,
0, 0, 0, 0, 3, 0, 6, 0, 0,
0, 0, 0, 9, 0, 0, 0, 0, 0);
printSolution(boardHandzettel);
System.out.println("Sudoku mit 2 Lösungen:");
System.out.println();
final Sudoku boardWithTwoSolutions = new Sudoku();
boardWithTwoSolutions.initialize(2, 9, 5, 7, 4, 3, 8, 6, 1,
4, 3, 1, 8, 6, 5, 9, 0, 0,
8, 7, 6, 1, 9, 2, 5, 4, 3,
3, 8, 7, 4, 5, 9, 2, 1, 6,
6, 1, 2, 3, 8, 7, 4, 9, 5,
5, 4, 9, 2, 1, 6, 7, 3, 8,
7, 6, 3, 5, 2, 4, 1, 8, 9,
9, 2, 8, 6, 7, 1, 3, 5, 4,
1, 5, 4, 9, 3, 8, 6, 0, 0);
System.out.println(boardWithTwoSolutions);
System.out.println("Solutions:");
System.out.println();
List<Sudoku> solutions = boardWithTwoSolutions.solveAll();
System.out.println(String.join(System.lineSeparator(), solutions.stream().map(Sudoku::toString).toList()));
}
private static void printSolution(Sudoku sudoku) {
System.out.println(sudoku);
System.out.println();
if (sudoku.solve()) {
System.out.println("Solution:");
System.out.println();
System.out.println(sudoku);
}
else
System.out.println("No solution!");
}
}

View File

@@ -0,0 +1,41 @@
package sudoku;
/**
* The enumeration type of all possible values of a Sudoku field.
*/
public enum Value {
ONE(1), TWO(2), THREE(3), FOUR(4), FIVE(5), SIX(6), SEVEN(7), EIGHT(8), NINE(9);
private final int id;
Value(int id) {
this.id = id;
}
/**
* Returns a string representation of this value. As a matter of fact, its id is returned.
*
* @return a string representation of this value.
*/
@Override
public String toString() {
return String.valueOf(id);
}
/**
* Returns the value with the specified id.
*
* @param id an id (1 <= id <= 9)
* @return the value with the specified id.
*/
public static Value of(int id) {
return values()[id - 1];
}
/**
* Returns the id of this instance.
*
* @return the id of this instance.
*/
public int getId() {return id;}
}

View File

@@ -0,0 +1,65 @@
package uebung06.test.sudoku;
import org.junit.Before;
import org.junit.Test;
import sudoku.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
public class FieldTest {
private static final int X = 8;
private static final int Y = 5;
private static final int NUL = -1;
private static final Value VAL = Value.EIGHT;
private static Sudoku sudoku;
private static Field field;
@Before
public void init() {
sudoku = new Sudoku();
field = new Field(sudoku, X, Y);
}
@Test(expected = IllegalArgumentException.class)
public void illegalXLowerBoundFieldTest() {
field = new Field(sudoku, NUL, Y);
}
@Test(expected = IllegalArgumentException.class)
public void illegalXUpperBoundFieldTest() {
field = new Field(sudoku, Sudoku.SIZE, Y);
}
@Test(expected = IllegalArgumentException.class)
public void illegalYLowerBoundFieldTest() {
field = new Field(sudoku, X, NUL);
}
@Test(expected = IllegalArgumentException.class)
public void illegalYUpperBoundFieldTest() {
field = new Field(sudoku, X, Sudoku.SIZE);
}
@Test
public void valueFieldTest() {
field.setValue(VAL);
assertEquals(field.getValue(), VAL);
}
@Test
public void clearValueFieldTest() {
field.setValue(VAL);
field.clearValue();
assertNull(field.getValue());
}
@Test
public void isValueEmptyFieldTest() {
field.setValue(VAL);
field.clearValue();
assertTrue(field.isEmpty());
}
}

View File

@@ -0,0 +1,129 @@
package uebung06.test.sudoku;
import org.junit.Before;
import org.junit.Test;
import sudoku.*;
import java.util.HashMap;
import java.util.HashSet;
import java.util.stream.IntStream;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public class SudokuAppTest {
private static final int MAX = 81;
private static final int MIN = 0;
private static final int SUDOKU_SIZE = 9;
private static Sudoku board;
private static int[] init;
private static int[] wrongSol;
@Before
public void init() {
board = new Sudoku();
init = IntStream.of(4, 5, 0, 0, 0, 0, 2, 0, 0,
6, 0, 0, 0, 2, 4, 8, 0, 0,
8, 0, 0, 0, 6, 1, 3, 0, 0,
0, 9, 0, 4, 0, 0, 0, 5, 0,
0, 1, 0, 2, 0, 8, 0, 7, 0,
0, 3, 0, 0, 0, 9, 0, 8, 0,
0, 0, 7, 1, 4, 0, 0, 0, 8,
0, 0, 2, 7, 9, 0, 0, 0, 6,
0, 0, 5, 0, 0, 0, 0, 2, 1).toArray();
wrongSol = IntStream.of(4, 5, 1, 8, 7, 3, 2, 6, 9,
6, 7, 3, 9, 2, 4, 8, 1, 5,
8, 2, 9, 5, 6, 1, 3, 4, 7,
2, 9, 8, 4, 1, 7, 6, 5, 3,
5, 1, 6, 2, 3, 8, 9, 7, 4,
7, 3, 4, 6, 5, 9, 1, 8, 2,
3, 6, 7, 1, 4, 2, 5, 9, 8,
1, 8, 2, 7, 9, 5, 4, 3, 6,
9, 5, 4, 3, 8, 6, 7, 2, 1).toArray();
}
//Testing first If-condition in Sudoku.init
@Test(expected = IllegalArgumentException.class)
public void illegalLowerBoundInitSudokuAppTest() {
board.initialize(IntStream.range(MIN, MIN+1).toArray());
}
//Testing first If-condition in Sudoku.init
@Test(expected = IllegalArgumentException.class)
public void illegalUpperBoundInitSudokuAppTest() {
board.initialize(IntStream.range(MIN, MAX+1).toArray());
}
//Testing ArrayIndex of Value
@Test(expected = ArrayIndexOutOfBoundsException.class)
public void illegalArrayIndexOutOfBoundInitSudokuAppTest() {
board.initialize(IntStream.range(MIN, MAX).toArray());
}
//Testing Sudoku.solve()
@Test
public void correctSolutionSudokuAppTest() {
board.initialize(init);
board.solve();
assertTrue(verifySudokuSol());
}
//Just testing verifySudokuSol-Method
@Test
public void wrongSolutionSudokuAppTest() {
try {
board.initialize(wrongSol);
} catch (final IllegalArgumentException e) {
//do nothing, just handling throwing exceptions from Sudoku-class
}
assertFalse(verifySudokuSol());
}
public Boolean verifySudokuSol() {
HashSet<Value> rows = new HashSet<>();
HashSet<Value> cols = new HashSet<>();
HashMap<Integer, HashSet<Value>> blocks = new HashMap<>();
IntStream.range(0, SUDOKU_SIZE)
.forEach(block -> blocks.put(block, new HashSet<>())); //init Hashsets for every block
for (int y = 0; y < SUDOKU_SIZE; y++) {
for (int x = 0; x < SUDOKU_SIZE; x++) {
//check column
Value val = board.field(x, y).getValue();
if (!cols.add(val))
return false;
if (cols.size() == SUDOKU_SIZE)
cols.clear();
//check row
val = board.field(y, x).getValue();
if (!rows.add(val))
return false;
if (rows.size() == SUDOKU_SIZE)
rows.clear();
//check 3x3 block
val = board.field(x, y).getValue();
if (!blocks.get(getBlockIndex(x, y)).add(val))
return false;
}
}
return true;
}
public int getBlockIndex(int x, int y) {
return getColIndex(x) + getRowIndex(y);
}
public int getColIndex(int x) {
return x / 3;
}
public int getRowIndex(int y) {
return (y/3)*3;
}
}

View File

@@ -5,30 +5,30 @@ import oop.ch05.secured.SecuredContent;
import oop.ch05.secured.SecurityClient;
public class AccountCard extends MensaCard {
private final SecuredContent<String> account;
private final SecuredContent<String> account;
public AccountCard(String key, String account, int password) {
super(key, Color.white);
this.account = new SecuredContent<>(password, account);
}
public AccountCard(String key, String account, int password) {
super(key, Color.white);
this.account = new SecuredContent<>(password, account);
}
public String getAccount() {
return account.getContent();
}
public String getAccount() {
return account.getContent();
}
public void setAccount(SecurityClient client, String account) throws AuthorizationException {
if (account == null || account.trim().length() == 0)
throw new IllegalArgumentException("Invalid account " + account);
this.account.setContent(client, account);
}
public void setAccount(SecurityClient client, String account) throws AuthorizationException {
if (account == null || account.trim().isEmpty())
throw new IllegalArgumentException("Invalid account " + account);
this.account.setContent(client, account);
}
@Override
public String toString() {
return super.toString() + " for account " + account.getContent();
}
@Override
public String toString() {
return super.toString() + " for account " + account.getContent();
}
@Override
public void pass(CashPoint cashPoint) {
cashPoint.charge(this);
}
@Override
public void pass(CashPoint cashPoint) throws RejectedException, AuthorizationException {
cashPoint.charge(this);
}
}

View File

@@ -0,0 +1,51 @@
package oop.ch05.mensa;
import oop.ch05.secured.AuthorizationException;
import java.util.Map;
import java.util.Random;
import java.util.TreeMap;
public class AccountManagement {
private final int password;
public final String name;
private final Random random = new Random();
private final Map<String, Integer> accounts = new TreeMap<>();
public AccountManagement(int password, String name) {
this.password = password;
this.name = name;
}
public void deposit(String account, int euros) {
final int amount = accounts.getOrDefault(account, 0);
accounts.put(account, amount + 100 * euros);
}
public void pay(AccountCard card, int price, CashPoint cashPoint) throws RejectedException, AuthorizationException {
final int amount = accounts.getOrDefault(card.getAccount(), 0);
final int challenge = nextChallenge();
if (cashPoint.challengeResponse(challenge) != requiredResponse(challenge))
throw new AuthorizationException(cashPoint + " is not authorized to access accounts on " + name);
if (amount < price)
throw new RejectedException(card + " bounced");
accounts.put(card.getAccount(), amount - price);
}
public int getAmount(String account) {
return accounts.getOrDefault(account, 0);
}
private int nextChallenge() {
return random.nextInt();
}
private int requiredResponse(int challenge) {
return challenge ^ password;
}
@Override
public String toString() {
return "Account Management " + name + " " + accounts;
}
}

View File

@@ -4,67 +4,62 @@ import oop.ch05.secured.AuthorizationException;
import oop.ch05.secured.SecurityClient;
public class CashPoint implements SecurityClient {
private final int password;
public final String name;
private int counter;
private int cents;
private final int password;
public final String name;
private final AccountManagement accountMgmt;
private int counter;
private int cents;
public CashPoint(String name, int password) {
this.name = name;
this.password = password;
this.counter = 0;
this.cents = 0;
}
public CashPoint(String name, int password, AccountManagement accountMgmt) {
this.name = name;
this.password = password;
this.accountMgmt = accountMgmt;
}
@Override
public int challengeResponse(int challenge) {
return challenge ^ password;
}
public int challengeResponse(int challenge) {
return challenge ^ password;
}
@Override
public String toString() {
return "Cash point " + name + " (" + getCounter() + " meals, " + getCents() + " cents charged)";
}
@Override
public String toString() {
return "Cash point " + name + " (" + getCounter() + " meals, " + getCents() + " cents charged)";
}
public int getCounter() {
return counter;
}
public int getCounter() {
return counter;
}
public int getCents() {
return cents;
}
public int getCents() {
return cents;
}
private int getPrice(Color color) {
return switch (color) {
case green -> 267;
case blue -> 357;
case white -> 495;
default -> 0;
};
}
private int getPrice(Color color) {
return switch (color) {
case green -> 267;
case blue -> 357;
case white -> 495;
default -> 0;
};
}
void count(MensaCard card) {
counter++;
}
void count(MensaCard card) {
counter++;
}
void charge(CashCard cashCard) throws AuthorizationException, RejectedException {
final int price = getPrice(cashCard.color);
if (cashCard.getBalance() < price)
throw new RejectedException("insufficient payment");
cashCard.charge(this, price);
count(cashCard);
cents += price;
}
void charge(CashCard cashCard) throws AuthorizationException, RejectedException {
final int price = getPrice(cashCard.color);
if (cashCard.getBalance() < price)
throw new RejectedException("insufficient payment");
cashCard.charge(this, price);
count(cashCard);
cents += price;
}
void charge(AccountCard accountCard) {
final int price = getPrice(accountCard.color);
// zahlen methode
/*
zahlen bein acmgmt
*/
System.out.println("Charging " + price + " cents on account " + accountCard.getAccount());
cents += price;
}
void charge(AccountCard accountCard) throws RejectedException, AuthorizationException {
final int price = getPrice(accountCard.color);
System.out.println("Charging " + price + " cents on account " + accountCard.getAccount());
accountMgmt.pay(accountCard, price, this);
count(accountCard);
cents += price;
}
}

View File

@@ -4,10 +4,11 @@ import oop.ch05.secured.AuthorizationException;
public class MensaExample {
public static void main(String[] args) {
AccountManagement mgmt = new AccountManagement(4711, "Uni");
VendingMachine vm1 = new VendingMachine("left", 4711);
VendingMachine vm2 = new VendingMachine("right", 4711);
VendingMachine tumVM = new VendingMachine("TUM Mensa", 3141);
CashPoint unibwMensa = new CashPoint("UniBw Mensa", 4711);
CashPoint unibwMensa = new CashPoint("UniBw Mensa", 4711, mgmt);
AccountCard conf = new AccountCard("conference", "33-1298", 42);
MensaCard frankSmith = new CountCard("Frank Smith", Color.gray);

View File

@@ -1,6 +1,8 @@
package oop.ch05.generic.mensa;
import oop.ch05.generic.secured.AuthorizationException;
import oop.ch05.secured.AuthorizationException;
import oop.ch05.mensa.*;
import org.junit.Before;
import org.junit.Test;

View File

@@ -0,0 +1,70 @@
package chess;
import java.util.Iterator;
import iterator.Array2dIterator;
import iterator.SkipNullIterator;
public class Board implements Iterable<Piece> {
private final Piece[][] field = new Piece[8][8];
//private final List<Piece> pieces = new ArrayList<>();
void add(Piece piece) {
if (piece.getBoard() != this)
throw new IllegalArgumentException("wrong board");
final Piece existing = pieceAt(piece.getRow(), piece.getCol());
if (existing != null)
throw new IllegalArgumentException("already occupied by " + existing);
field[piece.getRow() - 1][piece.getCol() - 1] = piece;
//pieces.add(piece);
}
public void printBoard() {
System.out.println(" 1 2 3 4 5 6 7 8");
System.out.println(" +---+---+---+---+---+---+---+---+");
for (int row = 1; row <= 8; row++) {
System.out.print("" + row + " ");
for (int col = 1; col <= 8; col++) {
final Piece p = pieceAt(row, col);
final char c = p == null ? ' ' : p.charRep();
System.out.print("| " + c + " ");
}
System.out.println("|");
System.out.println(" +---+---+---+---+---+---+---+---+");
}
}
public Piece pieceAt(int row, int col) {
return field[row - 1][col - 1];
}
public void check() {
for (Piece p1 : this) {
System.out.println(p1.toString());
for (Piece p2 : this)
if (p1 != p2)
if (p1.canCapture(p2))
System.out.println(" can capture " + p2.toString());
else
System.out.println(" cannot capture " + p2.toString());
}
}
public String toString() {
StringBuilder sb = new StringBuilder("[");
Iterator<Piece> it = iterator();
while (it.hasNext()) {
sb.append(it.next());
if (it.hasNext()) sb.append(", ");
}
sb.append("]");
return sb.toString();
}
@Override
public Iterator<Piece> iterator() {
return new SkipNullIterator<>(new Array2dIterator<>(field));
}
}

View File

@@ -0,0 +1,95 @@
package chess;
import java.io.InputStream;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Scanner;
public class ChessApp {
private static final String HELP = "h";
private static final String CHECK = "c";
private static final String ABORT = "a";
private static final String WHITE_QUEEN = "q";
private static final String BLACK_QUEEN = "Q";
private static final String WHITE_KNIGHT = "n";
private static final String BLACK_KNIGHT = "N";
private static final String ITERATOR = "i";
private final Scanner scanner;
private final Board board;
public static void main(String[] args) {
new ChessApp(System.in, new Board()).playChess();
}
private ChessApp(InputStream in, Board board) {
scanner = new Scanner(in);
this.board = board;
}
private void playChess() {
board.printBoard();
commandLoop();
System.out.println("Terminated");
}
private void commandLoop() {
while (true) {
System.out.printf("Type in command (%s for help):%n", HELP);
try {
final String command = scanner.next();
if (ABORT.equals(command)) return;
switch (command) {
case HELP -> help();
case CHECK -> board.check();
case BLACK_QUEEN -> addQueen(Color.black);
case WHITE_QUEEN -> addQueen(Color.white);
case BLACK_KNIGHT -> addKnight(Color.black);
case WHITE_KNIGHT -> addKnight(Color.white);
case ITERATOR -> iterate();
default -> System.out.println("Invalid command " + command);
}
}
catch (IllegalArgumentException ex) {
System.out.println(ex.getMessage());
}
catch (NoSuchElementException ex) {
return;
}
}
}
private void addKnight(Color color) {
final int row = scanner.nextInt();
final int col = scanner.nextInt();
new Knight(color, board, row, col);
board.printBoard();
}
private void addQueen(Color color) {
final int row = scanner.nextInt();
final int col = scanner.nextInt();
new Queen(color, board, row, col);
board.printBoard();
}
private void iterate() {
final Iterator<Piece> it = board.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
private void help() {
System.out.println("Commands:");
System.out.println(ABORT + ": terminate the program");
System.out.println(CHECK + ": check the pieces on the board");
System.out.println(WHITE_KNIGHT + " <int> <int>: place a new white knight at specified position");
System.out.println(BLACK_KNIGHT + " <int> <int>: place a new black knight at specified position");
System.out.println(WHITE_QUEEN + " <int> <int>: place a new white queen at specified position");
System.out.println(BLACK_QUEEN + " <int> <int>: place a new black queen at specified position");
System.out.println(ITERATOR + ": iterate over the board and print all pieces to the console");
}
}

View File

@@ -0,0 +1,5 @@
package chess;
public enum Color {
black, white
}

View File

@@ -0,0 +1,28 @@
package chess;
import static java.lang.Math.abs;
public class Knight extends Piece {
public Knight(Color color, Board board, int row, int col) {
super(color, board, row, col);
}
@Override
public char charRep() {
return getColor() == Color.white ? 'n' : 'N';
}
@Override
public String toString() {
return String.format("%s knight at (%d,%d)", getColor(), getRow(), getCol());
}
@Override
public boolean canCapture(Piece other) {
if (getBoard() != other.getBoard() || getColor() == other.getColor())
return false;
final int dr = abs(getRow() - other.getRow());
final int dc = abs(getCol() - other.getCol());
return dr == 2 && dc == 1 || dr == 1 && dc == 2;
}
}

View File

@@ -0,0 +1,38 @@
package chess;
public abstract class Piece {
private Color color;
private Board board;
private int row;
private int col;
protected Piece(Color color, Board board, int row, int col) {
if (row < 1 || row > 8 || col < 1 || col > 8)
throw new IllegalArgumentException("Invalid pos " + row + "/" + col);
this.color = color;
this.board = board;
this.row = row;
this.col = col;
board.add(this);
}
public Color getColor() {
return color;
}
public Board getBoard() {
return board;
}
public int getRow() {
return row;
}
public int getCol() {
return col;
}
public abstract char charRep();
public abstract boolean canCapture(Piece other);
}

View File

@@ -0,0 +1,40 @@
package chess;
import static java.lang.Integer.signum;
import static java.lang.Math.abs;
public class Queen extends Piece {
public Queen(Color color, Board board, int row, int col) {
super(color, board, row, col);
}
@Override
public char charRep() {
return getColor() == Color.white ? 'q' : 'Q';
}
@Override
public String toString() {
return String.format("%s queen at (%d,%d)", getColor(), getRow(), getCol());
}
@Override
public boolean canCapture(Piece other) {
if (getBoard() != other.getBoard() || getColor() == other.getColor())
return false;
if (other.getRow() != getRow() &&
other.getCol() != getCol() &&
abs(other.getRow() - getRow()) != abs(other.getCol() - getCol()))
return false;
final int dr = signum(other.getRow() - getRow());
final int dc = signum(other.getCol() - getCol());
int r = getRow() + dr;
int c = getCol() + dc;
while (r != other.getRow() || c != other.getCol()) {
if (getBoard().pieceAt(r, c) != null) return false;
r += dr;
c += dc;
}
return true;
}
}

View File

@@ -0,0 +1,106 @@
package collection;
import java.util.Iterator;
import java.util.Objects;
class BigSet<E> implements Set<E> {
private final E first;
private final Set<E> rest;
private final int size;
BigSet(E first, Set<E> rest) {
this.first = Objects.requireNonNull(first);
this.rest = Objects.requireNonNull(rest);
size = 1 + rest.size();
}
@Override
public int size() {
return size;
}
@Override
public boolean isEmpty() {
return false;
}
@Override
public boolean contains(Object el) {
return first.equals(el) || rest.contains(el);
}
@Override
public boolean subsetOf(Set<?> other) {
return other.contains(first) && rest.subsetOf(other);
}
@Override
public Set<E> union(Set<E> other) {
return rest.union(other).add(first);
}
@Override
public Set<E> add(E element) {
if (contains(element))
return this;
return new BigSet<>(element, this);
}
@Override
public Set<E> intersection(Set<E> other) {
final Set<E> set = rest.intersection(other);
if (!other.contains(first))
return set;
else if (set.isEmpty())
return SetFactory.create(first);
return new BigSet<>(first, set);
}
@Override
public Iterator<E> iterator() {
return new Iterator<>() {
private int cnt;
private Iterator<E> restIt;
@Override
public boolean hasNext() {
if (cnt == 0) return true;
if (restIt == null) restIt = rest.iterator();
return restIt.hasNext();
}
@Override
public E next() {
if (cnt++ == 0) return first;
if (restIt == null) restIt = rest.iterator();
return restIt.next();
}
};
}
@Override
public String toString() {
final StringBuilder b = new StringBuilder("{");
final Iterator<E> it = iterator();
while (it.hasNext()) {
b.append(it.next());
if (it.hasNext()) b.append(", ");
}
b.append('}');
return b.toString();
}
@Override
public int hashCode() {
return 31 * rest.hashCode() + first.hashCode();
}
@Override
public boolean equals(Object obj) {
if (obj instanceof Set<?>) {
final Set<?> other = (Set<?>) obj;
return this.size() == other.size() && this.subsetOf(other);
}
return false;
}
}

View File

@@ -0,0 +1,76 @@
package collection;
import java.util.Iterator;
import java.util.NoSuchElementException;
class EmptySet<E> implements Set<E> {
@Override
public int size() {
return 0;
}
@Override
public boolean isEmpty() {
return true;
}
@Override
public boolean contains(Object el) {
return false;
}
@Override
public boolean subsetOf(Set<?> other) {
return true;
}
@Override
public Set<E> union(Set<E> other) {
return other;
}
@Override
public Set<E> add(E element) {
return SetFactory.create(element);
}
@Override
public Set<E> intersection(Set<E> other) {
return this;
}
@Override
public Iterator<E> iterator() {
return new Iterator<>() {
@Override
public boolean hasNext() {
return false;
}
@Override
public E next() {
throw new NoSuchElementException();
}
};
}
@Override
public String toString() {
return "{}";
}
@Override
public int hashCode() {
return 31;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof Set<?>) {
@SuppressWarnings("unchecked") final Set<E> other = (Set<E>) obj;
return other.isEmpty();
}
return false;
}
}

View File

@@ -0,0 +1,61 @@
package collection;
/**
* A set of elements that does not contain any element twice.
*
* @param <E> the type of all contained elements.
*/
public interface Set<E> extends Iterable<E> {
/**
* Returns the number of elements stored in this set.
*
* @return the number of elements in this set
*/
int size();
/**
* Returns true if this set contains no elements.
*
* @return true if this set contains no elements
*/
boolean isEmpty();
/**
* Returns true if this set contains the specified element.
*
* @return true if this set contains the specified element.
*/
boolean contains(Object el);
/**
* Returns the union set of this set and the specified set.
*
* @param other a set
* @return the union set of this set and the specified set.
*/
Set<E> union(Set<E> other);
/**
* returns the set resulting from adding the specified element to this set.
*
* @param element an element (must not be null)
* @return the set resulting from adding the specified element to this set.
*/
Set<E> add(E element);
/**
* Returns the intersection of this set and the specified set.
*
* @param other a set
* @return the intersection of this set and the specified set.
*/
Set<E> intersection(Set<E> other);
/**
* Returns true if all elements of this set are contained in the specified set.
*
* @param other a set
* @return true if all elements of this set are contained in the specified set.
*/
boolean subsetOf(Set<?> other);
}

View File

@@ -0,0 +1,29 @@
package collection;
public class SetDemo {
public static void main(String[] args) {
final Set<String> set1 = SetFactory.create();
System.out.println("set1 = " + set1);
final Set<String> set2 = set1.add("foo");
System.out.println("set2 = " + set2);
final Set<String> set3 = set2.add("foo");
System.out.println("set3 = " + set3);
final Set<String> set4 = set3.add("bar");
System.out.println("set4 = " + set4);
final Set<String> set5 = SetFactory.create("foo", "baz", "foo");
System.out.println("set5 = " + set5);
final Set<String> set6 = set4.union(set5);
System.out.println("set6 = " + set6);
final Set<String> set7 = set4.intersection(set5);
System.out.println("set7 = " + set7);
final Set<String> set8 = set5.intersection(set4);
System.out.println("set8 = " + set8);
}
}

View File

@@ -0,0 +1,49 @@
package collection;
public class SetFactory {
// Notwendig um einen korrekten Vergleich von EmptySet zu ermöglichen
private static final EmptySet<Object> EMPTY_SET = new EmptySet<>();
private SetFactory() { /* don't instantiate */ }
/**
* Returns the empty set.
*
* @param <T> the element type of the returned set.
* @return the empty set.
*/
static <T> Set<T> create() {
return (Set<T>) EMPTY_SET;
}
/**
* Returns the singleton set containing the specified element.
*
* @param element an element (must not be null)
* @param <T> the element type of the returned set.
* @return the singleton set containing the specified element.
*/
static <T> Set<T> create(T element) {
return new SingeltonSet<>(element);
}
/**
* Returns a set containing the specified elements. The specified elements may contain equal elements.
*
* @param elems elements of the returned set (may contain equal elements)
* @param <T> the element type of the returned set.
* @return a set containing the specified elements.
*/
static <T> Set<T> create(T... elems) {
if (elems.length == 0) return create();
if (elems.length == 1) return create(elems[0]);
Set<T> result = create();
for (T t : elems) {
result = result.add(t);
}
return result;
}
}

View File

@@ -0,0 +1,89 @@
package collection;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Objects;
class SingeltonSet<E> implements Set<E> {
private final E element;
SingeltonSet(E element) {
this.element = Objects.requireNonNull(element);
}
@Override
public int size() {
return 1;
}
@Override
public boolean isEmpty() {
return false;
}
@Override
public boolean contains(Object el) {
return element.equals(el);
}
@Override
public boolean subsetOf(Set<?> other) {
return other.contains(element);
}
@Override
public Set<E> union(Set<E> other) {
return other.add(element);
}
@Override
public Set<E> add(E element) {
if (this.element.equals(element))
return this;
return new BigSet<>(element, this);
}
@Override
public Set<E> intersection(Set<E> other) {
return other.contains(element) ? this : SetFactory.create();
}
@Override
public Iterator<E> iterator() {
return new Iterator<>() {
boolean ready = true;
@Override
public boolean hasNext() {
return ready;
}
@Override
public E next() {
if (!ready)
throw new NoSuchElementException();
ready = false;
return element;
}
};
}
@Override
public String toString() {
return "{" + element + "}";
}
@Override
public int hashCode() {
return 47 * element.hashCode();
}
@Override
public boolean equals(Object obj) {
if (obj instanceof Set<?>) {
final Set<?> other = (Set<?>) obj;
return other.size() == 1 && other.contains(element);
}
return false;
}
}

View File

@@ -0,0 +1,38 @@
package iterator;
import java.util.Iterator;
import java.util.NoSuchElementException;
public class Array2dIterator<T> implements Iterator<T> {
private T[][] array;
private int row = 0;
private int col = 0;
public Array2dIterator(T[][] array) {
this.array = array;
}
@Override
public boolean hasNext() {
if (row < array.length) {
if (col >= array[row].length) {
col = 0;
row++;
}
}
if (row < array.length && col < array[row].length) {
return true;
}
return false;
}
@Override
public T next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
return array[row][col++];
}
}

View File

@@ -0,0 +1,97 @@
package iterator;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
public class IteratorDemo {
private static void demoArray2dIterator() {
System.out.println("Array2dIterator {{}, {\"foo\", \"bar\"}, {\"baz\"}, {}}");
final String[][] array = {{}, {"foo", "bar"}, {"baz"}, {}};
final Iterator<String> it = new Array2dIterator<>(array);
while (it.hasNext())
System.out.println(it.next());
}
private static void demoArray2dIteratorOnlyEmpty() {
System.out.println("Array2dIterator {{}, {}, {}}");
final String[][] array = {{}, {}, {}};
final Iterator<String> it = new Array2dIterator<>(array);
while (it.hasNext())
System.out.println(it.next());
}
private static void demoArray2dIteratorEmpty() {
System.out.println("Array2dIterator {}");
final String[][] array = {};
final Iterator<String> it = new Array2dIterator<>(array);
while (it.hasNext())
System.out.println(it.next());
}
private static void demoSkipNullIterator() {
System.out.println("SkipNullIterator [\"a\", \"b\", null, \"c\"]");
final Iterator<String> oriIt = Arrays.asList("a", "b", null, "c").iterator();
final Iterator<String> it = new SkipNullIterator<>(oriIt);
while (it.hasNext())
System.out.println(it.next());
}
private static void demoSkipNullIteratorSingleton() {
System.out.println("SkipNullIterator [\"foo\"]");
final Iterator<String> oriIt = List.of("foo").iterator();
final Iterator<String> it = new SkipNullIterator<>(oriIt);
while (it.hasNext())
System.out.println(it.next());
}
private static void demoSkipNullIteratorInfinity() {
System.out.println("SkipNullIterator [\"infinity\", \"infinity\", ...]");
final Iterator<String> oriIt = new Iterator<>() {
@Override
public boolean hasNext() {
return true;
}
@Override
public String next() {
return "infinity";
}
};
final Iterator<String> it = new SkipNullIterator<>(oriIt);
for (int i = 0; i < 10; i++)
System.out.println(i + ": " + it.next());
}
private static void demoPathological() {
System.out.println("SkipNullIterator [null, ..., null, \"infinity\", \"infinity\", ...]");
final Iterator<String> oriIt = new Iterator<>() {
private int ctr;
@Override
public boolean hasNext() {
return true;
}
@Override
public String next() {
return ctr++ > 100000 ? "infinity" : null;
}
};
final SkipNullIterator<String> it = new SkipNullIterator<>(oriIt);
for (int i = 0; i < 10; i++)
System.out.println(i + ": " + it.next());
}
public static void main(String[] args) {
demoArray2dIterator();
demoArray2dIteratorEmpty();
demoArray2dIteratorOnlyEmpty();
demoSkipNullIterator();
demoSkipNullIteratorSingleton();
demoSkipNullIteratorInfinity();
demoPathological();
}
}

View File

@@ -0,0 +1,34 @@
package iterator;
import java.util.Iterator;
import java.util.NoSuchElementException;
public class SkipNullIterator<T> implements Iterator<T> {
private final Iterator<T> iterator;
private T nextObj;
public SkipNullIterator(Iterator<T> iterator) {
this.iterator = iterator;
}
@Override
public boolean hasNext() {
while (nextObj == null && iterator.hasNext()) {
nextObj = iterator.next();
}
return nextObj != null;
}
@Override
public T next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
final T ret = nextObj;
nextObj = null;
return ret;
}
}

View File

@@ -0,0 +1,17 @@
package collection;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
public class SetTest {
@Test
public void testAdd() {
Set<Integer> set = SetFactory.create();
set = set.add(1);
assertTrue(set.contains(1));
set = set.add(3);
assertTrue(set.contains(3));
}
}

View File

@@ -0,0 +1,43 @@
package iterator;
import org.junit.Test;
import java.util.Iterator;
import java.util.NoSuchElementException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
public class Array2dIteratorTest {
@Test
public void testArray2dIterator() {
final String[][] array = {{}, {"foo", "bar"}, {"baz"}, {}};
final Iterator<String> it = new Array2dIterator<>(array);
assertTrue(it.hasNext());
assertEquals("foo", it.next());
assertTrue(it.hasNext());
assertEquals("bar", it.next());
assertTrue(it.hasNext());
assertEquals("baz", it.next());
assertFalse(it.hasNext());
assertThrows(NoSuchElementException.class, it::next);
}
@Test
public void testArray2dIteratorOnlyEmpty() {
final String[][] array = {{}, {}, {}};
final Iterator<String> it = new Array2dIterator<>(array);
assertFalse(it.hasNext());
assertThrows(NoSuchElementException.class, it::next);
}
@Test
public void testArray2dIteratorEmpty() {
final String[][] array = {};
final Iterator<String> it = new Array2dIterator<>(array);
assertFalse(it.hasNext());
assertThrows(NoSuchElementException.class, it::next);
}
}

View File

@@ -0,0 +1,122 @@
package iterator;
import org.junit.Test;
import java.util.Iterator;
import java.util.NoSuchElementException;
import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
public class SkipNullIteratorTest {
@Test
public void testEmpty() {
final Iterator<Object> it = new SkipNullIterator<>(emptyList().iterator());
assertFalse(it.hasNext());
assertThrows(NoSuchElementException.class, it::next);
}
@Test
public void testNull() {
final Iterator<Object> it = new SkipNullIterator<>(asList(null, null).iterator());
assertFalse(it.hasNext());
assertThrows(NoSuchElementException.class, it::next);
}
@Test
public void testNonNull() {
final Iterator<String> it = new SkipNullIterator<>(asList("foo").iterator());
assertTrue(it.hasNext());
assertEquals("foo", it.next());
assertFalse(it.hasNext());
assertThrows(NoSuchElementException.class, it::next);
}
@Test
public void testMixed() {
final Iterator<String> it = new SkipNullIterator<>(asList(null, "foo", null).iterator());
assertTrue(it.hasNext());
assertEquals("foo", it.next());
assertFalse(it.hasNext());
assertThrows(NoSuchElementException.class, it::next);
}
@Test
public void testMixed2() {
final Iterator<String> oriIt = asList("a", "b", null, "c").iterator();
Iterator<String> it = new SkipNullIterator<>(oriIt);
assertTrue(it.hasNext());
assertEquals("a", it.next());
assertTrue(it.hasNext());
assertEquals("b", it.next());
assertTrue(it.hasNext());
assertEquals("c", it.next());
assertFalse(it.hasNext());
assertThrows(NoSuchElementException.class, it::next);
}
@Test
public void testDontCallInCtor() {
final Iterator<String> dontCallNext = new Iterator<>() {
@Override
public boolean hasNext() {
throw new RuntimeException();
}
@Override
public String next() {
throw new RuntimeException();
}
};
new SkipNullIterator<>(dontCallNext);
}
@Test
public void testSkipNullIteratorInfinity() {
final Iterator<String> oriIt = new Iterator<>() {
@Override
public boolean hasNext() {
return true;
}
@Override
public String next() {
return "infinity";
}
};
final Iterator<String> it = new SkipNullIterator<>(oriIt);
for (int i = 0; i < 1000; i++) {
assertTrue(it.hasNext());
assertEquals("infinity", it.next());
}
}
@Test
public void testPathological() {
final Iterator<String> oriIt = new Iterator<>() {
private int ctr;
@Override
public boolean hasNext() {
return true;
}
@Override
public String next() {
return ctr++ > 100000 ? "infinity" : null;
}
};
final Iterator<String> it = new SkipNullIterator<>(oriIt);
for (int i = 0; i < 1000; i++) {
assertTrue(it.hasNext());
assertEquals("infinity", it.next());
}
}
}