導航:首頁 > 編程語言 > 發牌的java程序

發牌的java程序

發布時間:2023-07-21 19:28:47

java的問題(撲克牌)

代碼僅供參考,如有疑問,歡迎追問:

importjava.util.ArrayList;
importjava.util.List;
importjava.util.Scanner;

/**
*隨機撲克牌
*@authorSTONE
*@version2015-10-28
*/
publicclassPoker{
//用於記錄丟失的手牌數
privatestaticIntegerlostTotal=0;
//用於存放已下發的撲克
privatestaticInteger[]its1=newInteger[54];
//用於存放當前的撲克
privatestaticInteger[]its2=newInteger[54];
privatestaticList<Integer>lostList=newArrayList<Integer>();

publicstaticvoidmain(String[]args){
Scannerscn=newScanner(System.in);
for(inti=0;i<54;i++){
its1[i]=0;
its2[i]=0;
}
System.out.println("系統開始發牌...");
givePoker(5);
System.out.println("發牌完畢,當前手牌為:");
printPoker(its2);
IntegergameTotal=0;
while(gameTotal<10){//循環跑10次,請注意此處發牌沒做限制,如果循環次數過大,會導致死循環
System.out.println("請輸入需要丟棄的手牌(多張牌格式如下:黑桃1,黑桃2,黑桃3):");
Stringlost=scn.nextLine();
checkLost(lost);
System.out.println("丟失手牌完畢,當前手牌:");
printPoker(its2);
System.out.println("系統開始重發手牌...");
givePoker(lostTotal);
System.out.println("重發發牌完畢,當前手牌為:");
printPoker(its2);
gameTotal++;
}
scn.close();
}

publicstaticvoidgivePoker(Integernum){//隨機分發手牌
inttotal=0;
while(total<num){
intpoke=(int)(Math.random()*54);
if(its1[poke]==0){
its1[poke]=1;
its2[poke]=1;
total++;
}
}
lostTotal=0;
}

publicstaticBooleancheckLost(Stringlost){//檢查需要丟失的手牌
if(lost==null||lost.equals("")){
System.out.println("輸入手牌錯誤,系統無法處理...");
returnfalse;
}
if(lost.indexOf("黑桃")<0&&lost.indexOf("紅心")<0&&lost.indexOf("梅花")<0&&lost.indexOf("方塊")<0){
System.out.println("輸入手牌錯誤,系統無法處理...");
returnfalse;
}
String[]strs=lost.split(",");
Booleanbol=false;
for(Stringstr:strs){
str=str.trim();
if(str==null||str.equals(""))continue;//去掉,防止空格
bol=getPokerNum(str);
}
if(bol){
losePoker();
}
returnfalse;
}

publicstaticvoidlosePoker(){//丟掉手牌
lostTotal=lostList.size();
for(Integeritr:lostList){//丟掉手牌
its2[itr-1]=0;
}
lostList=newArrayList<Integer>();
}

(Stringstr){//獲取手牌點數並去掉
try{
Integeritr=0;
if(str.indexOf("黑桃")==0){
str=str.replace("黑桃","");
itr=Integer.parseInt(str);
if(itr>13){
System.out.println("輸入手牌不存在:黑桃"+str);
returnfalse;
}
if(its2[itr-1]==1){
lostList.add(itr);
}
}elseif(str.indexOf("紅心")==0){
str=str.replace("紅心","");
itr=Integer.parseInt(str);
if(itr>13){
System.out.println("輸入手牌不存在:紅心"+str);
returnfalse;
}
if(its2[itr+12]==1){
lostList.add(itr+13);
}
}elseif(str.indexOf("梅花")==0){
str=str.replace("梅花","");
itr=Integer.parseInt(str);
if(itr>13){
System.out.println("輸入手牌不存在:梅花"+str);
returnfalse;
}
if(its2[itr+25]==1){
lostList.add(itr+26);
}
}elseif(str.indexOf("方塊")==0){
str=str.replace("方塊","");
itr=Integer.parseInt(str);
if(itr>13){
System.out.println("輸入手牌不存在:方塊"+str);
returnfalse;
}
if(its2[itr+38]==1){
lostList.add(itr+39);
}
}elseif(str.indexOf("小王")==0){
if(its2[52]==1){
lostList.add(53);
}
}elseif(str.indexOf("大王")==0){
if(its2[53]==1){
lostList.add(54);
}
}
returntrue;
}catch(Exceptione){
System.out.println("輸入手牌有誤...");
returnfalse;
}
}

publicstaticvoidprintPoker(Integer[]its){//列印當前手牌
Stringresult="";
for(inti=0;i<its.length;i++){
if(its[i]==1){//等於1表示當前手牌存在
result+=pukerCheck(i+1)+",";
}
}
System.out.println(result);
}

publicstaticStringpukerCheck(Integeritr){//判斷撲克類型
/**
*1~13黑桃花色、14~26紅心花色、27~39梅花花色
*40~52方塊花色、53小王、54大王
*/
if(1<=itr&&itr<=13){
return"黑桃"+itr;
}elseif(14<=itr&&itr<=26){
return"紅心"+(itr-13);
}elseif(27<=itr&&itr<=39){
return"梅花"+(itr-26);
}elseif(40<=itr&&itr<=52){
return"方塊"+(itr-39);
}elseif(itr==53){
return"小王";
}elseif(itr==54){
return"大王";
}
return"";
}

}

測試運行結果:

② java鬥地主發牌程序

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張牌就是保留的底牌

③ 用Java for 編寫關於撲克牌的程序!急!

存儲時可以用數字,最後顯示時可以裝換為字母
import java.util.Random;

public class Poker {
public static void main(String[] args) {
Random random = new Random();
int[] computer = new int[5];
int[] player = new int[5];
for (int i = 0; i < 5; i++) {// 發牌
computer[i] = random.nextInt(13) + 2;// 2到14的隨機數,14表示A
player[i] = random.nextInt(13) + 2;// 2到14的隨機數,14表示A
}
int result = Judge(player, computer);
Show(player, computer, result);
}

// 比較
private static int Judge(int[] player, int[] computer) {
int p = 0, c = 0;
for (int i = 0; i < 5; i++) {
if (14 == player[i])
p++;
if (14 == computer[i])
c++;
}
if (p > 0 && 0 == c)
return 1;
if (p == 0 && c > 0)
return 2;
return 0;

}

private static void Show(int[] player, int[] computer, int result) {
System.out.print("Your cards are:");
for (int i = 0; i < 5; i++) {
PrintCard(player[i]);
}
System.out.print("\nThe computer's cards are:");
for (int i = 0; i < 5; i++) {
PrintCard(computer[i]);
}
switch (result) {
case 0:
System.out.print("\nYou and the computer are tied for the highest single card.");
break;
case 1:
System.out.print("\nYou has the highest single card.");
break;
case 2:
System.out.print("\nThe computer has the highest single card.");
break;

}

}

private static void PrintCard(int card) {
switch (card) {
case 11:
System.out.print("J ");
break;
case 12:
System.out.print("Q ");
break;
case 13:
System.out.print("K ");
break;
case 14:
System.out.print("A ");
break;
default:
System.out.print(card + " ");
}
}

void m() {
int[] personCard = new int[5];
int[] computerCard = new int[5];
int personCount = 0;
int computerCount = 0;

System.out.print("Your cards are: ");
for (int i = 0; i < 5; i++) {
personCard[i] = (int) (Math.random() * 13) + 2;
if (personCard[i] == 11) {
System.out.print("J" + "\t");
} else if (personCard[i] == 12) {
System.out.print("Q" + "\t");
} else if (personCard[i] == 13) {
System.out.print("K" + "\t");
} else if (personCard[i] == 14) {
personCount++;
System.out.print("A" + "\t");
} else {
System.out.print(personCard[i] + "\t");
}
}
System.out.println("\n");
System.out.print("The computer's cards are:");
for (int i = 0; i < 5; i++) {
computerCard[i] = (int) (Math.random() * 13) + 1;
if (computerCard[i] == 11) {
System.out.print("J" + "\t");
} else if (computerCard[i] == 12) {
System.out.print("Q" + "\t");
} else if (computerCard[i] == 13) {
System.out.print("K" + "\t");
} else if (computerCard[i] == 14) {
computerCount++;
System.out.print("A" + "\t");
} else {
System.out.print(computerCard[i] + "\t");
}
}
System.out.println("\n");
if (personCount > 0 && computerCount == 0) {
System.out.println("You has the highest single card");
} else if (personCount == 0 && computerCount > 0) {
System.out.println("The computer has the highest single card");
} else if (personCount == 0 && computerCount == 0) {
System.out.println("You and the computer are tied for the highest single card");
} else if (personCount > 0 && computerCount > 0) {
System.out.println("Both you and the computer have the highest single card");
}

}
}

閱讀全文

與發牌的java程序相關的資料

熱點內容
命令來自剃頭的用英語怎麼說 瀏覽:765
什麼app不花一分錢買東西 瀏覽:373
布林四線指標源碼 瀏覽:968
單片機的控制板 瀏覽:218
襄陽軟體編程 瀏覽:841
sshpass命令 瀏覽:106
logo伺服器怎麼下載 瀏覽:508
如何ftp連接伺服器 瀏覽:674
creo自動編程 瀏覽:161
雲伺服器在電腦怎麼開 瀏覽:432
ipad相冊如何在文件夾中建文件夾 瀏覽:621
和家親這個app有什麼用 瀏覽:575
什麼app裡面有種樹打折 瀏覽:374
編程外掛入門教學 瀏覽:974
pdf黑白轉彩色 瀏覽:725
英國投資加密貨幣嗎 瀏覽:887
看完程序員那麼可愛後的感受 瀏覽:131
廣播在什麼APP能聽 瀏覽:678
阿克曼小車連接什麼app 瀏覽:773
all100編程器 瀏覽:182