diff --git a/bin/uebung09/chess/Board.class b/bin/uebung09/chess/Board.class index 8dee0ab..fe2a88c 100644 Binary files a/bin/uebung09/chess/Board.class and b/bin/uebung09/chess/Board.class differ diff --git a/bin/uebung09/iterator/Array2dIterator.class b/bin/uebung09/iterator/Array2dIterator.class index 53abed5..6b430a2 100644 Binary files a/bin/uebung09/iterator/Array2dIterator.class and b/bin/uebung09/iterator/Array2dIterator.class differ diff --git a/src/uebung09/chess/Board.java b/src/uebung09/chess/Board.java index b0744ce..b69232a 100644 --- a/src/uebung09/chess/Board.java +++ b/src/uebung09/chess/Board.java @@ -1,15 +1,12 @@ package uebung09.chess; -import java.util.ArrayList; import java.util.Iterator; -import java.util.List; import uebung09.iterator.Array2dIterator; public class Board implements Iterable{ private final Piece[][] field = new Piece[8][8]; - private final List pieces = new ArrayList<>(); void add(Piece piece) { @@ -19,7 +16,6 @@ public class Board implements Iterable{ if (existing != null) throw new IllegalArgumentException("already occupied by " + existing); field[piece.getRow() - 1][piece.getCol() - 1] = piece; - pieces.add(piece); } public void printBoard() { @@ -42,9 +38,9 @@ public class Board implements Iterable{ } 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()); @@ -54,13 +50,22 @@ public class Board implements Iterable{ } public String toString() { - return pieces.toString(); + // StringBuilder s = new StringBuilder("["); + + // for(Piece p : this) { + // s.append(p.toString()); + // } + + // s.append("]"); + + // return s.toString(); + return ""; } @Override public Iterator iterator() { // TODO Auto-generated method stub - return new Array2dIterator<>(null); - //throw new UnsupportedOperationException("Unimplemented method 'iterator'"); + return new Array2dIterator<>(field); + // throw new UnsupportedOperationException("Unimplemented method 'iterator'"); } } diff --git a/src/uebung09/iterator/Array2dIterator.java b/src/uebung09/iterator/Array2dIterator.java index 0310dee..8d9b507 100644 --- a/src/uebung09/iterator/Array2dIterator.java +++ b/src/uebung09/iterator/Array2dIterator.java @@ -1,6 +1,5 @@ package uebung09.iterator; -import java.util.ArrayList; import java.util.Iterator; import java.util.NoSuchElementException; @@ -14,15 +13,30 @@ public class Array2dIterator implements Iterator{ this.array = array; } + + /** + * Returns {@code true} if the iteration has more elements. + * (In other words, returns {@code true} if {@link #next} would + * return an element rather than throwing an exception.) + * + * @return {@code true} if the iteration has more elements + */ @Override public boolean hasNext() { + while(row < array.length && (array[row] == null || col >= array[row].length)) { row++; col = 0; } - return row > array.length; + return row < array.length; } + /** + * Returns the next element in the iteration. + * + * @return the next element in the iteration + * @throws NoSuchElementException if the iteration has no more elements + */ @Override public T next() { if (!hasNext()) { @@ -30,4 +44,6 @@ public class Array2dIterator implements Iterator{ } return array[row][col++]; } + + }