Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

It is a c# poker game probleam. I have my own cardlibrary and programs.cs file but with some erro that I cant fixed. please help

It is a c# poker game probleam. I have my own cardlibrary and programs.cs file but with some erro that I cant fixed. please help me fixed the erro I have and those 3 project. Thank you so much !!! This is the first of 3 parts of the final phase of the project we will be doing in this course. The three parts are: [A] Prevent duplicate cards from being drawn from the deck. [B] Let the user replace one of their cards in their hand, and auto-replace a low card in the computers hand. [C] Recognize if entire hand is all the same suit (a flush), and treat that as a winning hand. In this assignment, we will be working on part A which makes sure that there are no duplicates in either the computers hand or the users hand. Read the document carefully to make sure you have implemented everything required. Print a copy and mark off each thing you have completed so you do not miss any step. The Goal Our Deck is made up of 52 unique card. During one cycle of the game, we deal out two hands, usually 5 cards, and as you will see later, sometimes one or two more cards. That means for each cycle of the game, each pass through the main loop, we use between 10 and 12 cards. They should never be duplicates. In a real deck of cards, if you take a card out and give it to a player, obviously it would be impossible to give out that same card again from the deck. So we will make our game comply. Also note carefully that at the end of each game loop, we put all the cards back in the deck, so the same cards can be seen over and over again in subsequent loops, you just should never see the same card repeated within one turn. Implementation To do this, we will create a new property in our SuperCard Class. It will be a bool property, called isInPlay Every time any method in our CardSet Class gives out a card, it must mark that card as beingin-Play by setting that cards isInPlay property to true. But also, this means every time it selects a card to give out, it must first check if that card is in play already (is the isInPlay property already true?). If it is, then we cannot use that card, and we must find another card that is not in play. At the beginning of each game round (pass through the while loop), we will reshuffle the deck. For our game, all that means is we will set all the cards isInPlay property back to false, so all cards are again available for the next hand. Steps: 1) In the SuperCard Class, add a new public property of type bool, called isInPlay with both get and set accessor. You can make it auto-implemented. When we give out a card, this property should be set to true and when we shuffle the deck, this property should be set to false. 2) In the CardSet Class, create a new public method called ResetUsage. This method needs to loop through all 52 cards in the array and for each card, it sets its isInPlay property to false. 3) In the CardSet Class, modify your existing GetCards method, to make sure that each card it gives out is not already in use. This will require some thinking. You will need a loop that keeps track count of notin-play cards picked so far and end this loop when that count equals the number of cards requested (the argument of this method, typically 5). Inside of this outer loop, you will need a loop that looks for the next random card that is not-in-play. When you find it, i) you should add it to the return array, ii) you should also set its isInPlay property to true (to indicate now it is handed out and is in play), iii) you should increment the count used by the outer loop that is keeping track of total not-in-play cards added so far, iv) break out of this inner loop. 4) In program.cs, at the very beginning of each round, the while loop, call the ResetUsage method on the myDeck object so that all cards are available again. Test your program carefully and make sure you dont see any duplicate cards within one turn/loop. Set your howManyCards variable to 10, so that each turn will show 10 cards for player, and 10 cards for computer, for a total of 20 cards. Since they are sorted, it will make finding duplicates pretty easy. Its easy to check visually, check by just looking at the red cards, then the blue, then black, and then dark red across both hands. If you see the same card showing up twice, you have a bug. Also, play at least 4 turns in a game to make sure you are putting the cards back in play. If you do not, your program will probably get stuck in an infinite loop. image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

-----Libraryfile

namespace CardLibrary

{

public enum Suit

{

Club = 1,

Diamond,

Heart,

Spade

}

public enum Rank

{

Deuce = 2,

Three = 3,

Four = 4,

Five = 5,

Six = 6,

Seven = 7,

Eight = 8,

Nine = 9,

Ten = 10,

Jack = 11,

Queen = 12,

King = 13,

Ace = 14

}

public abstract class SuperCard : IComparable

{

public Rank CardRank { get; set; }

public abstract Suit CardSuit { get; }

public abstract void Display();

public int CompareTo(SuperCard other)

{

if (other.CardSuit == CardSuit)

{

if (other.CardRank

{

return 1;

}

else if (other.CardRank == CardRank)

{

return 0;

}

return -1;

}

else if ( other.CardSuit

{

return 1;

}

else

return 0;

}

}

public class CardClub : SuperCard

{

public override Suit CardSuit

{

get

{

return Suit.Club;

}

}

public CardClub(Rank r)

{

this.CardRank = r;

}

public override void Display()

{

Console.BackgroundColor = ConsoleColor.White;

Console.ForegroundColor = ConsoleColor.Blue;

Console.WriteLine(CardRank + " of " + CardSuit + "s ?");

Console.ResetColor();

}

}

public class CardDiamond : SuperCard

{

public override Suit CardSuit

{

get

{

return Suit.Diamond;

}

}

public CardDiamond(Rank r)

{

this.CardRank = r;

}

public override void Display()

{

Console.BackgroundColor = ConsoleColor.White;

Console.ForegroundColor = ConsoleColor.DarkRed;

Console.WriteLine(CardRank + " of " + CardSuit + "s ?");

Console.ResetColor();

}

}

public class CardHeart : SuperCard

{

public override Suit CardSuit

{

get

{

return Suit.Heart;

}

}

public CardHeart(Rank r)

{

this.CardRank = r;

}

public override void Display()

{

Console.BackgroundColor = ConsoleColor.White;

Console.ForegroundColor = ConsoleColor.Red;

Console.WriteLine(CardRank + " of " + CardSuit + "s ?");

Console.ResetColor();

}

}

public class CardSpade : SuperCard

{

public override Suit CardSuit

{

get

{

return Suit.Spade;

}

}

public CardSpade(Rank r)

{

this.CardRank = r;

}

public override void Display()

{

Console.BackgroundColor = ConsoleColor.White;

Console.ForegroundColor = ConsoleColor.Black;

Console.WriteLine(CardRank + " of " + CardSuit + "s ?");

Console.ResetColor();

}

}

public class CardSet

{

public SuperCard[] cardArray;

public CardSet()

{

// Create the array

cardArray = new SuperCard[52];

int index1 = 0; //this is the index to be used as index of the cardArray array

// start by adding all the Club cards - one for each rank

foreach (Rank r in (Rank[])Enum.GetValues(typeof(Rank)))

{

cardArray[index1] = new CardClub(r);

index1++; // increment the index in the cardArray array

}

// Now add all the Diamond cards - one for each rank

foreach (Rank r in (Rank[])Enum.GetValues(typeof(Rank)))

{

cardArray[index1] = new CardDiamond(r);

index1++; // increment the index in the cardArray array

}

//Now add all the Heart cards - one for each rank

foreach (Rank r in (Rank[])Enum.GetValues(typeof(Rank)))

{

cardArray[index1] = new CardHeart(r);

index1++; // increment the index in the cardArray array

}

// Now add all the Spade cards - one for each rank

foreach (Rank r in (Rank[])Enum.GetValues(typeof(Rank)))

{

cardArray[index1] = new CardSpade(r);

index1++; // increment the index in the cardArray array

}

}

}

}

Programs.cs file

sing System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using CardLibrary;

namespace PokerProject

{

class Program

{

const int INITIAL_BALANCE = 10;

const int BETAMOUNT_PERHAND = 1;

static void Main(string[] args)

{

// a. Create an instance of your CardSet class, and name it myDeck.

CardLibrary.CardSet myDeck = new CardLibrary.CardSet();

int balance = INITIAL_BALANCE;

Console.ForegroundColor = ConsoleColor.Yellow;

Console.BackgroundColor = ConsoleColor.DarkBlue;

Console.WriteLine("Welcome to the Poker game");

Console.WriteLine("Your starting balance is $" + balance + " and you will bet $" + BETAMOUNT_PERHAND + " for each hand");

Console.Write("Press any key to start");

Console.ReadLine();

while (balance > 0)

{

SuperCard[] computerHand = new SuperCard[5];

SuperCard[] playersHand = new SuperCard[5];

Array.Sort(computerHand);

Array.Sort(playersHand);

DisplayHands(computerHand, playersHand);

bool won = CompareHands(computerHand, playersHand);

if (won)

{

balance = balance + BETAMOUNT_PERHAND;

Console.WriteLine(" You won. Your balance is $" + balance);

}

else

{

balance = balance - BETAMOUNT_PERHAND;

Console.WriteLine(" You lost. Your balance is $" + balance);

}

Console.WriteLine("Press any key to play next hand (press q to quit)");

string ans = Console.ReadLine().ToLower();

if (ans.StartsWith("q"))

{

break;

}

//}

if (balance == 0)

Console.WriteLine("Your balance is 0, game over. Press any key to exit");

else

Console.WriteLine("Good bye. Press any key to exit");

// end of program

Console.ReadLine();

}

}

private static void DisplayHands(SuperCard[] computerHand, SuperCard[] playersHand)

{

Console.Clear();

Console.BackgroundColor = ConsoleColor.DarkBlue;

Console.WriteLine("COMPUTER'S HAND:");

for (int i = 0; i

{

computerHand[i].Display();

}

Console.ForegroundColor = ConsoleColor.Yellow;

Console.BackgroundColor = ConsoleColor.DarkBlue;

Console.WriteLine();

Console.WriteLine("PLAYER'S HAND:");

for (int i = 0; i

{

playersHand[i].Display();

}

Console.ForegroundColor = ConsoleColor.Yellow;

Console.BackgroundColor = ConsoleColor.DarkBlue;

Console.WriteLine();

}

private static bool CompareHands(SuperCard[] computerHand, SuperCard[] playersHand)

{

int sumComputer = 0;

int sumPlayer = 0;

for (int i = 0; i

{

sumComputer += (int)computerHand[i].CardRank;

sumPlayer += (int)playersHand[i].CardRank;

}

if (sumPlayer > sumComputer)

{

Console.WriteLine("You WIN !!");

return true;

}

else

{

Console.WriteLine("Computer WIN, You Lost!");

return false;

}

}

}

}

[B] Let the user replace one of their cards in their hand, and auto-replace a low card in the computer's [C] Recognize if entire hand is all the same suit (a "flush"), and treat that as a winning hand In this assignment, we will be working on part B which lets user replace one of their cards in their hand, and auto-replace a low card in the computer's hand. Read the document carefully to make sure you have implemented everything required. The Goal We want to allow the user and the computer to replace one card to improve their hand before deciding who won Implementation Steps: 1) First add a new method in Cardset class called GetOneCard that will be used by the user to replace one card public SuperCard GetOneCard) // gets one card from the deck of 52. Using very similar code to your GetCards method, add this new method. It must have similar logic to make sure the card it returns is not already in use, and it must set the property of the card it returns to be true. Note also that this method returns a single SuperCard, and not an array of cards as is done in GetCards method In program.cs, implement a new method called PlayerDrawsOne that allows the user to replace one of the cards if the user wants to: public void PlayerDrawsOne (SuperCard[] hand, Cardset deck) Start by asking the user which card they want to replace, and allow them to type 1,2,3,4 or 5, etc, which is to indicate which card they want to replace, or they can enter a 0 if they don't want to change any card. If they do want to change a card, call your new GetOneCard method to get a new card, and replace that one card object in their hand array corresponding to the number they selected. Remember, arrays are "by reference" types, so when you pass in the array to the PlayerDrawsOne method, and the method modifies the hand array there, you don't have to return anything, as the original hand array is now modified too. Also note, if for example they say they want to replace their 3rd card, you should replace the card at index 2, since arrays are zero-index based. In program.cs, in the main game loop, right after the cards for the player are displayed, call the new method PlayerDrawsOne passing the player's hand and myDeck field of type Cardset. 2) 3) Get this working before going forward. You should be able to play the game, and pick any of your cards to be replaced, or select a 0 and replace none. You will now have an advantage over the computer, as [B] Let the user replace one of their cards in their hand, and auto-replace a low card in the computer's [C] Recognize if entire hand is all the same suit (a "flush"), and treat that as a winning hand In this assignment, we will be working on part B which lets user replace one of their cards in their hand, and auto-replace a low card in the computer's hand. Read the document carefully to make sure you have implemented everything required. The Goal We want to allow the user and the computer to replace one card to improve their hand before deciding who won Implementation Steps: 1) First add a new method in Cardset class called GetOneCard that will be used by the user to replace one card public SuperCard GetOneCard) // gets one card from the deck of 52. Using very similar code to your GetCards method, add this new method. It must have similar logic to make sure the card it returns is not already in use, and it must set the property of the card it returns to be true. Note also that this method returns a single SuperCard, and not an array of cards as is done in GetCards method In program.cs, implement a new method called PlayerDrawsOne that allows the user to replace one of the cards if the user wants to: public void PlayerDrawsOne (SuperCard[] hand, Cardset deck) Start by asking the user which card they want to replace, and allow them to type 1,2,3,4 or 5, etc, which is to indicate which card they want to replace, or they can enter a 0 if they don't want to change any card. If they do want to change a card, call your new GetOneCard method to get a new card, and replace that one card object in their hand array corresponding to the number they selected. Remember, arrays are "by reference" types, so when you pass in the array to the PlayerDrawsOne method, and the method modifies the hand array there, you don't have to return anything, as the original hand array is now modified too. Also note, if for example they say they want to replace their 3rd card, you should replace the card at index 2, since arrays are zero-index based. In program.cs, in the main game loop, right after the cards for the player are displayed, call the new method PlayerDrawsOne passing the player's hand and myDeck field of type Cardset. 2) 3) Get this working before going forward. You should be able to play the game, and pick any of your cards to be replaced, or select a 0 and replace none. You will now have an advantage over the computer, as

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

RP-7 What is natural selection?

Answered: 1 week ago