final predictiv system

This commit is contained in:
vova-rs committed 2026-10-15 23:13:45 +03:00
1 parent 27fd785034
commit 03c09101fe
2 files changed
+53 -17

No files matched your search

+32
View File
@@ -1,5 +1,7 @@
use std::io; use std::io;
use crate::chess_api::Move;
#[derive(Debug, Clone, Copy, PartialEq, Eq)] #[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SquareInput { pub enum SquareInput {
File(u8), // столбец 0..8 (a..h) File(u8), // столбец 0..8 (a..h)
@@ -42,3 +44,33 @@ pub fn read_square() -> SquareInput {
println!("не подходит, ещё раз:"); println!("не подходит, ещё раз:");
} }
} }
pub fn sq_to_string(sq: u8) -> String {
let file = (sq % 8) as u8 + b'a';
let rank = (sq / 8) as u8 + b'1';
format!("{}{}", file as char, rank as char)
}
pub fn has_unique_from(items: &[Move]) -> bool {
let mut froms: Vec<u8> = items.iter().map(|x| x.from).collect();
froms.sort_unstable();
froms.windows(2).any(|w| w[0] != w[1])
}
pub fn unique_froms(items: &[Move]) -> Vec<u8> {
let mut froms: Vec<u8> = items.iter().map(|x| x.from).collect();
froms.sort_unstable();
let mut result = Vec::new();
let n = froms.len();
let mut i = 0;
while i < n {
let mut j = i + 1;
while j < n && froms[j] == froms[i] { j += 1; }
if j - i == 1 {
result.push(froms[i]);
}
i = j;
}
result
}
+21 -17
View File
@@ -5,7 +5,7 @@ mod render_bitmask;
use render_bitmask::*; use render_bitmask::*;
mod gaming; mod gaming;
use gaming::read_square; use gaming::*;
fn main() { fn main() {
@@ -34,28 +34,32 @@ fn main() {
let moves = game.legal_moves(); let moves = game.legal_moves();
let to_input = read_square(); let to_input = read_square();
let candidates: Vec<Move> = moves.iter().filter(|m| to_input.matches(m.to)).copied().collect(); let cand: Vec<Move> = moves.iter().filter(|m| to_input.matches(m.to)).copied().collect();
let f_cand = unique_froms(&cand);
let chosen = match candidates.len() { match f_cand.clone().as_slice() {
0 => { [] => {
println!("no legal move to that square"); println!("incorekt");
continue; }
[f_input] => {
let s: Vec<Move> = moves.iter().filter(|m| to_input.matches(m.to) && m.from == *f_input).copied().collect();
game.apply(s[0]).unwrap();
} }
1 => candidates[0],
_ => { _ => {
let from_input = read_square(); for i in &f_cand {
let exact: Vec<Move> = candidates.iter().filter(|m| from_input.matches(m.from)).copied().collect(); print!("{}, ", sq_to_string(*i));
match exact.len() { }
0 => { print!("\n");
println!("no legal move from that square"); let f_input = read_square();
continue; let k: Vec<_> = f_cand.iter().filter(|m| f_input.matches(**m)).copied().collect();
if k.len() == 1 {
let s: Vec<Move> = moves.iter().filter(|m| to_input.matches(m.to) && m.from == k[0]).copied().collect();
game.apply(s[0]).unwrap();
} else {
println!("incorekt2");
} }
_ => exact[0],
} }
} }
};
game.apply(chosen).unwrap();
} }
} }