我正在使用java网站上的类卡示例来尝试构建游戏。
http://java.sun.com/j2se/1.5.0/docs/guide/language/enums.html
我将参与手数和每手牌数作为参数
喜欢:java -jar a02.jar 4 3
我已经为排名和套装增加了价值。我打印DECK然后打印HAND ......
我要做的是先打印卡座 与他们的价值观在一起。 例如:心中的十个
十= 10 心= 4
所以,我打印出十颗心(40)
当我打印卡座时,我遇到的问题是让他们按照每张卡的数量在一行上打印。像:
八个黑桃(8),十颗心(40),心脏(八)我该怎么做才能解决这个问题?
我的另一个问题是,有没有比我正在做的更好的方法来检索卡片的价值?我可以使用某种比较吗?
因为当我在牌组打印出手牌后,我很难获得价值和手牌(所以我注释掉了代码)
我还评论了main()中我正在检索值的部分代码。 我怎么能这样做?
请记住,我想要用Hand打印的值,就像我正在使用套牌
我会缩短输出..
这是输出:
C:\Java\a02>java -jar a1.jar 3 4
THREE of SPADES(3), // i would like this to print out like hand does below
FIVE of HEARTS(20),
DEUCE of DIAMONDS(6),
KING of CLUBS(20),
DEUCE of HEARTS(8),
TEN of HEARTS(40),
JACK of HEARTS(40),
ACE of HEARTS(44),
NINE of CLUBS(18),
EIGHT of SPADES(8),
QUEEN of SPADES(10),
KING of HEARTS(40),
NINE of SPADES(9),
SEVEN of SPADES(7),
SEVEN of HEARTS(28),
EIGHT of HEARTS(32),
TEN of SPADES(10),
DEUCE of CLUBS(4),
EIGHT of DIAMONDS(24),
// here is the hand - which i also want the values
[SIX of CLUBS, SEVEN of DIAMONDS, TEN of DIAMONDS, TEN of CLUBS]
[SIX of HEARTS, THREE of HEARTS, SIX of SPADES, DEUCE of SPADES]
[JACK of SPADES, NINE of DIAMONDS, SEVEN of CLUBS, THREE of DIAMONDS]
C:\Java\a02>
这是代码:
import java.util.*;
public class Cards{
public enum Rank { DEUCE(2), THREE(3), FOUR(4), FIVE(5), SIX(6),
SEVEN(7), EIGHT(8), NINE(9), TEN(10), JACK(10), QUEEN(10), KING(10), ACE(11);
private int Rankpoints;
private Rank(int points)
{
this.Rankpoints = points;
}
public int getRankpoints()
{
return Rankpoints;
}
}
public enum Suit { CLUBS(2), DIAMONDS(3), HEARTS(4), SPADES(1);
private int Suitpoints;
Suit(int points)
{
Suitpoints = points;
}
public int getSuitpoints()
{
return Suitpoints;
}
}
private final Rank rank;
private final Suit suit;
private Cards(Rank rank, Suit suit)
{
this.rank = rank;
this.suit = suit;
}
public Rank rank()
{
return this.rank;
}
public Suit suit()
{
return this.suit;
}
public String toString()
{
return rank + " of " + suit;
}
private static final List<Cards> protoDeck = new ArrayList<Cards>();
// Initialize prototype deck
static {
for (Suit suit : Suit.values())
for (Rank rank : Rank.values())
protoDeck.add(new Cards(rank, suit));
}
public static ArrayList<Cards> newDeck()
{
return new ArrayList<Cards>(protoDeck); // Return copy of prototype deck
}
}
这里是main()
import java.util.*;
public class Deal {
public static void main(String args[])
{
int numHands = Integer.parseInt(args[0]);
int cardsPerHand = Integer.parseInt(args[1]);
List<Cards> deck = Cards.newDeck();
Collections.shuffle(deck);
int total = 0;
// ArrayList<Cards> hand = new ArrayList<Cards>();
// this is how i am handling the values.. which is not good.
for (Cards card : deck)
{
for(Cards.Suit a : Cards.Suit.values())
{
for(Cards.Rank b : Cards.Rank.values())
{
if(card.rank() == b)
{
if(card.suit() == a) // as you can see this works. theres got to be a better way??
{
total = b.getRankpoints();
// hand.add(card);
System.out.println(card.rank() + " of " + card.suit() + "(" + a.getSuitpoints() * total + ")" + ",");
}
}
}
}
}
for (int i=0; i < numHands; i++)
System.out.println(deal(deck, cardsPerHand)); // prints the hand
//deal(deck,cardsPerHand,numHands);
}
// hand
public static ArrayList<Cards> deal(List<Cards> deck, int n) {
int deckSize = deck.size();
List<Cards> handView = deck.subList(deckSize-n, deckSize);
ArrayList<Cards> hand = new ArrayList<Cards>(handView);
handView.clear();
// Map<String, Map<String, Integer>> inputWords = new HashMap<String, Map<String, Integer>>();
int total = 0;
return hand;
}
}
//for (Cards card : hand)
//{
// for(Cards.Suit a : Cards.Suit.values())
// {
//for(Cards.Rank b : Cards.Rank.values())
// {
// if(card.rank() == b)
// {
// if(card.suit() == a)
//{
// total = b.getRankpoints();
// System.out.println("card.rank() + " of " + card.suit() + "(" + a.getSuitpoints() * total + ")" + ",");
// return hand;
//}
//}
//}
//}
//}
//return hand;
//}
//}
非常感谢任何帮助。代码片段或提示非常受欢迎=) 请记住,我对Java很新。
谢谢
答案 0 :(得分:2)
尝试将您的Card类toString()方法更改为以下内容:
public String toString()
{
return rank + " of " + suit+ "(" + suit.getSuitpoints() * rank.getRankpoints() + ")" + ",");
}
然后这个:
System.out.println(card.rank() + " of " + card.suit() + "(" + a.getSuitpoints() * total + ")" + ",");
可以成为:
System.out.println(card);
答案 1 :(得分:1)
关于将所有内容打印在一行上,您正在寻找System.out.print()而不是System.out.println()。