十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
扑克牌还是自己写个对象比较好吧 属性就是花色跟牌值
10年建站经验, 成都网站制作、网站建设、外贸网站建设客户的见证与正确选择。成都创新互联提供完善的营销型网页建站明细报价表。后期开发更加便捷高效,我们致力于追求更美、更快、更规范。
至于发牌什么的 还是用集合来吧 数组操作起来比较麻烦
代码实在是懒得写了 就提点建议
用集合的好处比如说洗牌直接就可以用 collections的静态方法 还有就是 回复初始状态排序 也可以也可以自定义比较器来比较
import java.util.ArrayList;
import java.util.Random;
public class PokerDemo {
ArrayListCard poker = new ArrayListCard();
Player[] players;
int n = 4;
String[] name = {"Wang", "Zhang", "Li", "Yuan"};
public PokerDemo() {
players = new Player[n];
for(int i = 0; i players.length; i++)
players[i] = new Player(name[i]);
for(int i = 0; i 4; i++)
for(int j = 0; j 13; j++)
poker.add(new Card(i, j));
System.out.println("洗牌前: " + poker);
xipai();
System.out.println("洗牌后: " + poker);
fapai();
System.out.println("发牌后: ");
for(int i = 0; i players.length; i++)
System.out.println(players[i]);
}
public void xipai() {
Random r = new Random();
ArrayListCard tmp = new ArrayListCard();
while(!poker.isEmpty()) {
int index = r.nextInt(poker.size());
Card card = poker.get(index);
tmp.add(card);
poker.remove(index);
}
poker = new ArrayListCard(tmp);
}
public void fapai() {
int p = 0;
while(!poker.isEmpty()) {
Card card = poker.get(0);
players[p].add(card);
p = (p + 1) % n;
poker.remove(0);
}
}
public static void main(String[] args) {
new PokerDemo();
}
}
class Card {
int val, sty;
final String[] value = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
final String[] style = {"黑桃", "红心", "梅花", "方块"};
public Card(int s, int v) {
sty = s;
val = v;
}
public String toString() {
return style[sty]+ " " + value[val];
}
}
class Player {
ArrayListCard cards = new ArrayListCard();
String name;
public Player(String n) {
name = n;
}
public void add(Card card) {
cards.add(card);
}
public String toString() {
return name + ":\t" + cards;
}
}
1、设计二维数组a[13][4],13代表2,3,4,5,...,K,A , 4代表红黑方梅 13*4=52
2、这样a[0][0]就是红桃2,a[12][3]就是梅花A
按照这么来随机发牌就很简单了,直接随机个一个a[i][j]就可以了。i和j都分别在13和4以内
思路明确了,代码还是自己写比较好
DeckOfCards()构造函数里面,注释掉的
//deck[count]=new Card(faces[count%13],suits[count/13]);
是对的,下面那行除17是错的
另外,没有把大小王赋值到数组里
最后main函数里,打印方法错了。应该为,原本少了一个%-20s
System.out.printf("%-20s%-20s%-20s\n", myDeckOfCards.dealCard(), myDeckOfCards.dealCard(), myDeckOfCards.dealCard());
发牌完成以后剩下的3张牌就是保留的底牌