score checkers work (made a function for variant checking)
This commit is contained in:
@@ -3,30 +3,76 @@ use array2d::Array2D;
|
||||
use super::Disk;
|
||||
|
||||
pub fn one_direction(board: &Array2D<Disk>, 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<Disk>, 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<T>(a: &T, b: &T) -> bool {
|
||||
std::mem::discriminant(a) == std::mem::discriminant(b)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user