Beispiel #1
0
        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();
                    }
                }
            }
        }
Beispiel #2
0
        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();
            }
        }