java数组扑克牌代码 java实现扑克牌
Java四人扑克发牌,请教高手将代码进行更好的缩进,在简洁点,最主要性能。
扑克牌还是自己写个对象比较好吧 属性就是花色跟牌值
专注于为中小企业提供网站设计制作、成都做网站服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业沈河免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了1000+企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。
至于发牌什么的 还是用集合来吧 数组操作起来比较麻烦
代码实在是懒得写了 就提点建议
用集合的好处比如说洗牌直接就可以用 collections的静态方法 还有就是 回复初始状态排序 也可以也可以自定义比较器来比较
java的问题(扑克牌)
代码仅供参考,如有疑问,欢迎追问:
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
* 随机扑克牌
* @author STONE
* @version 2015-10-28
*/
public class Poker {
//用于记录丢失的手牌数
private static Integer lostTotal = 0;
//用于存放已下发的扑克
private static Integer[] its1 = new Integer[54];
//用于存放当前的扑克
private static Integer[] its2 = new Integer[54];
private static ListInteger lostList = new ArrayListInteger();
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
for(int i=0;i54;i++){
its1[i] = 0;
its2[i] = 0;
}
System.out.println("系统开始发牌...");
givePoker(5);
System.out.println("发牌完毕,当前手牌为:");
printPoker(its2);
Integer gameTotal = 0;
while(gameTotal10){//循环跑10次,请注意此处发牌没做限制,如果循环次数过大,会导致死循环
System.out.println("请输入需要丢弃的手牌(多张牌格式如下:黑桃1,黑桃2,黑桃3):");
String lost = scn.nextLine();
checkLost(lost);
System.out.println("丢失手牌完毕,当前手牌:");
printPoker(its2);
System.out.println("系统开始重发手牌...");
givePoker(lostTotal);
System.out.println("重发发牌完毕,当前手牌为:");
printPoker(its2);
gameTotal++;
}
scn.close();
}
public static void givePoker(Integer num){//随机分发手牌
int total = 0;
while(totalnum){
int poke = (int)(Math.random()*54);
if(its1[poke] == 0){
its1[poke] = 1;
its2[poke] = 1;
total++;
}
}
lostTotal = 0;
}
public static Boolean checkLost(String lost){//检查需要丢失的手牌
if(lost==null || lost.equals("")){
System.out.println("输入手牌错误,系统无法处理...");
return false;
}
if(lost.indexOf("黑桃")0 lost.indexOf("红心")0 lost.indexOf("梅花")0 lost.indexOf("方块")0){
System.out.println("输入手牌错误,系统无法处理...");
return false;
}
String[] strs = lost.split(",");
Boolean bol = false;
for(String str : strs){
str = str.trim();
if(str==null || str.equals("")) continue;//去掉,防止空格
bol = getPokerNum(str);
}
if(bol){
losePoker();
}
return false;
}
public static void losePoker(){//丢掉手牌
lostTotal = lostList.size();
for(Integer itr : lostList){//丢掉手牌
its2[itr-1] = 0;
}
lostList = new ArrayListInteger();
}
public static Boolean getPokerNum(String str){//获取手牌点数并去掉
try{
Integer itr = 0;
if(str.indexOf("黑桃")==0){
str = str.replace("黑桃", "");
itr = Integer.parseInt(str);
if(itr13){
System.out.println("输入手牌不存在:黑桃"+str);
return false;
}
if(its2[itr-1]==1){
lostList.add(itr);
}
} else if(str.indexOf("红心")==0){
str = str.replace("红心", "");
itr = Integer.parseInt(str);
if(itr13){
System.out.println("输入手牌不存在:红心"+str);
return false;
}
if(its2[itr+12]==1){
lostList.add(itr+13);
}
} else if(str.indexOf("梅花")==0){
str = str.replace("梅花", "");
itr = Integer.parseInt(str);
if(itr13){
System.out.println("输入手牌不存在:梅花"+str);
return false;
}
if(its2[itr+25]==1){
lostList.add(itr+26);
}
} else if(str.indexOf("方块")==0){
str = str.replace("方块", "");
itr = Integer.parseInt(str);
if(itr13){
System.out.println("输入手牌不存在:方块"+str);
return false;
}
if(its2[itr+38]==1){
lostList.add(itr+39);
}
} else if(str.indexOf("小王")==0){
if(its2[52]==1){
lostList.add(53);
}
} else if(str.indexOf("大王")==0){
if(its2[53]==1){
lostList.add(54);
}
}
return true;
} catch( Exception e ){
System.out.println("输入手牌有误...");
return false;
}
}
public static void printPoker(Integer[] its){//打印当前手牌
String result = "";
for(int i=0;iits.length;i++){
if(its[i]==1){//等于1表示当前手牌存在
result+=pukerCheck(i+1)+",";
}
}
System.out.println(result);
}
public static String pukerCheck(Integer itr){//判断扑克类型
/**
* 1~13黑桃花色、14~26红心花色、27~39梅花花色
* 40~52方块花色、53小王、54大王
*/
if(1=itr itr=13){
return "黑桃"+itr;
} else if(14=itr itr=26){
return "红心"+(itr-13);
} else if(27=itr itr=39){
return "梅花"+(itr-26);
} else if(40=itr itr=52){
return "方块"+(itr-39);
} else if(itr==53){
return "小王";
} else if(itr==54){
return "大王";
}
return "";
}
}
测试运行结果:
请问用java数组编写洗牌程序怎么写?我刚学java,请多指教,谢谢!!!
package xly04;
import java.util.Random;
public class ArrayDemo3 {
/**
* 数组应用:
* 1 随机洗牌
* 2 顺序发牌
*/
public static void main(String[] args) {
String[] cards=new String[]{"黑桃A","黑桃2","黑桃3","黑桃4",
"黑桃5","黑桃6","黑桃7","黑桃8","黑桃9","黑桃10","黑桃J","黑桃Q","黑桃K",
"红桃A","红桃2","红桃3","红桃4","红桃5","红桃6","红桃7","红桃8","红桃9",
"红桃10","红桃J","红桃Q","红桃K","方块A","方块2","方块3","方块4","方块5",
"方块6","方块7","方块8","方块9","方块10","方块J","方块Q","方块K","梅花A",
"梅花2","梅花3","梅花4","梅花5","梅花6","梅花7","梅花8","梅花9","梅花10",
"梅花J","梅花Q","梅花K","大王","小王"};
//Random random=new Random();.......(1)
//int index=random.nextInt(10);//[0,10) %10
//System.out.println(index);
//String card=cards[random.nextInt(cards.length)];//长度为8,随机下标[0,8)
//System.out.println(card);//随机的某张牌
for(int i=cards.length-1;i=1;i--){
int j=new Random().nextInt(i);//或者(1)不注销用int j=random.nextInt(i);
String t=cards[i];
cards[i]=cards[j];
cards[j]=t;
}
for(int i=0;icards.length;i++){
System.out.print(cards[i]+",");
}
System.out.println();//输出回车字符
//顺序发牌:顺序的将每个牌发给每个人
String[] players=new String[]{"马伊琍","文章","六六"};
//int i=0;
//System.out.println(players[i++%players.length]);
//System.out.println(players[i++%players.length]);
//System.out.println(players[i++%players.length]);
//System.out.println(players[i++%players.length]);
//System.out.println(players[i++%players.length]);
//System.out.println(players[i++%players.length]);
for(int i=0;icards.length;i++){
//i=0,1,2,3,4,5,6,7
String card=cards[i];//card 代表每张扑克牌
String player=players[i%players.length];
//player代表每个玩游戏的人
System.out.println(player+":"+card);
}
}
}
本文标题:java数组扑克牌代码 java实现扑克牌
文章来源:http://azwzsj.com/article/ddgcjjj.html