卡改组和交易程序

时间:2018-11-24 23:30:21

标签: java

洗牌和交易程序。

该程序在纸牌和要分配纸牌的四个玩家之间进行交互。

本程序执行以下操作

  • 创建一副纸牌。
  • 随机播放甲板。
  • 在四位玩家之间平均交易卡。
  • 显示每位玩家的卡牌。

问题


DeckOfCards类中的 dealCards 方法用于在四个 Players 对象(每个包含13张卡)之间分配卡。

纸牌存储在每个Players对象的 playCards [] 的数组实例中。

但是当调用方法DealCards(player1,player2,player3,player4)时,它将离开数组playCards [] null 。我还创建了player1,.... player4的对象,然后将其传递给方法。但是它仍然不能填充数组并使它们为空

        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;
        }   
    }

当我调用类Players的方法 showPlayerCards()时,执行后会给出 NullPointerException 错误。

我应该怎么做才能填充玩家的数组纸牌,以免它保持为空?

请帮助我修复该问题

还指导我使此程序更好。


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

}

}

1 个答案:

答案 0 :(得分:0)

您的dealCards方法已损坏。如果逐步调试,您会发现局部变量count始终为1,因此它将始终执行最后一个else语句。另外,如果您对其进行修复,则可能会得到ArrayIndexOutOfBoundsException,因为您正试图达到固定数字13的数组的[count]值,在这种情况下,[count]似乎比该值大。您必须在那里修正逻辑。在将代码发布到这里之前,请至少调试您的代码。

编辑修补程序: DealCards方法应如下所示。

 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++;
  }
 }