late night grind: implementing cmdargs feature, fixing Args and Config, adding jobs field and more...
All checks were successful
CI / Saltfish (push) Successful in -3m1s
CI / Saltfish [custom_pieces] (push) Successful in -3m1s

This commit is contained in:
fabolous005 2024-04-04 06:01:52 +02:00
parent 54f0b38252
commit 32a3c9fb12
9 changed files with 126 additions and 34 deletions

View File

@ -4,10 +4,13 @@ version = "0.1.0"
edition = "2021" edition = "2021"
[features] [features]
default = ["cmdargs"]
custom_pieces = [] custom_pieces = []
cmdargs = ["dep:clap"]
[dependencies] [dependencies]
toml = { version = "0.8.12" } toml = { version = "0.8.12" }
clap = { version = "4.5.4", features = ["derive"] } clap = { version = "4.5.4", features = ["derive"], optional = true }
serde = { version = "1.0.197", features = ["derive"] } serde = { version = "1.0.197", features = ["derive"] }
serde_derive = { version = "1.0.197" } serde_derive = { version = "1.0.197" }
num_cpus = "1.16.0"

View File

@ -1,10 +1,43 @@
[piece.turtle] [piece.turtle]
change = [[1, 2], [2, 1]] change = [
[
3,
2,
],
[
3,
-2,
],
[
2,
-3,
],
[
2,
3,
],
[
-2,
-3,
],
[
-2,
3,
],
[
-3,
-2,
],
[
-3,
2,
],
]
# INFO: when one: number of must-moves # INFO: when one: number of must-moves
# if 0 infinite # if 0 infinite
# INFO: when two: [0] = min, [1] = max # INFO: when two: [0] = min, [1] = max
iterable = [1, 2] iterable = [0]
[engine] [engine]
depth = 23 depth = 23

View File

@ -1,6 +1,5 @@
use super::{piece::PieceVariant, square::Square}; use super::{piece::PieceVariant, square::Square};
use serde::Serialize;
#[cfg(feature = "custom_pieces")] #[cfg(feature = "custom_pieces")]
use serde_derive::Deserialize; use serde_derive::Deserialize;

View File

@ -1,6 +1,4 @@
use super::moves::Change;
use serde::Serialize;
#[cfg(feature = "custom_pieces")] #[cfg(feature = "custom_pieces")]
use serde_derive::Deserialize; use serde_derive::Deserialize;
@ -38,13 +36,13 @@ impl Default for Iterability {
#[cfg(feature = "custom_pieces")] #[cfg(feature = "custom_pieces")]
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
pub struct CustomPiece { pub struct CustomPiece {
change: Vec<[u8; 2]>, change: Vec<[i8; 2]>,
iterable: Vec<u8> iterable: Vec<u8>
} }
#[cfg(feature = "custom_pieces")] #[cfg(feature = "custom_pieces")]
impl CustomPiece { impl CustomPiece {
pub fn new(change: Vec<[u8; 2]>, iterable: Vec<u8>) -> Self { pub fn new(change: Vec<[i8; 2]>, iterable: Vec<u8>) -> Self {
Self { change, iterable } Self { change, iterable }
} }
} }

View File

@ -1,9 +1,6 @@
use crate::chess::piece::Piece; use crate::chess::piece::Piece;
use crate::chess::square::Square; use crate::chess::square::Square;
use crate::chess::moves::Move; use crate::chess::moves::Move;
use crate::chess::moves::MoveVariant;
use super::moves::Change;
pub struct Castling { pub struct Castling {
@ -71,7 +68,7 @@ impl Position {
Position::default() Position::default()
} }
pub fn from_fen(fen: String) -> Self { pub fn from_fen(_fen: String) -> Self {
todo!("Implement this function") todo!("Implement this function")
} }

View File

@ -1,7 +1,42 @@
use std::path::PathBuf;
#[derive(clap::Parser)] #[derive(clap::Parser)]
struct Args { pub struct Args {
#[arg(short, long, default_value = "config.toml")] #[arg(short, long="config", default_value = "config.toml")]
config_path: std::path::PathBuf, config_path: PathBuf,
depth: Option<u32>
#[arg(short, long, default_value_t = 20)]
depth: u8,
#[arg(short, long, default_value_t = 100)]
strenght: u8,
#[arg(short, long, default_value_t = {num_cpus::get() as u8})]
jobs: u8,
}
impl Default for Args {
fn default() -> Self {
Self { config_path: "config.toml".into(), depth: 20, strenght: 100, jobs: num_cpus::get() as u8 }
}
}
impl Args {
pub fn get_config_path(self) -> PathBuf {
self.config_path
}
pub fn get_depth(self) -> u8 {
self.depth
}
pub fn get_strenght(self) -> u8 {
self.strenght
}
pub fn get_jobs(self) -> u8 {
self.jobs
}
} }

View File

@ -1,43 +1,46 @@
use std::{collections::HashMap, fs::File, io::Read, path::Path}; use std::{fs::File, io::Read, path::Path};
use crate::chess::moves::Change;
use serde::Deserialize; use serde::Deserialize;
#[cfg(feature = "custom_pieces")] #[cfg(feature = "custom_pieces")]
use crate::chess::piece::{CustomPiece, Iterability}; use crate::chess::piece::CustomPiece;
#[cfg(feature = "custom_pieces")]
use std::collections::HashMap;
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
struct Engine { struct Engine {
depth: u32 depth: Option<u8>,
jobs: Option<u8>,
} }
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
#[serde(default)] #[serde(default)]
pub struct Config { pub struct Config {
// #[serde(rename = "piece")]
#[cfg(feature = "custom_pieces")] #[cfg(feature = "custom_pieces")]
piece: HashMap<String, CustomPiece>, #[serde(rename = "piece")]
pieces: Option<HashMap<String, CustomPiece>>,
engine: Engine engine: Option<Engine>
} }
impl Default for Config { impl Default for Config {
fn default() -> Self { fn default() -> Self {
Self { Self {
#[cfg(feature = "custom_pieces")] #[cfg(feature = "custom_pieces")]
piece: HashMap::from( pieces: None,
[("".to_string(), CustomPiece::new(vec![[0, 0]], vec![0]))]
), engine: Some(Engine {
depth: Some(20),
jobs: Some(num_cpus::get() as u8),
}),
engine: Engine {
depth: 20
}
} }
} }
} }
impl Config { impl Config {
pub fn from_file(path: &Path) -> Config { pub fn read(path: &Path) -> Config {
let mut file = File::open(path).expect("Failed to read config file"); let mut file = File::open(path).expect("Failed to read config file");
let mut config_string = String::new(); let mut config_string = String::new();
file.read_to_string(&mut config_string).expect("Failed to parse file to string"); file.read_to_string(&mut config_string).expect("Failed to parse file to string");

View File

@ -1,2 +1,4 @@
pub mod config; pub mod config;
#[cfg(feature = "cmdargs")]
pub mod args; pub mod args;

View File

@ -1,15 +1,37 @@
#![allow(dead_code)] #![allow(
dead_code,
clippy::module_inception
)]
use chess::position::Position;
use std::path::Path;
use config::config::Config; use config::config::Config;
#[cfg(feature = "cmdargs")]
use config::args::Args;
#[cfg(feature = "cmdargs")]
use clap::Parser;
#[cfg(not(feature = "cmdargs"))]
use std::path::Path;
mod chess; mod chess;
mod config; mod config;
fn main() { fn main() {
let config = Config::from_file(Path::new("./config.toml")); let config;
#[cfg(feature = "cmdargs")]
{
// TODO: implement error handling and logging and handle this case appropriate
let args = Args::try_parse().unwrap_or_else(|err|{println!("error parsing cmd args\n{:#?}", err); Args::default()});
config = Config::read(&args.get_config_path());
}
#[cfg(not(feature = "cmdargs"))]
{
config = Config::read(Path::new("./config.toml"));
}
println!("{config:#?}"); println!("{config:#?}");
} }