예제 #1
0
        //you are advised not to set more than 3 parallel threads if you are doing this on your laptop, otherwise the laptop will not survive
        private static void Main(string[] args)
        {
            //Console.WriteLine(Cards.FromName("This is an invalid name") == Cards.FromName("Default"));-ignore this, this was for testing
            Console.WriteLine("Starting test setup. v6.7: run in parallel " + parallelThreads + "x and in each parallel, no of tasks:" + testsInEachThread + " and inner parallel:" + parallelThreadsInner + " and each within inner parallel, inner tasks:" + testsInEachThreadInner + " times, different decks, get winrates and time avg of each and print max depth =" + maxDepth + " , max width = " + maxWidth + "");
            Sabbertest2.CreateAndMutate createMutateObj = new CreateAndMutate(); //this is the class I added which contains all functions1
                                                                                 //this above object will help you mutate or create a deck, without worrying about underlying code.
            Dictionary <int, List <Card> > victoryMany = new Dictionary <int, List <Card> >();
            //OneTurn();-ignore this
            Dictionary <int, string> allCards = getAllCards();//important function, must be done in main before anything else, this will get all the valid hearthstone cards (1300+ cards in total) from the data file
            // string[] results = new string[100];//max 15 tests, can be increased/changed without any problems, make sure max j * max i <= size of results array
            Dictionary <int, string> results = new Dictionary <int, string>();
            List <Card> victorious           = new List <Card>();
            List <Card> playerDeck           = createMutateObj.createRandomDeck(allCards, cardname);//this liine returns randomly created deck from all cards in hearthsone, which is passed as parameter
            bool        end         = false;
            List <Card> playerDeck2 = Decks.MidrangeJadeShaman;

            stopwatch.Start();
            //DateTime start = new DateTime();
            //DateTime stop = new DateTime();
            Parallel.For(0, parallelThreads, j =>
            {
                // int i = 0;

                // while (!end)
                // for (int i = testsInEachThread * j; i < (j + 1) * testsInEachThread; i++)//(int i = 0; i < 10 ; i++) //
                {
                    if (!results.ContainsKey(j))
                    {
                        Console.WriteLine("outer i, deck number=" + j);
                        Console.WriteLine("Printing Deck player 1, loop is here=" + j);
                        createMutateObj.print(playerDeck);
                        Console.WriteLine("Printing Deck player 2 loop is here=" + j);
                        createMutateObj.print(playerDeck);
                        string winRate_timeMean = getWinRateTimeMean(playerDeck, j, playerDeck2);

                        results.Add(j, winRate_timeMean);
                        Console.WriteLine(results[j]);
                    }

                    /*if (results[i].Contains("Player1: WON"))//-this is to get the deck only if it wins
                     * {
                     *  playerDeck2 = playerDeck;
                     *  Console.WriteLine("Testing with inital deck....");
                     *  string s = "Testing with inital deck....";
                     *  stopwatch.Start();
                     *  s = s + FullGame(playerDeck, i, Decks.MidrangeJadeShaman);
                     *  stopwatch.Stop();
                     *  long seconds = (stopwatch.ElapsedMilliseconds / 1000);//(stop - start).ToString();//
                     *  TimeSpan t = TimeSpan.FromSeconds(seconds);
                     *  if (s.Contains("Player1: LOST"))
                     *  {
                     *      s = s + " time taken to execute completely (hh:mm:ss): " + t.ToString();
                     *      results.Add(i+1, s);
                     *      break;
                     *  }
                     *  playerDeck = playerDeck = createMutateObj.createRandomDeck(allCards, cardname);
                     *
                     *  //break;
                     *  //victorious = playerDeck;
                     *  /* if (!victoryMany.ContainsKey(i))
                     *  {
                     *      victoryMany.Add(i, playerDeck); //this gives the last deck that played the game
                     *  }
                     *  //end = true;
                     * }
                     * else
                     * {
                     *     playerDeck = createMutateObj.mutate(playerDeck, allCards, cardname);//keep mutating till victory
                     * }*/
                    //i++;
                    if (j == 100)
                    {
                        //  end = true;
                    }

                    playerDeck = createMutateObj.createRandomDeck(allCards, cardname);
                }
            });

            /* for (int i = 0; i < results.Length; i++)//for 15 results here, if parallel threads * testInEachThread = 6, then 6 tests will show here
             * {
             *   Console.WriteLine("Game " + i + " : " + results[i] + "\n");
             * }*/
            foreach (int key in results.Keys)
            {
                Console.WriteLine("Game " + key + " : " + results[key] + "\n");
            }
            Console.WriteLine("Before Mutation Victory Decks:");
            stopwatch.Stop();
            TimeSpan tempeForOverall = TimeSpan.FromSeconds(stopwatch.ElapsedMilliseconds / 1000);

            Console.WriteLine("Overall time taken:" + tempeForOverall.ToString());

            /* createMutateObj.print(victorious);
             *
             *
             * List<Card> myDeck = new List<Card>();
             * myDeck = victorious;//myDeck can be anything, I have made it =victorious/last deck created in the loop.
             * List<Card> mutated = createMutateObj.mutate(myDeck, allCards, cardname);//make your deck myDeck and pass it here to mutate it.
             *
             * //RandomGames(); - ignore this line
             * Console.WriteLine("\n Mutated Deck: \n");
             * createMutateObj.print(mutated);
             * Console.WriteLine("Test end!");*/
            Console.ReadLine();
        }
예제 #2
0
        public static string getWinRateTimeMeanLinear(List <Card> player1Deck, int where, List <Card> player2Deck)
        {
            int[] wins          = Enumerable.Repeat(0, 1000).ToArray();
            long  sum_Timetaken = 0;
            int   winss         = 0;
            int   numOfTests    = 70;

            object[] temp()
            {
                object[] obj = new object[1002];
                for (int i = 0; i < numOfTests; i++)
                {
                    obj[i] = new Stopwatch();
                }
                return(obj);
            }

            object[] stopwatches = temp();
            //Parallel.For(0, parallelThreadsInner, parallelOptions, j =>
            {
                // int i = 0;
                long max = 0;
                // while (!end)
                for (int i = 0; i < numOfTests; i++) ////
                {
                    Console.WriteLine("Inner i, or here inside getWinRateTimeMean at here= " + i);
                    ((Stopwatch)stopwatches[i]).Start();
                    // start = DateTime.Now;
                    string s = "";
                    try
                    {
                        //s = FullGame(player1Deck, i, player2Deck);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                        CreateAndMutate createAndMutate = new CreateAndMutate();
                        Console.WriteLine("Player 1 deck that caused issue:");
                        createAndMutate.print(player1Deck);
                    }
                    //stop = DateTime.Now;
                    ((Stopwatch)stopwatches[i]).Stop();
                    long seconds = (((Stopwatch)stopwatches[i]).ElapsedMilliseconds / 1000);//(stop - start).ToString();//
                    Console.WriteLine("secondes:" + seconds);
                    if (max < seconds)
                    {
                        max = seconds;
                    }
                    TimeSpan tempe = TimeSpan.FromSeconds(seconds);
                    Console.WriteLine("time taken for " + i + ":" + tempe);
                    sum_Timetaken = sum_Timetaken + seconds;
                    Console.WriteLine("sum_TimeTaken in loop:" + sum_Timetaken);
                    //((Stopwatch)stopwatches[i]).Reset();
                    if (s.Contains("Player1: WON"))
                    {
                        wins[i]++;
                        winss++;
                        Console.WriteLine("Winss:" + winss);
                    }
                }
                Console.WriteLine("Max was:" + max);
                max = 0;
            }//);
            Console.WriteLine("Starting test setup. v6.7: all linear, 10 and in each 50 linear all randomly gen decks Not running in Parallel at ALL, this is in Linear Method run in parallel " + parallelThreads + "x and in each parallel, no of tasks:" + testsInEachThread + " and inner parallel:" + parallelThreadsInner + " and each within inner parallel, inner tasks:" + testsInEachThreadInner + " times, different decks, get winrates and time avg of each and print max depth =" + maxDepth + " , max width = " + maxWidth + "");

            for (int i = 0; i < (numOfTests); i++)
            {
                Console.WriteLine("i:" + i + " wins:" + wins[i]);
            }
            sum_Timetaken = 0;
            for (int i = 0; i < (numOfTests); i++)
            {
                Console.WriteLine("i:" + i + " Times:" + ((Stopwatch)stopwatches[i]).ElapsedMilliseconds / 1000);
                sum_Timetaken = sum_Timetaken + ((Stopwatch)stopwatches[i]).ElapsedMilliseconds / 1000;
            }
            Console.WriteLine("New sum_timetaken=" + sum_Timetaken);
            TimeSpan t       = TimeSpan.FromSeconds(sum_Timetaken / (numOfTests));
            float    winsSum = 0;

            for (int i = 0; i < (numOfTests); i++)
            {
                if (wins[i] > 0)
                {
                    winsSum++;
                }
            }
            //winsSum = winss;
            Console.WriteLine(winsSum + "is winsSum");
            Console.WriteLine(winss + "is winss");
            float winrateDiv = (float)((float)winsSum / ((float)numOfTests));

            return("Win rate =" + winrateDiv * 100 + "% and average time of each round (hh:mm:ss) = " + t.ToString());
        }
예제 #3
0
        //create

        /*
         *      public static Dictionary<int, int> createDeck(Dictionary<int, string> allcards)
         *      {
         *              Dictionary<int, int> chosenDeck = new Dictionary<int, int>();
         *              Random rand = new Random();
         *              int Count = 0;
         *              while (Count != 30)
         *              {
         *                      int oneRand = rand.Next(0, allcards.Count + 1);
         *                      if (allcards.ContainsKey(oneRand)) //&& (Cards.FromName(allcards[oneRand])))
         *                      {
         *                              if (chosenDeck.ContainsKey(oneRand) && (chosenDeck[oneRand] == 1))
         *                              {
         *                                      chosenDeck[oneRand] = 2;
         *                                      Count++;
         *                              }
         *                              else if (!chosenDeck.ContainsKey(oneRand))
         *                              {
         *                                      chosenDeck.Add(oneRand, 1);
         *                                      Count++;
         *                              }
         *                      }
         *              }
         *              //List<Card> playerDeck = convertDictToList(chosenDeck, allcards);
         *              Console.WriteLine("Count:" + Count);
         *              return chosenDeck;
         *      }
         */
        //build allcards in dictionary from txt file


        public static string getWinRateTimeMean(List <Card> player1Deck, int where, List <Card> player2Deck)
        {
            int[] wins          = Enumerable.Repeat(0, 1000).ToArray();
            long  sum_Timetaken = 0;
            int   winss         = 0;

            object[] temp()
            {
                object[] obj = new object[1002];
                for (int i = 0; i < parallelThreadsInner * testsInEachThreadInner; i++)
                {
                    obj[i] = new Stopwatch();
                }
                return(obj);
            }

            object[] stopwatches = temp();
            Parallel.For(0, parallelThreadsInner, j =>
            {
                // int i = 0;
                long max = 0;
                // while (!end)
                for (int i = testsInEachThreadInner * j; i < (j + 1) * testsInEachThreadInner; i++)//(int i = 0; i < 10 ; i++) //
                {
                    Console.WriteLine("Inner i, or here inside getWinRateTimeMean at here= " + i);
                    ((Stopwatch)stopwatches[i]).Start();
                    // start = DateTime.Now;
                    string s = "";
                    try
                    {
                        s = FullGame(player1Deck, i, player2Deck);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                        CreateAndMutate createAndMutate = new CreateAndMutate();
                        Console.WriteLine("Player 1 deck that caused issue:");
                        createAndMutate.print(player1Deck);
                    }
                    //stop = DateTime.Now;
                    ((Stopwatch)stopwatches[i]).Stop();
                    long seconds = (((Stopwatch)stopwatches[i]).ElapsedMilliseconds / 1000);//(stop - start).ToString();//
                    Console.WriteLine("secondes:" + seconds);
                    if (max < seconds)
                    {
                        max = seconds;
                    }
                    TimeSpan tempe = TimeSpan.FromSeconds(seconds);
                    Console.WriteLine("time taken for " + i + ":" + tempe);
                    sum_Timetaken = sum_Timetaken + seconds;
                    Console.WriteLine("sum_TimeTaken in loop:" + sum_Timetaken);
                    //((Stopwatch)stopwatches[i]).Reset();
                    if (s.Contains("Player1: WON"))
                    {
                        wins[i]++;
                        winss++;
                        Console.WriteLine("Winss:" + winss);
                    }
                }
                Console.WriteLine("Max was:" + max);
                max = 0;
            });

            for (int i = 0; i < (parallelThreadsInner * testsInEachThreadInner); i++)
            {
                Console.WriteLine("wins:" + wins[i]);
            }
            sum_Timetaken = 0;
            for (int i = 0; i < (parallelThreadsInner * testsInEachThreadInner); i++)
            {
                Console.WriteLine("Times:" + ((Stopwatch)stopwatches[i]).ElapsedMilliseconds / 1000);
                sum_Timetaken = sum_Timetaken + ((Stopwatch)stopwatches[i]).ElapsedMilliseconds / 1000;
            }
            Console.WriteLine("New sum_timetaken=" + sum_Timetaken);
            TimeSpan t       = TimeSpan.FromSeconds(sum_Timetaken / (parallelThreadsInner * testsInEachThreadInner));
            int      winsSum = 0;

            for (int i = 0; i < (parallelThreadsInner * testsInEachThreadInner); i++)
            {
                if (wins[i] > 0)
                {
                    winsSum++;
                }
            }
            //winsSum = winss;
            Console.WriteLine(winsSum + "is winsSum");
            Console.WriteLine(winss + "is winss");
            long winrate = ((winsSum / (parallelThreadsInner * testsInEachThreadInner)) * 100);

            return("Win rate = " + winrate + "% and average time of each round (hh:mm:ss) = " + t.ToString());
        }