Example #1
0
        public Direction SuggestMove(Game g)
        {
            Game gW = new Game(g);
            Game gA = new Game(g);
            Game gS = new Game(g);
            Game gD = new Game(g);
            bool wWorked = gW.Move(Direction.Up);
            bool aWorked = gA.Move(Direction.Left);
            bool sWorked = gS.Move(Direction.Down);
            bool dWorked = gD.Move(Direction.Right);
            int maxValue = Math.Max(gW.HeuristicValue, Math.Max(gA.HeuristicValue, Math.Max(gS.HeuristicValue, gD.HeuristicValue)));

            bool moveWorked = false;

            if (gW.HeuristicValue == maxValue && wWorked)
                return Direction.Up;
            else if (gA.HeuristicValue == maxValue && aWorked)
                return Direction.Left;
            else if (gS.HeuristicValue == maxValue && sWorked)
                return Direction.Down;
            else if (gD.HeuristicValue == maxValue && dWorked)
                return Direction.Right;

            if (wWorked)
                return Direction.Up;
            if (aWorked)
                return Direction.Left;
            if (sWorked)
                return Direction.Down;
            if (dWorked)
                return Direction.Right;

            throw new ApplicationException("No move could be found");
        }
Example #2
0
        static void Main(string[] args)
        {
            Game g = new Game();
            Random r = new Random();
            int gameCount = 0;
            int totalScore = 0;

            while (true)
            {
                IAi ai = new Engine.HeuristicAi();
                g = new Game();

                while (g.AreMovesAvailable())
                {
                    //Console.Write(g);

                    //char move = 'x';
                    ////ConsoleKeyInfo keyInfo = Console.ReadKey();
                    ////move = keyInfo.KeyChar;

                    //double mov = r.NextDouble();
                    //if (mov < 0.5) move = 'w';
                    //else if (mov < 0.96) move = 'a';
                    //else if (mov < 0.99) move = 'd';
                    //else move = 's';
                    //if (move == 'w')
                    //    g.Move(Direction.Up);
                    //else if (move == 'a')
                    //    g.Move(Direction.Left);
                    //else if (move == 's')
                    //    g.Move(Direction.Down);
                    //else if (move == 'd')
                    //    g.Move(Direction.Right);

                    Direction suggestedMove = ai.SuggestMove(g);
                    g.Move(suggestedMove);
                    g.AddRandomNumber();

                    if (g.Score > 7000) goto outOfHere;
                }

                gameCount++;
                totalScore += g.Score;
                if (gameCount % 100 == 0) Console.WriteLine(totalScore / gameCount);

            }

            outOfHere:

            Console.WriteLine();
            Console.Write(g);
            Console.WriteLine("Score: " + g.Score);
            Console.ReadLine();
        }