From 6d63ac4424907c9f4f259155dd6b75997dfa5742 Mon Sep 17 00:00:00 2001 From: Julien Rische Date: Wed, 10 Aug 2016 14:47:35 +0200 Subject: [PATCH] search winning paths --- src/main.rs | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 62 insertions(+), 8 deletions(-) diff --git a/src/main.rs b/src/main.rs index d04b02a..19b08b8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -202,6 +202,37 @@ impl Tree { }), } } + + fn only_winning(self, sign: Sign) -> Option { + match self.content { + Content::Node(Node { game: g, rest: r }) => { + let mut rest = Vec::new();/*r.iter().filter_map(|t| + t.only_winning(sign) + ).collect::>();*/ + for t in r { + if let Some(wt) = t.only_winning(sign) { + rest.push(wt); + } + } + if rest.len() > 0 { + Some(Tree { + id: self.id, + content: Content::Node(Node { + game: g, + rest: rest, + }), + }) + } else { + None + } + }, + Content::Leaf(s) => if s == sign { + Some(self) + } else { + None + }, + } + } } fn main() { @@ -219,7 +250,7 @@ fn main() { default_max_id }; - let max_depth = if args.len() == 3 { + let max_depth = if args.len() >= 3 { match args[2].parse() { Ok(v) => v, Err(_) => default_max_depth, @@ -228,14 +259,37 @@ fn main() { default_max_depth }; - if args.len() > 3 { + let ws = if args.len() == 4 { + match args[3].as_ref() { + "X" => Some(Sign::Cross), + "x" => Some(Sign::Cross), + "O" => Some(Sign::Circle), + "o" => Some(Sign::Circle), + "_" => Some(Sign::None), + _ => None, + } + } else { + None + }; + + if args.len() > 4 { println!("{}: error: too many arguments", args[0]); } else { - println!("digraph G {{"); - println!(" graph[fontname=\"Courier New\"];"); - println!(" node[fontname=\"Courier New\"];"); - println!(" edge[fontname=\"Courier New\"];"); - print!("{}", Tree::new(Game::new(), &mut 0, max_id, 0, max_depth)); - println!("}}"); + let t = Tree::new(Game::new(), &mut 0, max_id, 0, max_depth); + let wt = match ws { + Some(s) => t.only_winning(s), + None => Some(t), + }; + match wt { + Some(tree) => { + println!("digraph G {{"); + println!(" graph[fontname=\"Courier New\"];"); + println!(" node[fontname=\"Courier New\"];"); + println!(" edge[fontname=\"Courier New\"];"); + print!("{}", tree); + println!("}}"); + }, + None => println!("no result"), + } } } -- 2.11.4.GIT