mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-25 04:15:45 -08:00
fixed drawing
This commit is contained in:
@@ -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!(" ");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 }
|
||||
|
||||
@@ -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} ");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user