Skip to content

Commit

Permalink
add linter (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
jamespeilunli committed Sep 3, 2023
1 parent f70a159 commit 36a7e94
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 10 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@ Chess engine in Rust that compiles to WASM

[View the game](https://minecraftu.github.io/2023-computer-adventures/)

## compile & run instructions
## Compile & run instructions

```
wasm-pack build --target web --out-dir client/pkg
cd client && python3 -m http.server
```

## Linting

`cargo clippy`

Automatically apply suggestions: `cargo clippy --fix`
4 changes: 2 additions & 2 deletions src/evaluate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ fn material(board : &Board) -> f32 {
}

fn mobility(board : &Board) -> f32 {
let black_mobility = MoveGen::new_legal(&board).len() as f32;
let black_mobility = MoveGen::new_legal(board).len() as f32;
let new_board = board.null_move();
if new_board == None {
if new_board.is_none() {
return 0.0;
}
let white_mobility = MoveGen::new_legal(&new_board.unwrap()).len() as f32;
Expand Down
9 changes: 5 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(clippy::needless_return)]
use chess::{Piece, Board, ChessMove, Square, MoveGen};
use wasm_bindgen::prelude::*;
use std::str::FromStr;
Expand Down Expand Up @@ -30,7 +31,7 @@ pub fn get_engine_move(board_str : &str, source_str: &str, target_str: &str, pro
}

let engine_move_option = search::search(&result.unwrap());
if engine_move_option == None {
if engine_move_option.is_none() {
if result.unwrap().checkers().popcnt() == 0 {
return String::from("stalemate after player move");
} else {
Expand All @@ -47,9 +48,9 @@ pub fn get_engine_move(board_str : &str, source_str: &str, target_str: &str, pro

if MoveGen::new_legal(&engine_result.unwrap()).len() == 0 {
if engine_result.unwrap().checkers().popcnt() == 0 {
return format!("stalemate after engine move;{}", engine_result.unwrap().to_string());
return format!("stalemate after engine move;{}", engine_result.unwrap());
} else {
return format!("checkmate, engine won;{}", engine_result.unwrap().to_string());
return format!("checkmate, engine won;{}", engine_result.unwrap());
}
}

Expand All @@ -69,7 +70,7 @@ fn make_move(chess_move : ChessMove, board : &Board) -> Result<Board, &'static s

#[cfg(test)]
mod tests {
use pprof;

use pprof::protos::Message;
use std::fs::File;
use std::io::Write;
Expand Down
6 changes: 3 additions & 3 deletions src/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub fn alpha_beta_max(mut alpha : f32, beta : f32, depth : i32, board : &Board)
if depth == 0 {
return evaluate::evaluate(board);
};
for chess_move in MoveGen::new_legal(&board) {
for chess_move in MoveGen::new_legal(board) {
let mut result = *board;
board.make_move(chess_move, &mut result);
let score: f32 = alpha_beta_min(alpha, beta, depth - 1, &result);
Expand All @@ -29,7 +29,7 @@ pub fn alpha_beta_min(alpha : f32, mut beta : f32, depth : i32, board : &Board)
if depth == 0 {
return evaluate::evaluate(board);
};
for chess_move in MoveGen::new_legal(&board) {
for chess_move in MoveGen::new_legal(board) {
let mut result = *board;
board.make_move(chess_move, &mut result);
let score: f32 = alpha_beta_max(alpha, beta, depth - 1, &result);
Expand All @@ -48,7 +48,7 @@ pub fn search(board : &Board) -> Option<ChessMove> {
let mut best_move: Option<ChessMove> = None;
let mut max: f32 = f32::MIN;
// let mut move_hist =
for chess_move in MoveGen::new_legal(&board) {
for chess_move in MoveGen::new_legal(board) {
let mut result = *board;
board.make_move(chess_move, &mut result);
let score: f32 = alpha_beta_min(f32::MIN, f32::MAX, 3, &result);
Expand Down

0 comments on commit 36a7e94

Please sign in to comment.