我正在做一个关于纸牌游戏的作业。 它说"你将有三个课程(卡片,甲板和演示课程)"。 我不知道如何将规则写成代码,以及如何给2名玩家一半的牌。
在这个游戏中,有两个玩家每人获得一半牌(每张26张牌)。玩家同时翻转 在他们的堆上顶部卡。翻转较高价值/等级卡的玩家获胜并捕获两张牌。获胜的球员补充道 这两张牌都是他们的一堆牌。如果两个玩家都翻转了具有相同价值/等级的牌,那么#34; War"开始。何时"战争"宣布, 每个玩家从他们的桩中取出两张额外的牌;每个玩家翻转一张牌并将一张牌面朝下。翻转的玩家 超过最高牌赢得了#34;战争"堆(所有六张牌)。只要翻转的牌是相同的值,每个玩家继续采取 从他们的堆中添加两张额外的牌,翻转一张并且面朝下留下一张牌,直到一张牌具有更高的价值并赢得该牌 宣战。战争结束后,游戏恢复正常(一次翻转和比较一张牌)。一名球员在跑步时输了 没有卡;然后游戏就结束了。
这是我的代码,我不知道接下来该做什么。希望有人能给我一些关于这个游戏的想法。
public class Card {
private int rank;
private int suit;
private static String[] suits = {"clubs","diamonds","hearts","spades"};
public static final int CLUBS = 0;
public static final int DIAMONDS = 1;
public static final int HEARTS = 2;
public static final int SPADES = 3;
private static String[] ranks = {"2", "3", "4", "5", "6", "7", "8",
"9", "10", "jack", "queen", "king", "ace"};
public static final int JACK = 11;
public static final int QUEEN = 12;
public static final int KING = 13;
public static final int ACE = 14;
/* Constructor
* rank can be 2, 3, ..., 10, JACK, QUEEN, KING, ACE
* suit can be CLUBS, DIAMONDS, HEARTS, SPADES
*/
public Card(int rank,int suit){
this.rank = rank;
this.suit = suit;
}
//returns the suit of the card
public int getSuit(){
return this.suit;
}
//returns the rank of card
public int getRank(){
return this.rank;
}
//determines which Card has the higher value
public int compare(Card c){
return rank - c.rank;
}
/** Returns a negative number if this object is lower in rank than c,
* 0 if the cards are equal rank, and a positive number if this object
* is higher in rank than c. Aces are considered 'high'.
*/
public String toString(){
return ranks[rank] + " of " + suits[suit];
}
public boolean equals(Card c) {
return rank == c.rank && suit == c.suit;
}
}
public class Deck{
private ArrayList<Card> cards;
/**
* Create 52 cards.
*/
public Deck(){
cards= new ArrayList<Card>();
for(int i=0;i<=4;i++){
for(int j=2;j<14;j++){
cards.add(new Card(suits[i],ranks[j]));
}
}
}
//add a card at the end
public void add(Card c){
cards.add(c);
}
//get the top card
public Card getTopCard(){
return cards.remove(0);
}
/**
* Returns the number of cards in the hand.
*/
public int getCardsCount(){
return cards.size();
}
//clear the deck
public void clear(){
cards.clear();
}
//shuffle
public void shuffle(){
Random rand = new Random();
for (int i=0; i<2000; i++){ //repeat 2000 times
if (cards.size() > 0){
Card topCard = cards.remove(0);
int newPosition = rand.nextInt(cards.size());
cards.add(newPosition, topCard);
}
}
}
}