using System; using System.Collections.Generic; using System.Linq; using System.
ID: 3710921 • Letter: U
Question
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CardDealer
{
class Deck
{
// Include any private fields here
// ...
public Deck()
{
// Include your code here
// ...
}
public void Shuffle()
{
// Include your code here
// ...
}
public string Deal()
{
// Include your code here
// ...
}
}
}
Explanation / Answer
Below is your code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CardDealer
{
class Deck
{
Random rng = new Random();
private int card = 0;
private int[] Cards;
private const int NUMBER_OF_CARDS = 52;
// Include any private fields here
private string[] Number = {"Ace", "Two", "Three", "Four", "Five", "Six", "Seven",
"Eight", "Nine", "Ten", "Knave", "Knight", "King"};
private string[] Suit = { "Cups", "Coins", "Clubs", "Swords" };
// ...
public Deck()
{
Cards = new int[NUMBER_OF_CARDS];
for (int i = 0; i < NUMBER_OF_CARDS; i++)
{
Cards[i] = i + 1;
}
}
public void Shuffle()
{
// Include your code here
// ...
card = 0;
int[] rndArray = new int[NUMBER_OF_CARDS];
int newRnd;
for (int i = 0; i < rndArray.Length; i++)
{
do
{
newRnd = rng.Next(1, NUMBER_OF_CARDS + 1);
} while (RndCheck(rndArray, newRnd));
rndArray[i] = newRnd;
}
Cards = rndArray;
}
private static bool RndCheck(int[] rndArray, int newRnd)
{
bool inArray = false;
for (int i = 0; i < rndArray.Length; i++)
{
if (rndArray[i] == newRnd)
{
inArray = true;
}
}
return inArray;
}
public string Deal()
{
// Include your code here
//
if (card >= 52)
{
return "All cards have been dealt";
}
else
{
int dealtCard;
dealtCard = Cards[card];
int suit = (int)((dealtCard-1) / 13);
int faceval = (dealtCard-1) % 13;
card++;
return (Number[faceval] + " of " + Suit[suit]);
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.