I'm trying to create a console program to simulate having a deck of cards, the user should be able to;
- Pick a number of cards at random
- Shuffle the deck
- Return the deck to its original state
I'm struggling to figure out a way to return the deck to its starting point
When I try to just initialize the array again using; string[] Deck = { x,x,x } it doesn't seem to like that either
Any pointers would be greatly appreciated! Code below;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CardArranger
{
class Program
{
static void Main(string[] args)
{
string[] Deck =
{
"D1", "D2", "D3", "D4", "D5", "D6", "D7", "D8", "D9", "D10", "DJ", "DQ", "DK",
"H1", "H2", "H3", "H4", "H5", "H6", "H7", "H8", "H9", "H10", "HJ", "HQ", "HK",
"C1", "C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9", "C10", "CJ", "CQ", "CK",
"S1", "S2", "S3", "S4", "S5", "S6", "S7", "S8", "S9", "S10", "SJ", "SQ", "SK",
};
Random r1 = new Random();
while (true)
{
//display number of random cards
Console.WriteLine("write shuffle to shuffle or 'sort' to organise the deck again");
string Uinput = Console.ReadLine();
bool isCount = int.TryParse(Uinput, out int noCards);
if (isCount)
{
for (int i = 0; i < noCards; i++)
{
Console.WriteLine(Deck[r1.Next(0, 52)]);
}
}
else
{
if (Uinput.Equals("shuffle"))
{
Shuffle(ref Deck, r1);
Console.WriteLine("Shuffled Deck");
for (int i = 0; i < Deck.Length; i++)
{
Console.WriteLine(Deck[i] + " , ");
}
Console.WriteLine();
Console.WriteLine("---");
}
else if (Uinput.Equals("sort"))
{
//Implement your sort method here
Console.WriteLine("Sorted Deck");
for (int i = 0; i < Deck.Length; i++)
{
Console.WriteLine(Deck[i] + " , ");
}
Console.WriteLine();
Console.WriteLine("---");
}
else
{
Console.WriteLine("Unrecognised Command");
}
}
Console.WriteLine("Press Any Key to Repeat");
Console.ReadKey();
}
}
//Fisher-Yates Shuffle
static void Shuffle(ref string[] OriginalArray, Random Rnd)
{
for (int i = 0; i < OriginalArray.Length; i++)
{
string tmp = OriginalArray[i];
int r = Rnd.Next(0, OriginalArray.Length);
OriginalArray[i] = OriginalArray[r];
OriginalArray[r] = tmp;
}
}
static void Sort(ref string[] ShuffledArray)
{
// sort the deck back in order
}
}
}