I have a C# This is all one same project. Using classes at least 4 classes, I\'m
ID: 3714325 • Letter: I
Question
I have a C# This is all one same project. Using classes at least 4 classes, I'm not allowed to use anything but System, and I'm also not allowed to use inheritance.
1.) Create a deck of cards. 2.) Display your deck of cards. 3.) Randomly shuffle your deck of cards. 4.) Display your shuffled deck of cards. 5.) "Cut (Links to an external site.)Links to an external site." the shuffled deck of cards using a randomly chosen cut point. 6.) Display your cut deck of cards.
This is what I have so far, but I'm really stuck... could someone help me, please?
public enum Ranks
{
Ace,
Two,
Three,
Four,
Five,
Six,
Seven,
Eight,
Nine,
Ten,
Jack,
Queen,
King
};
public enum Suits
{
Club, Diamond, Heart, Spade
};
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
Deck D = new Deck();
D.Shuffle();
for (int i = 0; i < 52; i++)
{
Console.Write("{0, -19}", D.DealCard());
if ((i + 1) % 4 == 0)
Console.WriteLine();
}
Console.ReadLine();
//D.Shuffle();
//S.Shuffle(D);
// Console.WriteLine(D);
}
}
public class Card
{
private Suits suit;
private Ranks rank;
public Card(Ranks rank, Suits suit)
{
this.suit = suit;
this.rank = rank;
}
public void print()
{
Console.Write(rank + " of " + suit);
}
public override string ToString()
{
return "[" + rank + "of " + suit + "]";
}
}
public class Deck
{
private Card[] deck;
private int currentCard;
private int numberOfCards = 52;
public void setUpDeck()
{
this.deck = new Card[numberOfCards];
int i = 0;
currentCard = 0;
foreach (Suits s in Enum.GetValues(typeof(Suits)))
{
foreach (Ranks r in Enum.GetValues(typeof(Ranks)))
{
deck[i] = new Card(r, s);
i++;
}
}
}
}
Explanation / Answer
// Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Student.Name
{
class Program
{
static void Main(string[] args)
{
//1.) Create a deck of cards.
Deck deck = new Deck();
//2.) Display your deck of cards.
Console.WriteLine("Displaying Deck of cards");
Console.WriteLine(deck.ToString());
//3.) Randomly shuffle your deck of cards.
deck.ShuffleCards();
//4.) Display your shuffled deck of cards.
Console.WriteLine("Displaying Shuffled Deck of cards:");
Console.WriteLine(deck.ToString());
//5.) "Cut (Links to an external site.)Links to an external site." the shuffled deck of cards using a randomly chosen cut point.
int cutPosition = deck.CutDeckofCard();
//6.) Display your cut deck of cards.
Console.WriteLine("Displaying Cut Deck of cards at position {0}:", cutPosition);
Console.WriteLine(deck.ToString());
//Extra Credit: (10 points)
//7.) Deal 4 hands of 5 cards each to 4 players.
for (int i = 0; i < 4; i++)
{
String[] drawnCards = deck.DrawCards(i * 5 + 1, 5);
//8.) Display each player's hand
Console.WriteLine("Displaying hand for Player{0}:", i + 1);
for (int j = 0; j < 5; j++)
{
Console.WriteLine(drawnCards[j]);
}
}
//8.) Display remaining cards in the deck
Console.WriteLine("Displaying remaining cards in the deck:");
Console.WriteLine(deck.ToString());
}
}
}
//Deck.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Student.Name
{
class Deck
{
[Flags] enum suiteName { Heart = 1, Club, Spade, Diamond }
[Flags] enum cardName { Ace = 1, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King }
private String[] m_deck;
public Deck()
{
m_deck = CreateDeck();
}
public override string ToString()
{
String array = "";
for (int i = 0; i < m_deck.Length; i++)
{
if (m_deck[i].CompareTo("") == 0)
continue;
array += m_deck[i];
array += " ";
}
return array;
}
public void ShuffleIndexArray(int[] indexArray)
{
Random random = new Random();
for (int i = 0; i < 52; i++)
{
int rNum = i + random.Next(52 - i);
int temp = indexArray[rNum];
indexArray[rNum] = indexArray[i];
indexArray[i] = temp;
}
}
public void ShuffleCards()
{
// create random number array with distinct values
int [] indexArray = new int[52];
for (int i = 0; i < 52; i++)
{
indexArray[i] = i;
}
ShuffleIndexArray(indexArray);
String[] newDeck = new String[52];
for (int i = 0; i < 52; i++)
{
newDeck[i] = m_deck[indexArray[i]];
}
m_deck = newDeck;
}
public int CutDeckofCard()
{
Random random = new Random();
int cutIndex = random.Next(0, 51);
String[] newDeck = new String[52];
for (int i = 0; i < 52; i++)
{
newDeck[i] = m_deck[cutIndex];
if (cutIndex == 51)
cutIndex = 0;
else
cutIndex++;
}
m_deck = newDeck;
return cutIndex;
}
public String[] DrawCards(int startPosition, int numberOfcards)
{
String[] drawnCards = new String[numberOfcards];
int startIndex = startPosition - 1;
for (int itr = 0; itr < numberOfcards; itr++)
{
drawnCards[itr] = m_deck[startIndex];
m_deck[startIndex] = ""; // remove the card from the deck
startIndex++;
}
return drawnCards;
}
public String[] CreateDeck()
{
String[] theDeck = new String[52];
String sName;
for (int itr = 0; itr < 52; itr++)
{
String value;
sName = Enum.GetName(typeof(suiteName), (itr / 13 + 1));
int counter = itr % 13;
if (counter >= 0 && counter <= 13) // add cards for one suite
{
String cName = Enum.GetName(typeof(cardName), (counter + 1));
value = cName + " of " + sName;
theDeck[itr] = value;
}
}
return theDeck;
}
}
}
// output screen shots:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.