aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authoreug-vs <eugene@eug-vs.xyz>2023-01-28 00:22:44 +0300
committereug-vs <eugene@eug-vs.xyz>2023-01-28 00:22:44 +0300
commit690af97aa6c49a8ad76e47a2cee0c0b23b84ae6e (patch)
tree8ea802d97031dd86827117b86a5cc40ff3285090 /src
parentdbf3b8649c45aca6b80d9659363d3e878d54f4ef (diff)
downloadchessnost-690af97aa6c49a8ad76e47a2cee0c0b23b84ae6e.tar.gz
feat: implement read_move
Diffstat (limited to 'src')
-rw-r--r--src/board/mod.rs18
-rw-r--r--src/square.rs32
2 files changed, 50 insertions, 0 deletions
diff --git a/src/board/mod.rs b/src/board/mod.rs
index 06b61bb..ce8c076 100644
--- a/src/board/mod.rs
+++ b/src/board/mod.rs
@@ -1,3 +1,5 @@
+use std::io::stdin;
+
use rand::{rngs::StdRng,SeedableRng,Rng};
use crate::{bitboard::{Bitboard, serialize_bitboard, bitscan, pop_count}, moves::{Move, MoveKind}, attacks::Attacks, square::Square};
@@ -146,6 +148,22 @@ impl Board {
Self::from_FEN(default_fen)
}
+ pub fn read_move(&self) -> Move {
+ let mut s = String::new();
+ stdin().read_line(&mut s).unwrap();
+ let chars = &mut s.chars();
+ let source = Square::from_notation(chars);
+ let target = Square::from_notation(chars);
+
+ let moves = self.generate_pseudolegal_moves(self.color());
+
+ let mov = match moves.iter().find(|m| m.source == source && m.target == target) {
+ Some(m) => *m,
+ None => panic!("Move is not valid"),
+ };
+ mov
+ }
+
/// Color to move at this ply
pub fn color(&self) -> Color {
Color::from(self.ply as u8 % 2)
diff --git a/src/square.rs b/src/square.rs
index efa682c..f26ed4d 100644
--- a/src/square.rs
+++ b/src/square.rs
@@ -1,3 +1,5 @@
+use std::str::Chars;
+
use crate::bitboard::Bitboard;
/// Aliases to board square indexes
@@ -49,6 +51,36 @@ impl Square {
pub fn east_one(&self) -> Self {
Self::from(*self as u8 + 1)
}
+
+ pub fn from_notation(chars: &mut Chars) -> Self {
+ let file = match chars.next() {
+ Some(ch) => {
+ match ch {
+ 'a' => 0,
+ 'b' => 1,
+ 'c' => 2,
+ 'd' => 3,
+ 'e' => 4,
+ 'f' => 5,
+ 'g' => 6,
+ 'h' => 7,
+ _ => panic!("Incorrect file!")
+ }
+ },
+ None => panic!("Missing file"),
+ };
+ let rank = match chars.next() {
+ Some(ch) => {
+ match ch.to_digit(10) {
+ Some(digit) => digit - 1,
+ None => panic!("Incorrect rank")
+ }
+ }
+ None => panic!("Missing rank")
+ };
+
+ Self::from_coords(rank as u8, file)
+ }
}