From a36c89ad510f81ee6e8b57c0fac8d1f31851b6df Mon Sep 17 00:00:00 2001 From: LinlyBoi Date: Wed, 10 May 2023 12:25:31 +0300 Subject: [PATCH] let him COOK --- src/gamedata/algorithms.rs | 146 ++++++++++++++++----------------- src/gamedata/heuristic.rs | 28 ------- src/gamedata/score_checkers.rs | 57 ++++++++----- src/main.rs | 4 +- 4 files changed, 113 insertions(+), 122 deletions(-) diff --git a/src/gamedata/algorithms.rs b/src/gamedata/algorithms.rs index 4e15a0a..bcd9ccb 100644 --- a/src/gamedata/algorithms.rs +++ b/src/gamedata/algorithms.rs @@ -103,76 +103,76 @@ fn minimise_pruning( } } } -#[test] -fn minimax_test() { - let time = Instant::now(); - let mut board = Board::default(); - let mut disk = Disk::P2; - let _depth = 5; - let _turn1 = board.play(disk, minimax_decision(&board, disk, &5).last_move as usize); - dbg!(&board.columns); - disk = flip_disk(disk); - let _turn2 = board.play(disk, minimax_decision(&board, disk, &5).last_move as usize); - disk = flip_disk(disk); - let _turn3 = board.play(disk, minimax_decision(&board, disk, &5).last_move as usize); - disk = flip_disk(disk); - let _turn4 = board.play(disk, minimax_decision(&board, disk, &5).last_move as usize); - disk = flip_disk(disk); - let _turn5 = board.play(disk, minimax_decision(&board, disk, &5).last_move as usize); - for column in board.columns.as_rows() { - column - .iter() - .map(|x| { - print!("{:#?},", x); - x - }) - .count(); - println!(); - } - dbg!(time.elapsed().as_millis()); - assert!(false); -} -#[test] -fn minimax_pruning_test() { - let time = Instant::now(); - let mut board = Board::default(); - let mut disk = Disk::P2; - let _depth = 5; - let _turn1 = board.play( - disk, - minimax_decision_pruning(&board, disk, &5).last_move as usize, - ); - dbg!(&board.columns); - disk = flip_disk(disk); - let _turn2 = board.play( - disk, - minimax_decision_pruning(&board, disk, &5).last_move as usize, - ); - disk = flip_disk(disk); - let _turn3 = board.play( - disk, - minimax_decision_pruning(&board, disk, &5).last_move as usize, - ); - disk = flip_disk(disk); - let _turn4 = board.play( - disk, - minimax_decision_pruning(&board, disk, &5).last_move as usize, - ); - disk = flip_disk(disk); - let _turn5 = board.play( - disk, - minimax_decision_pruning(&board, disk, &5).last_move as usize, - ); - for column in board.columns.as_rows() { - column - .iter() - .map(|x| { - print!("{:#?},", x); - x - }) - .count(); - println!(); - } - dbg!(time.elapsed().as_millis()); - assert!(false); -} +//#[test] +//fn minimax_test() { +// let time = Instant::now(); +// let mut board = Board::default(); +// let mut disk = Disk::P2; +// let _depth = 5; +// let _turn1 = board.play(disk, minimax_decision(&board, disk, &5).last_move as usize); +// dbg!(&board.columns); +// disk = flip_disk(disk); +// let _turn2 = board.play(disk, minimax_decision(&board, disk, &5).last_move as usize); +// disk = flip_disk(disk); +// let _turn3 = board.play(disk, minimax_decision(&board, disk, &5).last_move as usize); +// disk = flip_disk(disk); +// let _turn4 = board.play(disk, minimax_decision(&board, disk, &5).last_move as usize); +// disk = flip_disk(disk); +// let _turn5 = board.play(disk, minimax_decision(&board, disk, &5).last_move as usize); +// for column in board.columns.as_rows() { +// column +// .iter() +// .map(|x| { +// print!("{:#?},", x); +// x +// }) +// .count(); +// println!(); +// } +// dbg!(time.elapsed().as_millis()); +// assert!(false); +//} +//#[test] +//fn minimax_pruning_test() { +// let time = Instant::now(); +// let mut board = Board::default(); +// let mut disk = Disk::P2; +// let _depth = 5; +// let _turn1 = board.play( +// disk, +// minimax_decision_pruning(&board, disk, &5).last_move as usize, +// ); +// dbg!(&board.columns); +// disk = flip_disk(disk); +// let _turn2 = board.play( +// disk, +// minimax_decision_pruning(&board, disk, &5).last_move as usize, +// ); +// disk = flip_disk(disk); +// let _turn3 = board.play( +// disk, +// minimax_decision_pruning(&board, disk, &5).last_move as usize, +// ); +// disk = flip_disk(disk); +// let _turn4 = board.play( +// disk, +// minimax_decision_pruning(&board, disk, &5).last_move as usize, +// ); +// disk = flip_disk(disk); +// let _turn5 = board.play( +// disk, +// minimax_decision_pruning(&board, disk, &5).last_move as usize, +// ); +// for column in board.columns.as_rows() { +// column +// .iter() +// .map(|x| { +// print!("{:#?},", x); +// x +// }) +// .count(); +// println!(); +// } +// dbg!(time.elapsed().as_millis()); +// assert!(false); +//} diff --git a/src/gamedata/heuristic.rs b/src/gamedata/heuristic.rs index 8f1c29b..b628007 100644 --- a/src/gamedata/heuristic.rs +++ b/src/gamedata/heuristic.rs @@ -222,25 +222,6 @@ fn heur_scan( //Tests because I am making everything public //TODO separate module here #[test] -fn streak_test_1() { - let mut board = Board::default(); - board.play(Disk::P2, 3); - board.play(Disk::P2, 3); - board.play(Disk::P2, 3); - board.play(Disk::P2, 2); - board.play(Disk::P2, 1); - let sequences = get_streaks(&board.columns, &Disk::P2); - assert_eq!(POT_STREAKS * 3, potential_streaks(&sequences, &Disk::P2)); - board.play(Disk::P2, 0); - let _sequences = get_streaks(&board.columns, &Disk::P2); - board.play(Disk::P2, 3); - board.play(Disk::P2, 3); - board.play(Disk::P2, 3); - board.play(Disk::P2, 3); - let sequences = get_streaks(&board.columns, &Disk::P2); - assert_eq!(POT_STREAKS * 2, potential_streaks(&sequences, &Disk::P2)); -} -#[test] fn win_test_flipping() { let mut board = Board::default(); board.play(Disk::P2, 3); @@ -256,12 +237,3 @@ fn win_test_flipping() { dbg!(&sequences); assert_eq!(POT_WIN, potential_wins(&sequences, &Disk::P2)); } -#[test] -fn win_test_flipping_hard() { - let mut board = Board::default(); - board.play(Disk::P2, 1); - board.play(Disk::P2, 2); - board.play(Disk::P2, 4); - let sequences = get_wins(&board.columns, &Disk::P2); - assert_eq!(POT_WIN, potential_wins(&sequences, &Disk::P2)); -} diff --git a/src/gamedata/score_checkers.rs b/src/gamedata/score_checkers.rs index ffe2f1f..115cb57 100644 --- a/src/gamedata/score_checkers.rs +++ b/src/gamedata/score_checkers.rs @@ -88,32 +88,37 @@ pub fn scan( None => break, } } - in_a_row.len() as i32 + in_a_row + .iter() + .filter(|d| variant_eq(*d, &player_disk)) + .count() as i32 } // board[(2,3)]; pub fn get_legal_moves( (row, col): &(usize, usize), (nrow, ncol): (usize, usize), ) -> Vec { - let _max_col = ncol - 1; - let _max_row = nrow - 1; + let max_col = ncol - 1; + let max_row = nrow - 1; let mut moves: Vec = vec![]; - match *row { - 0 => moves.push(Direction::Up), - _max_rows => moves.push(Direction::Down), - _ => { - moves.push(Direction::Up); - moves.push(Direction::Down); - } - }; - match *col { - 0 => moves.push(Direction::Right), - _max_col => moves.push(Direction::Left), - _ => { - moves.push(Direction::Left); - moves.push(Direction::Right) - } - }; + if *row > 0 && *row < max_row { + //dbg!(row, max_row); + moves.push(Direction::Up); + moves.push(Direction::Down); + } else if *row == 0 { + moves.push(Direction::Up); + } else if *row == max_row { + moves.push(Direction::Down); + } + if (*col > 0 && *col < max_col) { + moves.push(Direction::Left); + moves.push(Direction::Right); + } else if *col == 0 { + moves.push(Direction::Right); + } else if *col == max_col { + moves.push(Direction::Left); + } + if moves.contains(&Direction::Up) && moves.contains(&Direction::Left) { moves.push(Direction::UpLeft); } @@ -128,6 +133,20 @@ pub fn get_legal_moves( } moves } +#[test] +fn dir_test() { + let moves1 = get_legal_moves(&(0, 0), (6, 7)); + dbg!(&moves1); + assert_eq!(3, moves1.len()); + assert!(moves1.contains(&Direction::Up)); + let moves2 = get_legal_moves(&(0, 3), (6, 7)); + dbg!(&moves2); + assert_eq!(5, moves2.len()); + + let moves3 = get_legal_moves(&(1, 3), (6, 7)); + dbg!(&moves3); + assert_eq!(8, moves3.len()); +} #[derive(Clone, Debug, PartialEq, Eq)] pub enum Direction { diff --git a/src/main.rs b/src/main.rs index b72adf7..fd94953 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,7 +16,7 @@ const BOARDSTART: (i32, i32) = (0, 0); fn main() { let (mut rl, thread) = raylib::init() .size(WIDTH, HEIGHT) - .title("Hello, World") + .title("Connect 4 Game (bad)") .build(); //images @@ -101,7 +101,7 @@ fn main() { Disk::P2 => Color::YELLOW, Disk::EMPTY => Color::WHITE, }; - d.draw_texture(&circle_texture, x, y, color); + d.draw_texture(&circle_texture, x - 1, y + 2, color); } d.clear_background(Color::WHITE); }