簡體   English   中英

改組一副紙牌,直到獲得特定號碼

[英]Shuffling a deck of cards until getting a specific number

我在這方面還很陌生,並且在這個項目中很難過。 我需要洗牌,獲得前四張牌並計算四張牌的總和。 到目前為止,我已經完成了這項任務。 現在,我的問題是我需要重復此方法,直到獲得總計24的總和。我在如何循環該方法方面遇到了困難。 這是我到目前為止完成的代碼:

public class cards
{
    public static void main(String[] args)
    {
       int[] deck = new int[52];
       String[] suits = {"Spades", "Hearts", "Diamonds", "Clubs"};
       String[] ranks = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
       int rankedVal[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
       int tot = 0;
            if (tot != 24){  
       //Initialize cards
    tot = 0;
   for (int i = 0; i < deck.length; i++)
        deck[i] = i;



   //Shuffle cards    
    for (int i = 0; i < deck.length; i++)
   {
        int index = (int)(Math.random() * deck.length);
        int temp = deck[i];
        deck[i] = deck[index];
        deck[index] = temp;
   }

    //Display first four                
    for (int i = 0; i < 4; i++)
   {
        String suit = suits[deck[i] / 13];
        String rank = ranks[deck[i] % 13];
        System.out.println("Card number " + deck[i] + ": " + rank + " of " + suit);
   }  

     //find the total 
           for (int i = 0; i<4;i++){
        tot = tot + rankedVal[deck[i] % 13];
    }


 System.out.println("Total Hand = " + tot);
}
}              

}

我將使用一些通用的代碼,因為不同的表示形式適合不同的程序員,但這適用於任何情況:

int sum = 0;
While (sum != 24)
{
    Shuffle();
    sum = deck[0].value + deck[2].value + deck[3].value + deck[4].value;
}

最好將卡片組做成紙牌數組,並在改組時在成對之間交換,並且其中的卡中具有整數值。初始化一次,然后全部交換對象。 是Java,不是嗎?

無需破壞,就可以在while循環中使用條件,直到達到24,例如:

do
{
    // Get 4 cards and add sum.
 } while (sum < 24);

另外,您還需要檢查自己的卡是否還沒用完,因此可以改進以下內容:

int cardsUsed = 0
do
{
   // Get 4 cards and add sum.
   cardsUsed += 4;
} ((sum < 24) or (cards_used >= 52));

這就是為什么您應該學習如何使用方法和子例程的原因。 而且,如果您使用的是Java,那么絕對應該首先學習一些有關面向對象編程的知識。 如果您不了解OOP,那么我將不建議您將Java作為初學者使用。 也許去C或Python。

public class cards
{
    int[] deck = new int[52];
    String[] suits = {"Spades", "Hearts", "Diamonds", "Clubs"};
    String[] ranks = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
    int rankedVal[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};

    void init() {
       // initialise logic
       for (int i = 0; i < deck.length; i++)
           deck[i] = i;
    }

    void shuffleCards() {
       for (int i = 0; i < deck.length; i++) {
        // Shuffle logic
       }
    }

    void displayFirstFour() {
       // display logic
    }

    int sumFirstFour() {
      int sum = 0;
      // logic to sum first four cards
      return sum;
    }

    public static void main(String[] args)
    {
       init();
       int total = 0;
       // LOOP. NOT 'if' . 'if' simply tests condition once and moves on
       // loop tests condition continually until true
       while(total != 24) {
         //Shuffle cards    
         shuffleCards();  
         //Display first four                
         displayFirstFour();
         total = sumFirstFour();
       }
       System.out.println("Total Hand = " + total);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM