From 3a1c20ebba57632392443a050fc10d44e3048843 Mon Sep 17 00:00:00 2001 From: tslil clingman <> Date: Sun, 26 Jan 2020 20:57:55 -0800 Subject: Playing against the computer actually works! --- src/consulter.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 src/consulter.rs (limited to 'src/consulter.rs') 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 { + 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), + }, + } +} -- cgit v1.3.1