1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
use std::cmp::max;
use std::fmt;
use std::str::FromStr;
use crate::{
Color, Direction, Error, File, Rank, BOARD_CELL_PX_SIZE, BOARD_SIZE, NUM_FILES, NUM_RANKS,
};
/// Represent a square on the chess board.
#[rustfmt::skip]
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug)]
#[repr(u8)]
pub enum Square {
A1, B1, C1, D1, E1, F1, G1, H1,
A2, B2, C2, D2, E2, F2, G2, H2,
A3, B3, C3, D3, E3, F3, G3, H3,
A4, B4, C4, D4, E4, F4, G4, H4,
A5, B5, C5, D5, E5, F5, G5, H5,
A6, B6, C6, D6, E6, F6, G6, H6,
A7, B7, C7, D7, E7, F7, G7, H7,
A8, B8, C8, D8, E8, F8, G8, H8,
}
/// Numbers of [`Square`].
pub const NUM_SQUARES: usize = (BOARD_SIZE.0 * BOARD_SIZE.1) as usize;
/// Enumerate all [`Square`].
#[rustfmt::skip]
pub const ALL_SQUARES: [Square; NUM_SQUARES] = [
Square::A1, Square::B1, Square::C1, Square::D1, Square::E1, Square::F1, Square::G1, Square::H1,
Square::A2, Square::B2, Square::C2, Square::D2, Square::E2, Square::F2, Square::G2, Square::H2,
Square::A3, Square::B3, Square::C3, Square::D3, Square::E3, Square::F3, Square::G3, Square::H3,
Square::A4, Square::B4, Square::C4, Square::D4, Square::E4, Square::F4, Square::G4, Square::H4,
Square::A5, Square::B5, Square::C5, Square::D5, Square::E5, Square::F5, Square::G5, Square::H5,
Square::A6, Square::B6, Square::C6, Square::D6, Square::E6, Square::F6, Square::G6, Square::H6,
Square::A7, Square::B7, Square::C7, Square::D7, Square::E7, Square::F7, Square::G7, Square::H7,
Square::A8, Square::B8, Square::C8, Square::D8, Square::E8, Square::F8, Square::G8, Square::H8,
];
impl Square {
/// Create a new [`Square`], from an index.
///
/// # Panics
///
/// Panic if the index is not in the range 0..=63.
///
/// # Examples
///
/// ```
/// use chess::Square;
///
/// assert_eq!(Square::new(0), Square::A1);
/// assert_eq!(Square::new(63), Square::H8);
/// ```
#[inline]
pub fn new(index: usize) -> Self {
ALL_SQUARES[index]
}
/// Convert this [`Square`] into a [`usize`] from 0 to 63 inclusive.
#[inline]
pub fn to_index(&self) -> usize {
*self as usize
}
/// Make a square from [`File`] and [`Rank`].
///
/// # Examples
///
/// ```
/// use chess::{File, Rank, Square};
///
/// // Make the A1 square
/// let square = Square::make_square(File::A, Rank::First);
/// ```
#[inline]
pub fn make_square(file: File, rank: Rank) -> Square {
Square::new(file.to_index() + rank.to_index() * BOARD_SIZE.0 as usize)
}
/// Transform a screen coordinate into a [`Square`].
///
/// > **Reciprocal**: see [`Square::to_screen`].
///
/// The result depend of:
/// - [`BOARD_SIZE`]
/// - [`BOARD_CELL_PX_SIZE`]
#[inline]
pub fn from_screen(x: f32, y: f32) -> Square {
// Transpose to grid space
let x = x / BOARD_CELL_PX_SIZE.0;
let y = y / BOARD_CELL_PX_SIZE.1;
// transpose to Square (return the y-axis)
let y = BOARD_SIZE.1 - y as i16 - 1;
Square::make_square(File::new(x as usize), Rank::new(y as usize))
}
/// Transform a [`Square`] into a screen coordinate.
///
/// > **Reciprocal**: see [`Square::from_screen`].
///
/// The result depend of:
/// - [`BOARD_SIZE`]
/// - [`BOARD_CELL_PX_SIZE`]
#[inline]
pub fn to_screen(&self) -> (f32, f32) {
// transpose to grid space (return the y-axis)
let x = self.file().to_index() as f32;
let y = (BOARD_SIZE.1 as usize - self.rank().to_index() - 1) as f32;
// Transpose to screen space
let x = x * BOARD_CELL_PX_SIZE.0;
let y = y * BOARD_CELL_PX_SIZE.1;
(x, y)
}
/// Return the [`File`] of this square.
///
/// # Examples
///
/// ```
/// use chess::{File, Rank, Square};
///
/// let square = Square::make_square(File::D, Rank::Seventh);
///
/// assert_eq!(square.file(), File::D);
/// ```
#[inline]
pub fn file(&self) -> File {
File::new(self.to_index() % NUM_FILES)
}
/// Return the "relative" [`File`] of this square according the side.
///
/// # Examples
///
/// ```
/// use chess::{Color, File, Square};
///
/// assert_eq!(Square::A1.file_for(Color::White), File::A);
/// assert_eq!(Square::A1.file_for(Color::Black), File::H);
/// ```
#[inline]
pub fn file_for(&self, color: Color) -> File {
let file = self.file();
match color {
Color::White => file,
Color::Black => File::new(NUM_FILES - file.to_index() - 1),
}
}
/// Return the [`Rank`] of this square.
///
/// # Examples
///
/// ```
/// use chess::{File, Rank, Square};
///
/// let square = Square::make_square(File::D, Rank::Seventh);
///
/// assert_eq!(square.rank(), Rank::Seventh);
/// ```
#[inline]
pub fn rank(&self) -> Rank {
Rank::new(self.to_index() / NUM_RANKS)
}
/// Return the "relative" [`Rank`] of this square according the side.
/// (i.e. return ranks for black)
///
/// # Examples
///
/// ```
/// use chess::{Color, Rank, Square};
///
/// assert_eq!(Square::E1.rank_for(Color::White), Rank::First);
/// assert_eq!(Square::E8.rank_for(Color::White), Rank::Eighth);
/// assert_eq!(Square::E2.rank_for(Color::Black), Rank::Seventh);
/// assert_eq!(Square::E7.rank_for(Color::Black), Rank::Second);
/// ```
#[inline]
pub fn rank_for(&self, color: Color) -> Rank {
let rank = self.rank();
match color {
Color::White => rank,
Color::Black => Rank::new(NUM_RANKS - rank.to_index() - 1),
}
}
/// Go one [`Rank`] up.
///
/// # Examples
///
/// ```
/// use chess::Square;
///
/// assert_eq!(Square::B2.up(), Square::B3);
/// ```
#[inline]
pub fn up(&self) -> Self {
Square::make_square(self.file(), self.rank().up())
}
/// Go *n* [`Rank`] up.
///
/// # Examples
///
/// ```
/// use chess::Square;
///
/// assert_eq!(Square::B2.n_up(3), Square::B5);
/// ```
#[inline]
pub fn n_up(&self, n: usize) -> Self {
let mut square = *self;
for _ in 0..n {
square = square.up();
}
square
}
/// Go one [`Rank`] forward according to the side.
///
/// # Examples
///
/// ```
/// use chess::{Color, Square};
///
/// assert_eq!(Square::B2.forward(Color::White), Square::B3);
/// assert_eq!(Square::B2.forward(Color::Black), Square::B1);
/// ```
#[inline]
pub fn forward(&self, color: Color) -> Self {
match color {
Color::White => Square::make_square(self.file(), self.rank().up()),
Color::Black => Square::make_square(self.file(), self.rank().down()),
}
}
/// Go *n* [`Rank`] forward according to the side.
///
/// # Examples
///
/// ```
/// use chess::{Color, Square};
///
/// assert_eq!(Square::B2.n_forward(Color::White, 2), Square::B4);
/// assert_eq!(Square::B8.n_forward(Color::Black, 5), Square::B3);
/// ```
#[inline]
pub fn n_forward(&self, color: Color, n: usize) -> Self {
let mut square = *self;
for _ in 0..n {
square = square.forward(color);
}
square
}
/// Go one [`Rank`] down.
///
/// # Examples
///
/// ```
/// use chess::Square;
///
/// assert_eq!(Square::B2.down(), Square::B1);
/// ```
#[inline]
pub fn down(&self) -> Self {
Square::make_square(self.file(), self.rank().down())
}
/// Go *n* [`Rank`] down.
///
/// # Examples
///
/// ```
/// use chess::{Color, Square};
///
/// assert_eq!(Square::B4.n_down(2), Square::B2);
/// ```
#[inline]
pub fn n_down(&self, n: usize) -> Self {
let mut square = *self;
for _ in 0..n {
square = square.down();
}
square
}
/// Go one [`Rank`] backward according to the side.
///
/// # Examples
///
/// ```
/// use chess::{Color, Square};
///
/// assert_eq!(Square::B2.backward(Color::White), Square::B1);
/// assert_eq!(Square::B2.backward(Color::Black), Square::B3);
/// ```
#[inline]
pub fn backward(&self, color: Color) -> Self {
match color {
Color::White => Square::make_square(self.file(), self.rank().down()),
Color::Black => Square::make_square(self.file(), self.rank().up()),
}
}
/// Go *n* [`Rank`] backward according to the side.
///
/// # Examples
///
/// ```
/// use chess::{Color, Square};
///
/// assert_eq!(Square::B4.n_backward(Color::White, 2), Square::B2);
/// assert_eq!(Square::B3.n_backward(Color::Black, 5), Square::B8);
/// ```
#[inline]
pub fn n_backward(&self, color: Color, n: usize) -> Self {
let mut square = *self;
for _ in 0..n {
square = square.backward(color);
}
square
}
/// Go one [`File`] to the right.
///
/// # Examples
///
/// ```
/// use chess::Square;
///
/// assert_eq!(Square::B2.right(), Square::C2);
/// ```
#[inline]
pub fn right(&self) -> Self {
Square::make_square(self.file().right(), self.rank())
}
/// Go *n* [`File`] to the right.
///
/// # Examples
///
/// ```
/// use chess::{Color, Square};
///
/// assert_eq!(Square::A4.n_right(3), Square::D4);
/// ```
#[inline]
pub fn n_right(&self, n: usize) -> Self {
let mut square = *self;
for _ in 0..n {
square = square.right();
}
square
}
/// Go one [`File`] right according to the side.
///
/// # Examples
///
/// ```
/// use chess::{Color, Square};
///
/// assert_eq!(Square::B2.right_for(Color::White), Square::C2);
/// assert_eq!(Square::B2.right_for(Color::Black), Square::A2);
/// ```
#[inline]
pub fn right_for(&self, color: Color) -> Self {
match color {
Color::White => Square::make_square(self.file().right(), self.rank()),
Color::Black => Square::make_square(self.file().left(), self.rank()),
}
}
/// Go *n* [`File`] to the right according to the side.
///
/// # Examples
///
/// ```
/// use chess::{Color, Square};
///
/// assert_eq!(Square::A4.n_right_for(Color::White, 3), Square::D4);
/// assert_eq!(Square::D4.n_right_for(Color::Black, 3), Square::A4);
/// ```
#[inline]
pub fn n_right_for(&self, color: Color, n: usize) -> Self {
let mut square = *self;
for _ in 0..n {
square = square.right_for(color);
}
square
}
/// Go one [`File`] to the left.
///
/// # Examples
///
/// ```
/// use chess::Square;
///
/// assert_eq!(Square::B2.left(), Square::A2);
/// ```
#[inline]
pub fn left(&self) -> Self {
Square::make_square(self.file().left(), self.rank())
}
/// Go *n* [`File`] to the left.
///
/// # Examples
///
/// ```
/// use chess::{Color, Square};
///
/// assert_eq!(Square::D4.n_left(3), Square::A4);
/// ```
#[inline]
pub fn n_left(&self, n: usize) -> Self {
let mut square = *self;
for _ in 0..n {
square = square.left();
}
square
}
/// Go one [`File`] left according to the side.
///
/// # Examples
///
/// ```
/// use chess::{Color, Square};
///
/// assert_eq!(Square::B2.left_for(Color::White), Square::A2);
/// assert_eq!(Square::B2.left_for(Color::Black), Square::C2);
/// ```
#[inline]
pub fn left_for(&self, color: Color) -> Self {
match color {
Color::White => Square::make_square(self.file().left(), self.rank()),
Color::Black => Square::make_square(self.file().right(), self.rank()),
}
}
/// Go *n* [`File`] to the left according to the side.
///
/// # Examples
///
/// ```
/// use chess::{Color, Square};
///
/// assert_eq!(Square::D4.n_left_for(Color::White, 3), Square::A4);
/// assert_eq!(Square::A4.n_left_for(Color::Black, 3), Square::D4);
/// ```
#[inline]
pub fn n_left_for(&self, color: Color, n: usize) -> Self {
let mut square = *self;
for _ in 0..n {
square = square.left_for(color);
}
square
}
/// Go one [`Square`] in the given direction.
///
/// # Examples
///
/// ```
/// use chess::{Direction, Square};
///
/// assert_eq!(Square::B2.follow_direction(Direction::Up), Square::B3);
/// assert_eq!(Square::B2.follow_direction(Direction::DownRight), Square::C1);
/// ```
#[inline]
pub fn follow_direction(&self, direction: Direction) -> Self {
match direction {
Direction::Up => self.up(),
Direction::UpRight => self.up().right(),
Direction::Right => self.right(),
Direction::DownRight => self.down().right(),
Direction::Down => self.down(),
Direction::DownLeft => self.down().left(),
Direction::Left => self.left(),
Direction::UpLeft => self.up().left(),
}
}
/// The distance between the two squares, i.e. the number of king steps
/// to get from one square to the other.
///
/// # Examples
///
/// ```
/// use chess::Square;
///
/// assert_eq!(Square::A2.distance(Square::B5), 3);
/// ```
pub fn distance(&self, other: Square) -> u32 {
max(
self.file().distance(other.file()),
self.rank().distance(other.rank()),
)
}
}
impl fmt::Display for Square {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"{}{}",
(b'a' + (self.file() as u8)) as char,
(b'1' + (self.rank() as u8)) as char
)
}
}
impl FromStr for Square {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
if s.len() < 2 {
return Err(Error::InvalidSquare);
}
let ch: Vec<char> = s.chars().collect();
match ch[0] {
'a' | 'b' | 'c' | 'd' | 'e' | 'f' | 'g' | 'h' => {}
_ => return Err(Error::InvalidSquare),
}
match ch[1] {
'1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' => {}
_ => return Err(Error::InvalidSquare),
}
Ok(Square::make_square(
File::new((ch[0] as usize) - ('a' as usize)),
Rank::new((ch[1] as usize) - ('1' as usize)),
))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_square() {
for file in (0..8).map(File::new) {
for rank in (0..8).map(Rank::new) {
let square = Square::make_square(file, rank);
assert_eq!(square.file(), file);
assert_eq!(square.rank(), rank);
}
}
}
}