cargo fix my beloved

This commit is contained in:
LinlyBoi
2023-05-06 11:49:57 +03:00
parent f31f259715
commit 95108e1323
6 changed files with 83 additions and 84 deletions

View File

@@ -8,7 +8,7 @@ pub fn minimax_decision(board: &Board, disk: Disk, depth: &i32) -> Board {
} }
fn maximise(board: &Board, disk: &Disk, depth: &i32) -> (Option<Board>, i32) { fn maximise(board: &Board, disk: &Disk, depth: &i32) -> (Option<Board>, i32) {
match board.game_over() || *depth == 0 { match board.game_over() || *depth == 0 {
true => return (None, get_score(board, *disk)), true => (None, get_score(board, *disk)),
false => { false => {
let (mut max_child, mut max_utility): (Option<Board>, i32) = (None, i32::MIN); let (mut max_child, mut max_utility): (Option<Board>, i32) = (None, i32::MIN);
for child in board.get_children(*disk) { for child in board.get_children(*disk) {
@@ -23,7 +23,7 @@ fn maximise(board: &Board, disk: &Disk, depth: &i32) -> (Option<Board>, i32) {
} }
fn minimise(board: &Board, disk: &Disk, depth: &i32) -> (Option<Board>, i32) { fn minimise(board: &Board, disk: &Disk, depth: &i32) -> (Option<Board>, i32) {
match board.game_over() || *depth == 0 { match board.game_over() || *depth == 0 {
true => return (None, get_score(board, flip_disk(*disk))), true => (None, get_score(board, flip_disk(*disk))),
false => { false => {
let (mut min_child, mut min_utility): (Option<Board>, i32) = (None, i32::MAX); let (mut min_child, mut min_utility): (Option<Board>, i32) = (None, i32::MAX);
for child in board.get_children(*disk) { for child in board.get_children(*disk) {
@@ -40,16 +40,16 @@ fn minimise(board: &Board, disk: &Disk, depth: &i32) -> (Option<Board>, i32) {
fn minimax_test() { fn minimax_test() {
let mut board = Board::default(); let mut board = Board::default();
let mut disk = Disk::BLU; let mut disk = Disk::BLU;
let depth = 5; let _depth = 5;
let turn1 = board.play(disk, minimax_decision(&board, disk, &5).last_move); let _turn1 = board.play(disk, minimax_decision(&board, disk, &5).last_move);
disk = flip_disk(disk); 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); 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); 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); 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() { for column in board.columns.as_rows() {
column column
.iter() .iter()
@@ -58,7 +58,7 @@ fn minimax_test() {
x x
}) })
.count(); .count();
println!(""); println!();
} }
assert!(false); assert!(false);
} }

View File

@@ -24,7 +24,7 @@ pub fn get_score(board: &Board, disk: Disk) -> i32 {
potential_streaks(&sequences, &disk) + potential_wins(&sequences, &disk) + score * SCORE_DIFF potential_streaks(&sequences, &disk) + potential_wins(&sequences, &disk) + score * SCORE_DIFF
} }
fn potential_wins(sequences: &Vec<Vec<Disk>>, _disk: &Disk) -> i32 { fn potential_wins(sequences: &Vec<Vec<Disk>>, _disk: &Disk) -> i32 {
let count: i32 = sequences.iter().count() as i32; let count: i32 = sequences.len() as i32;
// for win in sequences { // for win in sequences {
// if win // if win
// .iter() // .iter()
@@ -70,7 +70,7 @@ fn get_streaks(board: &Array2D<Disk>, player_disk: &Disk) -> Vec<Vec<Disk>> {
for index in empty_indices { for index in empty_indices {
let moves = get_legal_moves(&index, (board.num_rows(), board.num_columns())); let moves = get_legal_moves(&index, (board.num_rows(), board.num_columns()));
for direction in moves { 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); //dbg!(&index, &direction, &poopy);
match sequence match sequence
.iter() .iter()
@@ -95,7 +95,7 @@ fn get_wins(board: &Array2D<Disk>, player_disk: &Disk) -> Vec<Vec<Disk>> {
for direction in moves { for direction in moves {
let mut win: Vec<Disk> = Vec::with_capacity(4); let mut win: Vec<Disk> = Vec::with_capacity(4);
win.append(&mut heur_scan( win.append(&mut heur_scan(
&board, board,
&index, &index,
direction.clone(), direction.clone(),
4, 4,
@@ -115,7 +115,7 @@ fn get_wins(board: &Array2D<Disk>, player_disk: &Disk) -> Vec<Vec<Disk>> {
} }
let opp_direction = flip_direction(direction.clone()); let opp_direction = flip_direction(direction.clone());
let mut opp_sequence = heur_scan( let mut opp_sequence = heur_scan(
&board, board,
&index, &index,
opp_direction, opp_direction,
(4 - win.len() + 1) as i32, (4 - win.len() + 1) as i32,
@@ -157,31 +157,31 @@ fn heur_scan(
// dbg!(current_index); // dbg!(current_index);
//go to next element //go to next element
match direction { match direction {
Direction::DOWN => { Direction::Down => {
if current_index.0 == 0 { if current_index.0 == 0 {
break; break;
} }
current_index = dec_row(&current_index, 1); current_index = dec_row(&current_index, 1);
} }
Direction::UP => { Direction::Up => {
if current_index.0 == board.num_rows() - 1 { if current_index.0 == board.num_rows() - 1 {
break; break;
} }
current_index = inc_row(&current_index, 1); current_index = inc_row(&current_index, 1);
} }
Direction::LEFT => { Direction::Left => {
if current_index.1 == 0 { if current_index.1 == 0 {
break; break;
} }
current_index = dec_col(&current_index, 1); current_index = dec_col(&current_index, 1);
} }
Direction::RIGHT => { Direction::Right => {
if current_index.1 == board.num_columns() - 1 { if current_index.1 == board.num_columns() - 1 {
break; break;
} }
current_index = inc_col(&current_index, 1); current_index = inc_col(&current_index, 1);
} }
Direction::UPRIGHT => { Direction::UpRight => {
if current_index.0 == board.num_rows() - 1 if current_index.0 == board.num_rows() - 1
|| current_index.1 == board.num_columns() - 1 || current_index.1 == board.num_columns() - 1
{ {
@@ -189,20 +189,20 @@ fn heur_scan(
} }
current_index = inc_both(&current_index, 1); current_index = inc_both(&current_index, 1);
} }
Direction::UPLEFT => { Direction::UpLeft => {
if current_index.0 == board.num_columns() - 1 || current_index.1 == 0 { if current_index.0 == board.num_columns() - 1 || current_index.1 == 0 {
break; break;
} }
current_index = inc_dec(&current_index, 1); current_index = inc_dec(&current_index, 1);
} }
Direction::DOWNRIGHT => { Direction::DownRight => {
if current_index.0 == 0 || current_index.1 == board.num_columns() - 1 { if current_index.0 == 0 || current_index.1 == board.num_columns() - 1 {
break; break;
} }
current_index = dec_inc(&current_index, 1); current_index = dec_inc(&current_index, 1);
} }
Direction::DOWNLEFT => { Direction::DownLeft => {
if current_index.0 == 0 || current_index.1 == 0 { if current_index.0 == 0 || current_index.1 == 0 {
break; break;
} }
@@ -233,7 +233,7 @@ fn streak_test_1() {
let sequences = get_streaks(&board.columns, &Disk::BLU); let sequences = get_streaks(&board.columns, &Disk::BLU);
assert_eq!(18, potential_streaks(&sequences, &Disk::BLU)); assert_eq!(18, potential_streaks(&sequences, &Disk::BLU));
board.play(Disk::BLU, 0); 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); board.play(Disk::BLU, 3);
board.play(Disk::BLU, 3); board.play(Disk::BLU, 3);

View File

@@ -1,15 +1,15 @@
pub fn inc_row((row, col): &(usize, usize), value: usize) -> (usize, usize) { 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) { 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) { 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) { 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) { pub fn inc_both((row, col): &(usize, usize), value: usize) -> (usize, usize) {
(row + value, col + value) (row + value, col + value)

View File

@@ -34,11 +34,11 @@ impl Board {
(self.red_score, self.blu_score) (self.red_score, self.blu_score)
} }
fn play(&mut self, disk: Disk, col: usize) -> bool { 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(); let empty = column.iter().filter(|&a| matches!(a, Disk::EMPTY)).count();
// dbg!(empty); // dbg!(empty);
let top = column.len() - empty; let top = column.len() - empty;
match self.columns.set(top, col as usize, disk) { match self.columns.set(top, col, disk) {
Ok(_) => { Ok(_) => {
self.score_check((top, col)); self.score_check((top, col));
self.last_move = col; self.last_move = col;
@@ -87,9 +87,9 @@ impl Board {
} }
} }
} }
Disk::EMPTY => return, Disk::EMPTY => (),
}, },
None => return, None => (),
} }
} }
fn game_over(&self) -> bool { fn game_over(&self) -> bool {

View File

@@ -10,9 +10,8 @@ pub fn scan(
direction: Direction, direction: Direction,
depth: i32, depth: i32,
) -> i32 { ) -> i32 {
let current_disk: &Disk; let current_disk: &Disk = match board.get(index.0, index.1) {
match board.get(index.0, index.1) { Some(disk) => disk,
Some(disk) => current_disk = disk,
None => return 0, None => return 0,
}; };
let mut current_index = *index; let mut current_index = *index;
@@ -28,33 +27,33 @@ pub fn scan(
// dbg!(current_index); // dbg!(current_index);
//go to next element //go to next element
match direction { match direction {
Direction::DOWN => { Direction::Down => {
if current_index.0 == 0 { if current_index.0 == 0 {
break; break;
} }
current_index = dec_row(&current_index, 1); current_index = dec_row(&current_index, 1);
} }
Direction::UP => { Direction::Up => {
if current_index.0 == board.num_rows() - 1 { if current_index.0 == board.num_rows() - 1 {
break; break;
} }
current_index = inc_row(&current_index, 1); current_index = inc_row(&current_index, 1);
} }
Direction::LEFT => { Direction::Left => {
if current_index.1 == 0 { if current_index.1 == 0 {
break; break;
} }
current_index = dec_col(&current_index, 1); current_index = dec_col(&current_index, 1);
// current_index.1 -= 1; // current_index.1 -= 1;
} }
Direction::RIGHT => { Direction::Right => {
if current_index.1 == board.num_columns() - 1 { if current_index.1 == board.num_columns() - 1 {
break; break;
} }
current_index = inc_col(&current_index, 1); current_index = inc_col(&current_index, 1);
// current_index.1 += 1; // current_index.1 += 1;
} }
Direction::UPRIGHT => { Direction::UpRight => {
if current_index.0 == board.num_rows() - 1 if current_index.0 == board.num_rows() - 1
|| current_index.1 == board.num_columns() - 1 || current_index.1 == board.num_columns() - 1
{ {
@@ -64,7 +63,7 @@ pub fn scan(
// current_index.1 += 1; // current_index.1 += 1;
// current_index.0 += 1; // current_index.0 += 1;
} }
Direction::UPLEFT => { Direction::UpLeft => {
if current_index.0 == board.num_columns() - 1 || current_index.1 == 0 { if current_index.0 == board.num_columns() - 1 || current_index.1 == 0 {
break; break;
} }
@@ -73,7 +72,7 @@ pub fn scan(
// current_index.0 += 1; // current_index.0 += 1;
current_index = inc_dec(&current_index, 1); current_index = inc_dec(&current_index, 1);
} }
Direction::DOWNRIGHT => { Direction::DownRight => {
if current_index.0 == 0 || current_index.1 == board.num_columns() - 1 { if current_index.0 == 0 || current_index.1 == board.num_columns() - 1 {
break; break;
} }
@@ -81,7 +80,7 @@ pub fn scan(
// current_index.1 += 1; // current_index.1 += 1;
// current_index.0 -= 1; // current_index.0 -= 1;
} }
Direction::DOWNLEFT => { Direction::DownLeft => {
if current_index.0 == 0 || current_index.1 == 0 { if current_index.0 == 0 || current_index.1 == 0 {
break; break;
} }
@@ -108,46 +107,46 @@ pub fn get_legal_moves(
let _max_row = ncol - 1; let _max_row = ncol - 1;
let mut moves: Vec<Direction> = vec![]; let mut moves: Vec<Direction> = vec![];
match *col { match *col {
0 => moves.push(Direction::UP), 0 => moves.push(Direction::Up),
_max_row => moves.push(Direction::DOWN), _max_row => moves.push(Direction::Down),
_ => { _ => {
moves.push(Direction::UP); moves.push(Direction::Up);
moves.push(Direction::DOWN); moves.push(Direction::Down);
} }
}; };
match *row { match *row {
0 => moves.push(Direction::RIGHT), 0 => moves.push(Direction::Right),
_max_row => moves.push(Direction::LEFT), _max_row => moves.push(Direction::Left),
_ => { _ => {
moves.push(Direction::LEFT); moves.push(Direction::Left);
moves.push(Direction::RIGHT) moves.push(Direction::Right)
} }
}; };
if moves.contains(&Direction::UP) && moves.contains(&Direction::LEFT) { if moves.contains(&Direction::Up) && moves.contains(&Direction::Left) {
moves.push(Direction::UPLEFT); moves.push(Direction::UpLeft);
} }
if moves.contains(&Direction::UP) && moves.contains(&Direction::RIGHT) { if moves.contains(&Direction::Up) && moves.contains(&Direction::Right) {
moves.push(Direction::UPRIGHT); moves.push(Direction::UpRight);
} }
if moves.contains(&Direction::DOWN) && moves.contains(&Direction::LEFT) { if moves.contains(&Direction::Down) && moves.contains(&Direction::Left) {
moves.push(Direction::DOWNLEFT); moves.push(Direction::DownLeft);
} }
if moves.contains(&Direction::DOWN) && moves.contains(&Direction::RIGHT) { if moves.contains(&Direction::Down) && moves.contains(&Direction::Right) {
moves.push(Direction::DOWNRIGHT); moves.push(Direction::DownRight);
} }
moves moves
} }
#[derive(Clone, Debug, PartialEq, Eq)] #[derive(Clone, Debug, PartialEq, Eq)]
pub enum Direction { pub enum Direction {
UP, Up,
DOWN, Down,
LEFT, Left,
RIGHT, Right,
UPLEFT, UpLeft,
UPRIGHT, UpRight,
DOWNLEFT, DownLeft,
DOWNRIGHT, DownRight,
} }
// serves nothing except do what matches!() should have done all along // serves nothing except do what matches!() should have done all along
// matches works too I'm just dumb // matches works too I'm just dumb
@@ -156,13 +155,13 @@ pub fn variant_eq<T>(a: &T, b: &T) -> bool {
} }
pub fn flip_direction(direction: Direction) -> Direction { pub fn flip_direction(direction: Direction) -> Direction {
match direction { match direction {
Direction::UP => Direction::DOWN, Direction::Up => Direction::Down,
Direction::DOWN => Direction::UP, Direction::Down => Direction::Up,
Direction::LEFT => Direction::RIGHT, Direction::Left => Direction::Right,
Direction::RIGHT => Direction::LEFT, Direction::Right => Direction::Left,
Direction::UPLEFT => Direction::DOWNRIGHT, Direction::UpLeft => Direction::DownRight,
Direction::UPRIGHT => Direction::DOWNLEFT, Direction::UpRight => Direction::DownLeft,
Direction::DOWNLEFT => Direction::UPRIGHT, Direction::DownLeft => Direction::UpRight,
Direction::DOWNRIGHT => Direction::UPLEFT, Direction::DownRight => Direction::UpLeft,
} }
} }

View File

@@ -54,8 +54,8 @@ fn scan_updown() {
board.play(Disk::BLU, 0); board.play(Disk::BLU, 0);
board.play(Disk::BLU, 0); 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, &(4, 0), Direction::Down, 4));
assert_eq!(4, scan(&board.columns, &(3, 0), Direction::DOWN, 4)); assert_eq!(4, scan(&board.columns, &(3, 0), Direction::Down, 4));
} }
#[test] #[test]
fn scan_updown2() { fn scan_updown2() {
@@ -64,7 +64,7 @@ fn scan_updown2() {
board.play(Disk::RED, 0); board.play(Disk::RED, 0);
board.play(Disk::BLU, 0); board.play(Disk::BLU, 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] #[test]
fn scan_forwardback() { fn scan_forwardback() {
@@ -74,8 +74,8 @@ fn scan_forwardback() {
board.play(Disk::BLU, 2); board.play(Disk::BLU, 2);
board.play(Disk::BLU, 3); board.play(Disk::BLU, 3);
assert_eq!(4, scan(&board.columns, &(0, 0), Direction::RIGHT, 4)); 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, 3), Direction::Left, 4));
} }
#[test] #[test]
fn scan_forwardback2() { fn scan_forwardback2() {
@@ -84,8 +84,8 @@ fn scan_forwardback2() {
board.play(Disk::BLU, 1); board.play(Disk::BLU, 1);
board.play(Disk::RED, 2); board.play(Disk::RED, 2);
board.play(Disk::BLU, 3); board.play(Disk::BLU, 3);
assert_eq!(2, scan(&board.columns, &(0, 0), Direction::RIGHT, 4)); assert_eq!(2, scan(&board.columns, &(0, 0), Direction::Right, 4));
assert_eq!(1, scan(&board.columns, &(0, 3), Direction::LEFT, 4)); assert_eq!(1, scan(&board.columns, &(0, 3), Direction::Left, 4));
} }
#[test] #[test]
fn scan_diag1() { fn scan_diag1() {
@@ -100,8 +100,8 @@ fn scan_diag1() {
board.play(Disk::RED, 3); board.play(Disk::RED, 3);
board.play(Disk::RED, 3); board.play(Disk::RED, 3);
board.play(Disk::BLU, 3); board.play(Disk::BLU, 3);
assert_eq!(4, scan(&board.columns, &(0, 0), Direction::UPRIGHT, 4)); 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, &(3, 3), Direction::DownLeft, 4));
} }
#[test] #[test]
fn scan_diag2() { fn scan_diag2() {
@@ -117,8 +117,8 @@ fn scan_diag2() {
board.play(Disk::RED, 0); board.play(Disk::RED, 0);
board.play(Disk::BLU, 0); board.play(Disk::BLU, 0);
dbg!(&board.columns.as_columns()); dbg!(&board.columns.as_columns());
assert_eq!(4, scan(&board.columns, &(0, 3), Direction::UPLEFT, 4)); 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, &(3, 0), Direction::DownRight, 4));
} }
#[test] #[test]
fn variant_eq_test() { fn variant_eq_test() {