Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9852f30581 | ||
|
|
7283bf7ecf |
No files matched your search
@@ -4,3 +4,11 @@ version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
bevy = "0.20"
|
||||
|
||||
[profildev]
|
||||
opt-level = 1
|
||||
debug = true
|
||||
|
||||
[profile.dev.package."*"]
|
||||
opt-level = 3
|
||||
@@ -0,0 +1,39 @@
|
||||
use bevy::prelude::*;
|
||||
|
||||
pub struct Cli;
|
||||
|
||||
impl Plugin for Cli {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.add_systems(Startup, setup_scene);
|
||||
// app.add_systems(Update, my_system);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn setup_scene(
|
||||
mut commands: Commands,
|
||||
) {
|
||||
commands.spawn(Camera2d);
|
||||
|
||||
commands.spawn((
|
||||
Node {
|
||||
width: Val::Percent(100.0),
|
||||
height: Val::Percent(100.0),
|
||||
justify_content: JustifyContent::Center,
|
||||
align_items: AlignItems::Center,
|
||||
..default()
|
||||
},
|
||||
BackgroundColor(Color::srgb(0.1, 0.1, 0.1)),
|
||||
children![
|
||||
(
|
||||
Node {
|
||||
width: Val::VMin(90.0),
|
||||
height: Val::VMin(90.0),
|
||||
aspect_ratio: Some(1.0),
|
||||
..default()
|
||||
},
|
||||
BackgroundColor(Color::srgb(0.2, 0.6, 0.8)),
|
||||
)
|
||||
],
|
||||
));
|
||||
}
|
||||
@@ -1,76 +0,0 @@
|
||||
use std::io;
|
||||
|
||||
use crate::chess_api::Move;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum SquareInput {
|
||||
File(u8), // столбец 0..8 (a..h)
|
||||
Rank(u8), // строка 0..8 (1..8)
|
||||
Cell(u8), // конкретная клетка 0..64
|
||||
}
|
||||
|
||||
impl SquareInput {
|
||||
pub fn matches(&self, sq: u8) -> bool {
|
||||
match *self {
|
||||
SquareInput::File(f) => (sq % 8) == f,
|
||||
SquareInput::Rank(r) => (sq / 8) == r,
|
||||
SquareInput::Cell(c) => sq == c,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn read_square() -> SquareInput {
|
||||
loop {
|
||||
let mut s = String::new();
|
||||
io::stdin().read_line(&mut s).unwrap();
|
||||
let b = s.trim().to_lowercase().into_bytes();
|
||||
|
||||
match b.len() {
|
||||
1 => {
|
||||
if (b'a'..=b'h').contains(&b[0]) {
|
||||
return SquareInput::File(b[0] - b'a');
|
||||
}
|
||||
if (b'1'..=b'8').contains(&b[0]) {
|
||||
return SquareInput::Rank(b[0] - b'1');
|
||||
}
|
||||
}
|
||||
2 => {
|
||||
if (b'a'..=b'h').contains(&b[0]) && (b'1'..=b'8').contains(&b[1]) {
|
||||
return SquareInput::Cell((b[1] - b'1') * 8 + (b[0] - b'a'));
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
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
|
||||
}
|
||||
+19
-55
@@ -4,62 +4,26 @@ use chess_api::*;
|
||||
mod render_bitmask;
|
||||
use render_bitmask::*;
|
||||
|
||||
mod gaming;
|
||||
use gaming::*;
|
||||
use bevy::prelude::*;
|
||||
|
||||
mod game_client;
|
||||
|
||||
fn main() {
|
||||
|
||||
let mut game = Breakthrough::new();
|
||||
|
||||
loop {
|
||||
println!("---<>---");
|
||||
|
||||
let b = game.get_board();
|
||||
|
||||
render_board(
|
||||
BoardColor::Checkerboard('•', ' '),
|
||||
&[
|
||||
(b.get_mask(Color::White, 0), '●'),
|
||||
(b.get_mask(Color::Black, 0), '○'),
|
||||
],
|
||||
);
|
||||
|
||||
let status = game.status();
|
||||
println!("side: {:?}", game.side_to_move());
|
||||
if status != GameStatus::Ongoing {
|
||||
println!("game over: {:?}", status);
|
||||
break;
|
||||
}
|
||||
|
||||
let moves = game.legal_moves();
|
||||
|
||||
let to_input = read_square();
|
||||
let cand: Vec<Move> = moves.iter().filter(|m| to_input.matches(m.to)).copied().collect();
|
||||
let f_cand = unique_froms(&cand);
|
||||
|
||||
match f_cand.clone().as_slice() {
|
||||
[] => {
|
||||
println!("incorekt");
|
||||
}
|
||||
[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();
|
||||
}
|
||||
_ => {
|
||||
for i in &f_cand {
|
||||
print!("{}, ", sq_to_string(*i));
|
||||
}
|
||||
print!("\n");
|
||||
let f_input = read_square();
|
||||
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");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
App::new()
|
||||
.add_plugins(DefaultPlugins.set(WindowPlugin {
|
||||
primary_window: Some(Window {
|
||||
title: "local-chess".into(),
|
||||
resolution: (800, 800).into(),
|
||||
resizable: true,
|
||||
..default()
|
||||
}),
|
||||
..default()
|
||||
}))
|
||||
.add_plugins(game_client::Cli)
|
||||
.add_systems(Startup, hello_world_system)
|
||||
.run();
|
||||
}
|
||||
|
||||
fn hello_world_system() {
|
||||
println!("Hello, World!");
|
||||
}
|
||||
Reference in new issue
Block a user