撲克牌還是自己寫個(gè)對象比較好吧 屬性就是花色跟牌值
10年建站經(jīng)驗(yàn), 成都網(wǎng)站制作、網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)客戶的見證與正確選擇。成都創(chuàng)新互聯(lián)提供完善的營銷型網(wǎng)頁建站明細(xì)報(bào)價(jià)表。后期開發(fā)更加便捷高效,我們致力于追求更美、更快、更規(guī)范。
至于發(fā)牌什么的 還是用集合來吧 數(shù)組操作起來比較麻煩
代碼實(shí)在是懶得寫了 就提點(diǎn)建議
用集合的好處比如說洗牌直接就可以用 collections的靜態(tài)方法 還有就是 回復(fù)初始狀態(tài)排序 也可以也可以自定義比較器來比較
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("發(fā)牌后: ");
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、設(shè)計(jì)二維數(shù)組a[13][4],13代表2,3,4,5,...,K,A , 4代表紅黑方梅 13*4=52
2、這樣a[0][0]就是紅桃2,a[12][3]就是梅花A
按照這么來隨機(jī)發(fā)牌就很簡單了,直接隨機(jī)個(gè)一個(gè)a[i][j]就可以了。i和j都分別在13和4以內(nèi)
思路明確了,代碼還是自己寫比較好
DeckOfCards()構(gòu)造函數(shù)里面,注釋掉的
//deck[count]=new Card(faces[count%13],suits[count/13]);
是對的,下面那行除17是錯(cuò)的
另外,沒有把大小王賦值到數(shù)組里
最后main函數(shù)里,打印方法錯(cuò)了。應(yīng)該為,原本少了一個(gè)%-20s
System.out.printf("%-20s%-20s%-20s\n", myDeckOfCards.dealCard(), myDeckOfCards.dealCard(), myDeckOfCards.dealCard());
發(fā)牌完成以后剩下的3張牌就是保留的底牌