INDAPlus21 / INDAPlus21/melg-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

**Superbra jobbat!!**

Creating, yet lazy, way of testing basic functionality.

Quick tip:
_Your code_:
```rust
// Check if a colour is checked in the current board
fn check_for_checked(colour_to_be_checked: Colour, game: &mut Game) -> bool {
// Loop through board and see if any opponent piece has a move that takes the king. Checked colour is the colour to check if they can check the opponent
for _x in 0..8 {
for _y in 0..8 {
if game.board[_x][_y].as_ref().is_none()
|| game.board[_x][_y].as_ref().unwrap().1 == colour_to_be_checked
{
continue;
}

// Get all moves for the piece
let piece = game.board[_x][_y].as_ref().unwrap().to_owned();
let piece_moves = piece.0.get_available_moves((_x, _y), false, game);

// Check if move conquers the oponents king
for _move in piece_moves.iter() {
let target_piece = game.board[_move.0][_move.1].as_ref();
if target_piece.is_some()
&& target_piece.unwrap().1 == colour_to_be_checked
&& target_piece.unwrap().0 == Piece::King
{
println!("CHECK: {} {}", _move.0, _move.1);
return true;
}
}
}
}

false
}

fn check_for_checkmate(colour_to_be_checked: Colour, game: &mut Game) -> bool {
// Loop through board and see if a colour has no available moves
for _x in 0..8 {
for _y in 0..8 {
if game.board[_x][_y].as_ref().is_none()
|| game.board[_x][_y].as_ref().unwrap().1 != colour_to_be_checked
{
continue;
}

// Get all moves for the piece
let piece = game.board[_x][_y].as_ref().unwrap().to_owned();
let piece_moves = piece.0.get_available_moves((_x, _y), true, game);

// A single possible moves means that the colour is not in checkmate
if piece_moves.len() > 0 {
return false;
}
}
}

true
}
```
_Shorter version using pattern matching_:
```rust
// Check if a colour is checked in the current board
fn check_for_checked(colour_to_be_checked: Colour, game: &mut Game) -> bool {
// Loop through board and see if any opponent piece has a move that takes the king. Checked colour is the colour to check if they can check the opponent
for _x in 0..8 {
for _y in 0..8 {
if let Some((_piece, _colour)) = game.board[_x][_y] {
if _colour != colour_to_be_checked {
// Check if move conquers the oponents king
for (_move_x, _move_y) in _piece.get_available_moves((_x, _y), false, game).iter() {
if let Some((Piece::King, _colour)) = game.board[*_move_x][*_move_y] {
if _colour == colour_to_be_checked {
println!("CHECK: {} {}", _move_x, _move_y);
return true;
}
}
}
}
}
}
}

false
}

fn check_for_checkmate(colour_to_be_checked: Colour, game: &mut Game) -> bool {
// Loop through board and see if a colour has no available moves
for _x in 0..8 {
for _y in 0..8 {
if let Some((_piece, _colour)) = game.board[_x][_y] {
if _colour == colour_to_be_checked {
// A single possible moves means that the colour is not in checkmate
if _piece.get_available_moves((_x, _y), true, game).len() > 0 {
return false;
}
}
}
}
}

true
}
```

Your `check_for_checked` looks neat, but is inefficient. A quicker solution whould be to search for attacking pieces from the king's perspective, rather than calculating all available moves of all of the opponent's pieces.

Contributor guide

No contributing guide indexed for this repository

Research direction

The issue discusses check_for_checked and check_for_checkmate; start by reading those functions and Piece::get_available_moves. Refactor check detection to search for attacking pieces from the king’s perspective rather than calculating all available moves for every opponent piece, while preserving the existing check result.

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.