score checkers work (made a function for variant checking)
This commit is contained in:
@@ -3,30 +3,76 @@ use array2d::Array2D;
|
|||||||
use super::Disk;
|
use super::Disk;
|
||||||
|
|
||||||
pub fn one_direction(board: &Array2D<Disk>, index: &(usize, usize), direction: Direction) -> i32 {
|
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 current_index = *index;
|
||||||
let mut in_a_row = 0;
|
let mut in_a_row = 0;
|
||||||
loop {
|
loop {
|
||||||
dbg!(in_a_row, current_index);
|
|
||||||
match board.get(current_index.0, current_index.1) {
|
match board.get(current_index.0, current_index.1) {
|
||||||
Some(_disk) => {
|
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
|
// add in a row by 1
|
||||||
in_a_row += 1;
|
in_a_row += 1;
|
||||||
//go to next element
|
//go to next element
|
||||||
match direction {
|
match direction {
|
||||||
Direction::BACKWARD => {
|
Direction::DOWN => {
|
||||||
if current_index.0 == 0 {
|
if current_index.0 == 0 {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
current_index.0 -= 1;
|
current_index.0 -= 1;
|
||||||
}
|
}
|
||||||
Direction::FORWARD => {
|
Direction::UP => {
|
||||||
if current_index.0 == board.num_rows() - 1 {
|
if current_index.0 == board.num_columns() - 1 {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
current_index.0 += 1;
|
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 {
|
} else {
|
||||||
break;
|
break;
|
||||||
@@ -36,6 +82,7 @@ pub fn one_direction(board: &Array2D<Disk>, index: &(usize, usize), direction: D
|
|||||||
None => break,
|
None => break,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
dbg!(in_a_row);
|
||||||
if in_a_row == 4 {
|
if in_a_row == 4 {
|
||||||
//score added
|
//score added
|
||||||
return 1;
|
return 1;
|
||||||
@@ -63,5 +110,12 @@ pub enum Direction {
|
|||||||
DOWN,
|
DOWN,
|
||||||
FORWARD,
|
FORWARD,
|
||||||
BACKWARD,
|
BACKWARD,
|
||||||
|
UPFORW,
|
||||||
|
UPBACK,
|
||||||
|
DOWNFORW,
|
||||||
|
DOWNBACK,
|
||||||
//TODO add more directions for diagonals
|
//TODO add more directions for diagonals
|
||||||
}
|
}
|
||||||
|
fn variant_eq<T>(a: &T, b: &T) -> bool {
|
||||||
|
std::mem::discriminant(a) == std::mem::discriminant(b)
|
||||||
|
}
|
||||||
|
|||||||
@@ -50,19 +50,54 @@ fn play() {
|
|||||||
assert!(!board.play(Disk::BLUE, 3));
|
assert!(!board.play(Disk::BLUE, 3));
|
||||||
}
|
}
|
||||||
#[test]
|
#[test]
|
||||||
fn one_direction_test() {
|
fn one_direction_updown() {
|
||||||
let mut board_true = Board::default();
|
let mut board = Board::default();
|
||||||
let board_false = Board::default();
|
board.play(Disk::BLUE, 0);
|
||||||
board_true.play(Disk::BLUE, 0);
|
board.play(Disk::BLUE, 0);
|
||||||
board_true.play(Disk::BLUE, 0);
|
board.play(Disk::BLUE, 0);
|
||||||
board_true.play(Disk::BLUE, 0);
|
board.play(Disk::BLUE, 0);
|
||||||
board_true.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!(
|
assert_eq!(
|
||||||
1,
|
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!(
|
assert_eq!(
|
||||||
0,
|
0,
|
||||||
one_direction(&board_false.columns, &(3, 0), Direction::BACKWARD)
|
one_direction(&board.columns, &(0, 3), Direction::BACKWARD)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user