alias result
This commit is contained in:
parent
ea962dc8cf
commit
23547f1626
26
src/lib.rs
26
src/lib.rs
|
@ -13,6 +13,8 @@ pub enum Error {
|
|||
BadMove(Pos),
|
||||
}
|
||||
|
||||
type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
#[derive(PartialEq, Copy, Clone)]
|
||||
enum Direction {
|
||||
U,
|
||||
|
@ -129,7 +131,7 @@ impl std::fmt::Display for Level {
|
|||
}
|
||||
|
||||
impl Level {
|
||||
fn read<R: std::io::BufRead>(file: R) -> Result<Self, ReadError> {
|
||||
fn read<R: std::io::BufRead>(file: R) -> std::result::Result<Self, ReadError> {
|
||||
let (mut player, mut size) = (Pos::default(), Pos::default());
|
||||
|
||||
let map = file
|
||||
|
@ -155,13 +157,13 @@ impl Level {
|
|||
'.' => Ok(Cell::None),
|
||||
_ => Err(ReadError::BadSymbol),
|
||||
})
|
||||
.collect::<Vec<Result<_, _>>>()
|
||||
.collect::<Vec<std::result::Result<_, _>>>()
|
||||
})
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
.collect::<std::result::Result<Vec<_>, _>>()?;
|
||||
Ok(Self { size, player, map })
|
||||
}
|
||||
|
||||
fn read_rle(map: String) -> Result<Self, ReadError> {
|
||||
fn read_rle(map: String) -> std::result::Result<Self, ReadError> {
|
||||
let mut n: usize = 1;
|
||||
|
||||
Self::read(
|
||||
|
@ -199,7 +201,7 @@ impl State {
|
|||
};
|
||||
}
|
||||
|
||||
fn move_pos(&mut self, pos: Pos) -> Result<(bool, bool), Error> {
|
||||
fn move_pos(&mut self, pos: Pos) -> Result<(bool, bool)> {
|
||||
let dpos = self.lvl.player + pos;
|
||||
|
||||
if ((dpos.1 * self.lvl.size.0) + dpos.0) as usize >= self.lvl.map.len() {
|
||||
|
@ -233,7 +235,7 @@ impl State {
|
|||
Ok((false, false))
|
||||
}
|
||||
|
||||
fn move_dir(&mut self, dir: Direction) -> Result<(bool, bool), Error> {
|
||||
fn move_dir(&mut self, dir: Direction) -> Result<(bool, bool)> {
|
||||
match dir {
|
||||
Direction::U => self.move_pos(Pos(0, -1)),
|
||||
Direction::D => self.move_pos(Pos(0, 1)),
|
||||
|
@ -369,7 +371,7 @@ impl Agent {
|
|||
}
|
||||
}
|
||||
|
||||
fn move_dna(&mut self) -> Result<Solution, Error> {
|
||||
fn move_dna(&mut self) -> Result<Solution> {
|
||||
if self.dir == self.dna.len() || self.goal == Solution::None {
|
||||
Ok(Solution::None)
|
||||
} else {
|
||||
|
@ -388,7 +390,7 @@ impl Agent {
|
|||
}
|
||||
}
|
||||
|
||||
fn evaluate(&mut self) -> Result<(), Error> {
|
||||
fn evaluate(&mut self) -> Result<()> {
|
||||
loop {
|
||||
let player = self.state.lvl.player;
|
||||
|
||||
|
@ -464,11 +466,11 @@ impl Population {
|
|||
}
|
||||
}
|
||||
|
||||
fn step(&mut self) -> Result<(), Error> {
|
||||
fn step(&mut self) -> Result<()> {
|
||||
self.life
|
||||
.par_iter_mut()
|
||||
.map(|a| a.evaluate())
|
||||
.collect::<Result<Vec<()>, _>>()?;
|
||||
.collect::<Result<Vec<()>>>()?;
|
||||
self.life.par_sort_by(|a, b| b.score.total_cmp(&a.score));
|
||||
|
||||
if self.life[0].goal != Solution::Win {
|
||||
|
@ -508,7 +510,7 @@ impl Population {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn run(config: Config, rle: Option<String>) -> Result<(), Error> {
|
||||
pub fn run(config: Config, rle: Option<String>) -> Result<()> {
|
||||
rayon::ThreadPoolBuilder::new()
|
||||
.num_threads(config.num_threads)
|
||||
.build_global()
|
||||
|
@ -538,7 +540,7 @@ pub fn run(config: Config, rle: Option<String>) -> Result<(), Error> {
|
|||
for _ in 0..config.gen_depth {
|
||||
let mut pop = Population::new(&config, map.clone());
|
||||
|
||||
if let Some(agent) = || -> Result<Option<Agent>, Error> {
|
||||
if let Some(agent) = || -> Result<Option<Agent>> {
|
||||
while pop.gen < config.gen_max {
|
||||
pop.step()?;
|
||||
|
||||
|
|
Loading…
Reference in New Issue