diff options
| author | tslil clingman <> | 2020-01-26 20:57:55 -0800 |
|---|---|---|
| committer | tslil clingman <> | 2020-01-26 20:57:55 -0800 |
| commit | 3a1c20ebba57632392443a050fc10d44e3048843 (patch) | |
| tree | 3f70bcbc43a40d64f58f8454e92d6da6bac7b3f7 /src/consulter.rs | |
| parent | 1930b067982a5ee324cd2832ace295e67603d2dc (diff) | |
Playing against the computer actually works!
Diffstat (limited to 'src/consulter.rs')
| -rw-r--r-- | src/consulter.rs | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/src/consulter.rs b/src/consulter.rs new file mode 100644 index 0000000..1f27124 --- /dev/null +++ b/src/consulter.rs @@ -0,0 +1,24 @@ +use std::fs::File; +use std::io::{Error, ErrorKind, Result, Write}; +use std::process::Command; + +use crate::game::*; +use crate::parser::*; + +const FILE_NAME: &str = "/tmp/takwrap_ptn_consult.ptn"; + +pub fn consult_next_action(game: &Game, proc: &str) -> Result<Action> { + let file = File::create(FILE_NAME)?; + + write!(&file, "{}", game)?; + + let output = Command::new(proc).args(&[FILE_NAME]).output()?; + + match String::from_utf8(output.stdout) { + Err(e) => Err(Error::new(ErrorKind::InvalidData, e)), + Ok(string) => match parse_action(game, &string) { + Err(string) => Err(Error::new(ErrorKind::InvalidData, string)), + Ok(action) => Ok(action), + }, + } +} |
