-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move reversi move to general move
- Loading branch information
Showing
8 changed files
with
76 additions
and
58 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
pub mod util; | ||
|
||
pub mod chomp; | ||
pub mod domineering; | ||
pub mod nim; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod move_natural; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// TODO: make generic as some n-tuple move (macro generation / dynamic type?) | ||
|
||
use std::{fmt::Display, iter, str::FromStr}; | ||
|
||
use itertools::Itertools; | ||
|
||
#[derive(Clone, Debug, Copy, PartialEq)] | ||
pub struct NaturalMove<const LENGTH: usize>(pub [usize; LENGTH]); | ||
|
||
impl<const LENGTH: usize> FromStr for NaturalMove<LENGTH> { | ||
type Err = String; | ||
|
||
fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
assert!(LENGTH > 0, "Length must be greater than 0"); | ||
|
||
let numbers = s.split("-").collect::<Vec<_>>(); | ||
|
||
if numbers.len() != LENGTH { | ||
return Err( | ||
format!( | ||
"Must be {} numbers separated by a hyphen ({})", | ||
LENGTH, | ||
iter::repeat("x").take(LENGTH).join("-") | ||
) | ||
); | ||
} | ||
|
||
let numbers = numbers.iter() | ||
.map(|num| num.parse::<usize>()) | ||
.collect::<Vec<_>>(); | ||
|
||
if let Some((position, _)) = numbers.iter().find_position(|x| x.is_err()) { | ||
let ordinal = ordinal::Ordinal(position + 1).to_string(); | ||
|
||
return Err(format!("The {} number is not a number.", ordinal)); | ||
} | ||
|
||
numbers.iter().map(|x| x.clone().unwrap()).collect::<Vec<_>>().try_into() | ||
.map_err(|_| "Could not convert Vec to fixed array; this is a bug.".to_string()) | ||
.map(|x| NaturalMove(x)) | ||
} | ||
} | ||
|
||
impl<const LENGTH: usize> Display for NaturalMove<LENGTH> { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
write!(f, "{}", self.0.iter().join("-")) | ||
} | ||
} |