1

I've seen some similar questions, but none seem to address the issue I'm having.

I believe the issue lies here:

    public static void dealCard(Card[] deck){

    System.out.println(deck[0]);
    Card [] decks = new Card[deck.length-1];

    for (int x = 0; x<deck.length-1; x++)
    decks[x] = deck[x];
    deck = decks;

}

So effectively what I'm trying to do every time I call this method is to print the first object in the array, and then subtract the first object from the array. Of course, neither of which are happening, because I don't know what the hell I'm doing.

Complete code:

import java.util.Random;
public class Main {
private Card [] deck;


    public static void printDeck(Card[] deck){
        for (Card d : deck)
        System.out.println("  " + d);
    }

    public static void shuffle(Card[] deck){ //  shuffles deck
        int index;

        Random ran = new Random();

        for (int i = deck.length -1; i > 0; i--){
        int poker = ran.nextInt(i + 1);
        Card b = deck[poker];
        deck[poker] = deck[i];
        deck[i] = b;

        }
    }
    public static void dealDeck(Card[] deck){

         for (Card a : deck)
         System.out.println("  " + a);
    }

    public static void dealCard(Card[] deck){

        System.out.println(deck[0]);
        Card [] decks = new Card[deck.length-1];

        for (int x = 0; x<deck.length-1; x++)
        decks[x] = deck[x];
        deck = decks;

    }




    public static void main(String[] args) {

        Card card0 = new Card();
        Card card1 = new Card();
        Card card2 = new Card();
        Card card3 = new Card();
        Card card4 = new Card();
        Card card5 = new Card();
        Card card6 = new Card();
        Card card7 = new Card();
        Card card8 = new Card();
        Card card9 = new Card();
        Card card10 = new Card();
        Card card11 = new Card();
        Card card12 = new Card();
        Card card13 = new Card();
        Card card14 = new Card();
        Card card15 = new Card();
        Card card16 = new Card();
        Card card17 = new Card();
        Card card18 = new Card();
        Card card19 = new Card();
        Card card20 = new Card();
        Card card21 = new Card();
        Card card22 = new Card();
        Card card23 = new Card();
        Card card24 = new Card();
        Card card25 = new Card();
        Card card26 = new Card();
        Card card27 = new Card();
        Card card28 = new Card();
        Card card29 = new Card();
        Card card30 = new Card();
        Card card31 = new Card();
        Card card32 = new Card();
        Card card33 = new Card();
        Card card34 = new Card();
        Card card35 = new Card();
        Card card36 = new Card();
        Card card37 = new Card();
        Card card38 = new Card();
        Card card39 = new Card();
        Card card40 = new Card();
        Card card41 = new Card();
        Card card42 = new Card();
        Card card43 = new Card();
        Card card44 = new Card();
        Card card45 = new Card();
        Card card46 = new Card();
        Card card47 = new Card();
        Card card48 = new Card();
        Card card49 = new Card();
        Card card50 = new Card();
        Card card51 = new Card();

        card0.setCard(1, 1);
        card1.setCard(2, 1);
        card2.setCard(3, 1);
        card3.setCard(4, 1);
        card4.setCard(5, 1);
        card5.setCard(6, 1);
        card6.setCard(7, 1);
        card7.setCard(8, 1);
        card8.setCard(9, 1);
        card9.setCard(10, 1);
        card10.setCard(11, 1);
        card11.setCard(12, 1);
        card12.setCard(13, 1);
        card13.setCard(1, 2);
        card14.setCard(2, 2);
        card15.setCard(3, 2);
        card16.setCard(4, 2);
        card17.setCard(5, 2);
        card18.setCard(6, 2);
        card19.setCard(7, 2);
        card20.setCard(8, 2);
        card21.setCard(9, 2);
        card22.setCard(10, 2);
        card23.setCard(11, 2);
        card24.setCard(12, 2);
        card25.setCard(13, 2);
        card26.setCard(1, 3);
        card27.setCard(2, 3);
        card28.setCard(3, 3);
        card29.setCard(4, 3);
        card30.setCard(5, 3);
        card31.setCard(6, 3);
        card32.setCard(7, 3);
        card33.setCard(8, 3);
        card34.setCard(9, 3);
        card35.setCard(10, 3);
        card36.setCard(11, 3);
        card37.setCard(12, 3);
        card38.setCard(13, 3);
        card39.setCard(1, 4);
        card40.setCard(2, 4);
        card41.setCard(3, 4);
        card42.setCard(4, 4);
        card43.setCard(5, 4);
        card44.setCard(6, 4);
        card45.setCard(7, 4);
        card46.setCard(8, 4);
        card47.setCard(9, 4);
        card48.setCard(10, 4);
        card49.setCard(11, 4);
        card50.setCard(12, 4);
        card51.setCard(13, 4);



        Card [] deck = {card0, card1, card2, card3, card4, card5, card6, card7, card8, card9, card10, card11, card12, card13,
                card14, card15, card16, card17, card18, card19, card20, card21, card22, card23, card24, card25, card26, card27, card28,
                card29, card30, card31, card32, card33, card34, card35, card36, card37, card38, card39, card40, card41, card42, card43,
                card44, card45, card46, card47, card48, card49, card50, card51,
                };

        System.out.println("The deck before being shuffled:");
        printDeck(deck);
        System.out.println("\n The deck has now been shuffled: \n");
        shuffle(deck);
        dealDeck(deck);
        System.out.println("deals a card:");
        dealCard(deck);
        dealCard(deck);
        dealCard(deck);
        dealCard(deck);


}
}

So I tried this:

System.out.println(deck[0]);
        Card [] decks = new Card[deck.length-1];
        int j = 0;

        for (int x = 1; x<deck.length-1; x++){

        decks[j] = deck[x];
        j++;
        
        }
        deck = decks;

and it still produces the same object with the prinln, if I put the println anywhere in this method the same object is printed over and over again.

Here is the Card class:

import java.util.Random;
public class Card {

   int face = 0;
        int suit = 0;
        String faceText;
        String faceValue;
        String suitValue;
        String suitText;

        public Card() // card constructor, initially generates card number and suit
        {
            int face = 0;
            int suit = 0;
            Random generator = new Random();
            face = generator.nextInt(13) + 1;
            suit = generator.nextInt(4) + 1;


        }

        public int getFace() // sets up card face value
        {
            Random generator = new Random();
            face = generator.nextInt(13) + 1;
            return face;
        }

        public int getSuit() // sets up card suit value
        {
            Random generator = new Random();
            suit = generator.nextInt(4) + 1;
            return suit;
        }
        public String getFacetext() // gets card numeric value textually
        {
            switch(face)
            {
                case 1:
                    faceText = "one";
                    break;

                case 2:
                    faceText = "two";
                    break;

                case 3:
                    faceText = "three";
                    break;

                case 4:
                    faceText = "four";
                    break;

                case 5:
                    faceText = "five";
                    break;

                case 6:
                    faceText = "six";
                    break;

                case 7:
                    faceText = "seven";
                    break;

                case 8:
                    faceText = "eight";
                    break;

                case 9:
                    faceText = "nine";
                    break;

                case 10:
                    faceText = "ten";
                    break;

                case 11:
                    faceText = "eleven";
                    break;

                case 12:
                    faceText = "twelve";
                    break;

                case 13:
                    faceText = "thirteen";
                    break;
            }

            return faceText;
        }

        public String getSuittext() // gets card's suit value
        {
            switch (suit)
            {   case 1:
                suitText = "hearts ";
                break;

                case 2:
                    suitText = "spades ";
                    break;

                case 3:
                    suitText = "diamonds ";
                    break;

                case 4:
                    suitText = "clubs ";
                    break;

            }
            return suitText;
        }
        public String setCard(int face, int suit) // sets the card to passed through toString
        {
            switch (face)
            {
                case 1:
                    faceValue = "ace";
                    break;

                case 2:
                    faceValue = "two";
                    break;

                case 3:
                    faceValue = "three";
                    break;

                case 4:
                    faceValue = "four";
                    break;

                case 5:
                    faceValue = "five";
                    break;

                case 6:
                    faceValue = "six";
                    break;

                case 7:
                    faceValue = "seven";
                    break;

                case 8:
                    faceValue = "eight";
                    break;

                case 9:
                    faceValue = "nine";
                    break;

                case 10:
                    faceValue = "ten";
                    break;

                case 11:
                    faceValue = "jack";
                    break;

                case 12:
                    faceValue = "queen";
                    break;

                case 13:
                    faceValue = "king";
                    break;
            }

            switch (suit)
            {
                case 1:
                    suitValue = "hearts ";
                    break;

                case 2:
                    suitValue = "spades ";
                    break;

                case 3:
                    suitValue = "diamonds ";
                    break;

                case 4:
                    suitValue = "clubs ";
                    break;
            }

            return suitValue;
        }

        public String toString()
        {
            String result = faceValue + " of " + suitValue;
            return result;
        }


}

3 Answers3

1

I think you are trying to do something like this. This is just and example to give you an idea .

Card class:

    class Card{
    
    String faceValue;
    String suitValue;   
    
    public String setCard(int face, int suit) // sets the card to passed through toString
    {
        switch (face)
        {
            case 1:
                faceValue = "ace";
                break;

            case 2:
                faceValue = "two";
                break;

            case 3:
                faceValue = "three";
                break;

            case 4:
                faceValue = "four";
                break;

            case 5:
                faceValue = "five";
                break;

            case 6:
                faceValue = "six";
                break;

            case 7:
                faceValue = "seven";
                break;

            case 8:
                faceValue = "eight";
                break;

            case 9:
                faceValue = "nine";
                break;

            case 10:
                faceValue = "ten";
                break;

            case 11:
                faceValue = "jack";
                break;

            case 12:
                faceValue = "queen";
                break;

            case 13:
                faceValue = "king";
                break;
        }

        switch (suit)
        {
            case 1:
                suitValue = "hearts ";
                break;

            case 2:
                suitValue = "spades ";
                break;

            case 3:
                suitValue = "diamonds ";
                break;

            case 4:
                suitValue = "clubs ";
                break;
        }

        return suitValue;
    }
}

Implementation method:

        Card card1 = new Card(2,2);
        Card card2 = new Card(3,3);
        Card card3 = new Card(4,4);
        Card card4 = new Card(5,1);
        
        Card arr[] = {card1,card2,card3,card4};// so arr has 2 of spades,3 of diamonds, 4 of clubs and 5 of hearts
        Card arr2[] = new Card[arr.length-1];
        int j=0;
        for (int i = 1; i < arr.length; i++) {
            arr2[j]=arr[i];
            j++;
        }
        
        arr = arr2 ; // Now the arr should have 3 of diamonds, 4 of clubs and 5 of hearts
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i].faceValue + " " +arr[i].suitValue);
        }
Harmandeep Singh Kalsi
  • 3,315
  • 2
  • 14
  • 26
1

Java passes arguments by-value, so that's why your code is not working. When we dereference objects we can change parameters. I've changed your code a bit in order to work.

public static void dealCard(Card[] deck){

    System.out.println(deck[0]);
    // Card [] decks = new Card[deck.length-1];

    int x;
    for (x = 0; (x<deck.length-1) && (deck[x+1]!=null); x++)
        deck[x] = deck[x+1];
    deck[x] = null;
}
Carlos Bazilio
  • 830
  • 9
  • 20
  • that produces this error: bad operand types for binary operator '&&' – Noobprogrammer Jul 07 '20 at 03:48
  • Wow, that worked!, can you tell me how and why that works! I'd like to understand. – Noobprogrammer Jul 07 '20 at 04:13
  • First, you are creating a local vector and assigning to the parameter (deck = decks;) It doesn't work because deck is a local value which contains a reference (an address) to a vector outside dealCard. So this assignment does not affect the vector passed when dealCard is called. So I've started dereferencing the parameter, which affects vector outside dealCard. When i write deck[x] = deck[x+1], compiler generates code to write to an address like deck base address + x positions. And this address is not local to dealCard, it points to an address outside. – Carlos Bazilio Jul 07 '20 at 04:30
-1

The problem lies here

You need to remove the first card, so your loop should start from second index which is 1

    public static void dealCard(Card[] deck){

        System.out.println(deck[0]);
        Card [] decks = new Card[deck.length-1];

        for (int x = 1; x<deck.length-1; x++) // This line
        decks[x] = deck[x];
        deck = decks;

    }
Franz Andel
  • 1,326
  • 12
  • 20