INDAPlus21 / INDAPlus21/ellmar-chess

Pass

Open
#1 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
0
Forks
0
PR merge metrics
No merged PRs in 30d

Description

_I recognise the struggle, därför får du inte komplettering._

## Komplettering (egentligen inte) (som den hade sett ut)

**Almost there!**

However, I failed to compile your library. I guess that this is a case of "but it works on my machine".

```rust
error[E0277]: `[[i8; 2]; 8]` is not an iterator
--> src\lib.rs:201:27
|
201 | for new_coords in possible_positions {
| ^^^^^^^^^^^^^^^^^^ borrow the array with `&` or call `.iter()` on it to iterate over it
|
= help: the trait `Iterator` is not implemented for `[[i8; 2]; 8]`
= note: arrays are not iterators, but slices like the following are: `&[1, 2, 3]`
= note: required because of the requirements on the impl of `IntoIterator` for `[[i8; 2]; 8]`
= note: required by `into_iter`

error[E0277]: `[[i8; 2]; 8]` is not an iterator
--> src\lib.rs:201:27
|
201 | for new_coords in possible_positions {
| ^^^^^^^^^^^^^^^^^^ borrow the array with `&` or call `.iter()` on it to iterate over it
|
= help: the trait `Iterator` is not implemented for `[[i8; 2]; 8]`
= note: arrays are not iterators, but slices like the following are: `&[1, 2, 3]`
= note: required because of the requirements on the impl of `IntoIterator` for `[[i8; 2]; 8]`
= note: required by `into_iter`

error[E0277]: `[[usize; 2]; 8]` is not an iterator
--> src\lib.rs:233:27
|
233 | for new_coords in possible_positions {
| ^^^^^^^^^^^^^^^^^^ borrow the array with `&` or call `.iter()` on it to iterate over it
|
= help: the trait `Iterator` is not implemented for `[[usize; 2]; 8]`
= note: arrays are not iterators, but slices like the following are: `&[1, 2, 3]`
= note: required because of the requirements on the impl of `IntoIterator` for `[[usize; 2]; 8]`
= note: required by `into_iter`

error[E0277]: `[[usize; 2]; 8]` is not an iterator
--> src\lib.rs:233:27
|
233 | for new_coords in possible_positions {
| ^^^^^^^^^^^^^^^^^^ borrow the array with `&` or call `.iter()` on it to iterate over it
|
= help: the trait `Iterator` is not implemented for `[[usize; 2]; 8]`
= note: arrays are not iterators, but slices like the following are: `&[1, 2, 3]`
= note: required because of the requirements on the impl of `IntoIterator` for `[[usize; 2]; 8]`
= note: required by `into_iter`

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0277`.
error: could not compile `ellmar-chess`

To learn more, run the command again with --verbose.
warning: build failed, waiting for other jobs to finish...
error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0277`.
error: build failed
```

Great effort! To pass this assignment you have to:
1) Write and pass tests which confirm basic piece movments.
2) Write and pass tests which confirm check.
3) Write and pass tests which confirm promotion.
4) Actually use `assert` macros within your tests to make them functional.

Your code has huge potential. For example, you have a lot of repearing patterns.

I rewrote your `bishop_possible(&self, position: &String) -> Option>` as an example:

_Your code_:
```rust
fn bishop_possible(&self, position: &String) -> Option> {
let coords = pos_from_string(position);
let piece = self.get_piece(coords).unwrap();
let iswhite = piece.iswhite();
let mut string_positions = vec!();
let mut new_coords: [usize; 2];
let mut other_piece: Option<&Piece>;
let mut x: usize;
let mut y: usize;

x = 0;
y = 0;
while x + coords[0] < 7 && y + coords[1] < 7{
x += 1;
y += 1;
new_coords = [coords[0]+x, coords[1]+y];
other_piece = self.get_piece(new_coords);
if !other_piece.is_none() {
if other_piece.unwrap().iswhite() != iswhite {
string_positions.push(pos_to_string(new_coords));
}
break;
}
else {
string_positions.push(pos_to_string(new_coords));
}
}

x = 0;
y = 0;
while coords[0] > 0 && coords[1] > 0 && coords[0] - x > 0 && coords[1] - y > 0 {
x += 1;
y += 1;
new_coords = [coords[0]-x, coords[1]-y];
other_piece = self.get_piece(new_coords);
if !other_piece.is_none() {
if other_piece.unwrap().iswhite() != iswhite {
string_positions.push(pos_to_string(new_coords));
}
break;
}
else {
string_positions.push(pos_to_string(new_coords));
}
}

x = 0;
y = 0;
while coords[0] > 0 && coords[0] - x > 0 && y + coords[1] < 7{
x += 1;
y += 1;
new_coords = [coords[0]-x, coords[1]+y];
other_piece = self.get_piece(new_coords);
if !other_piece.is_none() {
if other_piece.unwrap().iswhite() != iswhite {
string_positions.push(pos_to_string(new_coords));
}
break;
}
else {
string_positions.push(pos_to_string(new_coords));
}
}

x = 0;
y = 0;
while coords[1] > 0 && x + coords[0] < 7 && coords[1] - y > 0{
x += 1;
y += 1;
new_coords = [coords[0]+x, coords[1]-y];
other_piece = self.get_piece(new_coords);
if !other_piece.is_none() {
if other_piece.unwrap().iswhite() != iswhite {
string_positions.push(pos_to_string(new_coords));
}
break;
}
else {
string_positions.push(pos_to_string(new_coords));
}
}
return if string_positions.len() > 0 {Some(string_positions)} else {None};
}
```

_Shorter code_:
```rust
fn bishop_possible(&self, position: &String) -> Option> {
let [rank, file] = pos_from_string(position);

let mut output: Vec = Vec::new();

let add_position = |_coords: [usize; 2], _output: &mut Vec | {
if let Some(_piece) = self.get_piece(_coords) {
if _piece.iswhite() != (self.active == Color::White) {
_output.push(pos_to_string(_coords));
}
} else {
_output.push(pos_to_string(_coords));
}
};

for _i in 1..7 {
if _i + rank < 7 && _i + file < 7 {
add_position([rank + _i, file + _i], &mut output);
}
if rank > 0 && file > 0 && rank - _i > 0 && file - _i > 0 {
add_position([rank - _i, file - _i], &mut output);
}
if rank > 0 && rank - _i > 0 && _i + file < 7 {
add_position([rank - _i, file + _i], &mut output);
}
if file > 0 && _i + rank < 7 && file - _i > 0 {
add_position([rank + _i, file - _i], &mut output);
}
}

match output.len() {
0 => None,
_ => Some(output)
}
}
```

Contributor guide

No contributing guide indexed for this repository

Research direction

Start in src/lib.rs at the loops around lines 201 and 233, then inspect the existing piece-movement implementation and the bishop_possible example. Add tests for basic piece movements, check, and promotion, using assert macros as required. Done means the library compiles, all new tests pass, and the repeated movement logic is addressed.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
game-dev
Issue type
Refactor
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.