Exemplo n.º 1
0
        public void AddNodes(Stratego game)
        {
            bool[,] tmpBoard = (bool[, ])game.Board.Clone();
            foreach (Node node in Path)
            {
                tmpBoard[node.Move.X, node.Move.Y] = true;
            }

            for (int i = 0; i < game.SizeOfSide; i++)
            {
                for (int j = 0; j < game.SizeOfSide; j++)
                {
                    if (!tmpBoard[i, j])
                    {
                        Nodes.Add(new Node(new Move(i, j), Path));
                    }
                }
            }

            foreach (Node node in Nodes)
            {
                node.Move.CalculateScore(node.Path, game);
                if (game.Deep > node.Path.Count && game.NumberOfMoves - game.MovesDone > node.Path.Count)
                {
                    node.AddNodes(game);
                }
            }
        }
Exemplo n.º 2
0
        public void CalculateScore(Queue <Node> path, Stratego game)
        {
            List <Move> moves = new List <Move>();

            bool[,] tmpBoard = (bool[, ])game.Board.Clone();
            foreach (Node node in path)
            {
                tmpBoard[node.Move.X, node.Move.Y] = true;
            }
            foreach (var node in path)
            {
                moves.Add(node.Move);
            }

            Score = Stratego.CalculateScore(X, Y, game.SizeOfSide, tmpBoard, moves);
        }
Exemplo n.º 3
0
        public Move MakeMove(Stratego game)
        {
            List <Node> posibleMoves = new List <Node>();

            for (int i = 0; i < game.SizeOfSide; i++)
            {
                for (int j = 0; j < game.SizeOfSide; j++)
                {
                    if (!game.Board[i, j])
                    {
                        posibleMoves.Add(new Node(new Move(i, j), new Queue <Node>()));
                    }
                }
            }

            foreach (Node node in posibleMoves)
            {
                node.Move.CalculateScore(node.Path, game);
                if (game.Deep > node.Path.Count && game.NumberOfMoves - game.MovesDone > node.Path.Count)
                {
                    node.AddNodes(game);
                }

                int alfa = int.MinValue, beta = int.MaxValue;
                node.Evaluate(true, alfa, beta);
                Node tmp    = node;
                bool fliper = true;
                while (tmp.Nodes.Count > 0)
                {
                    //Console.Write("e: {0}, ms: {1}, x: {2}, y: {3} <-", tmp.Evaluation, tmp.Move.Score, tmp.Move.X, tmp.Move.Y);
                    tmp = fliper ? tmp.Nodes[0] : tmp.Nodes[tmp.Nodes.Count - 1];

                    fliper = !fliper;
                }
                //Console.ReadKey();
                //Console.WriteLine();
            }

            posibleMoves.Sort();

            int resultIndex = posibleMoves.Count - 1;

            if (UseRandomForEqual)
            {
                int i = resultIndex;
                for (; i >= 0; i--)
                {
                    if (posibleMoves[i].Evaluation != posibleMoves[resultIndex].Evaluation)
                    {
                        break;
                    }
                }
                Random random = new Random(DateTime.Now.GetHashCode());
                if (i < 0)
                {
                    i = 0;
                }
                resultIndex = random.Next(i, posibleMoves.Count);
            }

            Console.WriteLine("min:" + posibleMoves[0].Evaluation + " max:" + posibleMoves[resultIndex].Evaluation +
                              " " + posibleMoves[resultIndex].Move.X + " " + posibleMoves[resultIndex].Move.Y);
            return(posibleMoves[resultIndex].Move);
        }
Exemplo n.º 4
0
        static void Main(string[] args)
        {
            int firstPlayerWinsTotal = 0, secondPlayerWinsTotal = 0;
            int firstPlayerWins = 0, secondPlayerWins = 0;

            Stratego stratego = new Stratego(5);

            stratego.Deep = 6;

            //stratego.SetPlayers(
            //    new Player(new AlfaBeta { UseRandomForEqual = false }, false),
            //    new Player(new AlfaBeta { UseRandomForEqual = false }, false)
            //    );
            double time = 0;

            //for (int i = 0; i < 10; i++)
            //{
            //    stratego.Reinit();
            //    DateTime start = DateTime.Now;
            //    stratego.Start();
            //    time += (DateTime.Now - start).TotalMilliseconds;
            //    Console.WriteLine(time);
            //    switch (stratego.GetWinner())
            //    {
            //        case 0:
            //            firstPlayerWins++;
            //            firstPlayerWinsTotal++;
            //            break;
            //        case 1:
            //            secondPlayerWins++;
            //            secondPlayerWinsTotal++;
            //            break;
            //        default:
            //            break;
            //    }
            //}
            //time /= 10;
            //Console.WriteLine(time);
            //Console.WriteLine("p1: {0}, p2: {1}", firstPlayerWins, secondPlayerWins);

            //stratego = new Stratego(5);
            //stratego.Deep = 6;
            //stratego.SetPlayers(
            //    new Player(new AlfaBeta { UseRandomForEqual = true }, false),
            //    new Player(new AlfaBeta { UseRandomForEqual = true }, false)
            //);
            //firstPlayerWins = 0;
            //secondPlayerWins = 0;
            //time = 0;
            //for (int i = 0; i < 10; i++)
            //{
            //    stratego.Reinit();
            //    DateTime start = DateTime.Now;
            //    stratego.Start();
            //    time += (DateTime.Now - start).TotalMilliseconds;
            //    Console.WriteLine(time);
            //    switch (stratego.GetWinner())
            //    {
            //        case 0:
            //            firstPlayerWins++;
            //            firstPlayerWinsTotal++;
            //            break;
            //        case 1:
            //            secondPlayerWins++;
            //            secondPlayerWinsTotal++;
            //            break;
            //        default:
            //            break;
            //    }
            //}
            //time /= 10;
            //Console.WriteLine(time);
            //Console.WriteLine("p1: {0}, p2: {1}", firstPlayerWins, secondPlayerWins);

            //stratego = new Stratego(5);
            //stratego.Deep = 6;
            //stratego.SetPlayers(
            //    new Player(new AlfaBeta() { UseRandomForEqual = true }, false),
            //    new Player(new AlfaBeta() { UseRandomForEqual = false }, false)
            //);
            //firstPlayerWins = 0;
            //secondPlayerWins = 0;
            //time = 0;
            //for (int i = 0; i < 10; i++)
            //{
            //    stratego.Reinit();
            //    DateTime start = DateTime.Now;
            //    stratego.Start();
            //    time += (DateTime.Now - start).TotalMilliseconds;
            //    Console.WriteLine(time);
            //    switch (stratego.GetWinner())
            //    {
            //        case 0:
            //            firstPlayerWins++;
            //            firstPlayerWinsTotal++;
            //            break;
            //        case 1:
            //            secondPlayerWins++;
            //            secondPlayerWinsTotal++;
            //            break;
            //        default:
            //            break;
            //    }

            //}
            //time /= 10;
            //Console.WriteLine(time);
            //Console.WriteLine("p1: {0}, p2: {1}", firstPlayerWins, secondPlayerWins);

            //stratego = new Stratego(5);
            //stratego.Deep = 6;
            //stratego.SetPlayers(
            //    new Player(new AlfaBeta() { UseRandomForEqual = false }, false),
            //    new Player(new AlfaBeta() { UseRandomForEqual = true }, false)
            //);
            //firstPlayerWins = 0;
            //secondPlayerWins = 0;
            //time = 0;
            //for (int i = 0; i < 10; i++)
            //{
            //    stratego.Reinit();
            //    DateTime start = DateTime.Now;
            //    stratego.Start();
            //    time += (DateTime.Now - start).TotalMilliseconds;
            //    Console.WriteLine(time);
            //    switch (stratego.GetWinner())
            //    {
            //        case 0:
            //            firstPlayerWins++;
            //            firstPlayerWinsTotal++;
            //            break;
            //        case 1:
            //            secondPlayerWins++;
            //            secondPlayerWinsTotal++;
            //            break;
            //        default:
            //            break;
            //    }
            //}
            //time /= 10;
            //Console.WriteLine(time);
            //Console.WriteLine("p1: {0}, p2: {1}", firstPlayerWins, secondPlayerWins);


            //stratego = new Stratego(5);
            //stratego.Deep = 6;
            //stratego.SetPlayers(
            //    new Player(new MinMax() { UseRandomForEqual = false }, false),
            //    new Player(new MinMax() { UseRandomForEqual = false }, false)
            //);
            //firstPlayerWins = 0;
            //secondPlayerWins = 0;
            //time = 0;
            //for (int i = 0; i < 10; i++)
            //{
            //    stratego.Reinit();
            //    DateTime start = DateTime.Now;
            //    stratego.Start();
            //    time += (DateTime.Now - start).TotalMilliseconds;
            //    Console.WriteLine(time);
            //    switch (stratego.GetWinner())
            //    {
            //        case 0:
            //            firstPlayerWins++;
            //            firstPlayerWinsTotal++;
            //            break;
            //        case 1:
            //            secondPlayerWins++;
            //            secondPlayerWinsTotal++;
            //            break;
            //        default:
            //            break;
            //    }
            //}
            //time /= 10;
            //Console.WriteLine(time);
            //Console.WriteLine("p1: {0}, p2: {1}", firstPlayerWins, secondPlayerWins);

            //stratego = new Stratego(5);
            //stratego.Deep = 6;
            //stratego.SetPlayers(
            //    new Player(new MinMax() { UseRandomForEqual = true }, false),
            //    new Player(new MinMax() { UseRandomForEqual = true }, false)
            //);
            //firstPlayerWins = 0;
            //secondPlayerWins = 0;
            //time = 0;
            //for (int i = 0; i < 10; i++)
            //{
            //    stratego.Reinit();
            //    DateTime start = DateTime.Now;
            //    stratego.Start();
            //    time += (DateTime.Now - start).TotalMilliseconds;
            //    Console.WriteLine(time);
            //    switch (stratego.GetWinner())
            //    {
            //        case 0:
            //            firstPlayerWins++;
            //            firstPlayerWinsTotal++;
            //            break;
            //        case 1:
            //            secondPlayerWins++;
            //            secondPlayerWinsTotal++;
            //            break;
            //        default:
            //            break;
            //    }
            //}
            //time /= 10;
            //Console.WriteLine(time);
            //Console.WriteLine("p1: {0}, p2: {1}", firstPlayerWins, secondPlayerWins);

            //stratego = new Stratego(5);
            //stratego.Deep = 6;
            //stratego.SetPlayers(
            //    new Player(new MinMax() { UseRandomForEqual = true }, false),
            //    new Player(new MinMax() { UseRandomForEqual = false }, false)
            //);
            //firstPlayerWins = 0;
            //secondPlayerWins = 0;
            //time = 0;
            //for (int i = 0; i < 10; i++)
            //{
            //    stratego.Reinit();
            //    DateTime start = DateTime.Now;
            //    stratego.Start();
            //    time += (DateTime.Now - start).TotalMilliseconds;
            //    Console.WriteLine(time);
            //    switch (stratego.GetWinner())
            //    {
            //        case 0:
            //            firstPlayerWins++;
            //            firstPlayerWinsTotal++;
            //            break;
            //        case 1:
            //            secondPlayerWins++;
            //            secondPlayerWinsTotal++;
            //            break;
            //        default:
            //            break;
            //    }
            //}
            //time /= 10;
            //Console.WriteLine(time);
            //Console.WriteLine("p1: {0}, p2: {1}", firstPlayerWins, secondPlayerWins);

            stratego      = new Stratego(5);
            stratego.Deep = 6;
            stratego.SetPlayers(
                new Player(new MinMax()
            {
                UseRandomForEqual = false
            }, false),
                new Player(new MinMax()
            {
                UseRandomForEqual = true
            }, false)
                );
            time = 0;
            stratego.Reinit();
            DateTime start = DateTime.Now;

            stratego.Start();
            time += (DateTime.Now - start).TotalMilliseconds;
            Console.WriteLine(time);
            time /= 10;
        }