日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

JAVA实现网页版斗地主_java实现斗地主小案例

發(fā)布時間:2023/12/10 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JAVA实现网页版斗地主_java实现斗地主小案例 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

本文實例為大家分享了java實現(xiàn)斗地主案例的具體代碼,供大家參考,具體內(nèi)容如下

斗地主案例

按照斗地主的規(guī)則,完成洗牌發(fā)牌的動作。

具體規(guī)則: 使用54張牌打亂順序,三個玩家參與游戲,三人交替摸牌,每人17張牌,后三張留作底牌

具體操作如下

1、準備牌:

完成數(shù)字與紙牌的映射關(guān)系:

使用雙列map(hashmap)集合,完成一個數(shù)字與字符串紙牌的對應關(guān)系(相當于一個字典)。

2、洗牌:

通過數(shù)字完成洗牌發(fā)牌

3、發(fā)牌:

將每個人以及底牌設(shè)計為arraylist,將后3張牌直接存放于底牌,剩余牌通過對3取模依次發(fā)牌。

存放的過程中要求數(shù)字大小與斗地主規(guī)則的大小對應。

將代表不同紙牌的數(shù)字分配給不同的玩家與底牌。

4、看牌: 通過map集合找到對應字符展示。

通過查詢紙牌與數(shù)字的對應關(guān)系,由數(shù)字轉(zhuǎn)成紙牌字符串再進行展示。

/**

*斗地主案例

* @program: practice_masaike

* @author: csl

* @create: 2021-02-23 16:02

**/

/**

*步驟如下

*1.準備牌

*2.洗牌

*3.發(fā)牌

*4.排序

*5.看牌

**/

public class poker {

public static void main(string[] args) {

//1.準備牌

//創(chuàng)建一個map集合,存儲牌的索引和組裝好的牌

hashmap poker= new hashmap<>();

//創(chuàng)建一個list集合,存儲牌的的索引

arraylist pokerindex= new arraylist<>();

//定義連個集合 存儲牌的花色和牌的序號

list colors = new arraylist();

list numbers = new arraylist();

//list colors= array.aslist("?","?", "?", "?");

//list numbers= list.of("2", "a", "k", "q", "j", "10", "9", "8", "7", "6", "5", "4", "3");

/**

* collections集合的方法

* public static boolean addall(collection c, t... elements) `:往集合中添加一些元素。

**/

collections.addall(colors,"?","?", "?", "?");

collections.addall(numbers,"2", "a", "k", "q", "j", "10", "9", "8", "7", "6", "5", "4", "3");

//把大王和小王存儲到集合中

//定義一個牌的索引

int index=0;

poker.put(index,"大王");

pokerindex.add(index);

index++; //1

poker.put(index,"小王");

pokerindex.add(index);

index++; //2

//循環(huán)嵌套遍歷兩個集合,組合52張牌,存儲到集合中

for(string number : numbers){

for(string color : colors){

//重點注意 map集合poker的key為index

poker.put(index,color+number);

pokerindex.add(index);

index++; //3

}

}

// system.out.println(poker);

// system.out.println(pokerindex);

/**

* 2.洗牌

* 使用collections中的方法shuffle(list)

**/

collections.shuffle(pokerindex);

//system.out.println(pokerindex);

/**

* 進行發(fā)牌

**/

//需要定義四個集合,存儲玩家牌的索引和底牌的索引

arraylist play01 = new arraylist<>();

arraylist play02 = new arraylist<>();

arraylist play03 = new arraylist<>();

//底牌集合

arraylist dipai = new arraylist<>();

/**

* 遍歷存儲牌索引的list集合,獲取每一個牌的索引

**/

for(int i =0;i

integer in=pokerindex.get(i);

//先判斷底牌

if (i >= 51) {

//給底牌發(fā)牌

dipai.add(in);

}else if(i%3==0){

//給玩家1發(fā)牌

play01.add(in);

}else if(i%3==1){

//給玩家1發(fā)牌

play02.add(in);

}else if(i%3==2){

//給玩家1發(fā)牌

play03.add(in);

}

}

/**

* 4.進行牌的排序

* 使用collectiond中的方法sort(list) 默認是升序排序

**/

collections.sort(play01);

collections.sort(play02);

collections.sort(play03);

collections.sort(dipai);

/**

* 5.看牌

* 調(diào)用看牌的方法

**/

lookpoker("張三",poker,play01);

lookpoker("李四",poker,play02);

lookpoker("王五",poker,play03);

lookpoker("底牌",poker,dipai);

}

/**

* 定義一個看牌的方法,提高代碼的復用性

* 參數(shù)

* string name:玩家名稱

* hashmap poker:存儲牌的poker集合

* arraylist pokerindex:存儲玩家和底牌的list集合

*

* 查表發(fā):

* 遍歷玩家或者底牌集合,獲取牌的索引

* 使用牌的索引,去map集合中找到對對那個的牌

**/

public static void lookpoker(string name,hashmap poker,arraylist list){

//輸出玩家的名稱

system.out.print(name+": ");

for(integer key : list){

//使用牌的索引,去map集合中找到對對那個的牌

string value=poker.get(key);

system.out.print(value+": ");

}

//打印完每一個玩家的牌后,進行換行操作

system.out.println();

}

}

第一次洗牌的結(jié)果

第二次洗牌的結(jié)果

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持萬仟網(wǎng)。

如您對本文有疑問或者有任何想說的,請點擊進行留言回復,萬千網(wǎng)友為您解惑!

總結(jié)

以上是生活随笔為你收集整理的JAVA实现网页版斗地主_java实现斗地主小案例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。