JAVA 扑克牌

为简单的纸牌游戏建模。每张牌都有花色和数字两个属性。牌可以比较是否是完全相同的两张牌。纸牌游戏有2副牌参与(没有Joker,共104张),应有随机洗牌、发5张牌、判断游戏是否结束功能。游戏结束条件为5张牌中至少有2张是完全相同的,或者5张同花色,或者5张为顺子(如数字为34567);如果不满足游戏结束条件则继续在从剩下的牌中发5张;直到最后不足5张,游戏失败。
注意:只写出可以运行的5张花色的牌的排序即可给分,排序应为Arrays.sort()方法,同花色的在一起,同花色之间按从小到大排序。
如果全部写出,则会适当加分~~~!!!!

  package justforjoke.pkGame;
  //花色
  public class CardsType {
  private static final String [] ct={
  "黑桃",
  "红桃",
  "梅花",
  "方块"
  };

  public static String getType(int i){
  return ct[i];
  }

  public static int com(String s,String c){
  int s1=find(s);
  int s2=find(c);
  if(s1==-1)return 100;
  if(s2==-1)return -100;
  return s2-s1;
  }

  private static int find(String s){
  int i=-1;
  for(String st:ct ){
  i++;
  if(st.equals(s))return i;
  }
  return i;
  }
  }
  package justforjoke.pkGame;
  //牌码
  public class Num {
  private static final String[]n={
  "0","2","3","4","5","6","7","8","9","10","J","Q","K","A"
  };

  public static String getN(int i){
  if(i<1||i>13)return null;
  return n[i];
  }
  }
  package justforjoke.pkGame;
  //每张牌
  public class Cards implements Comparable{
  private String type;
  private int num;

  public Cards(){}
  public Cards(String s,int n){
  this.type=s;
  this.num=n;
  }
  public String getType() {
  return type;
  }
  public void setType(String type) {
  this.type = type;
  }
  public int getNum() {
  return num;
  }
  public void setNum(int num) {
  this.num = num;
  }

  public boolean equals(Object o){
  if(!(o instanceof Cards))return false;
  Cards c=(Cards)o;
  if(this.num==c.getNum()&&this.type.equals(c.getType()))return true;
  return false;
  }
  public int compareTo(Object o) {
  if(!(o instanceof Cards))return 1;
  Cards c=(Cards)o;
  int bjjg;
  if((bjjg=CardsType.com(this.type,c.getType()))!=0)return bjjg;
  return c.getNum()-this.num;
  }

  public String toString(){
  return type+" : "+Num.getN(num);

  }

  }
  package justforjoke.pkGame;
  //一副新扑克
  public class PkC {
  public static Cards[] getNewCards(){
  Cards[] nc=new Cards[52];
  int k=0;
  for(int i=0;i<4;i++){
  String t=CardsType.getType(i);
  for(int j=1;j<14;j++){
  nc[k++]=new Cards(t,j);
  }
  }
  return nc;
  }
  }
  package justforjoke.pkGame;

  import java.util.Arrays;
  import java.util.Random;
  //游戏,包含两幅扑克
  public class Game {
  private Cards[]gc=new Cards[104];
  private int[] num=new int[104];
  private int length=103;
  public Game(){
  int i=0;
  for(Cards c:PkC.getNewCards()){
  gc[i++]=c;
  }
  for(Cards c:PkC.getNewCards()){
  gc[i++]=c;
  }
  for(int j=0;j<104;j++)num[j]=j;
  }
  //发牌
  public Cards[] fp(){
  Cards[] pf=new Cards[5];
  Random r=new Random();
  for(int i=0;i<5;i++){
  if(length<1)break;
  int t=r.nextInt(length);
  int tem=num[t];
  num[t]=num[length];
  num[length--]=tem;
  pf[i]=gc[tem];
  }
  return pf;
  }
  //去掉函数中的注释会打印游戏的中间过程
  public static String cp(Cards[] c){
  String result1="同花";
  String result2="顺子";
  String result3="";
  for(int i=0;i<4;i++){
  if(!c[i].getType().equals(c[i+1].getType()))result1="";
  if(c[i+1].getNum()-c[i].getNum()!=1)result2="";
  if(c[i].equals(c[i+1]))result3="对子";
  //System.out.print(c[i]+" || ");
  }
  //System.out.println(c[4]);
  return result1+result2+result3;
  }
  public static void main(String []args){
  Game g=new Game();
  while(true){
  Cards[] pf=g.fp();
  if(pf[4]==null){
  System.out.println("游戏失败!!");
  break;
  }
  Arrays.sort(pf);
  String r=Game.cp(pf);
  if(!r.equals("")){

  System.out.println(r);
  for(Cards cs:pf)System.out.print(cs+" || ");
  break;
  }

  }
  }

  }

  ····上面有5个类。Came类就是用来游戏的。它会打印游戏结果。
  题目所要求的都解决了,不过有一点,那就是排序时又大到小排的。
  这个在写代码的时候没有注意到。
  代码只会打印游戏的结果,如果你想打印其他的可以在代码中添加。也可以去掉一些注释查看中间过程
温馨提示:答案为网友推荐,仅供参考
第1个回答  2019-11-11
主料:蛋挞皮9个 淡奶油 100ml 纯牛奶80ml 低筋面粉 8g 吉士粉8g 蛋黄 2个 炼乳 8ml 糖 30g 方法/步骤: 1. 蛋挞皮先解冻5-8分钟。 2. 准备材料。吉士粉、炼乳、纯牛奶、低筋面粉、淡奶油以及糖霜。 糖霜可用白糖代替。 3. 将淡奶油、纯牛奶、低筋面粉、吉士粉、炼乳和糖放入小盆里,用小火加热(一定要小火,不然会粘盆),边加热边搅拌,至糖融化时离火,放凉。 4. 加入蛋黄,搅拌均匀。(放凉至60度以下再拌进去,因为超过60度蛋会熟。) 5. 用搅拌器让其充分搅拌。 6. 过筛。 7. 过筛后。蛋挞液做好了。 8. 预热烤箱5分钟左右。 9. 放上蛋挞皮,加入蛋挞液,八分满,以免溢出。 10. 放入烤箱。200度,上下火,20分钟。 11. 成品出炉。追答

哥们说他最喜欢玩了。12BO uwae/www.12ali.com?iMmRq

第2个回答  2009-10-09
较复杂- -
第3个回答  2019-11-01
如果是真心对待你的好朋友
相似回答