我目前正在编写Gin Rummy的修改版本。我的问题是我是阵列的新手,我试图让每个玩家持有我的卡类中生成的5张随机卡。我如何制作它以便我可以将5张随机牌放入每个玩家手中?这是我的卡片课......
{
String suit;
String rank;
int score;
int a;
int b;
final int CARDS_IN_SUIT = 13;
final int SUITS_IN_DECK = 4;
public GinCard(String suit, String rank, int score, int a, int b){
this.suit = "";
this.rank = "";
this.score = 0;
this.a = 0;
this.b = 0;
}
//Sets up the cards(Suits and ranks)
public GinCard(){
switch(a){
case 1:
suit = "Clubs";
break;
case 2:
suit = "Diamonds";
break;
case 3:
suit = "Hearts";
break;
default:
suit = "Spades";
break;
}
switch(b){
case 1:
rank = "Ace";
break;
case 2:
rank ="2";
break;
case 3:
rank ="3";
break;
case 4:
rank ="4";
break;
case 5:
rank ="5";
break;
case 6:
rank ="6";
break;
case 7:
rank ="7";
break;
case 8:
rank ="8";
break;
case 9:
rank ="9";
break;
case 10:
rank ="10";
break;
case 11:
rank = "Jack";
break;
case 12:
rank = "Queen";
break;
case 13:
rank = "King";
break;
default:
break;
}
switch(score){
case 1:
score = 15;
break;
case 2:
score = 5;
break;
case 3:
score = 5;
break;
case 4:
score = 5;
break;
case 5:
score = 5;
break;
case 6:
score = 5;
break;
case 7:
score = 5;
break;
case 8:
score = 5;
break;
case 9:
score = 5;
break;
case 10:
score = 5;
break;
case 11:
score = 10;
break;
case 12:
score = 10;
break;
case 13:
score = 10;
break;
}
}
//Returns the value of the rank
int getRank(){
return b;
}
//Returns the value of the suit in case of a draw in ranks
int getSuit(){
return a;
}
int getScore(){
return score;
}
public void deal()
{
a = ((int)(Math.random() * 100) % SUITS_IN_DECK + 1);
b = ((int)(Math.random() * 100) % CARDS_IN_SUIT + 1);
}
}
这是我迄今为止的主要计划......
public class GinRummy {
public static final int CARDS_IN_HAND = 5;
public void RummyHand(){
int[] hand = new int[CARDS_IN_HAND];
};
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner keyboard = new Scanner(System.in);
GinCard player = new GinCard();
GinCard player2 = new GinCard();
答案 0 :(得分:0)
Set set = new HashSet();
Random randomGenerator = new Random();
while (set.size() < 5) {
set.add(randomGenerator.nextInt(52));
}
现在设定为玩家包含5张独特的牌。
然后将0-51翻译成牌组中的52张牌,每位牌手都有5张牌。
答案 1 :(得分:0)
这个简单的解决方案包含两个类。一个是卡类,另一个是卡经理类(经销商)。卡片类包含描述它的等级,套装和分数的字段。分数根据排名计算。顺便说一句,我只留下了String-typed套装并在这里排名,因为你有这样的,你应该真的使用ENUM来存储这些东西。
卡片管理器包含业务逻辑。用于处理卡(或生成卡)的方法创建GinCard类的实例。每个实例都被添加到五个选定卡片的列表中,只有它没有被添加到任何其他列表(也就是说,还没有被发送给任何人),否则它再次发送它。每个玩家的手都会调用方法。
在最后经理班评估球员的分数&#39;根据他们手中每张牌的得分总和来确定。
卡类:
public class GinCard() {
private String suit;
private String rank;
private int score = 0;
// getters and setters for those 3
public GinCard(final int suit, final int rank) {
this.suit = getSuitString(suit);
this.rank = getRankString(rank);
this.score = getScoreByRank(rank);
}
private int getScoreByRank(final int rank) {
switch(score){
case 1:
return 15;
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
case 10:
return 5;
case 11:
case 12:
case 13:
return 10;
default:
return 0;
}
}
private String getRankString(final int rank) {
switch(b){
case 1:
return "Ace";
case 2:
return "2";
case 3:
return "3";
case 4:
return "4";
case 5:
return "5";
case 6:
return "6";
case 7:
return "7";
case 8:
return ="8";
case 9:
return "9";
case 10:
return "10";
case 11:
return "Jack";
case 12:
return "Queen";
case 13:
return "King";
}
private String getSuitString(final int suit) {
switch(a){
case 1:
return "Clubs";
break;
case 2:
return "Diamonds";
break;
case 3:
return "Hearts";
break;
default:
return "Spades";
break;
}
}
}
制作一些生成器类:
public class CardManager {
/** Generate one five-card hand */
public List<GinCard> generateHand(final List<GinCard> globallyChosenCards) {
final Random random = new Random();
final List<GinCard> currentChosenCards = new ArrayList<GinCard>();
while(currentChosenCards.size < 5) {
final int suit = rand.nextInt() * 4 + 1;
final int rank = rand.nextInt() * 13 + 1;
if (!isCardChosen(globallyChosenCards, suit, rank) && !isCardChosen(chosenCards, suit, rank) {
final GinCard chosenCard = new GinCard(suit, rank);
currentChosenCards.add(chosenCard);
globallyChosenCards.add(chosenCard);
}
}
return currentChosenCards;
}
public int evaluateScore(final List<GinCard> hand) {
final int totalScore = 0;
for (final GinCard card : hand) {
totalScore += card.getScore();
}
return totalScore;
}
/** Check if card is already chosen by someone */
private boolean isCardChosen(final List<GinCard> chosenCards, final int chosenSuit, final int chosenRank) {
for (final GinCard chosenCard : chosenCards) {
if (chosenCard.getSuit().equals(chosenSuit) && chosenCard.getRank().equals(chosenRank)) {
return true;
}
}
return false;
}
}
创建对象并根据需要调用方法:
final CardManager cardManager = new CardManager();
final List<GinCard> allPlayersCards = new ArrayList<GinCard>();
final List<GinCard> playerOneHand = cardManager.generateHand(allPlayersCards);
final List<GinCard> playerTwoHand = cardManager.generateHand(allPlayersCards);
final int playerOneScore = cardManager.evaluateScore(playerOneHand);
final int playerTwoScore = cardManager.evaluateScore(playerTwoHand);
if (playerOneScore > playerTwoScore) {
// player one wins
} else if (playerOneScore < playerTwoScore) {
// player two wins
} else {
// tie
}
从理论上讲,它应该可行,但并未尝试过,所以可能会出现一些错误和拼写错误。当然它非常简单,例如你应该使用Enums for RANK和SUIT。