static void CompareStrategy(TheStrategy one, TheStrategy two) { List <long> oneList = new List <long>(); List <long> twoList = new List <long>(); List <long> diff = new List <long>(); Random rand = new Random(); while (true) { int seed = rand.Next(); Random randOne = new Random(seed); Random randTwo = new Random(seed); Parallel.Invoke( () => { oneList.Add(RunStrategy(randOne, one)); }, () => { twoList.Add(RunStrategy(randTwo, two)); }); diff.Add(oneList.Last() - twoList.Last()); Console.WriteLine("One = {0}, Two = {1}, Diff = {2}", oneList.Last(), twoList.Last(), diff.Last()); Console.WriteLine("Average One = {0}, Two = {1}, Diff = {2}", oneList.Average(), twoList.Average(), diff.Average()); Console.WriteLine("Total Call = {0}, One won = {1}, Two won = {2}, Draw = {3}", diff.Count, diff.Where(s => s < 0).Count(), diff.Where(s => s > 0).Count(), diff.Where(s => s == 0).Count() ); Thread.Sleep(100); } }
static void Test(TheStrategy strategy) { bool high; decimal bid; strategy(100, 0, out high, out bid); Debug.Assert(high == true); Debug.Assert(bid == 100); strategy(100, 50, out high, out bid); Debug.Assert(bid == 0); strategy(100, 99, out high, out bid); Debug.Assert(high == false); Debug.Assert(bid == 100); }
static long RunStrategy(Random rand, TheStrategy strategy) { decimal currentMoney = new decimal(10000); decimal millionDollars = new decimal(1000000); long bidCount; for (bidCount = 0; currentMoney > 0 && currentMoney < millionDollars; bidCount++) { int num = rand.Next(100); bool high; decimal bid; strategy(currentMoney, num, out high, out bid); if (bid > currentMoney) { continue; } int nextNum = rand.Next(100); if ((nextNum > num && high) || (nextNum < num && !high)) { currentMoney += bid; } else if (nextNum == num) { } else { currentMoney -= bid; } } if (currentMoney > millionDollars) { return(bidCount); } else { throw new Exception("Sorry you are Broke!!"); } }
static void Play(Random rand, TheStrategy strategy) { decimal currentMoney = new decimal(10000); decimal millionDollars = new decimal(1000000); Console.WriteLine("Running bot for bidding"); long bidCount; for (bidCount = 0; currentMoney > 0 && currentMoney < millionDollars; bidCount++) { int num = rand.Next(100); bool high; decimal bid; strategy(currentMoney, num, out high, out bid); if (bid > currentMoney) { Console.ForegroundColor = ConsoleColor.DarkRed; Console.WriteLine("Strategy returned Bid {0:C} > Current Money {1:C}", bid, currentMoney); continue; } if (bid == currentMoney) { Console.ForegroundColor = ConsoleColor.DarkRed; Console.WriteLine("All IN"); } int nextNum = rand.Next(100); PrintBid(currentMoney, num, high, bid, nextNum); if ((nextNum > num && high) || (nextNum < num && !high)) { PrintMoney("Won! ", bid, ConsoleColor.Green); currentMoney += bid; } else if (nextNum == num) { Console.ForegroundColor = ConsoleColor.Cyan; Console.WriteLine("Draw"); } else { PrintMoney("Lost!", bid, ConsoleColor.Red); currentMoney -= bid; } System.Threading.Thread.Sleep(100); } if (currentMoney > millionDollars) { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("Congrats you are a millionaire!!"); Console.WriteLine("Money = {0:C}. No of bids made = {1}", currentMoney, bidCount); } else { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Sorry you are Broke!!"); Console.WriteLine("No of bids made = {0}", bidCount); } Console.ReadLine(); Console.ResetColor(); }