java - 为什么我的代码会填满所有值

时间:2013-12-06 11:25:38

标签: java arrays

这是我的程序中的方法,它向游戏中的玩家发出“初始手牌”,玩家阵列是一个包含玩家对象的数组。 Player对象本身是数组或Card对象,它们在其他类中定义。

private static void dealInitialHand(int noPlayers, Player[] players, int noCardsInHand ){
   System.out.println( "\nPlease enter the name of the deck file" );
   File theDeck = new File (Checks.userInputCheck( "deckfile.txt" ) );
   int intLine;

   try{
       //fileReader = new BufferedReader ( new FileReader( theDeck ) );
       Scanner fileScanner = new Scanner( theDeck );

       int line = 0;

       for( int h = 0; h < noCardsInHand; h++){
           for( int p = 0; p < noPlayers; p++){
                System.out.println( "Player " + h + " Card" + p );

                line = Integer.parseInt( fileScanner.nextLine() );

                players[p].setHand( line, h );

           }
       }
       seePlayerHands();
   }catch (Exception e) {
       e.printStackTrace();
   }
}

我遇到的问题是,当我将牌值分配给某个牌的特定牌时,它会将该值分配给所有牌手的所有牌,我不明白为什么会这样做。

在遍历数组时打印,以显示它指向正确的位置。

 Player 0 Card0
 Player 0 Card1
 Player 0 Card2
 Player 1 Card0
 Player 1 Card1
 Player 1 Card2
 Player 2 Card0
 Player 2 Card1
 Player 2 Card2

但是当seePlayerHands()方法时,如果读取的最后一张卡是2

,这就是结果
 Player 1's hand is currently
 2
 2
 2
 Player 2's hand is currently
 2
 2
 2
 Player 3's hand is currently
 2
 2
 2

而不是这个

 Player 1's hand is currently
 4
 2
 5
 Player 2's hand is currently
 3
 2
 2
 Player 3's hand is currently
 5
 5
 2

来自

的文件
 4
 3
 5
 2
 2
 5
 5
 2
 2
 4
 4
 6
 3
 5
 4
 6
 2
 3

帮助将受到重视

将所有内容放在这里

  public class CardGame{
  static Player[] players;
  static int handSize;
  public static void main(String[] args){
    Scanner reader = new Scanner(System.in);

    System.out.println( "\nHello, how many players would you like" );
    int playersNum = Integer.parseInt(Checks.userInputCheck( "\\d" ));
    System.out.println( "\nHow many cards should each player begin with" );
    int handSize = Integer.parseInt(Checks.userInputCheck( "\\d" ));
    System.out.println( "\nWhich strategy would you like to use 1 or 2" );
    int strategy = Integer.parseInt(Checks.userInputCheck( "[12]$" ));

    Logger.createDeck( playersNum, handSize );

    makePlayers( playersNum, handSize, strategy );

    dealInitialHand( playersNum, players, handSize );

    makePlayerOutputs();

    for ( int i = 0; i < players.length; i++){
       logInitialHand(players[i]);
    }

    //for ( int i = 0; i < players.length; i++){
      //  new Thread(players[i]).start();
   // }
   }

    private static void makePlayers(  int noPlayers, int noCardsInHand, int strategyChosen){
   players = new Player[noPlayers];
   for( int i = 0; i < noPlayers; i++){
       players[i] = new Player( strategyChosen, noCardsInHand, i+1 );
       players[i].fillHand();
   }
}

   private static void dealInitialHand(int noPlayers, Player[] players, int noCardsInHand ){
   System.out.println( "\nPlease enter the name of the deck file" );
   File theDeck = new File (Checks.userInputCheck( "deckfile.txt" ) );
   //BufferedReader fileReader = null;
   int intLine;


   try{
       //fileReader = new BufferedReader ( new FileReader( theDeck ) );
       Scanner fileScanner = new Scanner( theDeck );

       int line = 0;

       for( int h = 0; h < noCardsInHand; h++){
           seePlayerHands();
           for( int p = 0; p < noPlayers; p++){
                System.out.println( "Player " + h + " Card" + p );

                line = Integer.parseInt( fileScanner.nextLine() );
                //seePlayerHands();
                players[p].setHand( line, h );
                //seePlayerHands();
           }
       }
   }catch (Exception e) {
       e.printStackTrace();
   }

   seePlayerHands();
    }

   private static void seePlayerHands(){
   for ( int i = 0; i < players.length; i++){
      System.out.println( players[i].getPlayerName() + "'s hand is currently" );
      players[i].seeHand();
    }
    }

玩家类

public class Player implements Runnable{
Card[] hand;
String playerName;
int strategyChosen;

public void run(){
    System.out.println( "les do dis" );
}

private Player(){
}

public Player( int strategy, int cardsInHand, int playerNumber ){
    hand = new Card[cardsInHand];
    strategyChosen = strategy;
    playerName = "Player " + playerNumber;
}

public String getPlayerName(){
    return playerName;
}

public void fillHand(){
    Card card = new Card(0);

    for ( int i = 0; i < hand.length; i++){
        hand[i] = card;
    } 
}

public void setHand( int value, int index ){
    hand[index].setCardValue( value );
}

public void seeHand(){
    for ( int i = 0; i < hand.length; i++){
        System.out.println( hand[i].getCardValue() );
    }
}

public int getHandValue( Card card, int handIndex ){
    return card.getCardValue( hand[handIndex] );
}
}

卡片类

public class Card{
static int cardValue;

private Card(){
}

public Card( int value ){
    cardValue = value;
}

public void setCardValue( int value ){
    cardValue = value;
}

public static int getCardValue(){
    return cardValue;
}

public static int getCardValue( Card card ){
    return cardValue;
}
}

这是Ross建议改变的结果

Player 1's hand is currently
0
0
0
Player 2's hand is currently
0
0
0
Player 3's hand is currently
0
0
0
Player 0 Card0value5
Player 1 Card0value3
Player 2 Card0value6
Player 1's hand is currently
6
6
6
Player 2's hand is currently
6
6
6
Player 3's hand is currently
6
6
6
Player 0 Card1value1
Player 1 Card1value3
Player 2 Card1value5
Player 1's hand is currently
5
5
5
Player 2's hand is currently
5
5
5
Player 3's hand is currently
5
5
5
Player 0 Card2value3
Player 1 Card2value3
Player 2 Card2value2
Player 1's hand is currently
2
2
2
Player 2's hand is currently
2
2
2
Player 3's hand is currently
2
2
2

感谢您的快速回复,希望这有助于您帮助我。

2 个答案:

答案 0 :(得分:2)

cardValuestatic。意味着所有卡都将获得相同的值

static int cardValue;

删除static

此外,甚至在您编辑之前发布了更多代码,我就想到了引用相同Card对象的可能性。如果这对你来说不起作用,我也会改变@ssssteffff建议的内容

public void fillHand(){
    for ( int i = 0; i < hand.length; i++){
        hand[i] = new Card(0);
    }
}

答案 1 :(得分:1)

fillHand方法中,您将手中的每张卡片设置为Card的同一个实例:

public void fillHand(){
    Card card = new Card(0);

    for ( int i = 0; i < hand.length; i++){
        hand[i] = card;
    }
}

玩家手中的每个Card都是一张独特的牌。每个玩家只有一张牌,手里拿着三张牌。

像这样更改此方法:

public void fillHand(){
    for ( int i = 0; i < hand.length; i++){
        hand[i] = new Card(0);
    }
}

它应该没问题。

修改 正如peeskillet指出的那样,cardValue不应该是静态的。