2D array instead of Vec<Vec<Disk>>
This commit is contained in:
@@ -2,11 +2,6 @@ mod score_checkers;
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests;
|
mod tests;
|
||||||
|
|
||||||
use std::{
|
|
||||||
sync::mpsc::{self, Receiver, Sender},
|
|
||||||
thread,
|
|
||||||
};
|
|
||||||
|
|
||||||
use array2d::Array2D;
|
use array2d::Array2D;
|
||||||
|
|
||||||
use self::score_checkers::{one_direction, two_direction};
|
use self::score_checkers::{one_direction, two_direction};
|
||||||
@@ -14,15 +9,13 @@ use self::score_checkers::{one_direction, two_direction};
|
|||||||
pub struct Board {
|
pub struct Board {
|
||||||
p1_score: i32,
|
p1_score: i32,
|
||||||
p2_score: i32,
|
p2_score: i32,
|
||||||
columns: Vec<Vec<Disk>>,
|
columns: Array2D<Disk>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Board {
|
impl Default for Board {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
let mut columns = Vec::with_capacity(6);
|
let columns = Array2D::filled_with(Disk::EMPTY, 7, 6);
|
||||||
for _num in 0..7 {
|
|
||||||
columns.push(Vec::with_capacity(7))
|
|
||||||
}
|
|
||||||
Self {
|
Self {
|
||||||
p1_score: 0,
|
p1_score: 0,
|
||||||
p2_score: 0,
|
p2_score: 0,
|
||||||
@@ -35,36 +28,26 @@ impl Board {
|
|||||||
fn getscore(&self) -> (i32, i32) {
|
fn getscore(&self) -> (i32, i32) {
|
||||||
(self.p1_score, self.p2_score)
|
(self.p1_score, self.p2_score)
|
||||||
}
|
}
|
||||||
fn play(&mut self, disk: Disk, col: i32) {
|
fn play(&mut self, disk: Disk, col: i32) -> bool {
|
||||||
match self.columns.get_mut(col as usize) {
|
let column = &self.columns.as_columns()[col as usize];
|
||||||
Some(column) => {
|
let empty = column.iter().filter(|&a| matches!(a, Disk::EMPTY)).count();
|
||||||
if column.len() != column.capacity() {
|
dbg!(empty);
|
||||||
column.push(disk)
|
let top = column.len() - empty;
|
||||||
} else {
|
match self.columns.set(top, col as usize, disk) {
|
||||||
println!("Column full")
|
Ok(_) => true,
|
||||||
}
|
Err(_) => false,
|
||||||
}
|
|
||||||
None => println!("column not exist"), //TODO properly handle this
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn score_check(&mut self, index: (usize, usize)) {
|
fn score_check(&mut self, index: (usize, usize)) {
|
||||||
let _board = Array2D::from_columns(&self.columns);
|
unimplemented!()
|
||||||
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"),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub enum Disk {
|
pub enum Disk {
|
||||||
RED,
|
RED,
|
||||||
BLUE,
|
BLUE,
|
||||||
|
EMPTY,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user