0

EDIT: I have partially resolved the issue. I had previously removed the for loop fro it's own method and put it into main for it to work, but for some reason it was not working this time. That's because a new Cards object was being initialized (underneath //The Flop comment), which I guess reset the deck. If i remove that and place the loop there instead of it's own method, it works. However, the issue still happens within the method because I need to create a new object to "communicate" with the deck class. I will try to either pass in the object as an argument or set it somewhere accessible and see if that works, then I will re-edit this post.

Edit 2: Passing in the Cards Object as an argument has worked! Will post as answer.

Start of post: I'm trying to write a Texas Hold 'em game for my class. I'm trying to use methods to tidy up the main(). Unfortunately, it seams Cards are being duplicated between the other arrays I have (The Player's Hand, Opponents Hand, and Board. I don't seem to be on my teacher's favorites list so he doesn't provide much help when asked for it.

Here is the main class:

  public class PokerGame
{
    private int pot = 0;
    public static void main(String[] args)
    {
        //Create Objects to connect classes.
        Deck Cards = new Deck(0, "s");
        Player Player = new Player();
        Opponents Op1 = new Opponents();

        //Bring deck into this main class.
        Deck[] aDeck = new Deck[52];
        Deck[] fDeck = Cards.buildDeck(aDeck);
        Deck[] mDeck = Cards.shuffle(fDeck, 100);


        //Create the Board and Hand arrays.
        Deck[] Board = new Deck[5];
        Deck[] Burned = new Deck[3];
        Deck[] pHand = new Deck[2];
        Deck[] o1Hand = new Deck[2];

        int count;
        for(count = 0; count < pHand.length; count++)
        {
            pHand[count] = Cards.deal(mDeck);
            System.out.println("P " + pHand[count].namedRank + " of " + pHand[count].s);
            o1Hand[count] = Cards.deal(mDeck);
        }

        System.out.println("o1 " + o1Hand[0].namedRank + " of " + o1Hand[0].s);
        System.out.println("o1 " + o1Hand[1].namedRank + " of " + o1Hand[1].s);

        //The Flop
        Cards = new Deck(0, "s");
        Burned[0] = Cards.deal(mDeck);
        Board = flop(Board, mDeck);

        //The Turn
        Burned[1] = Cards.deal(mDeck);
        Board[3] = Cards.deal(mDeck);
        System.out.println("\n\n");
        System.out.println("Now the Board is...");

        for(int b = 0; b < 4; b++)
        {
            System.out.println(Board[b].namedRank + " of " + Board[b].s);
        }

        showdown(Board, pHand, o1Hand);
    }

    public static Deck[] flop(Deck[] Board, Deck[] mDeck)
    {
        Deck Cards = new Deck(0, "s");
        System.out.println();
        System.out.println("The Board is...");
        for(int b = 0; b < 3; b++)
        {
            Board[b] = Cards.deal(mDeck);
            System.out.println(Board[b].namedRank + " of " + Board[b].s);
        }
        return Board;
    }

And this is the Deck class where I shuffle and deal.

public class Deck
{
    public Deck[] Deck = new Deck[52];
    private int index = 0;
    public int rank;
    public String s;
    public String namedRank;
    private int x = 0;

    /**
     * Constructor for card Objects
     * 
     * Cannot get to work, not sure where issue is.
     */
    public Deck(int rank, String s)
    {
        if(rank == 1)
        {
            this.rank = 14;
        }
        else
        {
            this.rank = rank;
        }

        String numRank = String.valueOf(rank);
        if(this.rank == 11)
        {
            this.namedRank = "Jack";
        }
        else if(this.rank == 12)
        {   
            this.namedRank = "Queen";
        }
        else if(this.rank == 13)
        {   
            this.namedRank = "King";
        }
        else if(this.rank == 14)
        {   
            this.namedRank = "Ace";
        }
        else
        {
            this.namedRank = numRank;
        }
        this.s = s;
    }

    /**
     * Assigns each element in the array a card
     * 
     * @param Deck array before elements assigned
     * @return Deck array after elemnts assigned
     */
    public Deck[] buildDeck(Deck[] fDeck)
    {
        String[] suit = {"Club", "Diamond", "Heart", "Spade"};

        int c;
        String of = " of ";
        for(c = 0; c < suit.length; c++)
        {
            for(rank = 1; rank < 14; rank++)
            {
                s = suit[c];
                fDeck[x] = new Deck(rank, s);
                x++;
            }
        }
        return fDeck;
    }


    /**
     * Shuffles the cards by picking to random numbers and switching them
     * 
     * @param Deck array after elemetns assigned
     * @return Deck array after elements are mixed up
     */
    public Deck[] shuffle(Deck[] mDeck, int shuffle)
    {
        for(int count = 0; count < shuffle; count++)
        {
            int f = ((int)(Math.random() * 100) % 51 + 1);
            int s = 0;

            Deck move = mDeck[f];
            mDeck[f] = mDeck[s];
            mDeck[s] = move;

        }

        return mDeck;
    }

    /**
     * Deals the cards and keeps track by incrasing the index
     * 
     * @param Deck array after elements are mixed up
     * @return single Deck array element at whatever position int index is at
     */
    public Deck deal(Deck[] dDeck)
    {
        index++;
        return dDeck[index];
    }
}

Literally any help is much appreciated as I've been scratching my head on this for a long time already. Will be continuing what I can mess around with before I have to head to work.

  • 2
    I'm confused on if the Deck class is actually a Deck or a Card. Each Deck seems to have an array of 52 Decks (itself) and then in main you create an array of Decks for hands as if they were individual cards. But each Deck in the array has that 52 element array. There seems to be a lot of wasted memory. Also your variable names are confusing, Deck is an array of Decks inside class Deck... – Tyler Nov 13 '19 at 21:08
  • Yeah, apologies, I certainly make confusingly written code. I'm reusing an class fro a previous assignment. in the Deck class, from my bad understanding of Java, I'm creating the array, then "filling in" the elements, then shuffling the deck, and lastly dealing fro that shuffled deck. So what you're saying is I'm just creating multiple arrays? Should I rename them all to just Deck? // As for the game class, I created the 4 arrays to have them be filled by the dealt cards in the shuffled deck. – Irre Levant Nov 13 '19 at 21:14
  • 1
    I think your first step is to have a `Deck` class, with a field for an array of 52 `Card` objects. Also, you'll need a `Card` class. – mypetlion Nov 13 '19 at 21:26
  • I'm not sure if the Card class would be necessary, as the Deck class has a constructor that builds each element in the array. However, I have resolved the issue and edited/answered the post. – Irre Levant Nov 13 '19 at 21:41

0 Answers0