Ejemplo n.º 1
0
        public float Minimax(Grid g, out GridCells best, int depth = 1)
        {
            miniMaxCount++;
            best = GridCells.None;
            var       bestResult = -10f;
            GridCells garbage;

            if (g.IsDraw)
            {
                return(0f);
            }

            if (g.CurrentIsWinner)
            {
                return(1f / depth);
            }

            if (g.CurrentIsLoser)
            {
                return(-1f / depth);
            }

            foreach (var move in g.GetMoves())
            {
                var other  = g.MakeMove(move);
                var result = -Minimax(other, out garbage, depth + 1);

                if (result > bestResult)
                {
                    best       = move;
                    bestResult = result;
                }
            }
            return(bestResult);
        }
Ejemplo n.º 2
0
        public float Minimax(Grid g, out GridCells best, float alpha, float beta, int depth = 1)
        {
            miniMaxCount++;
            best = GridCells.None;
            var bestResult = -10f;
            GridCells garbage;
            if (g.IsDraw)
                return 0f;

            if (g.CurrentIsWinner)
                return 1f / depth;

            if (g.CurrentIsLoser)
                return -1f / depth;

            foreach (var move in g.GetMoves())
            {
                var other = g.MakeMove(move);
                alpha = -Minimax(other, out garbage, -beta, -alpha, depth + 1);

                if (beta <= alpha)
                    return alpha;

                if (alpha > bestResult)
                {
                    best = move;
                    bestResult = alpha;
                }
            }
            return bestResult;
        }
Ejemplo n.º 3
0
        public Grid MakeMove(Grid g)
        {
            GridCells move;

            Minimax(g, out move);
            return(g.MakeMove(move));
        }
Ejemplo n.º 4
0
        public int Minimax(Grid g, out GridCells best)
        {
            best = GridCells.None;
            var       bestResult = -10;
            GridCells garbage;

            if (g.IsDraw)
            {
                return(0);
            }

            if (g.CurrentIsWinner)
            {
                return(1);
            }

            if (g.CurrentIsLoser)
            {
                return(-1);
            }

            foreach (var move in g.GetMoves())
            {
                var other  = g.MakeMove(move);
                var result = -Minimax(other, out garbage);

                if (result > bestResult)
                {
                    best       = move;
                    bestResult = result;
                }
            }
            return(bestResult);
        }
Ejemplo n.º 5
0
        public int Minimax(Grid g, out GridCells best)
        {
            best = GridCells.None;
            var bestResult = -10;
            GridCells garbage;
            if (g.IsDraw)
                return 0;

            if (g.CurrentIsWinner)
                return 1;

            if (g.CurrentIsLoser)
                return -1;

            foreach (var move in g.GetMoves())
            {
                var other = g.MakeMove(move);
                var result = -Minimax(other, out garbage);

                if (result > bestResult)
                {
                    best = move;
                    bestResult = result;
                }
            }
            return bestResult;
        }
Ejemplo n.º 6
0
 public Grid MakeMove(Grid g)
 {
     GridCells move;
     miniMaxCount = 0;
     Minimax(g, out move, float.MinValue, float.MaxValue);
     Console.WriteLine("Minimax method calls: {0}", miniMaxCount);
     return g.MakeMove(move);
 }
Ejemplo n.º 7
0
        public Grid MakeMove(Grid g)
        {
            GridCells move;

            miniMaxCount = 0;
            Minimax(g, out move);
            Console.WriteLine("Minimax method calls: {0}", miniMaxCount);
            return(g.MakeMove(move));
        }
Ejemplo n.º 8
0
 public Grid MakeMove(Grid g)
 {
     GridCells move;
     while (true)
     {
         ConsoleGridRenderer.Render(g);
         Console.WriteLine("\nEnter {0} move: ", g.CurrentIsO ? "O" : "X");
         if (Enum.TryParse<GridCells>(Console.ReadLine().ToUpper(), out move) && g.CanMove(move))
             return g.MakeMove(move);
         else
             Console.WriteLine("Invalid Move!");
     }
 }
Ejemplo n.º 9
0
        public Grid MakeMove(Grid g)
        {
            GridCells move;

            while (true)
            {
                ConsoleGridRenderer.Render(g);
                Console.WriteLine("\nEnter {0} move: ", g.CurrentIsO ? "O" : "X");
                if (Enum.TryParse <GridCells>(Console.ReadLine().ToUpper(), out move) && g.CanMove(move))
                {
                    return(g.MakeMove(move));
                }
                else
                {
                    Console.WriteLine("Invalid Move!");
                }
            }
        }
Ejemplo n.º 10
0
 public Grid MakeMove(Grid g)
 {
     GridCells move;
     Minimax(g, out move);
     return g.MakeMove(move);
 }