From c5a49aa3d4779825d7cd9b6923adfbd0b82b89a1 Mon Sep 17 00:00:00 2001 From: LinlyBoi Date: Mon, 1 May 2023 22:27:08 +0300 Subject: [PATCH] score checkers work (made a function for variant checking) --- src/gamedata/score_checkers.rs | 66 ++++++++++++++++++++++++++++++---- src/gamedata/tests.rs | 53 ++++++++++++++++++++++----- 2 files changed, 104 insertions(+), 15 deletions(-) diff --git a/src/gamedata/score_checkers.rs b/src/gamedata/score_checkers.rs index 8956f7b..c96c18f 100644 --- a/src/gamedata/score_checkers.rs +++ b/src/gamedata/score_checkers.rs @@ -3,30 +3,76 @@ use array2d::Array2D; use super::Disk; pub fn one_direction(board: &Array2D, index: &(usize, usize), direction: Direction) -> i32 { - let current_disk = board.get(index.0, index.1); + let current_disk: &Disk; + match board.get(index.0, index.1) { + Some(disk) => current_disk = disk, + None => return 0, + }; let mut current_index = *index; let mut in_a_row = 0; loop { - dbg!(in_a_row, current_index); match board.get(current_index.0, current_index.1) { Some(_disk) => { - if matches!(current_disk, _disk) && !matches!(_disk, Disk::EMPTY) { + if variant_eq(current_disk, _disk) && !matches!(_disk, Disk::EMPTY) { + dbg!(current_index, in_a_row, current_disk, _disk); // add in a row by 1 in_a_row += 1; //go to next element match direction { - Direction::BACKWARD => { + Direction::DOWN => { if current_index.0 == 0 { break; } current_index.0 -= 1; } - Direction::FORWARD => { - if current_index.0 == board.num_rows() - 1 { + Direction::UP => { + if current_index.0 == board.num_columns() - 1 { break; } current_index.0 += 1; } + Direction::BACKWARD => { + if current_index.1 == 0 { + break; + } + current_index.1 -= 1; + } + Direction::FORWARD => { + if current_index.1 == board.num_rows() - 1 { + break; + } + current_index.1 += 1; + } + Direction::UPFORW => { + if current_index.0 == board.num_columns() - 1 + || current_index.1 == board.num_rows() - 1 + { + break; + } + current_index.1 += 1; + current_index.0 += 1; + } + Direction::UPBACK => { + if current_index.0 == 0 || current_index.1 == board.num_rows() - 1 { + break; + } + current_index.1 -= 1; + current_index.0 += 1; + } + Direction::DOWNFORW => { + if current_index.1 == 0 || current_index.0 == board.num_columns() - 1 { + break; + } + current_index.1 += 1; + current_index.0 -= 1; + } + Direction::DOWNBACK => { + if current_index.1 == 0 || current_index.0 == 0 { + break; + } + current_index.1 -= 1; + current_index.0 -= 1; + } } } else { break; @@ -36,6 +82,7 @@ pub fn one_direction(board: &Array2D, index: &(usize, usize), direction: D None => break, } } + dbg!(in_a_row); if in_a_row == 4 { //score added return 1; @@ -63,5 +110,12 @@ pub enum Direction { DOWN, FORWARD, BACKWARD, + UPFORW, + UPBACK, + DOWNFORW, + DOWNBACK, //TODO add more directions for diagonals } +fn variant_eq(a: &T, b: &T) -> bool { + std::mem::discriminant(a) == std::mem::discriminant(b) +} diff --git a/src/gamedata/tests.rs b/src/gamedata/tests.rs index 48dc54d..1686e2b 100644 --- a/src/gamedata/tests.rs +++ b/src/gamedata/tests.rs @@ -50,19 +50,54 @@ fn play() { assert!(!board.play(Disk::BLUE, 3)); } #[test] -fn one_direction_test() { - let mut board_true = Board::default(); - let board_false = Board::default(); - board_true.play(Disk::BLUE, 0); - board_true.play(Disk::BLUE, 0); - board_true.play(Disk::BLUE, 0); - board_true.play(Disk::BLUE, 0); +fn one_direction_updown() { + let mut board = Board::default(); + board.play(Disk::BLUE, 0); + board.play(Disk::BLUE, 0); + board.play(Disk::BLUE, 0); + board.play(Disk::BLUE, 0); + assert_eq!(1, one_direction(&board.columns, &(3, 0), Direction::DOWN)); +} +#[test] +fn one_direction_updown2() { + let mut board = Board::default(); + board.play(Disk::BLUE, 0); + board.play(Disk::RED, 0); + board.play(Disk::BLUE, 0); + board.play(Disk::BLUE, 0); + assert_eq!(0, one_direction(&board.columns, &(3, 0), Direction::DOWN)); +} +#[test] +fn one_direction_forwardback() { + let mut board = Board::default(); + board.play(Disk::BLUE, 0); + board.play(Disk::BLUE, 1); + board.play(Disk::BLUE, 2); + board.play(Disk::BLUE, 3); + + assert!(!matches!(Disk::RED, Disk::BLUE)); assert_eq!( 1, - one_direction(&board_true.columns, &(3, 0), Direction::BACKWARD) + one_direction(&board.columns, &(0, 0), Direction::FORWARD) + ); + assert_eq!( + 1, + one_direction(&board.columns, &(0, 3), Direction::BACKWARD) + ); +} +#[test] +fn one_direction_forwardback2() { + let mut board = Board::default(); + board.play(Disk::BLUE, 0); + board.play(Disk::BLUE, 1); + board.play(Disk::BLUE, 3); + board.play(Disk::RED, 2); + assert_eq!( + 0, + one_direction(&board.columns, &(0, 0), Direction::FORWARD) ); assert_eq!( 0, - one_direction(&board_false.columns, &(3, 0), Direction::BACKWARD) + one_direction(&board.columns, &(0, 3), Direction::BACKWARD) ); }