Skip to content

Commit

Permalink
Clean up examples, make it more "Rusty"
Browse files Browse the repository at this point in the history
  • Loading branch information
nyurik committed Sep 29, 2024
1 parent d055c8f commit f233e0a
Showing 1 changed file with 21 additions and 54 deletions.
75 changes: 21 additions & 54 deletions examples/connect5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ const SCORE_NONE: i32 = -EVAL_INF - 1;
/// DIRECTION 2: top left to bottom right\
/// DIRECTION 3: top right to bottom left
#[rustfmt::skip]
#[allow(clippy::identity_op)]
const DIRECTION: [[i32; 5]; 4] = [ [1, 2, 3, 4, 5],
[1 * (FILE_SIZE + 1), 2 * (FILE_SIZE + 1), 3 * (FILE_SIZE + 1), 4 * (FILE_SIZE + 1), 5 * (FILE_SIZE + 1)],
[1 * (FILE_SIZE + 2), 2 * (FILE_SIZE + 2), 3 * (FILE_SIZE + 2), 4 * (FILE_SIZE + 2), 5 * (FILE_SIZE + 2)],
Expand Down Expand Up @@ -311,7 +312,7 @@ impl Pos {

match self.p_turn {
Color::Black => {
self.state[mv as usize] = Color::Black;
self.state[mv] = Color::Black;
// update black move and remove empty move in bitboard
self.bitboard[black][0][MAPMOVEIDX[0][mv] as usize] |= MAPMOVEVALUE[0][mv];
self.bitboard[empty][0][MAPMOVEIDX[0][mv] as usize] ^= MAPMOVEVALUE[0][mv];
Expand All @@ -323,7 +324,7 @@ impl Pos {
self.bitboard[empty][1][MAPMOVEIDX[3][mv] as usize] ^= MAPMOVEVALUE[3][mv];
}
Color::White => {
self.state[mv as usize] = Color::White;
self.state[mv] = Color::White;
// update white move and remove empty move in bitboard
self.bitboard[white][0][MAPMOVEIDX[0][mv] as usize] |= MAPMOVEVALUE[0][mv];
self.bitboard[empty][0][MAPMOVEIDX[0][mv] as usize] ^= MAPMOVEVALUE[0][mv];
Expand All @@ -345,11 +346,7 @@ impl Pos {
}

pub fn can_play(&self, from: Square) -> bool {
if self.state[from as usize] == Color::Empty {
true
} else {
false
}
self.state[from as usize] == Color::Empty
}
}

Expand Down Expand Up @@ -421,14 +418,14 @@ fn pos_is_draw(pos: &Pos) -> bool {
break;
}

if found == false {
if !found {
break;
}
}
}

let mut out: bool = false;
if found == true && !pos_is_winner(pos) {
let mut out = false;
if found && !pos_is_winner(pos) {
out = true;
}

Expand All @@ -447,19 +444,11 @@ unsafe fn pos_is_draw_avx512(pos: &Pos) -> bool {
// if all empty is 0, all board is filled.
let temp_mask = _mm512_mask_cmpneq_epi32_mask(0b11111111_11111111, answer, board0org);

if _popcnt32(temp_mask as i32) == 0 && !pos_is_winner_avx512(pos) {
return true;
} else {
return false;
}
_popcnt32(temp_mask as i32) == 0 && !pos_is_winner_avx512(pos)
}

fn pos_is_end(pos: &Pos) -> bool {
if pos_is_winner(pos) || pos_is_draw(pos) {
true
} else {
false
}
pos_is_winner(pos) || pos_is_draw(pos)
}

fn pos_disp(pos: &Pos) {
Expand All @@ -475,7 +464,7 @@ fn pos_disp(pos: &Pos) {
}
}

println!("");
println!();
}

match pos.turn() {
Expand Down Expand Up @@ -581,7 +570,7 @@ fn search(pos: &Pos, alpha: i32, beta: i32, depth: i32, _ply: i32) -> i32 {
}
}

assert!(bm != MOVE_NONE);
assert_ne!(bm, MOVE_NONE);
assert!(bs >= -EVAL_INF && bs <= EVAL_INF);

if _ply == 0 {
Expand All @@ -605,10 +594,8 @@ fn eval(pos: &Pos, _ply: i32) -> i32 {
return -4096;
}
}
} else {
if check_patternlive4(pos, def) {
return -4096;
}
} else if check_patternlive4(pos, def) {
return -4096;
}
}

Expand All @@ -628,10 +615,8 @@ fn eval(pos: &Pos, _ply: i32) -> i32 {
return 2560;
}
}
} else {
if check_patternlive4(pos, atk) {
return 2560;
}
} else if check_patternlive4(pos, atk) {
return 2560;
}
}

Expand All @@ -651,10 +636,8 @@ fn eval(pos: &Pos, _ply: i32) -> i32 {
return 2560;
}
}
} else {
if check_patterndead4(pos, atk) > 0 {
return 2560;
}
} else if check_patterndead4(pos, atk) > 0 {
return 2560;
}
}

Expand Down Expand Up @@ -773,11 +756,7 @@ fn check_pattern5(pos: &Pos, sd: Side) -> bool {
}
}

if n > 0 {
true
} else {
false
}
n > 0
}

/// Check <b>-OOOO-</b>
Expand Down Expand Up @@ -809,11 +788,7 @@ fn check_patternlive4(pos: &Pos, sd: Side) -> bool {
}
}

if n > 0 {
true
} else {
false
}
n > 0
}

/// Check <b>OOOO-, OOO-O, OO-OO, O-OOO, -OOOO</b>
Expand Down Expand Up @@ -961,11 +936,7 @@ unsafe fn pos_is_winner_avx512(pos: &Pos) -> bool {
}
}

if count_match > 0 {
return true;
} else {
return false;
}
count_match > 0
}

#[target_feature(enable = "avx512f,avx512bw")]
Expand Down Expand Up @@ -1027,11 +998,7 @@ unsafe fn check_patternlive4_avx512(pos: &Pos, sd: Side) -> bool {
}
}

if count_match > 0 {
return true;
} else {
return false;
}
count_match > 0
}

#[target_feature(enable = "avx512f,avx512bw")]
Expand Down

0 comments on commit f233e0a

Please sign in to comment.