0

I hope I can make this work without uploading my code. I have a Draw Poker console application in VS 2012 with about 5 classes that is object oriented. Yes, it is homework but I am an experienced developer learning C#.

My objective is to replenish the deck after every hand so that the player can draw from 52 cards and conceivably lose 100 points at 1 point per draw. This is not possible if the deck is depleted after a few draws, which is happening.

Problem: I was told to call a CreateDeck method but I don't see how to do so without calling the constructor. Yet if I do, a new Deck object is instantiated but not referenced; the original deck is referenced and depleted rapidly through each draw.

Associated problem is that multiple DealHands are executed per draw thereby multiply the pace at which the deck is depleted.

Since this system is hundreds of lines of code scattered across five classes, I don't see how to include my code. While I could, obviously include the method calls, I was told that the constructor will not work on itself. Not sure what the instructor meant.

I can email the zipped system to an interested party.

Thank you.

  • 4
    Emailing a zip to interested parties is not a useful mechanism, because anybody reading this question and any answers has no way to see it. Can you distill your problem into a simple example, and post that code? – hatchet - done with SOverflow Aug 11 '13 at 02:34
  • Post the constructor code and `CreateMethod()`? – David Tansey Aug 11 '13 at 02:35
  • 3
    My guess, based on obviously incomplete information, is that the suggestion was to make the `CreateDeck()` method static. Static methods are attached to the type, and not any particular instance of the type. That means that you can call `Deck.CreateDeck()` from anywhere, without creating an instance. Not really enough information here to determine if this is what you are looking for. – Chris Shain Aug 11 '13 at 02:37
  • Use [Pastebin](http://pastebin.com/) to show your code to interested people. – Olcay Ertaş Aug 11 '13 at 02:37
  • I am confused as to why the deck is depleted after a few draws, is this multi-player or single player? Are there multiple rounds of draws? – Karl Anderson Aug 11 '13 at 02:39
  • 2
    Even pastebin is not ideal. To make the question useful to people reading stackoverflow in the future, the question should include all the needed context, without external dependencies. – hatchet - done with SOverflow Aug 11 '13 at 02:40
  • What is the difference between replenishing a deck after every hand, and just creating an entire new deck after every hand? – hatchet - done with SOverflow Aug 11 '13 at 02:41
  • It is [_possible_](http://stackoverflow.com/questions/17675720/how-can-it-be-that-this-null/17675957#17675957) to call an instance non-virtual method on a class without instantiating it. But I think this really needs to be solved not by this hack, but by refactoring/cleaning your code. There's no reason for something as simple as this to be several hundred lines of code across five classes. – Chris Sinclair Aug 11 '13 at 03:31

1 Answers1

2

Create a new Deck every time you start a new game:

Deck deck = new Deck();
...
Card card = deck.NextCard();

In the constructor of Deck, have code that sets up a new 52 card shuffled deck.

public class Deck
{
    public Deck ()
    {
        // Create List/Array of shuffled cards here
    }
}

or

If you really want this as a static method add a Shuffle() method to Deck

public static void Shuffle()
{
    // Shuffle cards here
}

And call

Deck.Shuffle() 

Note that you are calling Shuffle on the class not the deck instance, as in the first example. In this case, your List/Array of cards must also be static.

Ryan
  • 3,924
  • 6
  • 46
  • 69
  • 2
    I am pretty sure the OP is looking for an answer involving `static`, as the question states calling a method without first calling its constructor; not saying your answer is wrong, but that it really does not address the intended question of the OP. – Karl Anderson Aug 11 '13 at 02:44