Hi I\'m having toruble with a C# visual studio task I have. The details are belo
ID: 3715086 • Letter: H
Question
Hi I'm having toruble with a C# visual studio task I have. The details are below. Any help would be appreicated :)
For this task you are asked to create a Deck class representing a deck of 52 playing cards.
The deck will be made up of 52 regular playing cards and will not include any extra cards that may be found in a typical deck (such as jokers or rule cards).
The 52 cards will be divided up into four suits of 13 cards each, with the suits named after the suits in a pack of French playing cards.
The suit names are, in order: Hearts, Diamonds, Clubs, Spades
The names of the 13 cards are, in order: Ace, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King.
When a Deck is first created, before the cards in it are shuffled, the cards should be in a particular order. That order is that the 13 cards (in order from Ace to King) from the Hearts suit should appear first, followed by the 13 cards from the Diamonds suit, followed by the Clubs, followed by the Spades suit. Therefore the first card in a newly created Deck should be the Ace of Hearts, while the last card should be the King of Spades.
The Deck class will provide three public methods: a constructor for creating a new deck in the order listed above. A Shuffle() method for shuffling the cards in the deck into a random order, and a Deal() method for dealing one card off the top of the deck..
public Deck()
The default constructor. This creates a new deck of 52 cards.
The cards in this newly-created deck are not shuffled, but are instead in the new deck order given above.
public void Shuffle()
This method shuffles the cards in the deck into a random order. As this method is to be random it should result in a different order each time it is called.
In addition, calling this method should shuffle any previously dealt cards back into the deck. For example, if you create a new deck and deal 10 cards from it with Deal() and then call Shuffle() the deck will have all 52 cards in it again.
public string Deal()
This method deals the top card off the deck, removing that card from the deck (until it is next shuffled). Because this method deals the first card off the top, if the deck is a newly-created deck and has not been shuffled yet, the first call to Deal() will return the Ace of Hearts. Calling Deal() again 51 more times will return each card successive in the deck in order-- the Two of Hearts, the Three of Hearts etc. etc. all the way up to the King of Spades. This method returns the name of the card as a string, in this format: "{card} of {suit}" (for example, "Jack of Diamonds")
Naturally, if the deck has been shuffled, the cards could come out in any order- however, as dealing a card removes it from the deck until it is next shuffled, the same card will not be dealt twice.
If Deal() is called when there are no cards left in the deck (in other words, Deal() has been called 52 times without shuffling in between) must not return the name of a card, but must instead return "(no more cards)". Subsequent calls to Deal() will continue returning (no more cards) until the deck is shuffled again.
For this exercise, you are not provided with a Main() method; you should write your own to test your Deck class.
Explanation / Answer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;
namespace ConsoleApplication1
{
public class Card
{
string _suitName;
string _cardName;
public string SuitName { get { return _suitName; } }
public string CardName { get { return _cardName; } }
public Card(string suitName, string cardName)
{
_suitName = suitName;
_cardName = cardName;
}
}
class Deck
{
string[] _possibleSuitName = new string[] { "Hearts", "Diamonds", "Clubs", "Spades" };
string[] _possibleCardName = new string[]{"Ace", "Two", "Three", "Four",
"Five", "Six", "Seven", "Eight",
"Nine", "Ten", "Jack", "Queen", "King"};
List<Card> _cardList;
public Deck()
{
_cardList = new List<Card>();
Initialize();
}
private void Initialize()
{
for (int suitNumber = 0; suitNumber < 4; suitNumber++)
{
for (int cardNumber = 0; cardNumber < 13; cardNumber++)
{
Card card = new Card(_possibleSuitName[suitNumber], _possibleCardName[cardNumber]);
_cardList.Add(card);
}
}
}
public void Display()
{
Console.WriteLine("=============Card Order==================");
foreach (Card c in _cardList)
{
Console.WriteLine("{0} of {1}", c.CardName, c.SuitName);
int index = _cardList.IndexOf(c);
Console.WriteLine("index {0}", index);
}
}
public void Shuffle()
{
_cardList = new List<Card>();
Initialize();
// The class is genrates random numbers with better quality
RNGCryptoServiceProvider provider = new RNGCryptoServiceProvider();
int n = _cardList.Count;
while (n > 1)
{
byte[] b = new byte[1];
do provider.GetBytes(b);
while (!(b[0] < n * (Byte.MaxValue / n)));
int k = (b[0] % n);
n--;
//shuffling the list values
Card value = _cardList[k];
_cardList[k] = _cardList[n];
_cardList[n] = value;
}
}
public string Deal()
{
if (_cardList.Count > 0)
{
Card res = _cardList[0];
_cardList.Remove(res);
return string.Format("{0} of {1}", res.CardName, res.SuitName);
}
else
{
return "no more cards";
}
}
}
class Program
{
static void Main(string[] args)
{
Deck deck = new Deck();
// display the card data
deck.Display();
//Shuffle the cards
deck.Shuffle();
//display the cards after shuffle
deck.Display();
//Demo the remove value
string deal = deck.Deal();
Console.WriteLine("Deal result = {0}", deal);
//Full demo for deal function
for (int i = 0; i < 55; i++)
{
string dealResult = deck.Deal();
Console.WriteLine("Deal result = {0}", dealResult);
}
//calling shuffle again printing the values
deck.Shuffle();
deck.Display();
}
}
}
//PLEASE PROVIDE FEEDBACK THUMBS UP
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.