Skip to content

Commit f233e0a

Browse files
committed
Clean up examples, make it more "Rusty"
1 parent d055c8f commit f233e0a

File tree

1 file changed

+21
-54
lines changed

1 file changed

+21
-54
lines changed

examples/connect5.rs

Lines changed: 21 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ const SCORE_NONE: i32 = -EVAL_INF - 1;
7474
/// DIRECTION 2: top left to bottom right\
7575
/// DIRECTION 3: top right to bottom left
7676
#[rustfmt::skip]
77+
#[allow(clippy::identity_op)]
7778
const DIRECTION: [[i32; 5]; 4] = [ [1, 2, 3, 4, 5],
7879
[1 * (FILE_SIZE + 1), 2 * (FILE_SIZE + 1), 3 * (FILE_SIZE + 1), 4 * (FILE_SIZE + 1), 5 * (FILE_SIZE + 1)],
7980
[1 * (FILE_SIZE + 2), 2 * (FILE_SIZE + 2), 3 * (FILE_SIZE + 2), 4 * (FILE_SIZE + 2), 5 * (FILE_SIZE + 2)],
@@ -311,7 +312,7 @@ impl Pos {
311312

312313
match self.p_turn {
313314
Color::Black => {
314-
self.state[mv as usize] = Color::Black;
315+
self.state[mv] = Color::Black;
315316
// update black move and remove empty move in bitboard
316317
self.bitboard[black][0][MAPMOVEIDX[0][mv] as usize] |= MAPMOVEVALUE[0][mv];
317318
self.bitboard[empty][0][MAPMOVEIDX[0][mv] as usize] ^= MAPMOVEVALUE[0][mv];
@@ -323,7 +324,7 @@ impl Pos {
323324
self.bitboard[empty][1][MAPMOVEIDX[3][mv] as usize] ^= MAPMOVEVALUE[3][mv];
324325
}
325326
Color::White => {
326-
self.state[mv as usize] = Color::White;
327+
self.state[mv] = Color::White;
327328
// update white move and remove empty move in bitboard
328329
self.bitboard[white][0][MAPMOVEIDX[0][mv] as usize] |= MAPMOVEVALUE[0][mv];
329330
self.bitboard[empty][0][MAPMOVEIDX[0][mv] as usize] ^= MAPMOVEVALUE[0][mv];
@@ -345,11 +346,7 @@ impl Pos {
345346
}
346347

347348
pub fn can_play(&self, from: Square) -> bool {
348-
if self.state[from as usize] == Color::Empty {
349-
true
350-
} else {
351-
false
352-
}
349+
self.state[from as usize] == Color::Empty
353350
}
354351
}
355352

@@ -421,14 +418,14 @@ fn pos_is_draw(pos: &Pos) -> bool {
421418
break;
422419
}
423420

424-
if found == false {
421+
if !found {
425422
break;
426423
}
427424
}
428425
}
429426

430-
let mut out: bool = false;
431-
if found == true && !pos_is_winner(pos) {
427+
let mut out = false;
428+
if found && !pos_is_winner(pos) {
432429
out = true;
433430
}
434431

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

450-
if _popcnt32(temp_mask as i32) == 0 && !pos_is_winner_avx512(pos) {
451-
return true;
452-
} else {
453-
return false;
454-
}
447+
_popcnt32(temp_mask as i32) == 0 && !pos_is_winner_avx512(pos)
455448
}
456449

457450
fn pos_is_end(pos: &Pos) -> bool {
458-
if pos_is_winner(pos) || pos_is_draw(pos) {
459-
true
460-
} else {
461-
false
462-
}
451+
pos_is_winner(pos) || pos_is_draw(pos)
463452
}
464453

465454
fn pos_disp(pos: &Pos) {
@@ -475,7 +464,7 @@ fn pos_disp(pos: &Pos) {
475464
}
476465
}
477466

478-
println!("");
467+
println!();
479468
}
480469

481470
match pos.turn() {
@@ -581,7 +570,7 @@ fn search(pos: &Pos, alpha: i32, beta: i32, depth: i32, _ply: i32) -> i32 {
581570
}
582571
}
583572

584-
assert!(bm != MOVE_NONE);
573+
assert_ne!(bm, MOVE_NONE);
585574
assert!(bs >= -EVAL_INF && bs <= EVAL_INF);
586575

587576
if _ply == 0 {
@@ -605,10 +594,8 @@ fn eval(pos: &Pos, _ply: i32) -> i32 {
605594
return -4096;
606595
}
607596
}
608-
} else {
609-
if check_patternlive4(pos, def) {
610-
return -4096;
611-
}
597+
} else if check_patternlive4(pos, def) {
598+
return -4096;
612599
}
613600
}
614601

@@ -628,10 +615,8 @@ fn eval(pos: &Pos, _ply: i32) -> i32 {
628615
return 2560;
629616
}
630617
}
631-
} else {
632-
if check_patternlive4(pos, atk) {
633-
return 2560;
634-
}
618+
} else if check_patternlive4(pos, atk) {
619+
return 2560;
635620
}
636621
}
637622

@@ -651,10 +636,8 @@ fn eval(pos: &Pos, _ply: i32) -> i32 {
651636
return 2560;
652637
}
653638
}
654-
} else {
655-
if check_patterndead4(pos, atk) > 0 {
656-
return 2560;
657-
}
639+
} else if check_patterndead4(pos, atk) > 0 {
640+
return 2560;
658641
}
659642
}
660643

@@ -773,11 +756,7 @@ fn check_pattern5(pos: &Pos, sd: Side) -> bool {
773756
}
774757
}
775758

776-
if n > 0 {
777-
true
778-
} else {
779-
false
780-
}
759+
n > 0
781760
}
782761

783762
/// Check <b>-OOOO-</b>
@@ -809,11 +788,7 @@ fn check_patternlive4(pos: &Pos, sd: Side) -> bool {
809788
}
810789
}
811790

812-
if n > 0 {
813-
true
814-
} else {
815-
false
816-
}
791+
n > 0
817792
}
818793

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

964-
if count_match > 0 {
965-
return true;
966-
} else {
967-
return false;
968-
}
939+
count_match > 0
969940
}
970941

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

1030-
if count_match > 0 {
1031-
return true;
1032-
} else {
1033-
return false;
1034-
}
1001+
count_match > 0
10351002
}
10361003

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

0 commit comments

Comments
 (0)