ahhhhhhhhh
This commit is contained in:
parent
dad5a33695
commit
62da4a9ea5
Binary file not shown.
Binary file not shown.
@ -1,15 +1,12 @@
|
|||||||
package uebung09.chess;
|
package uebung09.chess;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import uebung09.iterator.Array2dIterator;
|
import uebung09.iterator.Array2dIterator;
|
||||||
|
|
||||||
|
|
||||||
public class Board implements Iterable<Piece>{
|
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<>();
|
|
||||||
|
|
||||||
|
|
||||||
void add(Piece piece) {
|
void add(Piece piece) {
|
||||||
@ -19,7 +16,6 @@ public class Board implements Iterable<Piece>{
|
|||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void printBoard() {
|
public void printBoard() {
|
||||||
@ -42,9 +38,9 @@ public class Board implements Iterable<Piece>{
|
|||||||
}
|
}
|
||||||
|
|
||||||
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());
|
||||||
@ -54,13 +50,22 @@ public class Board implements Iterable<Piece>{
|
|||||||
}
|
}
|
||||||
|
|
||||||
public String toString() {
|
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
|
@Override
|
||||||
public Iterator<Piece> iterator() {
|
public Iterator<Piece> iterator() {
|
||||||
// TODO Auto-generated method stub
|
// TODO Auto-generated method stub
|
||||||
return new Array2dIterator<>(null);
|
return new Array2dIterator<>(field);
|
||||||
//throw new UnsupportedOperationException("Unimplemented method 'iterator'");
|
// throw new UnsupportedOperationException("Unimplemented method 'iterator'");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
package uebung09.iterator;
|
package uebung09.iterator;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.NoSuchElementException;
|
import java.util.NoSuchElementException;
|
||||||
|
|
||||||
@ -14,15 +13,30 @@ public class Array2dIterator<T> implements Iterator<T>{
|
|||||||
this.array = array;
|
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
|
@Override
|
||||||
public boolean hasNext() {
|
public boolean hasNext() {
|
||||||
|
|
||||||
while(row < array.length && (array[row] == null || col >= array[row].length)) {
|
while(row < array.length && (array[row] == null || col >= array[row].length)) {
|
||||||
row++;
|
row++;
|
||||||
col = 0;
|
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
|
@Override
|
||||||
public T next() {
|
public T next() {
|
||||||
if (!hasNext()) {
|
if (!hasNext()) {
|
||||||
@ -30,4 +44,6 @@ public class Array2dIterator<T> implements Iterator<T>{
|
|||||||
}
|
}
|
||||||
return array[row][col++];
|
return array[row][col++];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user