Exemplo n.º 1
0
        /// <summary>
        /// In this function the AiS letters are parsed from external classes to actually make
        /// a move (This function uses all the other functions found in this class to make a move).
        /// It does this by first generating all possible moves, it then parsed these possible
        /// words into a generateAllMovesForAllWords to try and generate all the moves possible.
        /// If it failes to do this then this means there is no moves possible for the ai to make.
        /// if there is an avaliable move the ai will tell the game engine to make the move.
        ///
        /// The Function also uses a GrabATile variable to check if the ai has actually used any of its
        /// tiles, this is a relic when one of the newer move conditions was not introduced (DrawnATile).
        /// I left the check still in as a backup to prevent the ai being caught in a logic loop. This
        /// is where the ai would make its best move without using any of its tiles and not drawing any
        /// new tiles, meaning the ai would be stuck making the same move over and over again.
        /// </summary>
        /// <param name="Letters"></param>
        /// <returns></returns>
        public int CalculateAndMakeBestMove(string Letters)
        {
            var Words     = GenerateAllPossibleWords(Letters); //Stores the list of all possible words Generated from the parameter "Letters"
            int GrabATile = 0;                                 //defines and sets the GrabATile variable to 0

            try
            {
                Dictionary <string, int> MoveDict   = GenerateAllMovesForAllWords(Words);                                            //Stores the moves in a dictionary (string as a key and the score of the move as a value)
                List <string>            BrokenMove = BreakMoveIntoList("" + MoveDict.First().Key);                                  //this stores the best move from the dictionary as a broken move (a list) to the varaible BrokenMove
                GrabATile = MyGame.AI_MakeMove(BrokenMove[4], int.Parse(BrokenMove[1]), int.Parse(BrokenMove[2]), BrokenMove[3][0]); //This tells the game engine to make the move stored int BrokenMove and stores the return value to GrabATile
                if (GrabATile != 0)
                {
                    MyGame.Refill1AIHand();                                                                                  //Tells the Game engine to draw a new tile and add it to the Ais hand
                }//It will only draw a new tile for the ai if no tiles were taken from the Ais hand
                Console.WriteLine("WordMade= " + BrokenMove[4] + " ------------------------------------------------------"); //This writes to the debug console the word that was chosen for the move
                return(CalcFinalScore("" + MoveDict.First().Key));                                                           //This return the final score for the Ais Move
            }//To catch an exception due to no moves being avaliable
            catch (Exception e)
            {
                Console.WriteLine("NoMovesAvaliable");
                Console.WriteLine("Exception = " + e.ToString());
                return(0);
            }
        }