Guest User

Deck of Fate Simulation

a guest
Dec 30th, 2023
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.18 KB | None | 0 0
  1. using System.Data.Common;
  2.  
  3. namespace DeckOfFateSim
  4. {
  5.     internal class Program
  6.     {
  7.         private static void Main(string[] args)
  8.         {
  9.             int rows = 5;
  10.             int columns = 8;
  11.             int fragments = 14;
  12.             int iterations = 100000;
  13.  
  14.             int[] outcomes = new int[rows * columns];
  15.             for (int i = 0; i < outcomes.Length; i++)
  16.                 outcomes[i] = -1;
  17.  
  18.             Console.WriteLine("SequentialFlip Results:");
  19.             Console.WriteLine("=======================");
  20.  
  21.             //SequentialFlip
  22.             for (int i = 0; i < iterations; i++)
  23.             {
  24.                 GameBoard gameboard = new GameBoard(rows, columns, fragments);
  25.  
  26.                 int remaining = SequentialFlip(gameboard);
  27.                 outcomes[remaining]++;
  28.             }
  29.  
  30.             ReportResults(outcomes, iterations);
  31.             for (int i = 0; i < outcomes.Length; i++)
  32.                 outcomes[i] = -1;
  33.  
  34.             Console.WriteLine();
  35.             Console.WriteLine("RandomFlip Results:");
  36.             Console.WriteLine("===================");
  37.  
  38.             //RandomFlip
  39.             for (int i = 0; i < iterations; i++)
  40.             {
  41.                 GameBoard gameboard = new GameBoard(rows, columns, fragments);
  42.  
  43.                 int remaining = RandomFlip(gameboard);
  44.                 outcomes[remaining]++;
  45.             }
  46.  
  47.             ReportResults(outcomes, iterations);
  48.         }
  49.  
  50.         private static void ReportResults(int[] outcomes, int iterations)
  51.         {
  52.             for (int i = 0; i < outcomes.Length; i++)
  53.             {
  54.                 if (outcomes[i] == -1)
  55.                     continue;
  56.  
  57.                 decimal percentage = (decimal)outcomes[i] / (decimal)iterations;
  58.  
  59.                 Console.WriteLine(string.Format("Cards Remaining: {0}\t[{1:P}]", i, percentage));
  60.             }
  61.         }
  62.  
  63.         private static int SequentialFlip(GameBoard gameboard)
  64.         {
  65.             for (int x = 0; x < gameboard.Rows; x++)
  66.             {
  67.                 for (int y = 0; y < gameboard.Columns; y++)
  68.                 {
  69.                     gameboard[x, y].Flip();
  70.  
  71.                     if (gameboard.FragmentsRemaining() == 0)
  72.                     {
  73.                         return gameboard.CardsRemaining();
  74.                     }
  75.                 }
  76.             }
  77.  
  78.             return 0;
  79.         }
  80.  
  81.         private static int RandomFlip(GameBoard gameboard)
  82.         {
  83.             int cells = (gameboard.Rows * gameboard.Columns);
  84.             Random rng = new Random();
  85.  
  86.             for (int i = 0; i < cells; i++)
  87.             {
  88.                 bool flipped = false;
  89.  
  90.                 while (!flipped)
  91.                 {
  92.                     int row = rng.Next(gameboard.Rows);
  93.                     int column = rng.Next(gameboard.Columns);
  94.  
  95.                     Card card = gameboard[row, column];
  96.  
  97.                     if (card.IsFlipped == false)
  98.                     {
  99.                         card.Flip();
  100.                         flipped = true;
  101.                     }
  102.                 }
  103.  
  104.                 if (gameboard.FragmentsRemaining() == 0)
  105.                 {
  106.                     return gameboard.CardsRemaining();
  107.                 }
  108.             }
  109.  
  110.             return 0;
  111.         }
  112.     }
  113.  
  114.     internal class Card
  115.     {
  116.         public bool IsFlipped { get; private set; }
  117.         public bool HasFragment { get; }
  118.  
  119.  
  120.         public Card(bool hasFragment)
  121.         {
  122.             IsFlipped = false;
  123.             HasFragment = hasFragment;
  124.         }
  125.  
  126.         public bool Flip()
  127.         {
  128.             IsFlipped = true;
  129.  
  130.             return HasFragment;
  131.         }
  132.     }
  133.  
  134.     internal class GameBoard
  135.     {
  136.         private Card[,] _cells;
  137.  
  138.         public int Rows { get; }
  139.         public int Columns { get; }
  140.         public int TotalFragments { get; }
  141.  
  142.         public Card this[int row, int column]
  143.         {
  144.             get
  145.             {
  146.                 return _cells[row, column];
  147.             }
  148.         }
  149.  
  150.  
  151.         public GameBoard(int rows, int columns, int fragments)
  152.         {
  153.             Rows = rows;
  154.             Columns = columns;
  155.             TotalFragments = fragments;
  156.  
  157.             _cells = new Card[Rows, Columns];
  158.             Initialize();
  159.         }
  160.  
  161.         private void Initialize()
  162.         {
  163.             Random rng = new Random();
  164.  
  165.             for (int i = 0; i < TotalFragments; i++)
  166.             {
  167.                 bool placed = false;
  168.  
  169.                 while (!placed)
  170.                 {
  171.                     int row = rng.Next(Rows);
  172.                     int column = rng.Next(Columns);
  173.  
  174.                     if (_cells[row, column] is null)
  175.                     {
  176.                         _cells[row, column] = new Card(true);
  177.                         placed = true;
  178.                     }
  179.                 }
  180.             }
  181.  
  182.             for (int x = 0; x < Rows; x++)
  183.             {
  184.                 for (int y = 0; y < Columns; y++)
  185.                 {
  186.                     if (_cells[x, y] is null)
  187.                     {
  188.                         _cells[x, y] = new Card(false);
  189.                     }
  190.                 }
  191.             }
  192.         }
  193.  
  194.         public int CardsRemaining()
  195.         {
  196.             int count = 0;
  197.  
  198.             for (int x = 0; x < Rows; x++)
  199.             {
  200.                 for (int y = 0; y < Columns; y++)
  201.                 {
  202.                     Card card = _cells[x, y];
  203.  
  204.                     if (!card.IsFlipped)
  205.                     {
  206.                         count++;
  207.                     }
  208.                 }
  209.             }
  210.  
  211.             return count;
  212.         }
  213.  
  214.         public int FragmentsRemaining()
  215.         {
  216.             int count = 0;
  217.  
  218.             for (int x = 0; x < Rows; x++)
  219.             {
  220.                 for (int y = 0; y < Columns; y++)
  221.                 {
  222.                     Card card = _cells[x, y];
  223.  
  224.                     if (!card.IsFlipped && card.HasFragment)
  225.                     {
  226.                         count++;
  227.                     }
  228.                 }
  229.             }
  230.  
  231.             return count;
  232.         }
  233.  
  234.         public bool Flip(int row, int column)
  235.         {
  236.             return _cells[row, column].Flip();
  237.         }
  238.     }
  239. }
Advertisement
Add Comment
Please, Sign In to add comment