This commit is contained in:
Johannes Schmelz 2025-06-12 11:59:41 +02:00
parent f42c1b23ab
commit 57aae3eda6
9 changed files with 354 additions and 12 deletions

View File

@ -1,11 +1,14 @@
package chess;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Board {
import iterator.Array2dIterator;
public class Board implements Iterable<Piece> {
private final Piece[][] field = new Piece[8][8];
private final List<Piece> pieces = new ArrayList<>();
//private final List<Piece> pieces = new ArrayList<>();
void add(Piece piece) {
if (piece.getBoard() != this)
@ -14,7 +17,7 @@ public class Board {
if (existing != null)
throw new IllegalArgumentException("already occupied by " + existing);
field[piece.getRow() - 1][piece.getCol() - 1] = piece;
pieces.add(piece);
//pieces.add(piece);
}
public void printBoard() {
@ -37,9 +40,9 @@ public class Board {
}
public void check() {
for (Piece p1 : pieces) {
for (Piece p1 : this) {
System.out.println(p1.toString());
for (Piece p2 : pieces)
for (Piece p2 : this)
if (p1 != p2)
if (p1.canCapture(p2))
System.out.println(" can capture " + p2.toString());
@ -49,6 +52,20 @@ public class Board {
}
public String toString() {
return pieces.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 Array2dIterator<>(field);
}
}

View File

@ -1,6 +1,7 @@
package chess;
import java.io.InputStream;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Scanner;
@ -12,6 +13,7 @@ public class ChessApp {
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;
@ -44,6 +46,7 @@ public class ChessApp {
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);
}
}
@ -70,6 +73,15 @@ public class ChessApp {
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");
@ -78,5 +90,6 @@ public class ChessApp {
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,80 @@
package collection;
import java.util.Iterator;
class BigSet<E> implements Set<E> {
private E first;
private Set<E> rest;
public BigSet(E first, Set<E> rest) {
this.first = first;
this.rest = rest;
}
@Override
public Iterator<E> iterator() {
return new Iterator<E>() {
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 int size() {
return rest.size() + 1;
}
@Override
public boolean isEmpty() {
return false;
}
@Override
public boolean contains(Object el) {
return first.equals(el) || rest.contains(el);
}
@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<E>(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 boolean subsetOf(Set<?> other) {
return other.contains(first) && rest.subsetOf(other);
}
}

View File

@ -0,0 +1,61 @@
package collection;
import java.util.Iterator;
import java.util.NoSuchElementException;
class EmptySet<E> implements Set<E> {
@Override
public Iterator<E> iterator() {
return new Iterator<E>() {
@Override
public boolean hasNext() {
return false;
}
@Override
public E next() {
throw new NoSuchElementException();
}
};
}
@Override
public int size() {
return 0;
}
@Override
public boolean isEmpty() {
return true;
}
@Override
public boolean contains(Object el) {
return false;
}
@Override
public Set union(Set other) {
return other;
}
@Override
public Set add(Object element) {
return SetFactory.create(element);
}
@Override
public Set intersection(Set other) {
return new EmptySet();
//return this;
}
@Override
public boolean subsetOf(Set other) {
return true;
}
}

View File

@ -11,8 +11,7 @@ public class SetFactory {
* @return the empty set.
*/
static <T> Set<T> create() {
// TODO implement
return null;
return new EmptySet<T>();
}
/**
@ -23,8 +22,7 @@ public class SetFactory {
* @return the singleton set containing the specified element.
*/
static <T> Set<T> create(T element) {
// TODO implement
return null;
return new SingeltonSet<T>(element);
}
/**
@ -35,7 +33,14 @@ public class SetFactory {
* @return a set containing the specified elements.
*/
static <T> Set<T> create(T... elems) {
// TODO implement
return null;
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,77 @@
package collection;
import java.util.Iterator;
import java.util.NoSuchElementException;
class SingeltonSet<E> implements Set<E> {
private final E elem;
public SingeltonSet(E elem) {
this.elem = elem;
}
@Override
public Iterator iterator() {
return new Iterator<E>() {
boolean returned = true;
@Override
public boolean hasNext() {
return returned;
}
@Override
public E next() {
if(!hasNext()) {
throw new NoSuchElementException();
}
returned = false;
return elem;
}
};
}
@Override
public int size() {
return 1;
}
@Override
public boolean isEmpty() {
return false;
}
@Override
public boolean contains(Object el) {
return elem.equals(el);
}
@Override
public Set union(Set other) {
return other.add(elem);
}
@Override
public Set add(Object element) {
if (elem.equals(element)) {
return this;
}
return new BigSet(element, this);
}
@Override
public Set intersection(Set other) {
return other.contains(elem) ? this : SetFactory.create();
}
@Override
public boolean subsetOf(Set other) {
return other.contains(elem);
}
}

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,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));
}
}