Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Data.Common;
- namespace DeckOfFateSim
- {
- internal class Program
- {
- private static void Main(string[] args)
- {
- int rows = 5;
- int columns = 8;
- int fragments = 14;
- int iterations = 100000;
- int[] outcomes = new int[rows * columns];
- for (int i = 0; i < outcomes.Length; i++)
- outcomes[i] = -1;
- Console.WriteLine("SequentialFlip Results:");
- Console.WriteLine("=======================");
- //SequentialFlip
- for (int i = 0; i < iterations; i++)
- {
- GameBoard gameboard = new GameBoard(rows, columns, fragments);
- int remaining = SequentialFlip(gameboard);
- outcomes[remaining]++;
- }
- ReportResults(outcomes, iterations);
- for (int i = 0; i < outcomes.Length; i++)
- outcomes[i] = -1;
- Console.WriteLine();
- Console.WriteLine("RandomFlip Results:");
- Console.WriteLine("===================");
- //RandomFlip
- for (int i = 0; i < iterations; i++)
- {
- GameBoard gameboard = new GameBoard(rows, columns, fragments);
- int remaining = RandomFlip(gameboard);
- outcomes[remaining]++;
- }
- ReportResults(outcomes, iterations);
- }
- private static void ReportResults(int[] outcomes, int iterations)
- {
- for (int i = 0; i < outcomes.Length; i++)
- {
- if (outcomes[i] == -1)
- continue;
- decimal percentage = (decimal)outcomes[i] / (decimal)iterations;
- Console.WriteLine(string.Format("Cards Remaining: {0}\t[{1:P}]", i, percentage));
- }
- }
- private static int SequentialFlip(GameBoard gameboard)
- {
- for (int x = 0; x < gameboard.Rows; x++)
- {
- for (int y = 0; y < gameboard.Columns; y++)
- {
- gameboard[x, y].Flip();
- if (gameboard.FragmentsRemaining() == 0)
- {
- return gameboard.CardsRemaining();
- }
- }
- }
- return 0;
- }
- private static int RandomFlip(GameBoard gameboard)
- {
- int cells = (gameboard.Rows * gameboard.Columns);
- Random rng = new Random();
- for (int i = 0; i < cells; i++)
- {
- bool flipped = false;
- while (!flipped)
- {
- int row = rng.Next(gameboard.Rows);
- int column = rng.Next(gameboard.Columns);
- Card card = gameboard[row, column];
- if (card.IsFlipped == false)
- {
- card.Flip();
- flipped = true;
- }
- }
- if (gameboard.FragmentsRemaining() == 0)
- {
- return gameboard.CardsRemaining();
- }
- }
- return 0;
- }
- }
- internal class Card
- {
- public bool IsFlipped { get; private set; }
- public bool HasFragment { get; }
- public Card(bool hasFragment)
- {
- IsFlipped = false;
- HasFragment = hasFragment;
- }
- public bool Flip()
- {
- IsFlipped = true;
- return HasFragment;
- }
- }
- internal class GameBoard
- {
- private Card[,] _cells;
- public int Rows { get; }
- public int Columns { get; }
- public int TotalFragments { get; }
- public Card this[int row, int column]
- {
- get
- {
- return _cells[row, column];
- }
- }
- public GameBoard(int rows, int columns, int fragments)
- {
- Rows = rows;
- Columns = columns;
- TotalFragments = fragments;
- _cells = new Card[Rows, Columns];
- Initialize();
- }
- private void Initialize()
- {
- Random rng = new Random();
- for (int i = 0; i < TotalFragments; i++)
- {
- bool placed = false;
- while (!placed)
- {
- int row = rng.Next(Rows);
- int column = rng.Next(Columns);
- if (_cells[row, column] is null)
- {
- _cells[row, column] = new Card(true);
- placed = true;
- }
- }
- }
- for (int x = 0; x < Rows; x++)
- {
- for (int y = 0; y < Columns; y++)
- {
- if (_cells[x, y] is null)
- {
- _cells[x, y] = new Card(false);
- }
- }
- }
- }
- public int CardsRemaining()
- {
- int count = 0;
- for (int x = 0; x < Rows; x++)
- {
- for (int y = 0; y < Columns; y++)
- {
- Card card = _cells[x, y];
- if (!card.IsFlipped)
- {
- count++;
- }
- }
- }
- return count;
- }
- public int FragmentsRemaining()
- {
- int count = 0;
- for (int x = 0; x < Rows; x++)
- {
- for (int y = 0; y < Columns; y++)
- {
- Card card = _cells[x, y];
- if (!card.IsFlipped && card.HasFragment)
- {
- count++;
- }
- }
- }
- return count;
- }
- public bool Flip(int row, int column)
- {
- return _cells[row, column].Flip();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment