-1

A shuffling and dealing program.

The program interacts between cards and four players among whom cards are to be distributed.

The Program does the following action

  • Creates a deck of cards.
  • Shuffle the deck.
  • Deal cards equally among four players.
  • Show the cards of each Player.

Problem


The method dealCards in class DeckOfCards is for distributing cards among four Players objects (each contains 13 cards).

The cards are stored in array instance of playCards [] of each Players object.

But when the method dealCards(player1, player2, player3, player4) is called It leaves the array playCards[] null . I have also created objects of player1,....player4 and then passed it to method. But still it does not fill arrays and leave them null

        public void dealCards(Players player1,Players player2,Players player3,Players player4){

    int count = 1;
    for (Card card : deckOfCards){

        if (count>39){
            player1.playCards[count] = card;
        }
        else if (count>26){
            player2.playCards[count] = card;
        }
        else if (count>13){
            player3.playCards[count] = card;
        }
        else{
            player4.playCards[count] = card;
        }   
    }

And when I call method showPlayerCards( ) of class Players the NullPointerException error is given after execution.

What should I do to fill array playCards of Players so it do not stay null?

kindly help me to fix it

Also guide me to make this program better.


package cardgame;


public class Card {

String suit;
String rank;

public Card(String cardSuit, String cardRank){
    this.suit = cardSuit;
    this.rank = cardRank;
}

}

package cardgame;

import java.util.*;


public class DeckOfCards {

final int size = 52;
Card[] deckOfCards = new Card[size];


public DeckOfCards(){

    int count=0;

    String[] suits = {"Diamonds","Clubs","Hearts","Spades"};
    String[] ranks ={"King","Queen","Jack","Ten","Nine","Eight","Seven","Six","Five","Four","Three","Deuce","Ace",};

    for (String s:suits){
        for (String r:ranks){

            Card card = new Card(s, r);
            this.deckOfCards[count] = card;
            count++;
        }
    }

}
public void ShuffleCards(){
    Random rand = new Random();
    int j;
    for(int i=0; i<size; i++){
        j = rand.nextInt(52);
        Card temp = deckOfCards[i];
        deckOfCards[i]=deckOfCards[j];
        deckOfCards[j]= temp;
    }
}
public void showCards(){
    System.out.println("---------------------------------------------");
    int count =0;
    for (Card card : deckOfCards){
        System.out.print(card.rank + " of " + card.suit + "     ");
        count++;
        if(count%4==0)
            System.out.println("");
    }
    System.out.println("---------------------------------------------");
}

 //_________________________________

 /* In this method dealCard, four Players objects are passed as 
    parameters and then from the deck of 52 cards, 
     cards are equally distributed among four players(each contain 13 cards) 
     which are stored in array playcard[] instance of Players. */

public void dealCards(Players player1,Players player2,Players player3,Players player4){

    int count = 1;
    for (Card card : deckOfCards){

        if (count>39){
            player1.playCards[count] = card;
        }
        else if (count>26){
            player2.playCards[count] = card;
        }
        else if (count>13){
            player3.playCards[count] = card;
        }
        else{
            player4.playCards[count] = card;
        }   
    }
}


}

package cardgame;

public class Players {

String name;
Card[] playCards = new Card[13];


public Players(String name){
    this.name = name; }

//this method shows cards which are distributed among players
public void ShowPlayerCards(){
    System.out.println("---------------------------------------------");
    for (Card card : playCards){
        //if(card==null)
            //System.out.println("null");
        //else
            System.out.println(card.rank + "  of  " + card.suit);
    }
    System.out.println("---------------------------------------------");
}

}

package cardgame;

import java.util.Scanner;


public class CardGame {

public static void main(String[] args) {

    DeckOfCards deck = new DeckOfCards(); //creates deck of cards
    deck.showCards(); //prints each card of deck
    deck.ShuffleCards(); // shuffles cards
    deck.showCards(); //prints each card of deck after shuffling

    Scanner input = new Scanner(System.in);

    //four player objects are created

    System.out.println("Player One...\nEnter Name:");
    Players player1 = new Players(input.nextLine());

    System.out.println("Player Two...\nEnter Name:");
    Players player2 = new Players(input.nextLine());

    System.out.println("Player Three...\nEnter Name:");
    Players player3 = new Players(input.nextLine());

    System.out.println("Player Four...\nEnter Name:");
    Players player4 = new Players(input.nextLine());

    // the method dealCard do not work properly and leaves arrays null. 

    deck.dealCards(player1, player2, player3, player4);  // distribute cards among player1,2,3,4.

    System.out.println("Player One Cards.");
    player1.ShowPlayerCards(); // shows cards of player1 

    System.out.println("Player Two Cards.");
    player2.ShowPlayerCards(); // shows cards of player2

    System.out.println("Player Three Cards.");
    player3.ShowPlayerCards(); // shows cards of player3

    System.out.println("Player Four Cards.");
    player4.ShowPlayerCards(); // shows cards of player4

}

}

Kh Ahmed
  • 11
  • 4
  • Please take the time to do some [research](http://idownvotedbecau.se/noresearch/) and share what you've found and why it doesn't help you, then do some [debugging](http://idownvotedbecau.se/nodebugging/) and share the results of your efforts along with what you're still stuck on. – D.B. Nov 24 '18 at 23:41

1 Answers1

0

Your dealCards method is broken. If you debugged step by step you would see that your local variable count is always 1, so it is always stepping the last else statement. Also if you fix it then you will have ArrayIndexOutOfBoundsException probably since you are trying to reach [count] value of the array which is fixed number 13, in this case [count] seem to be bigger than that. You have to fix your logic there. Please at least debug your code before posting it here.

EDIT-THE FIX: The method dealCards should be like below.

 public void dealCards( Players player1, Players player2, Players player3, Players player4 )
 {
  int count = 0;
  for ( final Card card : this.deckOfCards )
  {
     if ( count >= 39 )
     {
        player1.playCards[count % 13] = card;
     }
     else if ( count >= 26 )
     {
        player2.playCards[count % 13] = card;
     }
     else if ( count >= 13 )
     {
        player3.playCards[count % 13] = card;
     }
     else
     {
        player4.playCards[count % 13] = card;
     }
     count++;
  }
 }
Bleach
  • 309
  • 3
  • 18