① 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");
}
}
}