public ConstantSet <ValidWord> FindLongest(Rack rack) { return(FindLongest(rack.Value)); }
public PlayGraph(WordGraph graph, Board board, Rack rack) : this(new CtorHelper(graph, board, rack)) { }
public void Play() { var board = new Board(); var rack = new Rack(graph.GetRandom()); Console.WriteLine("rack: " + rack.Value); WordPart part = GetFirstWord(rack); Console.WriteLine("word: {0}", part); board = board.Play(part); var letters = new List <char>(rack.Letters); foreach (char c in part.Word) { letters.Remove(c); } board.Write(); while (!board.Score.Other.Wins) { var time = new Chrono(); Console.Write("------------------------------\n"); rack = new Rack(graph.GetRandom(new string(letters.ToArray()))); Console.WriteLine("rack: " + rack.Value); if (board.IsEmpty) { part = GetFirstWord(rack); Console.WriteLine("word: {0}", part); board = board.Play(part); letters = new List <char>(rack.Letters); foreach (char c in part.Word) { letters.Remove(c); } board.Write(); } else { var chrono = new Chrono(); PlayGraph play = GetPlayGraph(board, rack); double seconds = .1 * Convert.ToInt32(chrono.Elapsed.TotalSeconds * 10); Console.WriteLine("{0} moves in {1} s", play.Valids.Count, seconds); chrono = new Chrono(); var moveFinder = new MoveFinder(graph, board, play); Tuple <PlayInfo, PlayPath> best = moveFinder.GetBestMove(); seconds = .1 * Convert.ToInt32(chrono.Elapsed.TotalSeconds * 10); Console.WriteLine("Analyzed {0} moves in {1} s", play.Valids.Count, seconds); Console.WriteLine(); { PlayInfo info = best.Item1; PlayPath path = best.Item2; path.Write(); board = board.Play(path); board.Write(); if (info.HasVortex) { board = board.Clear(); letters.Clear(); } else { letters = new List <char>(rack.Letters); foreach (LetterPlay lp in path.Played) { letters.Remove(lp.Letter); } } } if (time.Elapsed.TotalSeconds > 40) { throw new TimeoutException(); } } } }
private PlayGraph GetPlayGraph(Board board, Rack rack) { return(new PlayGraph(graph, board, rack)); }
public void Run() { Rack rack = null; while (true) { try { bool skipped = false; var pending = new List <char>(); while (!board.Score.Other.Wins) { board.Write(); Console.WriteLine(); if (!skipped) { rack = ReadRack(pending, board.Current); } skipped = false; PlayPath move = ReadMove(board, rack); if (move == null) { board = board.Skip(); skipped = true; continue; } using (PlayerScore.GetCurrentColor(board.Current)) move.Write(); board = board.Play(move); pending.Clear(); pending.AddRange(move.Pending); bool vortex = false; foreach (LetterPlay lp in move.Played) { vortex |= lp.Cell.IsVortex; } if (vortex) { board.Write(); Console.WriteLine(); board = board.Clear(); pending.Clear(); } } board.Write(); rack = null; using (new DisposableColor(PlayerScore.GetColor(board.Other))) Console.WriteLine("{0} wins", board.Other); Console.WriteLine(); } catch (GiveUpException) { if (board.IsEmpty && rack == null) { return; } rack = null; } board = new Board(); } }
private PlayPath ReadMove(Board board, Rack rack) { var regex = new Regex(@"^\s*(?<word>[a-z]+)\s*(?<row>\d),(?<column>\d)\s*(?<direction>(down|right|))\s*$", RegexOptions.IgnoreCase); using (DisposableColor.Prompt) Console.Write("move? "); Console.Write("[word r,c down|right] [play|guess|skip] "); while (true) { try { string line = null; using (new DisposableColor(PlayerScore.GetColor(board.Current))) line = ReadLine().Trim(); if (line == "skip") { return(null); } if (line == "play" || line == "guess") { if (board.IsEmpty) { var game = new Game(random, graph); WordPart part = game.GetFirstWord(rack); Console.WriteLine(part); Console.WriteLine(); var played = part.GetPlayed(); var pending = new List <char>(rack.Letters); foreach (LetterPlay lp in played) { pending.Remove(lp.Letter); } var path = new PlayPath(part, new WordPartCollection(), part.GetPlayed(), pending.ToConstant()); if (line == "play") { return(path); } } else { PlayPath path = FindMove(rack); if (line == "play") { return(path); } } } Match m = regex.Match(line); if (m.Success) { string word = m.Groups["word"].Value; int row = int.Parse(m.Groups["row"].Value, CultureInfo.InvariantCulture); int column = int.Parse(m.Groups["column"].Value, CultureInfo.InvariantCulture); Direction?direction = Parse(m.Groups["direction"].Value); PlayPath path = GetPath(board, rack, word, row, column, direction); if (path != null) { Console.WriteLine(); return(path); } } Console.Write("? "); } catch (FormatException e) { var color = Console.ForegroundColor; Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("{0} : {1}", e.GetType().Name, e.Message); Console.ForegroundColor = color; Console.Write("? "); } } }
public void TestCheck(string value) { Assert.IsFalse(Rack.Check(value)); }