2D array instead of Vec<Vec<Disk>>

This commit is contained in:
LinlyBoi
2023-05-01 00:05:40 +03:00
parent d61f9c83f7
commit bf0bdecd34

View File

@@ -2,11 +2,6 @@ mod score_checkers;
#[cfg(test)]
mod tests;
use std::{
sync::mpsc::{self, Receiver, Sender},
thread,
};
use array2d::Array2D;
use self::score_checkers::{one_direction, two_direction};
@@ -14,15 +9,13 @@ use self::score_checkers::{one_direction, two_direction};
pub struct Board {
p1_score: i32,
p2_score: i32,
columns: Vec<Vec<Disk>>,
columns: Array2D<Disk>,
}
impl Default for Board {
fn default() -> Self {
let mut columns = Vec::with_capacity(6);
for _num in 0..7 {
columns.push(Vec::with_capacity(7))
}
let columns = Array2D::filled_with(Disk::EMPTY, 7, 6);
Self {
p1_score: 0,
p2_score: 0,
@@ -35,36 +28,26 @@ impl Board {
fn getscore(&self) -> (i32, i32) {
(self.p1_score, self.p2_score)
}
fn play(&mut self, disk: Disk, col: i32) {
match self.columns.get_mut(col as usize) {
Some(column) => {
if column.len() != column.capacity() {
column.push(disk)
} else {
println!("Column full")
}
}
None => println!("column not exist"), //TODO properly handle this
fn play(&mut self, disk: Disk, col: i32) -> bool {
let column = &self.columns.as_columns()[col as usize];
let empty = column.iter().filter(|&a| matches!(a, Disk::EMPTY)).count();
dbg!(empty);
let top = column.len() - empty;
match self.columns.set(top, col as usize, disk) {
Ok(_) => true,
Err(_) => false,
}
}
fn score_check(&mut self, index: (usize, usize)) {
let _board = Array2D::from_columns(&self.columns);
match _board {
Ok(board) => {
// thread::scope(|scope| {
// let (tx, rx): (Sender<i32>, Receiver<i32>) = mpsc::channel();
// let one = scope.spawn(|| tx.clone().send(one_direction(&board, &index)));
// let two = scope.spawn(|| two_direction(&board, &index));
// one.join();
// two.join();
// }); //ignore this is threading for later TODO
}
Err(_err) => println!("Error parsing the thing"),
}
unimplemented!()
}
}
#[derive(Clone)]
pub enum Disk {
RED,
BLUE,
EMPTY,
}