use rand::distr::{Distribution, Uniform};
pub struct Board{
width : usize,
length : usize,
value : Vec<char>,
score : Option<isize>
}
impl Board{
pub fn new(value : Vec<char>, width : usize ,length : usize, score: Option<isize>) -> Self{
Self{
width : width,
length : length,
value : value,
score : score,
}
}
pub fn width(&self) -> usize{
self.width
}
pub fn length(&self) -> usize{
self.length
}
pub fn score(&self) -> Option<isize>{
self.score
}
pub fn value(&self) -> &Vec<char>{
&self.value
}
pub fn new_random(width : usize, length : usize)->Self{
let mut brd = Self{
width : width,
length : length,
value : vec![' '; width * length],
score : None
};
let uniform = Uniform::try_from(0..26).unwrap();
let mut rng = rand::rng();
for i in 0..width*length {
let rnd = uniform.sample(&mut rng);
let random_char = (b'A' + rnd) as char;
brd.value[i]= random_char;
}
brd
}
pub fn copy(&self) -> Self{
Self{
width : self.width,
length : self.length,
value : self.value.to_vec(),
score : self.score,
}
}
pub fn to_string(&self)->String{
let mut s = String::new();
for (i,val) in self.value.iter().enumerate() {
if i>0 && i%self.width == 0{
s.push('\n');
}
s.push(*val);
}
s
}
pub fn hash(&self)->String{
let mut s = String::new();
for val in self.value.iter() {
s.push(*val);
}
s
}
pub fn get(&self, i:usize,j:usize)->Option<char>{
if i>=self.width || j>=self.length
{
return None;
}
Some(self.value[i* self.width + j])
}
pub fn set(&mut self, i:usize,j:usize,ch:char){
if !(i>=self.width || j>=self.length)
{
self.value[i* self.width + j] = ch;
}
}
}