INDAPlus21 / INDAPlus21/osveijer-chess-gui

Pass

Open
#1 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
0
Forks
0
PR merge metrics
No merged PRs in 30d

Description

Very well done Oliver!

Only 284 lines! Whoah.

You made the most of the template; I'm happy.

The little code you wrote yourself looks good.

Thanks! :D

Some tips:

  • Use conditional match statements to avoid copy-paste.
    Your code:
match self.selected_square {
    Some(pos) => {
        if pos == (rank, file) {
            self.selected_square = None;
            self.highlighted_squares = Vec::new();
        } else if self.highlighted_squares.iter().any(|p| p == &(rank,file)) {
            self.game.take_turn(move_string(pos, (rank,file)));
            self.selected_square = None;
            self.highlighted_squares = Vec::new();
        } else {
            self.selected_square = Some((rank, file));
            self.highlighted_squares = Vec::new();
            if let Some(c) = get_colour(self.game.board[rank][file]) {
                if c == self.game.current_turn {
                    self.highlighted_squares = self.game.board[rank][file].get_valid_moves( (rank, file), &self.game.board, self.game.en_passant_square, self.game.castlings, self.game.current_turn);
                };
            };
        }
    },
    None => {
        self.selected_square = Some((rank, file));
        self.highlighted_squares = Vec::new();
        if let Some(c) = get_colour(self.game.board[rank][file]) {
            if c == self.game.current_turn {
                self.highlighted_squares = self.game.board[rank][file].get_valid_moves( (rank, file), &self.game.board, self.game.en_passant_square, self.game.castlings, self.game.current_turn);
            };
        };
    }
}

With conditional match cases:

match self.selected_square {
    Some(pos) if pos == (rank, file) => {
        self.selected_square = None;
        self.highlighted_squares = Vec::new();
    },
    Some(pos) if self.highlighted_squares.iter().any(|p| p == &(rank,file)) => {
        self.game.take_turn(move_string(pos, (rank,file)));
        self.selected_square = None;
        self.highlighted_squares = Vec::new();
    },
    _ => {
        self.selected_square = Some((rank, file));
        self.highlighted_squares = Vec::new();
        if let Some(c) = get_colour(self.game.board[rank][file]) {
            if c == self.game.current_turn {
                self.highlighted_squares = self.game.board[rank][file].get_valid_moves( (rank, file), &self.game.board, self.game.en_passant_square, self.game.castlings, self.game.current_turn);
            };
        };
    }
}
  • Use arithmetics to avoid branching.
    Your code:
string.push(match _from.1 {
    0 => 'a',
    1 => 'b',
    2 => 'c',
    3 => 'd',
    4 => 'e',
    5 => 'f',
    6 => 'g',
    7 => 'h',
    _ => panic!("File wrong")
});
match _from.0 {
    0..=7 => string.push(char::from_digit(8 - _from.0 as u32, 10).unwrap()),
    _ => panic!("Rank wrong"),
};
string.push(' ');
string.push(match _to.1 {
    0 => 'a',
    1 => 'b',
    2 => 'c',
    3 => 'd',
    4 => 'e',
    5 => 'f',
    6 => 'g',
    7 => 'h',
    _ => panic!("File wrong")
});
match _to.0 {
    0..=7 => string.push(char::from_digit(8 - _to.0 as u32, 10).unwrap()),
    _ => panic!("Rank wrong"),
};

With arithmetics:

// I would write this as a helper-function instread of a closure.
let parse_push_coord = |_coord: (usize, usize), _s: &mut String| {
    _s.push(match _coord.1 {
        0..=7 => (_coord + 97) as u8 as char,
        _ => panic!("File wrong")
    }) 
    match _coord.0 {
        0..=7 => string.push(char::from_digit(8 - _coord.0 as u32, 10).unwrap()),
        _ => panic!("Rank wrong"),
    };
};

parse_push_coord(_from, &mut _s);
string.push(' ');
parse_push_coord(_to, &mut _s);

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No source file or test is named. Start by locating the self.selected_square match and coordinate-string construction shown in the issue, then verify that the suggested refactoring preserves chess move selection and coordinate formatting behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
game-dev
Issue type
Refactor
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.