From 95108e132366e0b85712201e80bdb1d0115e354d Mon Sep 17 00:00:00 2001 From: LinlyBoi Date: Sat, 6 May 2023 11:49:57 +0300 Subject: [PATCH] cargo fix my beloved --- src/gamedata/algorithms.rs | 18 +++---- src/gamedata/heuristic.rs | 26 +++++------ src/gamedata/indices.rs | 8 ++-- src/gamedata/mod.rs | 8 ++-- src/gamedata/score_checkers.rs | 85 +++++++++++++++++----------------- src/gamedata/tests.rs | 22 ++++----- 6 files changed, 83 insertions(+), 84 deletions(-) diff --git a/src/gamedata/algorithms.rs b/src/gamedata/algorithms.rs index 31e7919..6bf0171 100644 --- a/src/gamedata/algorithms.rs +++ b/src/gamedata/algorithms.rs @@ -8,7 +8,7 @@ pub fn minimax_decision(board: &Board, disk: Disk, depth: &i32) -> Board { } fn maximise(board: &Board, disk: &Disk, depth: &i32) -> (Option, i32) { match board.game_over() || *depth == 0 { - true => return (None, get_score(board, *disk)), + true => (None, get_score(board, *disk)), false => { let (mut max_child, mut max_utility): (Option, i32) = (None, i32::MIN); for child in board.get_children(*disk) { @@ -23,7 +23,7 @@ fn maximise(board: &Board, disk: &Disk, depth: &i32) -> (Option, i32) { } fn minimise(board: &Board, disk: &Disk, depth: &i32) -> (Option, i32) { match board.game_over() || *depth == 0 { - true => return (None, get_score(board, flip_disk(*disk))), + true => (None, get_score(board, flip_disk(*disk))), false => { let (mut min_child, mut min_utility): (Option, i32) = (None, i32::MAX); for child in board.get_children(*disk) { @@ -40,16 +40,16 @@ fn minimise(board: &Board, disk: &Disk, depth: &i32) -> (Option, i32) { fn minimax_test() { let mut board = Board::default(); let mut disk = Disk::BLU; - let depth = 5; - let turn1 = board.play(disk, minimax_decision(&board, disk, &5).last_move); + let _depth = 5; + let _turn1 = board.play(disk, minimax_decision(&board, disk, &5).last_move); disk = flip_disk(disk); - let turn2 = board.play(disk, minimax_decision(&board, disk, &5).last_move); + let _turn2 = board.play(disk, minimax_decision(&board, disk, &5).last_move); disk = flip_disk(disk); - let turn3 = board.play(disk, minimax_decision(&board, disk, &5).last_move); + let _turn3 = board.play(disk, minimax_decision(&board, disk, &5).last_move); disk = flip_disk(disk); - let turn4 = board.play(disk, minimax_decision(&board, disk, &5).last_move); + let _turn4 = board.play(disk, minimax_decision(&board, disk, &5).last_move); disk = flip_disk(disk); - let turn5 = board.play(disk, minimax_decision(&board, disk, &5).last_move); + let _turn5 = board.play(disk, minimax_decision(&board, disk, &5).last_move); for column in board.columns.as_rows() { column .iter() @@ -58,7 +58,7 @@ fn minimax_test() { x }) .count(); - println!(""); + println!(); } assert!(false); } diff --git a/src/gamedata/heuristic.rs b/src/gamedata/heuristic.rs index a16b44c..413af63 100644 --- a/src/gamedata/heuristic.rs +++ b/src/gamedata/heuristic.rs @@ -24,7 +24,7 @@ pub fn get_score(board: &Board, disk: Disk) -> i32 { potential_streaks(&sequences, &disk) + potential_wins(&sequences, &disk) + score * SCORE_DIFF } fn potential_wins(sequences: &Vec>, _disk: &Disk) -> i32 { - let count: i32 = sequences.iter().count() as i32; + let count: i32 = sequences.len() as i32; // for win in sequences { // if win // .iter() @@ -70,7 +70,7 @@ fn get_streaks(board: &Array2D, player_disk: &Disk) -> Vec> { for index in empty_indices { let moves = get_legal_moves(&index, (board.num_rows(), board.num_columns())); for direction in moves { - let mut sequence = heur_scan(&board, &index, direction.clone(), 4, *player_disk); + let sequence = heur_scan(board, &index, direction.clone(), 4, *player_disk); //dbg!(&index, &direction, &poopy); match sequence .iter() @@ -95,7 +95,7 @@ fn get_wins(board: &Array2D, player_disk: &Disk) -> Vec> { for direction in moves { let mut win: Vec = Vec::with_capacity(4); win.append(&mut heur_scan( - &board, + board, &index, direction.clone(), 4, @@ -115,7 +115,7 @@ fn get_wins(board: &Array2D, player_disk: &Disk) -> Vec> { } let opp_direction = flip_direction(direction.clone()); let mut opp_sequence = heur_scan( - &board, + board, &index, opp_direction, (4 - win.len() + 1) as i32, @@ -157,31 +157,31 @@ fn heur_scan( // dbg!(current_index); //go to next element match direction { - Direction::DOWN => { + Direction::Down => { if current_index.0 == 0 { break; } current_index = dec_row(¤t_index, 1); } - Direction::UP => { + Direction::Up => { if current_index.0 == board.num_rows() - 1 { break; } current_index = inc_row(¤t_index, 1); } - Direction::LEFT => { + Direction::Left => { if current_index.1 == 0 { break; } current_index = dec_col(¤t_index, 1); } - Direction::RIGHT => { + Direction::Right => { if current_index.1 == board.num_columns() - 1 { break; } current_index = inc_col(¤t_index, 1); } - Direction::UPRIGHT => { + Direction::UpRight => { if current_index.0 == board.num_rows() - 1 || current_index.1 == board.num_columns() - 1 { @@ -189,20 +189,20 @@ fn heur_scan( } current_index = inc_both(¤t_index, 1); } - Direction::UPLEFT => { + Direction::UpLeft => { if current_index.0 == board.num_columns() - 1 || current_index.1 == 0 { break; } current_index = inc_dec(¤t_index, 1); } - Direction::DOWNRIGHT => { + Direction::DownRight => { if current_index.0 == 0 || current_index.1 == board.num_columns() - 1 { break; } current_index = dec_inc(¤t_index, 1); } - Direction::DOWNLEFT => { + Direction::DownLeft => { if current_index.0 == 0 || current_index.1 == 0 { break; } @@ -233,7 +233,7 @@ fn streak_test_1() { let sequences = get_streaks(&board.columns, &Disk::BLU); assert_eq!(18, potential_streaks(&sequences, &Disk::BLU)); board.play(Disk::BLU, 0); - let sequences = get_streaks(&board.columns, &Disk::BLU); + let _sequences = get_streaks(&board.columns, &Disk::BLU); board.play(Disk::BLU, 3); board.play(Disk::BLU, 3); board.play(Disk::BLU, 3); diff --git a/src/gamedata/indices.rs b/src/gamedata/indices.rs index f49bc40..2b4f9b1 100644 --- a/src/gamedata/indices.rs +++ b/src/gamedata/indices.rs @@ -1,15 +1,15 @@ pub fn inc_row((row, col): &(usize, usize), value: usize) -> (usize, usize) { - (row + value as usize, *col) + (row + value, *col) } pub fn inc_col((row, col): &(usize, usize), value: usize) -> (usize, usize) { - (*row, col + value as usize) + (*row, col + value) } pub fn dec_row((row, col): &(usize, usize), value: usize) -> (usize, usize) { - (row - value as usize, *col) + (row - value, *col) } pub fn dec_col((row, col): &(usize, usize), value: usize) -> (usize, usize) { - (*row, col - value as usize) + (*row, col - value) } pub fn inc_both((row, col): &(usize, usize), value: usize) -> (usize, usize) { (row + value, col + value) diff --git a/src/gamedata/mod.rs b/src/gamedata/mod.rs index 31c78f0..877b08c 100644 --- a/src/gamedata/mod.rs +++ b/src/gamedata/mod.rs @@ -34,11 +34,11 @@ impl Board { (self.red_score, self.blu_score) } fn play(&mut self, disk: Disk, col: usize) -> bool { - let column = &self.columns.as_columns()[col as usize]; + let column = &self.columns.as_columns()[col]; let empty = column.iter().filter(|&a| matches!(a, Disk::EMPTY)).count(); // dbg!(empty); let top = column.len() - empty; - match self.columns.set(top, col as usize, disk) { + match self.columns.set(top, col, disk) { Ok(_) => { self.score_check((top, col)); self.last_move = col; @@ -87,9 +87,9 @@ impl Board { } } } - Disk::EMPTY => return, + Disk::EMPTY => (), }, - None => return, + None => (), } } fn game_over(&self) -> bool { diff --git a/src/gamedata/score_checkers.rs b/src/gamedata/score_checkers.rs index 387f344..e2730a9 100644 --- a/src/gamedata/score_checkers.rs +++ b/src/gamedata/score_checkers.rs @@ -10,9 +10,8 @@ pub fn scan( direction: Direction, depth: i32, ) -> i32 { - let current_disk: &Disk; - match board.get(index.0, index.1) { - Some(disk) => current_disk = disk, + let current_disk: &Disk = match board.get(index.0, index.1) { + Some(disk) => disk, None => return 0, }; let mut current_index = *index; @@ -28,33 +27,33 @@ pub fn scan( // dbg!(current_index); //go to next element match direction { - Direction::DOWN => { + Direction::Down => { if current_index.0 == 0 { break; } current_index = dec_row(¤t_index, 1); } - Direction::UP => { + Direction::Up => { if current_index.0 == board.num_rows() - 1 { break; } current_index = inc_row(¤t_index, 1); } - Direction::LEFT => { + Direction::Left => { if current_index.1 == 0 { break; } current_index = dec_col(¤t_index, 1); // current_index.1 -= 1; } - Direction::RIGHT => { + Direction::Right => { if current_index.1 == board.num_columns() - 1 { break; } current_index = inc_col(¤t_index, 1); // current_index.1 += 1; } - Direction::UPRIGHT => { + Direction::UpRight => { if current_index.0 == board.num_rows() - 1 || current_index.1 == board.num_columns() - 1 { @@ -64,7 +63,7 @@ pub fn scan( // current_index.1 += 1; // current_index.0 += 1; } - Direction::UPLEFT => { + Direction::UpLeft => { if current_index.0 == board.num_columns() - 1 || current_index.1 == 0 { break; } @@ -73,7 +72,7 @@ pub fn scan( // current_index.0 += 1; current_index = inc_dec(¤t_index, 1); } - Direction::DOWNRIGHT => { + Direction::DownRight => { if current_index.0 == 0 || current_index.1 == board.num_columns() - 1 { break; } @@ -81,7 +80,7 @@ pub fn scan( // current_index.1 += 1; // current_index.0 -= 1; } - Direction::DOWNLEFT => { + Direction::DownLeft => { if current_index.0 == 0 || current_index.1 == 0 { break; } @@ -108,46 +107,46 @@ pub fn get_legal_moves( let _max_row = ncol - 1; let mut moves: Vec = vec![]; match *col { - 0 => moves.push(Direction::UP), - _max_row => moves.push(Direction::DOWN), + 0 => moves.push(Direction::Up), + _max_row => moves.push(Direction::Down), _ => { - moves.push(Direction::UP); - moves.push(Direction::DOWN); + moves.push(Direction::Up); + moves.push(Direction::Down); } }; match *row { - 0 => moves.push(Direction::RIGHT), - _max_row => moves.push(Direction::LEFT), + 0 => moves.push(Direction::Right), + _max_row => moves.push(Direction::Left), _ => { - moves.push(Direction::LEFT); - moves.push(Direction::RIGHT) + moves.push(Direction::Left); + moves.push(Direction::Right) } }; - if moves.contains(&Direction::UP) && moves.contains(&Direction::LEFT) { - moves.push(Direction::UPLEFT); + if moves.contains(&Direction::Up) && moves.contains(&Direction::Left) { + moves.push(Direction::UpLeft); } - if moves.contains(&Direction::UP) && moves.contains(&Direction::RIGHT) { - moves.push(Direction::UPRIGHT); + if moves.contains(&Direction::Up) && moves.contains(&Direction::Right) { + moves.push(Direction::UpRight); } - if moves.contains(&Direction::DOWN) && moves.contains(&Direction::LEFT) { - moves.push(Direction::DOWNLEFT); + if moves.contains(&Direction::Down) && moves.contains(&Direction::Left) { + moves.push(Direction::DownLeft); } - if moves.contains(&Direction::DOWN) && moves.contains(&Direction::RIGHT) { - moves.push(Direction::DOWNRIGHT); + if moves.contains(&Direction::Down) && moves.contains(&Direction::Right) { + moves.push(Direction::DownRight); } moves } #[derive(Clone, Debug, PartialEq, Eq)] pub enum Direction { - UP, - DOWN, - LEFT, - RIGHT, - UPLEFT, - UPRIGHT, - DOWNLEFT, - DOWNRIGHT, + Up, + Down, + Left, + Right, + UpLeft, + UpRight, + DownLeft, + DownRight, } // serves nothing except do what matches!() should have done all along // matches works too I'm just dumb @@ -156,13 +155,13 @@ pub fn variant_eq(a: &T, b: &T) -> bool { } pub fn flip_direction(direction: Direction) -> Direction { match direction { - Direction::UP => Direction::DOWN, - Direction::DOWN => Direction::UP, - Direction::LEFT => Direction::RIGHT, - Direction::RIGHT => Direction::LEFT, - Direction::UPLEFT => Direction::DOWNRIGHT, - Direction::UPRIGHT => Direction::DOWNLEFT, - Direction::DOWNLEFT => Direction::UPRIGHT, - Direction::DOWNRIGHT => Direction::UPLEFT, + Direction::Up => Direction::Down, + Direction::Down => Direction::Up, + Direction::Left => Direction::Right, + Direction::Right => Direction::Left, + Direction::UpLeft => Direction::DownRight, + Direction::UpRight => Direction::DownLeft, + Direction::DownLeft => Direction::UpRight, + Direction::DownRight => Direction::UpLeft, } } diff --git a/src/gamedata/tests.rs b/src/gamedata/tests.rs index b003439..3686800 100644 --- a/src/gamedata/tests.rs +++ b/src/gamedata/tests.rs @@ -54,8 +54,8 @@ fn scan_updown() { board.play(Disk::BLU, 0); board.play(Disk::BLU, 0); board.play(Disk::BLU, 0); - assert_eq!(4, scan(&board.columns, &(4, 0), Direction::DOWN, 4)); - assert_eq!(4, scan(&board.columns, &(3, 0), Direction::DOWN, 4)); + assert_eq!(4, scan(&board.columns, &(4, 0), Direction::Down, 4)); + assert_eq!(4, scan(&board.columns, &(3, 0), Direction::Down, 4)); } #[test] fn scan_updown2() { @@ -64,7 +64,7 @@ fn scan_updown2() { board.play(Disk::RED, 0); board.play(Disk::BLU, 0); board.play(Disk::BLU, 0); - assert_eq!(1, scan(&board.columns, &(0, 0), Direction::UP, 4)); + assert_eq!(1, scan(&board.columns, &(0, 0), Direction::Up, 4)); } #[test] fn scan_forwardback() { @@ -74,8 +74,8 @@ fn scan_forwardback() { board.play(Disk::BLU, 2); board.play(Disk::BLU, 3); - assert_eq!(4, scan(&board.columns, &(0, 0), Direction::RIGHT, 4)); - assert_eq!(4, scan(&board.columns, &(0, 3), Direction::LEFT, 4)); + assert_eq!(4, scan(&board.columns, &(0, 0), Direction::Right, 4)); + assert_eq!(4, scan(&board.columns, &(0, 3), Direction::Left, 4)); } #[test] fn scan_forwardback2() { @@ -84,8 +84,8 @@ fn scan_forwardback2() { board.play(Disk::BLU, 1); board.play(Disk::RED, 2); board.play(Disk::BLU, 3); - assert_eq!(2, scan(&board.columns, &(0, 0), Direction::RIGHT, 4)); - assert_eq!(1, scan(&board.columns, &(0, 3), Direction::LEFT, 4)); + assert_eq!(2, scan(&board.columns, &(0, 0), Direction::Right, 4)); + assert_eq!(1, scan(&board.columns, &(0, 3), Direction::Left, 4)); } #[test] fn scan_diag1() { @@ -100,8 +100,8 @@ fn scan_diag1() { board.play(Disk::RED, 3); board.play(Disk::RED, 3); board.play(Disk::BLU, 3); - assert_eq!(4, scan(&board.columns, &(0, 0), Direction::UPRIGHT, 4)); - assert_eq!(4, scan(&board.columns, &(3, 3), Direction::DOWNLEFT, 4)); + assert_eq!(4, scan(&board.columns, &(0, 0), Direction::UpRight, 4)); + assert_eq!(4, scan(&board.columns, &(3, 3), Direction::DownLeft, 4)); } #[test] fn scan_diag2() { @@ -117,8 +117,8 @@ fn scan_diag2() { board.play(Disk::RED, 0); board.play(Disk::BLU, 0); dbg!(&board.columns.as_columns()); - assert_eq!(4, scan(&board.columns, &(0, 3), Direction::UPLEFT, 4)); - assert_eq!(4, scan(&board.columns, &(3, 0), Direction::DOWNRIGHT, 4)); + assert_eq!(4, scan(&board.columns, &(0, 3), Direction::UpLeft, 4)); + assert_eq!(4, scan(&board.columns, &(3, 0), Direction::DownRight, 4)); } #[test] fn variant_eq_test() {