Initial layout implementation.

This commit is contained in:
hal8174 2024-08-21 01:37:20 +02:00
parent 1596bf180d
commit f284b692cc
7 changed files with 374 additions and 6 deletions

View file

@ -1,6 +1,7 @@
use core::panic;
use std::io::{self, Write};
use rand::prelude::Distribution;
use termcolor::{ColorSpec, StandardStream, WriteColor};
pub type PositionType = i32;
@ -89,6 +90,19 @@ impl Direction {
}
}
impl Distribution<Direction> for rand::distributions::Standard {
fn sample<R: rand::Rng + ?Sized>(&self, rng: &mut R) -> Direction {
let a = [
Direction::Up,
Direction::Right,
Direction::Down,
Direction::Left,
];
let r = rng.gen_range(0..4);
a[r]
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct Position {
pub x: PositionType,
@ -132,6 +146,16 @@ impl Position {
}
}
impl std::ops::Add for Position {
type Output = Position;
fn add(mut self, rhs: Self) -> Self::Output {
self.x += rhs.x;
self.y += rhs.y;
self
}
}
impl std::ops::Sub for Position {
type Output = Position;
@ -208,8 +232,8 @@ where
{
let stdout = &mut StandardStream::stdout(termcolor::ColorChoice::Always);
let width_digits = width.ilog10() + 1;
let height_digits = height.ilog10() + 1;
let width_digits = (width - 1).ilog10() + 1;
let height_digits = (height - 1).ilog10() + 1;
// print header
for i in 0..width_digits {