Skip to content

Commit 1fd06b8

Browse files
committed
Some more implementation that didn't work out
1 parent e66c539 commit 1fd06b8

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

examples/reversi/board.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,26 @@ impl Piece {
3434
}
3535

3636
fn valid_moves_for(tiles: &Tiles, piece: Piece) -> Vec<Position> {
37-
Default::default() //TODO
37+
// ALGORITHM: Go through all rows, columns and diagonals. Look through each row forwards and
38+
// backwards. Find an empty tile followed by the other piece. If you can then find another
39+
// `piece` before finding an empty tile, the empty tile is a valid move.
40+
41+
let other = piece.other();
42+
//TODO: Convert each row into an iterator of (pos, tile)
43+
let rows = search_row(tiles.rows().enumerate().map(|(i, r)| r.enumerate().map(|(j, c)| ((i, j), c))))
44+
.chain(search_row(tiles.cols().enumerate().map(|(j, c)| c.enumerate().map(|(i, r)| ((i, j), c)))))
45+
.chain(tiles.diagonals_tlbr())
46+
.chain(tiles.diagonals_trbl())
47+
.collect();
48+
for pieces in rows {
49+
let potential_move = None;
50+
}
51+
}
52+
53+
//TODO: Change return type into iterator
54+
fn search_row<R, C>(row: R) -> Vec<Position>
55+
where R: Iterator<Item=C>,
56+
C: Iterator<Item=(Position, Option<Piece>)> {
3857
}
3958

4059
#[derive(Debug)]

0 commit comments

Comments
 (0)