作爲一個編程項目的一部分,我試圖編寫一個簡單的Slapjack遊戲(簡單,因爲玩家不會丟失)。我有一個卡片班,一個卡組班和一個主班。在大多數情況下,一切正常,甲板洗牌,我可以發牌,等等。由於它是Slapjack,我需要該程序知道何時處理了Jack,但是我不知道如何做到這一點。該方案如下:Java程序如何判斷一個陣列中的對象是否被拉出
卡類
public class Card2
{
private int rank, suit;
private static String[] suits = { "hearts", "spades", "diamonds", "clubs" };
private static String[] ranks = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace" };
public String toString()
{
return ranks[rank] + " of " + suits[suit];
}
public int getRank()
{
return rank;
}
public int getSuit()
{
return suit;
}
Card2 (int suit, int rank)
{
this.rank=rank;
this.suit=suit;
}
}
甲板類
import java.util.Random;
public class Deck2
{
private Card2[] cards;
int i;
Deck2()
{
i=51;
cards = new Card2[52];
int x=0;
for (int a=0; a<=3; a++)
{
for (int b=0; b<=12; b++)
{
cards[x] = new Card2(a,b);
x++;
}
}
}
public Card2 Draw()
{
Random generator = new Random();
int index=0;
do {
index = generator.nextInt(52);
} while (cards[index] == null);
i--;
Card2 temp = cards[index];
cards[index]= null;
return temp;
}
public int getTotal()
{
return i;
}
}
主程序
public class Main2
{
public static void main(String[] args)
{
Deck2 deck = new Deck2();
System.out.println (deck.Draw());
}
}
_「既然是屁股,我需要程序來知道什麼時候發了一個傑克」_。你有嘗試過嗎? –
你從哪裏拉數組中的對象?那是什麼操作?你只需要將數組中選定的對象存儲在另一個數組中,例如,將保留一個處理項目列表 –