DeckOfCards()構(gòu)造函數(shù)里面,注釋掉的
專注于為中小企業(yè)提供做網(wǎng)站、成都做網(wǎng)站服務(wù),電腦端+手機端+微信端的三站合一,更高效的管理,為中小企業(yè)長海免費做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了近1000家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實現(xiàn)規(guī)模擴充和轉(zhuǎn)變。
//deck[count]=new Card(faces[count%13],suits[count/13]);
是對的,下面那行除17是錯的
另外,沒有把大小王賦值到數(shù)組里
最后main函數(shù)里,打印方法錯了。應(yīng)該為,原本少了一個%-20s
System.out.printf("%-20s%-20s%-20s\n", myDeckOfCards.dealCard(), myDeckOfCards.dealCard(), myDeckOfCards.dealCard());
發(fā)牌完成以后剩下的3張牌就是保留的底牌
撲克牌還是自己寫個對象比較好吧 屬性就是花色跟牌值
至于發(fā)牌什么的 還是用集合來吧 數(shù)組操作起來比較麻煩
代碼實在是懶得寫了 就提點建議
用集合的好處比如說洗牌直接就可以用 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è)計二維數(shù)組a[13][4],13代表2,3,4,5,...,K,A , 4代表紅黑方梅 13*4=52
2、這樣a[0][0]就是紅桃2,a[12][3]就是梅花A
按照這么來隨機發(fā)牌就很簡單了,直接隨機個一個a[i][j]就可以了。i和j都分別在13和4以內(nèi)
思路明確了,代碼還是自己寫比較好