2013-12-03 172 views
-2

我在這裏有點問題,真的,我不明白爲什麼。我寫了大量代碼,它們在參數中使用字段變量,然後更改它們。下面是方法:方法不改變變量

public void hit(Array<Card> cards, int score, float spacing) { 
    // Take the last card from the deck and store it in a temp card variable 
    nextCard = deck.pop(); 
    // If the card value is equal to 11, it must be an ace, if it puts the 
    // player over 21 it makes the ace have a value of 1 
    if ((nextCard.getCardRealValue() == 11) 
      && nextCard.getCardRealValue() + score > 21) { 
     nextCard.setCardRealValue(1); 
    } 
    // Add the card to the array passed in the parameter 
    cards.add(nextCard); 
    // Check the last card in the array (the one just added) grab it's value, add it to total 
    score += cards.peek().getCardRealValue(); 
    // Shrink the main deck, not sure if neccessary but not point in leaving empty memory 
    deck.shrink(); 
    // Move the sprites 20 pixels 
    spacing -= 20; 
    // tbh this bit never gets called, stupid useless code lol 
    if (score > 21) { 
     if (cards.peek().getCardRealValue() == 11) { 
      cards.peek().setCardRealValue(1); 
      score -= 10; 
     } 
    } 
    // if the first card has not been checked and score is over 21, if it is an ace change it to a value of 1 
    if (!aceOne && score > 21 && cards.get(0).getCardRealValue() == 11) { 
     cards.get(0).setCardRealValue(1); 
     score -= 10; 
     aceOne = true; 
     // Same as above, just if second card is ace 
    } else if (!aceTwo && score > 21 
      && cards.get(1).getCardRealValue() == 11) { 
     cards.get(1).setCardRealValue(1); 
     score -= 10; 
     aceTwo = true; 

    } 
} 

這基本上是當玩家決定「打」被調用的方法,它需要保存玩家的牌陣,球員得分,然後這個東西我有叫間距,這基本上將卡精靈-20左移停止,然後拖到屏幕的右側,看起來很蠢。

現在,當我嘗試調用這個方法:

hit(playerCards, playerScore, playerSpacing); 

爲什麼playerScore和playerSpacing沒有更新?牌被添加得很好,因爲他們畫出了受人尊敬的雪碧,我錯過了什麼?

+2

就我所知「Java總是按值傳遞」並不是一個真實的說法。請參閱http://stackoverflow.com/questions/40480/is-java-pass-by-reference –

回答

2

Java總是按價值傳遞。您正在更改本地變量scorespacing,但這些變量是playerScoreplayerSpacing的副本。

但爲什麼更改爲cards更新playerCards?因爲對象參考是按值傳遞的。你有一個引用的副本,它指向同一個對象。對象已更改(可通過playerCardscards進行更改)。如果您已完成cards = new Array<Card>();,那麼當您更改cards時,您應該給予本地變量cardsplayerCards的新值。

+0

。你能給我一個參考嗎? – guness

+0

@bluebrain請參閱[Java是否爲「pass-by-reference」?](http://stackoverflow.com/questions/40480/is-java-pass-by-reference)。 – rgettman

+0

這是什麼:http://stackoverflow.com/a/18950805/1281930 – guness

0

這些值必須更新,但會反射回調用語句,因爲在java中,所有內置數據類型都是按值傳遞的,並且所有類對象都是通過引用傳遞的,因此,在您重新使用後呼籲聲明。替換內置數據類型與代碼中各處的相應類,int -> Integerfloat -> Float,它應該工作。

+0

好吧,我明白,但是這確實改變了一件事,它仍然沒有更新分數和間距。 – Gibbo

2

這裏是固定的代碼:

保持該選手對象它自己的手,得分和間距:

public class Player { 

int score = 0; 
float spacing = 390; 
Array<Card> hand = new Array<Card>(); 

public Player(){ 
    hand.ordered = false; 
} 

public int getScore() { 
    return score; 
} 

public void setScore(int score) { 
    this.score = score; 
} 

public float getSpacing() { 
    return spacing; 
} 

public void setSpacing(float spacing) { 
    this.spacing = spacing; 
} 

public Array<Card> getHand() { 
    return hand; 
} 

public void addToHand(Card card){ 
    hand.add(card); 
} 

}

新方法:

public void hit(Array<Card> cards, Player player){ 
    nextCard = deck.pop(); 
    if ((nextCard.getCardRealValue() == 11) 
      && nextCard.getCardRealValue() + player.getScore() > 21) { 
     nextCard.setCardRealValue(1); 
    } 
    cards.add(nextCard); 
    player.setScore(player.getScore() + player.getHand().peek().getCardRealValue()) ; 
    deck.shrink(); 
    player.setSpacing(player.getSpacing() - 20); 
    cardDeal.play(); 
    if (player.getScore() > 21) { 
     if (cards.peek().getCardRealValue() == 11) { 
      cards.peek().setCardRealValue(1); 
      player.setScore(player.getScore() - 10); 
     } 
    } 
    if (!aceOne && player.getScore() > 21 
      && cards.get(0).getCardRealValue() == 11) { 
     cards.get(0).setCardRealValue(1); 
     player.setScore(player.getScore() - 10); 
     aceOne = true; 
    } else if (!aceTwo && player.getScore() > 21 
      && cards.get(1).getCardRealValue() == 11) { 
     cards.get(1).setCardRealValue(1); 
     player.setScore(player.getScore() - 10); 
     aceTwo = true; 

    } 
} 

除了現在我的代碼是可重用的,一切都按預期工作,這就是我寫這個方法的原因。由於經銷商將使用相同的代碼進行遊戲。多謝你們。