fixed drawing

This commit is contained in:
Uğur Küpeli
2022-05-06 13:22:57 +03:00
parent b86c30e89c
commit ece3fe0f52
4 changed files with 15 additions and 13 deletions

View File

@@ -8,9 +8,9 @@ impl Disk {
}
pub fn draw(&self) {
let space_amount = (15 - self.size) / 2;
let draw_space = || {
let space_amount = (15 - self.size) / 2;
if space_amount > 0 {
for _ in 0..space_amount {
print!(" ");
@@ -23,5 +23,6 @@ impl Disk {
print!("*");
}
draw_space();
print!(" ");
}
}

View File

@@ -9,15 +9,14 @@ impl Game {
pub fn new() -> Self {
let mut needles = Vec::new();
for i in 0..3 {
let number = (i + 1) as u8;
let disks = match number {
for i in 1..=3 {
let disks = match i {
1 => util::generate_disks(4),
2 => util::generate_disks(3),
_ => Vec::new(),
};
needles.push(Needle { disks, number });
needles.push(Needle { disks, number:i });
}
Game { needles, _moves: 0 }

View File

@@ -9,18 +9,16 @@ impl Needle {
pub fn draw(&self, row: u8) {
//println!("printing row: {}", row);
let offset = match self.number {
1 => " ",
_ => "\t\t\t",
};
let row = row as usize;
if self.disks.len() >= row {
self.disks[row - 1].draw();
} else {
let offset = " ";
print!("{offset}");
print!("*");
print!("{offset} ");
}
}
}

View File

@@ -5,10 +5,14 @@ pub fn generate_disks(amount: u8) -> Vec<Disk> {
println!("CANNOT HAVE MORE THAN 7 DISKS!");
}
// check for if amount == 0
let mut disks = Vec::new();
for i in (1..=amount).rev() {
disks.push(Disk::new(i * 2 + 1));
let mut half_size = 7;
for _ in (1..=amount).rev() {
disks.push(Disk::new(half_size * 2 + 1));
half_size -= 1;
}
disks