Rename row/column to rank/file

This is how they're "officially" called
This commit is contained in:
2021-12-04 18:54:09 +01:00
parent e45fd3981a
commit 458f87fd61

View File

@@ -5,16 +5,16 @@ enum Color {
} }
impl Color { impl Color {
fn get_home_row(&self) -> Row { fn get_home_rank(&self) -> Rank {
match self { match self {
Color::Black => Row::_8, Color::Black => Rank::_8,
Color::White => Row::_1, Color::White => Rank::_1,
} }
} }
fn get_pawn_row(&self) -> Row { fn get_pawn_rank(&self) -> Rank {
match self { match self {
Color::Black => Row::_7, Color::Black => Rank::_7,
Color::White => Row::_2, Color::White => Rank::_2,
} }
} }
fn get_direction(&self) -> i8 { fn get_direction(&self) -> i8 {
@@ -27,22 +27,22 @@ impl Color {
#[derive(Clone)] #[derive(Clone)]
struct Position { struct Position {
row: Row, rank: Rank,
column: Column, file: File,
} }
impl Position { impl Position {
fn delta(&self, delta_row_column: (i8, i8)) -> Result<Position, ()> { fn delta(&self, delta_rank_file: (i8, i8)) -> Result<Position, ()> {
let (delta_row, delta_column) = delta_row_column; let (delta_rank, delta_file) = delta_rank_file;
Ok(Position { Ok(Position {
row: self.row.delta(delta_row)?, rank: self.rank.delta(delta_rank)?,
column: self.column.delta(delta_column)?, file: self.file.delta(delta_file)?,
}) })
} }
} }
#[derive(PartialEq, Clone, Copy)] #[derive(PartialEq, Clone, Copy)]
enum Row { enum Rank {
_1, _1,
_2, _2,
_3, _3,
@@ -54,7 +54,7 @@ enum Row {
} }
#[derive(PartialEq, Clone, Copy)] #[derive(PartialEq, Clone, Copy)]
enum Column { enum File {
A, A,
B, B,
C, C,
@@ -98,32 +98,32 @@ where
} }
} }
impl GridAxis for Row { impl GridAxis for Rank {
const ALL_VALUES: [Row; 8] = [ const ALL_VALUES: [Rank; 8] = [
Row::_1, Rank::_1,
Row::_2, Rank::_2,
Row::_3, Rank::_3,
Row::_4, Rank::_4,
Row::_5, Rank::_5,
Row::_6, Rank::_6,
Row::_7, Rank::_7,
Row::_8, Rank::_8,
]; ];
fn get_index(&self) -> u8 { fn get_index(&self) -> u8 {
*self as u8 *self as u8
} }
} }
impl GridAxis for Column { impl GridAxis for File {
const ALL_VALUES: [Column; 8] = [ const ALL_VALUES: [File; 8] = [
Column::A, File::A,
Column::B, File::B,
Column::C, File::C,
Column::D, File::D,
Column::E, File::E,
Column::F, File::F,
Column::G, File::G,
Column::H, File::H,
]; ];
fn get_index(&self) -> u8 { fn get_index(&self) -> u8 {
*self as u8 *self as u8
@@ -141,11 +141,11 @@ impl PieceType {
PieceType::Pawn => [Color::Black, Color::White] PieceType::Pawn => [Color::Black, Color::White]
.iter() .iter()
.flat_map(|color| { .flat_map(|color| {
Column::ALL_VALUES.iter().map(|&column| Piece { File::ALL_VALUES.iter().map(|&file| Piece {
piece_type: self.clone(), piece_type: self.clone(),
position: Position { position: Position {
row: color.get_pawn_row(), rank: color.get_pawn_rank(),
column, file,
}, },
color: color.clone(), color: color.clone(),
}) })
@@ -185,14 +185,14 @@ impl Piece {
) -> Vec<Position> { ) -> Vec<Position> {
deltas deltas
.iter() .iter()
.filter_map(|&delta_row_column| { .filter_map(|&delta_rank_file| {
self.position.delta(delta_row_column).ok() self.position.delta(delta_rank_file).ok()
}) })
.collect() .collect()
} }
fn _pawn_get_move_deltas(&self) -> Vec<(i8, i8)> { fn _pawn_get_move_deltas(&self) -> Vec<(i8, i8)> {
let direction = self.color.get_direction(); let direction = self.color.get_direction();
if self.position.row == self.color.get_pawn_row() { if self.position.rank == self.color.get_pawn_rank() {
vec![(1 * direction, 0), (2 * direction, 0)] vec![(1 * direction, 0), (2 * direction, 0)]
} else { } else {
vec![(1 * direction, 0)] vec![(1 * direction, 0)]
@@ -205,13 +205,13 @@ impl Piece {
} }
struct Board { struct Board {
state: [[Option<Piece>; Column::ALL_VALUES.len()]; Row::ALL_VALUES.len()], state: [[Option<Piece>; File::ALL_VALUES.len()]; Rank::ALL_VALUES.len()],
} }
impl Board { impl Board {
fn set_at(&mut self, position: &Position, piece: Piece) { fn set_at(&mut self, position: &Position, piece: Piece) {
self.state[position.row.get_index() as usize] self.state[position.rank.get_index() as usize]
[position.column.get_index() as usize] = Some(piece); [position.file.get_index() as usize] = Some(piece);
} }
fn new() -> Board { fn new() -> Board {
let mut board = Board { let mut board = Board {