pub struct HexagonalBoard<T> { /* private fields */ }
Expand description
A board composed of hexagons.
You can construct a HexagonalBoard
the same ways you would construct a HashMap
,
where the keys are something that can be converted to a glam::IVec2
(e.g., [i32; 2]
).
The positions are interpreted as hexagonal coordinates, where the basis [1, 0]
is the
hexagon to the left and up, and [0, 1]
is the one to the right and up.
You can render a HexagonalBoard
using HexagonalBoard::render
if T: Into<char> + Clone
. Otherwise, you can use HexagonalBoard::render_with
to specify how to convert the
T
into a char
.
You can also use HexagonalBoard::char_map
if you want to easily get what character should
be printed where, but you want to do the rendering yourself.
§Examples
Basic usage:
use hext_boards::HexagonalBoard;
// Put an `'A'` at `[0, 0]` and a `'B'` at `[1, 1]`.
let board = HexagonalBoard::from([
([0, 0], 'A'),
([1, 1], 'B'),
]);
let output = board.render();
let expected = indoc::indoc!(
r"
/---\
⟨ B ⟩
⟩---⟨
⟨ A ⟩
\---/
"
).trim_end_matches('\n');
println!("{output}");
assert_eq!(output, expected)
Using HexagonalBoard::render_with
:
use hext_boards::HexagonalBoard;
let board = HexagonalBoard::from([
([0, 0], 5),
([0, 1], 13),
([1, 1], 25),
]);
// Everything needs to be one char, so we have to use hexadecimal or some other radix to
// output higher numbers.
let output = board.render_with(|n| char::from_digit(*n, 36).expect("`n` is less than 36."));
let expected = indoc::indoc!(
r"
/---\
⟨ p ⟩---\
⟩---⟨ d ⟩
⟨ 5 ⟩---/
\---/
"
).trim_end_matches('\n');
assert_eq!(output, expected)
Implementations§
Source§impl<T> HexagonalBoard<T>
impl<T> HexagonalBoard<T>
Sourcepub fn char_map(&self, into_char: impl Fn(&T) -> char) -> HashMap<IVec2, char>
pub fn char_map(&self, into_char: impl Fn(&T) -> char) -> HashMap<IVec2, char>
Map from coordinates
Sourcepub fn render_with(&self, into_char: impl Fn(&T) -> char) -> String
pub fn render_with(&self, into_char: impl Fn(&T) -> char) -> String
Renders the hexagonal board into a String
, converting the T
s into char
s using
into_char
.
If T
can easily be converted to a char
(i.e., T: Into<char> + Copy
), you can
use Self::render
.
Trait Implementations§
Source§impl<T: Clone> Clone for HexagonalBoard<T>
impl<T: Clone> Clone for HexagonalBoard<T>
Source§fn clone(&self) -> HexagonalBoard<T>
fn clone(&self) -> HexagonalBoard<T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more