首页 > 编程 > Java > 正文

Java基础高级综合练习题扑克牌的创建

2019-11-26 09:20:26
字体:
来源:转载
供稿:网友

最近学了很多的知识,脑容量小,记不清,还是得做做练习!

今天就做了一个扑克牌的练习

首先呢..这个逻辑一定要非常清楚,我们要想做出一副扑克牌,必定要弄清楚每一张牌和整的一副牌

首先分析 一张扑克

一张牌里面有什么?相信大家看图(图不是我写的)就应该懂了,一张扑克有属于它自己的花色(红桃,黑桃,梅花,方块) 以及自己的点数(A,2,3…..J,Q,K)就这两种属性,对吧!

那么花色符号,点数符号是个啥? 花色符号就是来代替我们的花色的,我们不可能拿着“红桃”这种文字写进程序吧!所以我们可以用数字来代替

我们就按照下面的,一 一对应

/*** 王 ♥ ♠ ♣ ♦ * 1 2 3 4 5 * A J Q K 小王 大王 * 1 11 12 13 14 15 **/

好了,我们已经把每张特殊一点的扑克给对应好了!我们可以开始写代码了

我的代码文件:

  • APoker.java――――一张扑克
  • Poker.java―――――-一副扑克
  • Test.java――――――测试

APoker.java先给大家展示

public class APoker {  //implements Comparable<APoker>   //花色   private int color;   //点数     private int count;   //花色符号   private String colorText;   //点数符号   private String countText;   //写构造方法  public APoker(int color, int count, String colorText, String countText) {    super();    this.color = color;    this.count = count;    this.colorText = colorText;    this.countText = countText;  }  //GET SET 方法,进行封装  public int getColor() {    return color;  }  public void setColor(int color) {    this.color = color;  }  public int getCount() {    return count;  }  public void setCount(int count) {    this.count = count;  }  public String getColorText() {    return colorText;  }  public void setColorText(String colorText) {    this.colorText = colorText;  }  public String getCountText() {    return countText;  }  public void setCountText(String countText) {    this.countText = countText;  }  //重写 toString 方法,因为我们需要显示每张牌的具体情况  @Override  public String toString() {    return "APoker [color=" + color + ", count=" + count + ", colorText=" + colorText + ", countText=" + countText        + "]/n";  }  }

这里还是非常容易理解的,无非就是进行了封装和重写toString方法。

OK,一张扑克写完了,我们接下来写一副扑克牌

一副扑克牌

我再把那个图拿下来

我们发现一副扑克牌里面有花色数量,扑克牌的数量,以及所有牌(所有牌也就是一个集合)。另外这里面还有着几个方法,这里我就写创建扑克(),洗牌()抽取一张() 吧。

现在看下

Poker.java:

import java.util.ArrayList;import java.util.Collections;import java.util.List;import java.util.Random;public class Poker {  //花色数量 private int colorcount;  //牌的数量 private int pokercount; //牌的集合 private List<APoker> mList; //进行封装public int getColorcount() {  return colorcount;}public void setColorcount(int colorcount) {  this.colorcount = colorcount;}public int getPokercount() {  return pokercount;}public void setPokercount(int pokercount) {  this.pokercount = pokercount;}public List<APoker> getmList() {  return mList;}public void setmList(List<APoker> mList) {  this.mList = mList;}/** * 王 ♥ ♠ ♣ ♦ * 1 2 3 4 5 * A J  Q  K  小王  大王 * 1 11 12 13  14  15 **/      //创建一副扑克牌     public List<APoker> creatPoker() {      //初始化colorcount pokercount       colorcount=5;//一副扑克有 王 ♥ ♠ ♣ ♦这五种花色       pokercount=54;//一副扑克共有54张牌       mList=new ArrayList<APoker>();       //  ♥ ♠ ♣ ♦----------先分析这四种,因为这四种里面才含有A-K的值,小王大王后面处理       for (int i = 2; i <=5; i++) {         //得到每种花色里面的牌         for (int j = 1; j <= 13; j++) {           String colorText=null;           String countText=null;          switch (i) {           case 2:            colorText="♥";            break;           case 3:             colorText="♠";              break;           case 4:             colorText="♣";              break;           case 5:             colorText="♦";            break;          }          switch (j) {          case 1:            countText="A";            break;          case 11:            countText="J";            break;          case 12:            countText="Q";            break;          case 13:            countText="K";            break;          default:            countText=j+""; //除了A,J,Q,K,都直接使用数字,这里是将j转化为字符            break;          }           APoker aPoker1=new APoker(i, j, colorText, countText);           mList.add(aPoker1); //把♥ ♠ ♣ ♦这四种花色塞进一副扑克里面        }      }       APoker aPoker2=new APoker(1, 14, "王", "小王");//写小王       APoker aPoker3=new APoker(1, 14, "王", "大王");//写大王       mList.add(aPoker2);//把小王塞进一副扑克里面去       mList.add(aPoker3);//把大王塞进一副扑克里面去      return mList;    }      /**      *洗牌方法      **/     public List<APoker> shufflePoker() {      Collections.shuffle(mList); //这是Collections的一个把集合打乱的方法      return mList;    }     /**     * 随机抽牌     **/     public APoker getRandomPoker() {       Random random=new Random();//获取一个随机数       int index=random.nextInt(54);       return mList.get(index);    }}

这里慢慢看也很容易的,我已经全部把每一步解释了,大家根据那个对应关系应该很容易理解。

两个写好了,我们可以进行使用了

Test.java就是我们用来测试我们之前写好的代码!

创建一副扑克

import java.util.List;public class Test {    public static void main(String[] args) {    Poker poker=new Poker();//创建一副扑克对象    List<APoker> mList=poker.creatPoker(); 调用creatPoker()方法,创建一副扑克    System.out.println(mList);打印出来!  }}

我们来看结果

OK 54张扑克被创建了!

洗牌

我们修改一下Test.java的内容

import java.util.List;public class Test {    public static void main(String[] args) {    Poker poker=new Poker();    List<APoker> mList=poker.creatPoker();    List<APoker> mList2=poker.shufflePoker();    System.out.println(mList2);  }}

打印一下

果然,,牌的顺序已经乱了,我们进行了洗牌

随机抽牌

我们继续重写一下Test.java

import java.util.List;public class Test {    public static void main(String[] args) {    Poker poker=new Poker();    List<APoker> mList=poker.creatPoker();    APoker ap=poker.getRandomPoker();   System.out.println(ap);   }}

打印一下

果然它随机抽取了一张,每次打印抽取的牌都是不同的,这里就不展示了!

OK,大家继续学习吧,come on!

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对武林网的支持。如果你想了解更多相关内容请查看下面相关链接

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表