uebung09
This commit is contained in:
parent
f42c1b23ab
commit
57aae3eda6
@ -1,11 +1,14 @@
|
|||||||
package chess;
|
package chess;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
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 Piece[][] field = new Piece[8][8];
|
||||||
private final List<Piece> pieces = new ArrayList<>();
|
//private final List<Piece> pieces = new ArrayList<>();
|
||||||
|
|
||||||
void add(Piece piece) {
|
void add(Piece piece) {
|
||||||
if (piece.getBoard() != this)
|
if (piece.getBoard() != this)
|
||||||
@ -14,7 +17,7 @@ public class Board {
|
|||||||
if (existing != null)
|
if (existing != null)
|
||||||
throw new IllegalArgumentException("already occupied by " + existing);
|
throw new IllegalArgumentException("already occupied by " + existing);
|
||||||
field[piece.getRow() - 1][piece.getCol() - 1] = piece;
|
field[piece.getRow() - 1][piece.getCol() - 1] = piece;
|
||||||
pieces.add(piece);
|
//pieces.add(piece);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void printBoard() {
|
public void printBoard() {
|
||||||
@ -37,9 +40,9 @@ public class Board {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void check() {
|
public void check() {
|
||||||
for (Piece p1 : pieces) {
|
for (Piece p1 : this) {
|
||||||
System.out.println(p1.toString());
|
System.out.println(p1.toString());
|
||||||
for (Piece p2 : pieces)
|
for (Piece p2 : this)
|
||||||
if (p1 != p2)
|
if (p1 != p2)
|
||||||
if (p1.canCapture(p2))
|
if (p1.canCapture(p2))
|
||||||
System.out.println(" can capture " + p2.toString());
|
System.out.println(" can capture " + p2.toString());
|
||||||
@ -49,6 +52,20 @@ public class Board {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public String toString() {
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package chess;
|
package chess;
|
||||||
|
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
import java.util.Iterator;
|
||||||
import java.util.NoSuchElementException;
|
import java.util.NoSuchElementException;
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
||||||
@ -12,6 +13,7 @@ public class ChessApp {
|
|||||||
private static final String BLACK_QUEEN = "Q";
|
private static final String BLACK_QUEEN = "Q";
|
||||||
private static final String WHITE_KNIGHT = "n";
|
private static final String WHITE_KNIGHT = "n";
|
||||||
private static final String BLACK_KNIGHT = "N";
|
private static final String BLACK_KNIGHT = "N";
|
||||||
|
private static final String ITERATOR = "i";
|
||||||
|
|
||||||
private final Scanner scanner;
|
private final Scanner scanner;
|
||||||
private final Board board;
|
private final Board board;
|
||||||
@ -44,6 +46,7 @@ public class ChessApp {
|
|||||||
case WHITE_QUEEN -> addQueen(Color.white);
|
case WHITE_QUEEN -> addQueen(Color.white);
|
||||||
case BLACK_KNIGHT -> addKnight(Color.black);
|
case BLACK_KNIGHT -> addKnight(Color.black);
|
||||||
case WHITE_KNIGHT -> addKnight(Color.white);
|
case WHITE_KNIGHT -> addKnight(Color.white);
|
||||||
|
case ITERATOR -> iterate();
|
||||||
default -> System.out.println("Invalid command " + command);
|
default -> System.out.println("Invalid command " + command);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -70,6 +73,15 @@ public class ChessApp {
|
|||||||
board.printBoard();
|
board.printBoard();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void iterate() {
|
||||||
|
final Iterator<Piece> it = board.iterator();
|
||||||
|
|
||||||
|
|
||||||
|
while (it.hasNext()) {
|
||||||
|
System.out.println(it.next());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void help() {
|
private void help() {
|
||||||
System.out.println("Commands:");
|
System.out.println("Commands:");
|
||||||
System.out.println(ABORT + ": terminate the program");
|
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(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(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(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");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
80
uebung09/src/collection/BigSet.java
Normal file
80
uebung09/src/collection/BigSet.java
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
61
uebung09/src/collection/EmptySet.java
Normal file
61
uebung09/src/collection/EmptySet.java
Normal 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -11,8 +11,7 @@ public class SetFactory {
|
|||||||
* @return the empty set.
|
* @return the empty set.
|
||||||
*/
|
*/
|
||||||
static <T> Set<T> create() {
|
static <T> Set<T> create() {
|
||||||
// TODO implement
|
return new EmptySet<T>();
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -23,8 +22,7 @@ public class SetFactory {
|
|||||||
* @return the singleton set containing the specified element.
|
* @return the singleton set containing the specified element.
|
||||||
*/
|
*/
|
||||||
static <T> Set<T> create(T element) {
|
static <T> Set<T> create(T element) {
|
||||||
// TODO implement
|
return new SingeltonSet<T>(element);
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -35,7 +33,14 @@ public class SetFactory {
|
|||||||
* @return a set containing the specified elements.
|
* @return a set containing the specified elements.
|
||||||
*/
|
*/
|
||||||
static <T> Set<T> create(T... elems) {
|
static <T> Set<T> create(T... elems) {
|
||||||
// TODO implement
|
if (elems.length == 0) return create();
|
||||||
return null;
|
if (elems.length == 1) return create(elems[0]);
|
||||||
|
Set<T> result = create();
|
||||||
|
|
||||||
|
for (T t : elems) {
|
||||||
|
result = result.add(t);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
77
uebung09/src/collection/SingeltonSet.java
Normal file
77
uebung09/src/collection/SingeltonSet.java
Normal 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
38
uebung09/src/iterator/Array2dIterator.java
Normal file
38
uebung09/src/iterator/Array2dIterator.java
Normal 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++];
|
||||||
|
}
|
||||||
|
}
|
34
uebung09/src/iterator/SkipNullIterator.java
Normal file
34
uebung09/src/iterator/SkipNullIterator.java
Normal 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
17
uebung09/test/collection/SetTest.java
Normal file
17
uebung09/test/collection/SetTest.java
Normal 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));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user