中国电力建设集团有限公司网站,网站功能开发,学做衣服网 缤纷网站,WordPress动漫源码【顺序表使用练习】发牌游戏 1. 介绍游戏2. 实现52张牌3. 实现洗牌4. 实现发牌5. 效果展示 1. 介绍游戏 首先先为大家介绍一下设计要求 实现52张牌#xff08;这里排除大小王#xff09;洗牌——打乱牌的顺序发牌——3个人#xff0c;1人5张牌 2. 实现52张牌
创建Code对象创… 【顺序表使用练习】发牌游戏 1. 介绍游戏2. 实现52张牌3. 实现洗牌4. 实现发牌5. 效果展示 1. 介绍游戏 首先先为大家介绍一下设计要求 实现52张牌这里排除大小王洗牌——打乱牌的顺序发牌——3个人1人5张牌 2. 实现52张牌
创建Code对象创建codelist顺序表储存52张牌
下面是代码演示
public class Code {//创建Code类String suit;//定义花色int number;//定义数字//创建一个数组储存4种花色public static final String[] suits{❤,♠,♦,♣};Override//重写输出格式public String toString() {return Code{ suit number };}public Code() {}public Code(String suit, int number) {this.suitsuit;this.numbernumber;}public static ListCode buycode(){//初始化牌面ListCode codeListnew ArrayList();//创建顺序表for (int i 0; i 13; i) {for (int j 0; j 4; j) {String suitsuits[j];int numberi;Code codenew Code(suit,number);//创建codecodeList.add(code);//将每次创建的code都add尾插到顺序表中//实现52张牌的存储}}return codeList;}3. 实现洗牌 如何实现洗牌 第一时间大部分人都会想到随机数那么如何使用随机数实现洗牌呢 下面是代码实现 public static ListCode randcode(ListCode codeList){//洗牌Random randomnew Random();for (int i codeList.size()-1; i0; i--) {int arandom.nextInt(i);swap(codeList,i,a);}return codeList;}4. 实现发牌 创建一个二维顺序表h h1 h2 h3 分别是三个人被分到的牌 下面是代码实现
public static ListListCode play(ListCode codeList){ListCode h1new ArrayList();ListCode h2new ArrayList();ListCode h3new ArrayList();ListListCode hnew ArrayList();h.add(h1);h.add(h2);h.add(h3);for (int i 0; i 3; i) {for (int j 0; j 5; j) {Code codecodeList.remove(0);h.get(i).add(code);}}return h;}5. 效果展示
main函数
public class Test {public static void main(String[] args) {Code codenew Code();//初始化牌面ListCode codeListcode.buycode();System.out.println(初始化牌面codeList);//洗牌System.out.println(洗牌code.randcode(codeList));//发牌ListListCode hcode.play(codeList);System.out.println(发牌);for (int i 0; i h.size(); i) {System.out.println(h.get(i));}}
}