Skip to content

Commit

Permalink
Fix player::from_str
Browse files Browse the repository at this point in the history
  • Loading branch information
Terkwood committed Mar 14, 2020
1 parent 4cc7371 commit 1a203ec
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
17 changes: 15 additions & 2 deletions micro-model/moves/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ pub enum Player {

impl Player {
pub fn from_str(s: &str) -> Player {
let mut trimmed = s.trim().to_ascii_lowercase();
if trimmed.pop() == Some('w') {
let trimmed = s.trim().to_ascii_lowercase();
if trimmed.chars().next() == Some('w') {
Player::WHITE
} else {
Player::BLACK
Expand Down Expand Up @@ -155,4 +155,17 @@ mod tests {
let back = GameState::from(&bytes).unwrap();
assert_eq!(back, gs);
}

#[test]
fn player_from_string() {
assert_eq!(Player::from_str("WHITE"), Player::WHITE);
assert_eq!(Player::from_str("BLACK"), Player::BLACK);
assert_eq!(Player::from_str("W"), Player::WHITE);
assert_eq!(Player::from_str("B"), Player::BLACK);
assert_eq!(Player::from_str("white"), Player::WHITE);
assert_eq!(Player::from_str("black"), Player::BLACK);
assert_eq!(Player::from_str("w"), Player::WHITE);
assert_eq!(Player::from_str("b"), Player::BLACK);
assert_eq!(Player::from_str(""), Player::BLACK);
}
}
6 changes: 6 additions & 0 deletions tinybrain/src/err.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,15 @@ impl From<std::num::ParseIntError> for CoordOutOfRange {
pub enum KataGoParseErr {
UuidErr(uuid::Error),
WrongFormat,
Coord,
}
impl From<uuid::Error> for KataGoParseErr {
fn from(u: uuid::Error) -> Self {
KataGoParseErr::UuidErr(u)
}
}
impl From<CoordOutOfRange> for KataGoParseErr {
fn from(_: CoordOutOfRange) -> Self {
KataGoParseErr::Coord
}
}
19 changes: 11 additions & 8 deletions tinybrain/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl MoveComputed {
pub fn from(response: KataGoResponse) -> Result<Self, err::KataGoParseErr> {
let game_id = response.game_id()?;
let player = response.player()?;
let coord: Option<Coord> = todo!("figure out the reverse conversion");
let coord: Option<Coord> = Some(from_alphanum(&response.move_infos[0].r#move)?);
let req_id = ReqId(Uuid::new_v4());
Ok(MoveComputed(MakeMoveCommand {
game_id,
Expand All @@ -51,13 +51,16 @@ fn from_alphanum(a: &str) -> Result<Coord, err::CoordOutOfRange> {
let letter: char = a.chars().collect::<Vec<char>>()[0];
let number = &a[1..];
let y_plus_one = number.to_string().parse::<u16>()?;
let r = (b'A'..=b'Z');
todo!();

Ok(Coord {
x: todo!(),
y: y_plus_one - 1,
})
let r: Vec<char> = (b'A'..=b'Z').map(char::from).collect();
let maybe_x = r.iter().position(|l| l == &letter);
if let Some(x) = maybe_x {
Ok(Coord {
x: x as u16,
y: y_plus_one - 1,
})
} else {
Err(err::CoordOutOfRange)
}
}
}

Expand Down

0 comments on commit 1a203ec

Please sign in to comment.