Пример #1
0
        public ProbableMove MiniMaxRoot(int depth, Game game, bool isMaximisingPlayer)
        {
            var          _game     = game.Clone();
            var          bestScore = -9999;
            ProbableMove bestMove  = null;


            var moves = Probables(_game);

            for (var i = 0; i < moves.Count; i++)
            {
                var move = moves[i];

                _game.Apply(move, isMaximisingPlayer);

                //Debug.WriteLine(depth + " " + i + " " + _game.Scores.MyScore + " " + _game.Scores.OppScore + " " + isMaximisingPlayer);

                var score = MiniMax(depth - 1, _game, -10000, 10000, !isMaximisingPlayer);
                _game = game.Clone();

                if (score >= bestScore)
                {
                    bestScore = score;
                    bestMove  = move;
                }
            }

            return(bestMove);
        }
Пример #2
0
        public ProbableMove MiniMaxRoot(int depth, ScrabbleGame game, bool isMaximisingPlayer)
        {
            var          _game     = game.Clone();
            var          bestScore = -9999;
            ProbableMove bestMove  = null;

            var moves = Probables(_game);

            for (var i = 0; i < moves.Count; i++)
            {
                var move = moves[i];

                Apply(_game, move, isMaximisingPlayer);

                var score = MiniMax(depth - 1, _game, -10000, 10000, !isMaximisingPlayer);
                _game = game.Clone();

                if (score >= bestScore)
                {
                    bestScore = score;
                    bestMove  = move;
                }
            }

            return(bestMove);
        }
Пример #3
0
        public void Apply(ScrabbleGame game, ProbableMove PM, bool mePlaying)
        {
            var Board = game.Board;

            var Vowels  = new List <string>(Board.Vowels.Split(' '));
            var Conso   = new List <string>(Board.Conso.Split(' '));
            var Special = new List <string>(Board.Special.Replace(",", "").Split(' '));

            foreach (var move in PM.Moves)
            {
                string current = Board.Cells[move.Index];
                string cell    = PlaceTiles(current, move.Tiles);

                Board.Cells[move.Index] = cell;
                foreach (string tile in move.Tiles.Split(','))
                {
                    Vowels.Remove(tile);
                    Conso.Remove(tile);
                    Special.Remove("(" + tile + ")");
                }
            }
            Board.Vowels  = string.Join(" ", Vowels);
            Board.Conso   = string.Join(" ", Conso);
            Board.Special = Join(Special);
            var Scores = game.Scores;

            if (mePlaying)
            {
                Scores.MyScore = Scores.MyScore + PM.Score;
            }
            else
            {
                Scores.OppScore = Scores.OppScore + PM.Score;
            }
        }
Пример #4
0
 static bool Validate(ProbableMove Move, List <Word> AllWords, List <int> Probables)
 {
     Move.Words = Move.Words.Distinct(new ProbableWordComparer()).ToList();
     if (Move.Words.Count == 0 || Move.Moves.Count == 0)
     {
         return(false);
     }
     return(Validate(Move.Words, AllWords, Probables));
 }
Пример #5
0
        protected static List <ProbableMove> EmptyExtensions(string[] Cells, int size, CharSet CharSet, int star, string botId, List <Word> AllWords, List <int> Probables, List <string> Movables, Dictionary <string, Regex> SpeicalDict)
        {
            using (new Watcher("\tEmpty Extesnsions"))
            {
                List <ProbableMove> Moves = new List <ProbableMove>();
                {
                    foreach (var indx in Probables)
                    {
                        Word   word   = AllWords[indx];
                        string Pre    = "";
                        string Center = "";
                        string Post   = "";

                        int f = word.Tiles.IndexOf(',');

                        Center = word.Tiles.Substring(0, f);
                        Post   = word.Tiles.Substring(f + 1);

                        string[] Pres    = Pre == "" ? new string[] { } : Pre.TrimEnd(',').Split(',');
                        string[] Centers = Center.Split(',');
                        string[] Posts   = Post == "" ? new string[] { } : Post.TrimStart(',').Split(',');

                        var  Tiles = Movables.GetRange(0, Movables.Count);
                        bool res   = Resolve(Pres, Centers, Posts, Tiles, SpeicalDict);
                        if (!res)
                        {
                            continue;
                        }
                        int totalCells = Pres.Length + Centers.Length + Posts.Length;
                        int centroid   = totalCells % 2 == 0 ? (totalCells / 2 - 1) : totalCells / 2;

                        ProbableMove WH = TryHarizontal(0, star, Cells, size, star - centroid, 0, Pres, Centers, Posts);
                        ProbableMove WV = TryVertical(0, star, Cells, size, star - centroid, 0, Pres, Centers, Posts);

                        bool WHValid = Validate(WH, AllWords, Probables);
                        bool WVValid = Validate(WV, AllWords, Probables);

                        if (WHValid)
                        {
                            Moves.Add(WH);
                        }
                        if (WVValid)
                        {
                            Moves.Add(WV);
                        }
                    }
                }
                return(Moves);
            }
        }
Пример #6
0
 public void BestMove(ScrabbleGame game)
 {
     var          moves = Probables(game);
     ProbableMove move  = MiniMaxRoot(2, game, true);
 }
Пример #7
0
 public void Run(Game game)
 {
     var          moves = Probables(game);
     ProbableMove move  = MiniMaxRoot(2, game, true);
 }
Пример #8
0
        protected static List <ProbableMove> WordExtensions(string[] Cells, int size, CharSet CharSet, string botId, List <Word> AllWords, List <int> Probables, List <string> Movables, Dictionary <string, Regex> SpeicalDict)
        {
            using (new Watcher("\tWord Extensions"))
            {
                List <ProbableMove> Moves = new List <ProbableMove>();
                {
                    var WordsOnBoard = GetWordsOnBoard(Cells, size, false);
                    if (WM)
                    {
                        RefreshCache(botId + ":W", WordsOnBoard);
                    }
                    foreach (Word wordOnBoard in WordsOnBoard)
                    {
                        string raw = wordOnBoard.Tiles.Replace("(", "").Replace(")", "").Replace(",", "").Replace("|", ",");
                        int    len = raw.Split(',').Length;

                        string pattern = GenWordPattern(CharSet, wordOnBoard.Tiles, "(?<Center{0}>.*?)", "", "(?<Center{0}>.*?)", "(?<Pre>.*?)", "(?<Post>.*?)", true);
                        pattern = string.Format("^{0}$", pattern.TrimEnd('|'));
                        Regex R = new Regex(pattern, RegexOptions.Compiled);

                        //Printer.PrintLine("\t\t Word Pattern: " + pattern);
                        //using (new Watcher("\t\t Match Word: ", true))
                        {
                            List <int> Probables2 = new List <int>();

                            if (WM && len > WS)
                            {
                                Probables2 = ShortList(botId + ":W", wordOnBoard.Tiles, R, AllWords, Probables);
                            }
                            else
                            {
                                Probables2 = ShortList(R, AllWords, Probables);
                            }

                            foreach (int indx in Probables2)
                            {
                                var word = AllWords[indx];
                                if (raw == word.Tiles)
                                {
                                    continue;
                                }
                                Match M = R.Match(word.Tiles);
                                if (!M.Success)
                                {
                                    continue;
                                }

                                string Pre    = "";
                                string Post   = "";
                                string Center = "";

                                Pre = MatchedString(M.Groups["Pre"], "");
                                for (int i = 0; i < word.Syllables; i++)
                                {
                                    Center = Center + MatchedString(M.Groups["Center" + (i + 1)], ",") + ":";
                                }
                                Center = Center.TrimEnd(':');
                                Post   = MatchedString(M.Groups["Post"], "");

                                string[] Pres    = Pre == "" ? new string[] { } : Pre.TrimEnd(',').Split(',');
                                string[] Centers = Center.Split(':');
                                string[] Posts   = Post == "" ? new string[] { } : Post.TrimStart(',').Split(',');

                                if (Centers.Length != len)
                                {
                                    Array.Resize(ref Centers, len);
                                    if (!Post.StartsWith(",") && Posts.Length > 0)
                                    {
                                        Centers[len - 1] = Posts[0];
                                        Posts            = Posts.Skip(1).ToArray();
                                    }
                                }

                                var Tiles = Movables.GetRange(0, Movables.Count);

                                bool res = Resolve(Pres, Centers, Posts, Tiles, SpeicalDict);
                                if (!res)
                                {
                                    continue;
                                }

                                if (wordOnBoard.Position == "R")
                                {
                                    ProbableMove WH      = TryHarizontal(2, -1, Cells, size, wordOnBoard.Index, wordOnBoard.Syllables - 1, Pres, Centers, Posts);
                                    bool         WHValid = Validate(WH, AllWords, Probables);
                                    if (WHValid)
                                    {
                                        Moves.Add(WH);
                                    }
                                }
                                if (wordOnBoard.Position == "C")
                                {
                                    ProbableMove WH      = TryVertical(2, -1, Cells, size, wordOnBoard.Index, wordOnBoard.Syllables - 1, Pres, Centers, Posts);
                                    bool         WHValid = Validate(WH, AllWords, Probables);
                                    if (WHValid)
                                    {
                                        Moves.Add(WH);
                                    }
                                }
                            }
                        }
                    }
                }
                Printer.PrintLine("\t\t Moves found: " + Moves.Count);
                return(Moves);
            }
        }
Пример #9
0
        protected static List <ProbableMove> SyllableExtensions(string[] Cells, int size, CharSet CharSet, string botId, List <Word> AllWords, List <int> Probables, List <string> Movables, Dictionary <string, Regex> SpeicalDict)
        {
            using (new Watcher("\tSyllable Extensions"))
            {
                List <ProbableMove> Moves = new List <ProbableMove>();
                {
                    List <Word> All = GetSyllableList2(Cells, size, false, true);
                    if (SM)
                    {
                        RefreshCache(botId + ":W", All);
                    }
                    foreach (var syllable in All)
                    {
                        string pattern = GetSyllablePattern2(CharSet, syllable.Tiles.Replace("(", "").Replace(")", ""), "(?<Center>.*?)", "(?<Pre>.*?)", "(?<Post>.*?)");
                        pattern = string.Format("^{0}$", pattern);
                        Regex R = new Regex(pattern, RegexOptions.Compiled);

                        //Printer.PrintLine("\t\t Syllable Pattern: " + pattern);
                        //using (new Watcher("\t\t Match Syllable: ", true))
                        {
                            var Probables2 = new List <int>();
                            if (SM)
                            {
                                Probables2 = ShortList(botId + ":S", syllable.Tiles, R, AllWords, Probables);
                            }
                            else
                            {
                                Probables2 = ShortList(R, AllWords, Probables);
                            }
                            foreach (int indx in Probables2)
                            {
                                Word  probable = AllWords[indx];
                                Match M        = R.Match(probable.Tiles);
                                if (!M.Success)
                                {
                                    continue;
                                }

                                string Pre    = MatchedString(M.Groups["Pre"], "");
                                string Center = MatchedString(M.Groups["Conso"], "");
                                string Post   = MatchedString(M.Groups["Post"], "");

                                string[] Pres    = Pre == "" ? new string[] { } : Pre.TrimEnd(',').Split(',');
                                string[] Centers = Center == "" ? new string[] { } : Center.Split(',');
                                string[] Posts   = Post == "" ? new string[] { } : Post.TrimStart(',').Split(',');

                                if (!Post.StartsWith(",") && Posts.Length > 0)
                                {
                                    Centers = (Center + Posts[0]).Split(',');
                                    Posts   = Posts.Skip(1).ToArray();
                                }

                                var  Tiles = Movables.GetRange(0, Movables.Count);
                                bool res   = Resolve(Pres, Centers, Posts, Tiles, SpeicalDict);
                                if (!res)
                                {
                                    continue;
                                }

                                ProbableMove WH = TryHarizontal(1, -1, Cells, size, syllable.Index, 0, Pres, Centers, Posts);
                                ProbableMove WV = TryVertical(1, -1, Cells, size, syllable.Index, 0, Pres, Centers, Posts);

                                bool WHValid = Validate(WH, AllWords, Probables);
                                bool WVValid = Validate(WV, AllWords, Probables);

                                if (WHValid)
                                {
                                    Moves.Add(WH);
                                }
                                if (WVValid)
                                {
                                    Moves.Add(WV);
                                }
                            }
                        }
                    }
                }
                Printer.PrintLine("\t\t Moves found: " + Moves.Count);
                return(Moves);
            }
        }