pub struct Coords {
pub row: CoordValue,
pub column: CoordValue,
}
Expand description
Coordinates of a single cell of the board.
hexgame
uses a zero-based (row, column)-format analogous to matrix-indices.
The following diagram shows on the left the format used by Coords
and on the right
the “c4” format similar to Chess that is commonly used in the literature.
Note that the order of row-index and column-index is swapped between both formats:
The marked cell has coordinates (1, 3) and d2, respectively.
0 1 2 3 4 a b c d e
0\. . . . .\0 1\. . . . .\1
1\. . . ● .\1 2\. . . ● .\2
2\. . . . .\2 3\. . . . .\3
3\. . . . .\3 4\. . . . .\4
4\. . . . .\4 5\. . . . .\5
0 1 2 3 4 a b c d e
The from_str
and to_string
methods can be used to convert between the formats.
use std::str::FromStr;
let coords = Coords::new(7, 0);
// Note the different order!
assert_eq!(coords.to_string(), "a8");
let other_coords = Coords::from_str("a8").unwrap();
assert_eq!(coords, other_coords);
Fields§
§row: CoordValue
Zero-based row index, counted from top to bottom.
column: CoordValue
Zero-based column index, counted from left to right.
Implementations§
Source§impl Coords
impl Coords
Sourcepub fn new(row: CoordValue, column: CoordValue) -> Self
pub fn new(row: CoordValue, column: CoordValue) -> Self
Create a new Coords instance. Watch out: Order of parameters is different from the commonly used “c4” format.
Sourcepub fn is_on_board_with_size(&self, size: CoordValue) -> bool
pub fn is_on_board_with_size(&self, size: CoordValue) -> bool
Return whether this coordinate exist on a board of the given size.