Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C# In this assignment, you will be creating a rather simple program that sport a

ID: 3818448 • Letter: C

Question

C#

In this assignment, you will be creating a rather simple program that sport a deck of cards, a dealer, and allow you to choose how many players are at the table. The dealer will shuffle the deck and distribute cards in a round-robin style to each player. While this assignment will not have any game rules, we will be able to use it in the future to build any card game we wish!

Requirements

               First, let us set some ground rules:

You are FORBIDDEN from using the main method for anything other than simply starting the application. Minimal code for startup is expected here.

You will practice proper encapsulation of ALL data fields in ALL classes.

You are to practice proper abstraction. Every class should represent one specific thing and everything needed for the application should have a class. Every method should have one purpose. Generally speaking, methods should not exceed 12 statements. The 12-statement “rule” is more of a guideline, mind you, but that should help you identify methods that may need to be re-architected.

With all that said, here are the requirements for the application:

Be in C#

Create a new console application.

Add two enums, Suit and Rank

Suit represents the four suits of a standard deck of cards

Rank represents the 13 distinct values of the cards

Create a Card class. Each card has a rank and a suit. Once set, neither of these fields should EVER be able to change. In other words, once an instance of Card is a 4 of Hearts, it is ALWAYS a 4 of Hearts. The class should also override the ToString method to represent the Card’s data.

Create a Deck class. The Deck should have an array of 52 Cards. The Deck is only a standard deck of cards, so it knows which cards it needs and how to build them. The Deck is the ONLY one allowed to directly access the array of Cards. The Deck knows how to shuffle and how to deal a single Card. Once the Deck is out of Cards, it must be shuffled again before it can deal the next Card. The Deck also overrides the ToString to create a string representing the remaining Cards in the Deck.

Create a Player class. Each Player has a name and a hand (which is a List). A Player can receive a Card into its hand, can return a Card from its hand, and return all cards from its hand. The Player overrides the ToString to represent the Player’s name and its current hand of Cards (if any).

Create a Dealer class. The Dealer has a collection of Players and a Deck. The Dealer is the class that interfaces directly with the user.

At startup, create a new instance of Dealer, who in turn creates a new instance of Deck (remember, the Deck knows how to build the Cards it needs; the Dealer doesn’t have to build the Cards for the Deck). The Deck should NOT be shuffled yet. Think of the Deck as fresh out of the box.

When the application begins, prompt the user for how many Players they want.

For each Player, prompt the user to provide a name

i.Null, empty, and whitespace-only entries are NOT valid inputs for a Player name

After the Players are set, present the user with the following menu:

Print the Deck

Shuffle the Deck

Deal Cards

Deal one Card to a Player

Print the Players

Set new Players

Exit

Print the deck displays all of the remaining Cards in the Deck to the console in their current order. Make sure this is easily readable and user friendly in its format.

Shuffle the Deck results in the Deck being shuffled. If any Player has Cards in hand, those Cards are removed from the Player hand prior to the shuffle. Essentially, shuffling the Deck resets the Cards dealt.

Deal Cards prompts the user for how many Cards to deal each player and then, in a round-robin style, deals that many cards to each Player. If there are not enough cards left to meet the user’s request, the Dealer will still deal all the Cards it can. For example, if there are 3 Players and only 10 cards left, then when the user tries to deal 5 cards to each Player, the first Player will have 4 Cards, and the remaining two Players each receive 3 Cards.

Deal one Card to a Player prompts the user for which Player should receive the Card. Once selected, and assuming there are Cards left in the Deck, the Dealer will deal the next Card from the Deck to the selected Player. If there are no Cards in the Deck when this menu option is selected, a message indicating there are no cards left should be displayed INSTEAD of the prompt to select a Player.

Print Players prints each Player (meaning their name and hand) to the console. Again, make sure this is done in a readable, user-friendly format.

Set new Players empties the collection of Players, returns all Cards to the Deck, and starts over by prompting the user for the number of Players (as found in #8 above). After the Players are set up with names, the application continues as described above.

All invalid inputs should result in a useful error message and prompting the user again. No exceptions from bad input should reach the console.

Keep looping the application until the user selects Exit. Once they select Exit from the menu in #9, close the application.

Explanation / Answer

import java.util.Random;

open class DeckOfCard {

/*Collections of cards*/

private Card cards[];

/*Remaining number of cards in the deck*/

private int currentCount;

/*Size of the deck which can deal with cards*/

private int estimate;

private Suit suits[];

private Value values[];

open DeckOfCard(){

size=52;

currentCount=0;

cards = new Card[size];

suits=Suit.values();

values=Value.values();

for(int i =0 ; i<suits.length;i++){

for(int j =0 ; j<values.length;j++){

cards[currentCount++] = new Card(suits[i],values[j]);

}

}

}

/*This is to rearrange the cards in the deck with the rest of the cards.

Variable numberOftime speaks to the quantity of time need to rearrange the cards in deck*/

open void shuffle(int numberOftime){

Irregular rand= new Random();

for(int i=0;i<numberOftime;i++){

int m=rand.nextInt(currentCount);

int n=rand.nextInt(currentCount);

Card temp=cards[m];

cards[m]=cards[n];

cards[n]=temp;

}

}

/*This capacity is to bargain the cards whatever is on top of the deck.*/

open void deal(){

System.out.println(cards[- - currentCount]);

}

/*This is to speaks to the String portrayal of the present cards inthe deck.*/

open String toString(){

StringBuilder sb=new StringBuilder();

for(int i=0;i < currentCount;i++){

sb.append(cards[i]);

sb.append(" ");

}

return sb.toString();

}

/* Sorting the cards in light of the suit and after that numbers.

This sorting is utilizing Bucket Sort to sort the cards runtime= O(n) space=O(n);*/

open void sort(){

Card bucketCards[][]= new Card[suits.length][values.length];

for(int i=0;i<currentCount;i++){

bucketCards[cards[i].getSuit().ordinal()][cards[i].getValue().ordinal()]=cards[i];

}

int pointer=0;

for(int i=0;i<suits.length;i++){

for(int j=0;j<values.length;j++){

if(bucketCards[i][j]!=null)

cards[pointer++]=bucketCards[i][j];

}

}

}

/*This will reset the deck. i.e it will return every one of the cards in the deck in the event that it has been dealt.*/

open void resetdesk(){

currentCount=52;

}

/*Printing the pile of cards in format*/

open void printStack(){

int cardPointer=0;

for(int i=0;i<suits.length;i++){

for(int j=0;j<values.length;j++){

System.out.print(cards[cardPointer++]+" ");

}

System.out.println(" ");

}

}

/*Get size of the deck*/

open int getSize() {

return estimate;

}

/*This speaks to the suit of the card*/

private enum Suit{

CLUB,DIAMOND,SPADE,HEART

}

/* This speaks to the quantity of the card*/

private enum Value{

ACE,TWO,THREE,FOUR,FIVE,SIX,SEVEN,EIGHT,NINE,TEN,JACK,QUEEN,KING

}

/* This class speaks to the card with particular suit and esteem.

* Cannot change the esteem once instansiated */

private class Card{

private last suit;

private last Value esteem;

open Card(Suit suit, Value value){

this.suit=suit;

this.value=value;

}

open Suit getSuit() {

return suit;

}

open Value getValue() {

return esteem;

}

@Override

open String toString() {

/TODO Auto-created technique stub

return suit+"- "+value;

}

}

}

open class Test {

/**

* @param args

* @throws StackOverFlow

* @throws StackUnderFlow

*/

open static void main(String[] args) {

int n=10;/Number of cards to bargain for test

DeckOfCard deck=new DeckOfCard();

deck.printStack();

deck.shuffle(10);

System.out.println("- - - After rearrange - - - ");

deck.shuffle(30);

deck.printStack();

System.out.println("- - - Dealing 10 cards - - - ");

for(int i=0;i<n;i++){

deck.deal();

}

System.out.println("- - - After Sort - - - ");

deck.sort();

deck.printStack();

System.out.println("- - - Dealing 10 cards - - - ");

for(int i=0;i<n;i++){

deck.deal();

}

System.out.println("- - - Putting back all cards - - - ");

deck.resetdesk();

System.out.println("- - - After Sort - - - ");

deck.sort();

deck.printStack();

System.out.println("- - - Dealing 10 cards - - - ");

for(int i=0;i<n;i++){

deck.deal();

}

}

}