Advertisement
stictt

paradox Monti Hoola C#.

Jan 15th, 2021 (edited)
819
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.81 KB | None | 0 0
  1. using System;
  2. using System.Threading.Tasks;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6.  
  7. namespace Plus
  8. {
  9.     public class Shool
  10.     {
  11.  
  12.         static void Main(string[] args)
  13.         {
  14.             Game game = new Game(10000);
  15.             game.StartGames();
  16.  
  17.             double percentСhangesСhoice =
  18.                 (Convert.ToDouble(game.statistics.CountVinsChangesСhoice)
  19.                 / game.statistics.totalGame) * 100;
  20.             Console.WriteLine($"Процент победы по стратегии изменения выбора {percentСhangesСhoice}");
  21.             double percentNotСhangesСhoice =
  22.                 (Convert.ToDouble(game.statistics.CountVinsNotChangesСhoice)
  23.                 / game.statistics.totalGame) * 100;
  24.             Console.WriteLine($"Процент победы по стратегии комбинаторики {percentNotСhangesСhoice}");
  25.  
  26.         }
  27.     }
  28.  
  29.     public class Player
  30.     {
  31.         public int selectedElement;
  32.         public int discontItem;
  33.     }
  34.  
  35.     public class Game
  36.     {
  37.         Player changesСhoice;
  38.         Player NotchangesСhoice;
  39.  
  40.         List<Street> firstPlayerList;
  41.         List<Street> secondPlayerList;
  42.  
  43.         int countGame;
  44.         static Random random = new Random();
  45.  
  46.         public Statistics statistics = new Statistics();
  47.  
  48.  
  49.         public Game(int count)
  50.         {
  51.             countGame = count;
  52.             statistics.totalGame = count;
  53.             firstPlayerList = InitStreet(count);
  54.             secondPlayerList = InitStreet(count);
  55.         }
  56.  
  57.         public List<Street> InitStreet(int count)
  58.         {
  59.             List<Street> streets = new List<Street>();
  60.             for (int i = 0; i < count; i++)
  61.             {
  62.                 streets.Add(new Street());
  63.             }
  64.             return streets;
  65.         }
  66.  
  67.         public void StartGames()
  68.         {
  69.             for (int i = 0; i < countGame; i++)
  70.             {
  71.                 changesСhoice = new Player();
  72.                 NotchangesСhoice = new Player();
  73.                 changesСhoice.selectedElement = random.Next(0, 3);
  74.                 NotchangesСhoice.selectedElement = random.Next(0, 3);
  75.  
  76.                 changesСhoice.discontItem = GetNothingInStreet(firstPlayerList[i], changesСhoice.selectedElement);
  77.                 NotchangesСhoice.discontItem = GetNothingInStreet(secondPlayerList[i], NotchangesСhoice.selectedElement);
  78.  
  79.                 changesСhoice.selectedElement = AnotherСhoice(changesСhoice, firstPlayerList[i]);
  80.  
  81.                 if (IsWinner(changesСhoice, firstPlayerList[i]))
  82.                 {
  83.                     statistics.CountVinsChangesСhoice++;
  84.                 }
  85.  
  86.                 if (IsWinner(NotchangesСhoice, secondPlayerList[i]))
  87.                 {
  88.                     statistics.CountVinsNotChangesСhoice++;
  89.                 }
  90.             }
  91.         }
  92.  
  93.         public bool IsWinner(Player player, Street street)
  94.         {
  95.             int vinIndex = street.doors.FindIndex(x => x == Items.Car);
  96.             return player.selectedElement == vinIndex;
  97.         }
  98.  
  99.         public int AnotherСhoice(Player player, Street street)
  100.         {
  101.             return new int[] { 0, 1, 2 }
  102.                 .Where(x => x != player.discontItem && x != player.selectedElement)
  103.                 .First();
  104.         }
  105.  
  106.         public int GetNothingInStreet(Street street, int discont)
  107.         {
  108.             Street Localstreet = street;
  109.             List<int?> allNothingIndex = new List<int?>();
  110.             allNothingIndex.Add(Localstreet.doors
  111.                 .FindAll(x => x == Items.nothing)
  112.                 .Select(x => Localstreet.doors.FindIndex(y => y == x))
  113.                 .FirstOrDefault());
  114.             allNothingIndex.Add(Localstreet.doors
  115.                 .FindAll(x => x == Items.nothing)
  116.                 .Select(x => Localstreet.doors.FindLastIndex(y => y == x))
  117.                 .Where(x => x != allNothingIndex[0])
  118.                 .FirstOrDefault());
  119.             allNothingIndex = allNothingIndex.Where(x => x != null && x != discont).ToList();
  120.             int count = allNothingIndex.Count;
  121.             return allNothingIndex[random.Next(0, count)].GetValueOrDefault();
  122.         }
  123.     }
  124.  
  125.     public class Street
  126.     {
  127.         public List<Items> doors;
  128.         static Random random = new Random();
  129.         public Street()
  130.         {
  131.             int car = random.Next(0, 3);
  132.             doors = new List<Items>() { Items.nothing, Items.nothing, Items.nothing };
  133.             doors[car] = Items.Car;
  134.         }
  135.     }
  136.  
  137.     public class Statistics
  138.     {
  139.         public int totalGame;
  140.         public int CountVinsChangesСhoice = 0;
  141.         public int CountVinsNotChangesСhoice = 0;
  142.     }
  143.  
  144.     public enum Items
  145.     {
  146.         Car = 1,
  147.         nothing = 2
  148.     }
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement