First commit

This commit is contained in:
Timothy Warren 2022-07-13 15:29:52 -04:00
commit 24c686c52d
5 changed files with 171 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

7
Cargo.lock generated Normal file
View File

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "chess"
version = "0.1.0"

8
Cargo.toml Normal file
View File

@ -0,0 +1,8 @@
[package]
name = "chess"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

80
src/main.rs Normal file
View File

@ -0,0 +1,80 @@
mod piece;
enum MoveType {
VerticalOne,
VerticalTwo,
VerticalMany,
HorizontalMany,
}
#[derive(Debug, Copy, Clone, PartialEq)]
enum BoardPosition {
A1,
A2,
A3,
A4,
A5,
A6,
A7,
A8,
B1,
B2,
B3,
B4,
B5,
B6,
B7,
B8,
C1,
C2,
C3,
C4,
C5,
C6,
C7,
C8,
D1,
D2,
D3,
D4,
D5,
D6,
D7,
D8,
E1,
E2,
E3,
E4,
E5,
E6,
E7,
E8,
F1,
F2,
F3,
F4,
F5,
F6,
F7,
F8,
G1,
G2,
G3,
G4,
G5,
G6,
G7,
G8,
H1,
H2,
H3,
H4,
H5,
H6,
H7,
H8,
}
fn main() {
println!("Hello, world!");
}

75
src/piece.rs Normal file
View File

@ -0,0 +1,75 @@
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum PieceColor {
Black,
White,
}
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum BishopType {
Dark,
Light,
}
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum PieceType {
Pawn { promoted: bool },
Rook,
Knight,
King,
Queen,
Bishop { kind: BishopType },
}
#[derive(Debug, PartialEq)]
pub struct Piece {
kind: PieceType,
color: PieceColor,
symbol: char,
}
impl Piece {
pub fn new(color: PieceColor, kind: PieceType) -> Self {
Self {
kind,
color,
symbol: Self::get_symbol(color, kind),
}
}
fn get_symbol(color: PieceColor, kind: PieceType) -> char {
use PieceColor::*;
use PieceType::*;
match (color, kind) {
(White, Pawn { promoted: _ }) => '♙',
(White, Knight) => '♘',
(White, Bishop { kind: _ }) => '♗',
(White, Rook) => '♖',
(White, Queen) => '♕',
(White, King) => '♔',
(Black, Pawn { promoted: _ }) => '♟',
(Black, Knight) => '♞',
(Black, Bishop { kind: _ }) => '♝',
(Black, Rook) => '♜',
(Black, Queen) => '♛',
(Black, King) => '♚',
}
}
}
mod test {
use super::*;
#[test]
fn test_make_black_rook() {
let piece = Piece::new(PieceColor::Black, PieceType::Rook);
assert_eq!(piece.symbol, '♜');
assert_eq!(piece.kind, PieceType::Rook);
assert_eq!(piece.color, PieceColor::Black);
}
#[test]
fn test_get_white_knight() {
assert_eq!(Piece::get_symbol(PieceColor::White, PieceType::Knight), '♘');
}
}