pls help
This commit is contained in:
@@ -4,7 +4,7 @@ mod tests;
|
|||||||
|
|
||||||
use array2d::Array2D;
|
use array2d::Array2D;
|
||||||
|
|
||||||
use self::score_checkers::{one_direction, two_direction};
|
use self::score_checkers::scan;
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct Board {
|
pub struct Board {
|
||||||
p1_score: i32,
|
p1_score: i32,
|
||||||
@@ -42,17 +42,17 @@ impl Board {
|
|||||||
unimplemented!()
|
unimplemented!()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn get_indices(
|
// pub fn get_indices(
|
||||||
index: &(usize, usize),
|
// index: &(usize, usize),
|
||||||
op: fn(&(usize, usize), usize) -> (usize, usize),
|
// op: fn(&(usize, usize), usize) -> (usize, usize),
|
||||||
values: Vec<usize>,
|
// values: Vec<usize>,
|
||||||
) -> Vec<(usize, usize)> {
|
// ) -> Vec<(usize, usize)> {
|
||||||
let mut indices: Vec<(usize, usize)> = Vec::with_capacity(3);
|
// let mut indices: Vec<(usize, usize)> = Vec::with_capacity(3);
|
||||||
for num in values {
|
// for num in values {
|
||||||
indices.push(op(index, num));
|
// indices.push(op(index, num));
|
||||||
}
|
// }
|
||||||
indices
|
// indices
|
||||||
}
|
// }
|
||||||
|
|
||||||
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 as usize, *col)
|
||||||
|
|||||||
@@ -1,143 +1,144 @@
|
|||||||
use array2d::Array2D;
|
use array2d::Array2D;
|
||||||
|
|
||||||
use super::{dec_col, get_indices, inc_col, Disk};
|
use crate::gamedata::{dec_both, dec_inc, dec_row, inc_both, inc_dec, inc_row};
|
||||||
|
|
||||||
pub fn one_direction(board: &Array2D<Disk>, index: &(usize, usize), direction: Direction) -> i32 {
|
use super::{dec_col, inc_col, Disk};
|
||||||
// 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;
|
|
||||||
// for _num in 0..4 {
|
|
||||||
// match board.get(current_index.0, current_index.1) {
|
|
||||||
// Some(_disk) => {
|
|
||||||
// if variant_eq(current_disk, _disk) && !matches!(_disk, Disk::EMPTY) {
|
|
||||||
// // add in a row by 1
|
|
||||||
// in_a_row += 1;
|
|
||||||
// //go to next element
|
|
||||||
// match direction {
|
|
||||||
// Direction::DOWN => {
|
|
||||||
// if current_index.0 == 0 {
|
|
||||||
// break;
|
|
||||||
// }
|
|
||||||
// current_index.0 -= 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 == board.num_columns() - 1 || current_index.1 == 0 {
|
|
||||||
// break;
|
|
||||||
// }
|
|
||||||
// current_index.1 -= 1;
|
|
||||||
// current_index.0 += 1;
|
|
||||||
// }
|
|
||||||
// Direction::DOWNFORW => {
|
|
||||||
// if current_index.0 == 0 || current_index.1 == board.num_columns() - 1 {
|
|
||||||
// break;
|
|
||||||
// }
|
|
||||||
// current_index.1 += 1;
|
|
||||||
// current_index.0 -= 1;
|
|
||||||
// }
|
|
||||||
// Direction::DOWNBACK => {
|
|
||||||
// if current_index.0 == 0 || current_index.1 == 0 {
|
|
||||||
// break;
|
|
||||||
// }
|
|
||||||
// current_index.1 -= 1;
|
|
||||||
// current_index.0 -= 1;
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// } else {
|
|
||||||
// break;
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// None => break,
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// if in_a_row == 4 {
|
|
||||||
// //score added
|
|
||||||
// return 1;
|
|
||||||
// } else {
|
|
||||||
// return 0;
|
|
||||||
// }
|
|
||||||
// //+-3
|
|
||||||
todo!()
|
|
||||||
}
|
|
||||||
// board[(2,3)];
|
|
||||||
|
|
||||||
pub fn two_direction(board: &Array2D<Disk>, index: &(usize, usize), direction: Direction) -> i32 {
|
pub fn scan(
|
||||||
let mut added_score = 0;
|
board: &Array2D<Disk>,
|
||||||
|
index: &(usize, usize),
|
||||||
|
direction: Direction,
|
||||||
|
depth: 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) => current_disk = disk,
|
Some(disk) => current_disk = disk,
|
||||||
None => return 0,
|
None => return 0,
|
||||||
};
|
};
|
||||||
|
let mut current_index = *index;
|
||||||
|
let mut in_a_row = 0;
|
||||||
|
for _num in 0..depth {
|
||||||
|
match board.get(current_index.0, current_index.1) {
|
||||||
|
Some(_disk) => {
|
||||||
|
dbg!(_disk, current_disk);
|
||||||
|
if variant_eq(current_disk, _disk) && !variant_eq(_disk, &Disk::EMPTY) {
|
||||||
|
// add in a row by 1
|
||||||
|
in_a_row += 1;
|
||||||
|
dbg!(current_index);
|
||||||
|
//go to next element
|
||||||
match direction {
|
match direction {
|
||||||
Direction::HORIZONTAL => {
|
Direction::DOWN => {
|
||||||
//get values to increase/decrease by
|
if current_index.0 == 0 {
|
||||||
let two = vec![1, 2];
|
break;
|
||||||
let one = vec![1];
|
}
|
||||||
//get surrounding indices
|
current_index = dec_row(¤t_index, 1);
|
||||||
let mut indices: Vec<(usize, usize)> = vec![];
|
//current_index.0 -= 1;
|
||||||
indices.append(&mut get_indices(index, inc_col, two));
|
}
|
||||||
indices.append(&mut get_indices(index, dec_col, one));
|
Direction::UP => {
|
||||||
dbg!(indices.clone());
|
if current_index.0 == board.num_rows() - 1 {
|
||||||
let mut neighbours: Vec<Disk> = vec![];
|
break;
|
||||||
//get neighbours
|
}
|
||||||
for index in indices {
|
current_index = inc_row(¤t_index, 1);
|
||||||
match board.get(index.0, index.1) {
|
// current_index.0 += 1;
|
||||||
Some(disk) => neighbours.push(*disk),
|
}
|
||||||
|
Direction::LEFT => {
|
||||||
|
if current_index.1 == 0 {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
current_index = dec_col(¤t_index, 1);
|
||||||
|
// current_index.1 -= 1;
|
||||||
|
}
|
||||||
|
Direction::RIGHT => {
|
||||||
|
if current_index.1 == board.num_columns() - 1 {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
current_index = inc_col(¤t_index, 1);
|
||||||
|
// current_index.1 += 1;
|
||||||
|
}
|
||||||
|
Direction::UPRIGHT => {
|
||||||
|
if current_index.0 == board.num_rows() - 1
|
||||||
|
|| current_index.1 == board.num_columns() - 1
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
current_index = inc_both(¤t_index, 1);
|
||||||
|
// current_index.1 += 1;
|
||||||
|
// current_index.0 += 1;
|
||||||
|
}
|
||||||
|
Direction::UPLEFT => {
|
||||||
|
if current_index.0 == board.num_columns() - 1 || current_index.1 == 0 {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// current_index.1 -= 1;
|
||||||
|
// current_index.0 += 1;
|
||||||
|
current_index = inc_dec(¤t_index, 1);
|
||||||
|
}
|
||||||
|
Direction::DOWNRIGHT => {
|
||||||
|
if current_index.0 == 0 || current_index.1 == board.num_columns() - 1 {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
current_index = dec_inc(¤t_index, 1);
|
||||||
|
// current_index.1 += 1;
|
||||||
|
// current_index.0 -= 1;
|
||||||
|
}
|
||||||
|
Direction::DOWNLEFT => {
|
||||||
|
if current_index.0 == 0 || current_index.1 == 0 {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
current_index = dec_both(¤t_index, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
None => break,
|
None => break,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let in_a_row = neighbours
|
in_a_row
|
||||||
.iter()
|
// //+-3
|
||||||
.filter(|&a| variant_eq(a, current_disk))
|
}
|
||||||
.count();
|
// board[(2,3)];
|
||||||
if in_a_row == 3 {
|
pub fn get_legal_moves(
|
||||||
added_score += 1;
|
(row, col): &(usize, usize),
|
||||||
|
direction: Direction,
|
||||||
|
(nrow, ncol): (usize, usize),
|
||||||
|
) -> Vec<Direction> {
|
||||||
|
let max_col = nrow - 1;
|
||||||
|
let max_row = ncol - 1;
|
||||||
|
let mut moves: Vec<Direction> = vec![];
|
||||||
|
match *row {
|
||||||
|
0 => moves.push(Direction::UP),
|
||||||
|
max_row => moves.push(Direction::DOWN),
|
||||||
|
_ => {
|
||||||
|
moves.push(Direction::UP);
|
||||||
|
moves.push(Direction::DOWN);
|
||||||
}
|
}
|
||||||
|
};
|
||||||
added_score
|
match *col {
|
||||||
}
|
0 => moves.push(Direction::RIGHT),
|
||||||
Direction::VERTICAL => todo!(),
|
max_row => moves.push(Direction::LEFT),
|
||||||
Direction::DIAGONAL => todo!(),
|
_ => {
|
||||||
|
moves.push(Direction::LEFT);
|
||||||
|
moves.push(Direction::RIGHT)
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
moves
|
||||||
}
|
}
|
||||||
|
|
||||||
pub enum Direction {
|
pub enum Direction {
|
||||||
HORIZONTAL,
|
UP,
|
||||||
VERTICAL,
|
DOWN,
|
||||||
DIAGONAL,
|
LEFT,
|
||||||
|
RIGHT,
|
||||||
|
UPLEFT,
|
||||||
|
UPRIGHT,
|
||||||
|
DOWNLEFT,
|
||||||
|
DOWNRIGHT,
|
||||||
}
|
}
|
||||||
// serves nothing except do what matches!() should have done all along
|
// serves nothing except do what matches!() should have done all along
|
||||||
fn variant_eq<T>(a: &T, b: &T) -> bool {
|
pub fn variant_eq<T>(a: &T, b: &T) -> bool {
|
||||||
std::mem::discriminant(a) == std::mem::discriminant(b)
|
std::mem::discriminant(a) == std::mem::discriminant(b)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
use crate::gamedata::{dec_col, score_checkers::Direction, Board};
|
use crate::gamedata::score_checkers::Direction;
|
||||||
|
|
||||||
use super::{get_indices, inc_col, score_checkers::two_direction, Disk};
|
use super::*;
|
||||||
|
|
||||||
// #[test]
|
// #[test]
|
||||||
// fn board_default() {
|
// fn board_default() {
|
||||||
@@ -47,7 +47,7 @@ fn play() {
|
|||||||
assert!(!board.play(Disk::BLU, 3));
|
assert!(!board.play(Disk::BLU, 3));
|
||||||
}
|
}
|
||||||
#[test]
|
#[test]
|
||||||
fn one_direction_updown() {
|
fn scan_updown() {
|
||||||
let mut board = Board::default();
|
let mut board = Board::default();
|
||||||
board.play(Disk::BLU, 0);
|
board.play(Disk::BLU, 0);
|
||||||
board.play(Disk::BLU, 0);
|
board.play(Disk::BLU, 0);
|
||||||
@@ -56,54 +56,42 @@ fn one_direction_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!(1, one_direction(&board.columns, &(4, 0), Direction::DOWN));
|
assert_eq!(3, scan(&board.columns, &(4, 0), Direction::DOWN, 3));
|
||||||
// assert_eq!(1, one_direction(&board.columns, &(3, 0), Direction::DOWN));
|
assert_eq!(3, scan(&board.columns, &(3, 0), Direction::DOWN, 3));
|
||||||
}
|
}
|
||||||
#[test]
|
#[test]
|
||||||
fn one_direction_updown2() {
|
fn scan_updown2() {
|
||||||
let mut board = Board::default();
|
let mut board = Board::default();
|
||||||
board.play(Disk::BLU, 0);
|
board.play(Disk::BLU, 0);
|
||||||
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!(0, one_direction(&board.columns, &(3, 0), Direction::DOWN));
|
assert_eq!(0, scan(&board.columns, &(0, 0), Direction::UP, 3));
|
||||||
}
|
}
|
||||||
#[test]
|
#[test]
|
||||||
fn one_direction_forwardback() {
|
fn scan_forwardback() {
|
||||||
let mut board = Board::default();
|
let mut board = Board::default();
|
||||||
board.play(Disk::BLU, 0);
|
board.play(Disk::BLU, 0);
|
||||||
board.play(Disk::BLU, 1);
|
board.play(Disk::BLU, 1);
|
||||||
board.play(Disk::BLU, 2);
|
board.play(Disk::BLU, 2);
|
||||||
board.play(Disk::BLU, 3);
|
board.play(Disk::BLU, 3);
|
||||||
|
|
||||||
// assert!(!matches!(Disk::RED, Disk::BLU));
|
assert!(!matches!(Disk::RED, Disk::BLU));
|
||||||
// assert_eq!(
|
assert_eq!(1, scan(&board.columns, &(0, 0), Direction::RIGHT, 3));
|
||||||
// 1,
|
assert_eq!(1, scan(&board.columns, &(0, 3), Direction::LEFT, 3));
|
||||||
// one_direction(&board.columns, &(0, 0), Direction::FORWARD)
|
|
||||||
// );
|
|
||||||
// assert_eq!(
|
|
||||||
// 1,
|
|
||||||
// one_direction(&board.columns, &(0, 3), Direction::BACKWARD)
|
|
||||||
// );
|
|
||||||
}
|
}
|
||||||
#[test]
|
#[test]
|
||||||
fn one_direction_forwardback2() {
|
fn scan_forwardback2() {
|
||||||
let mut board = Board::default();
|
let mut board = Board::default();
|
||||||
board.play(Disk::BLU, 0);
|
board.play(Disk::BLU, 0);
|
||||||
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!(
|
assert_eq!(0, scan(&board.columns, &(0, 0), Direction::RIGHT, 3));
|
||||||
// 0,
|
assert_eq!(0, scan(&board.columns, &(0, 3), Direction::LEFT, 3));
|
||||||
// one_direction(&board.columns, &(0, 0), Direction::FORWARD)
|
|
||||||
// );
|
|
||||||
// assert_eq!(
|
|
||||||
// 0,
|
|
||||||
// one_direction(&board.columns, &(0, 3), Direction::BACKWARD)
|
|
||||||
// );
|
|
||||||
}
|
}
|
||||||
#[test]
|
#[test]
|
||||||
fn one_direction_diag1() {
|
fn scan_diag1() {
|
||||||
let mut board = Board::default();
|
let mut board = Board::default();
|
||||||
board.play(Disk::BLU, 0);
|
board.play(Disk::BLU, 0);
|
||||||
board.play(Disk::RED, 1);
|
board.play(Disk::RED, 1);
|
||||||
@@ -115,14 +103,11 @@ fn one_direction_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!(1, one_direction(&board.columns, &(0, 0), Direction::UPFORW));
|
assert_eq!(1, scan(&board.columns, &(0, 0), Direction::UPRIGHT, 3));
|
||||||
// assert_eq!(
|
assert_eq!(1, scan(&board.columns, &(3, 3), Direction::DOWNLEFT, 3));
|
||||||
// 1,
|
|
||||||
// one_direction(&board.columns, &(3, 3), Direction::DOWNBACK)
|
|
||||||
// );
|
|
||||||
}
|
}
|
||||||
#[test]
|
#[test]
|
||||||
fn one_direction_diag2() {
|
fn scan_diag2() {
|
||||||
let mut board = Board::default();
|
let mut board = Board::default();
|
||||||
board.play(Disk::BLU, 3);
|
board.play(Disk::BLU, 3);
|
||||||
board.play(Disk::RED, 2);
|
board.play(Disk::RED, 2);
|
||||||
@@ -135,30 +120,13 @@ fn one_direction_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!(1, one_direction(&board.columns, &(0, 3), Direction::UPBACK));
|
assert_eq!(1, scan(&board.columns, &(0, 3), Direction::UPLEFT, 3));
|
||||||
// assert_eq!(
|
assert_eq!(1, scan(&board.columns, &(3, 0), Direction::DOWNRIGHT, 3));
|
||||||
// 1,
|
|
||||||
// one_direction(&board.columns, &(3, 0), Direction::DOWNFORW)
|
|
||||||
// );
|
|
||||||
}
|
}
|
||||||
#[test]
|
#[test]
|
||||||
fn get_indices_test() {
|
fn variant_eq_test() {
|
||||||
let values: Vec<usize> = vec![1, 2];
|
assert!(score_checkers::variant_eq(&Disk::RED, &Disk::RED));
|
||||||
let indices = get_indices(&(2, 1), inc_col, values);
|
assert!(matches!(Disk::RED, Disk::RED));
|
||||||
assert_eq!(vec![(2, 2), (2, 3)], indices);
|
assert!(!score_checkers::variant_eq(&Disk::BLU, &Disk::RED));
|
||||||
let indices = get_indices(&(2, 1), dec_col, vec![1]);
|
assert!(!matches!(Disk::BLU, Disk::RED));
|
||||||
assert_eq!(vec![(2, 0)], indices);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn two_direction_test() {
|
|
||||||
let mut board = Board::default();
|
|
||||||
board.play(Disk::BLU, 0);
|
|
||||||
board.play(Disk::BLU, 1);
|
|
||||||
board.play(Disk::BLU, 2);
|
|
||||||
board.play(Disk::BLU, 3);
|
|
||||||
board.play(Disk::BLU, 4);
|
|
||||||
board.play(Disk::BLU, 5);
|
|
||||||
let added = two_direction(&board.columns, &(0, 3), Direction::HORIZONTAL);
|
|
||||||
assert_eq!(1, added)
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user