java實(shí)現(xiàn)斗地主小案例
本文實(shí)例為大家分享了java實(shí)現(xiàn)斗地主案例的具體代碼,供大家參考,具體內(nèi)容如下
斗地主案例
按照斗地主的規(guī)則,完成洗牌發(fā)牌的動作。
具體規(guī)則: 使用54張牌打亂順序,三個玩家參與游戲,三人交替摸牌,每人17張牌,后三張留作底牌
具體操作如下
1、準(zhǔn)備牌:
完成數(shù)字與紙牌的映射關(guān)系:
使用雙列Map(HashMap)集合,完成一個數(shù)字與字符串紙牌的對應(yīng)關(guān)系(相當(dāng)于一個字典)。
2、洗牌:
通過數(shù)字完成洗牌發(fā)牌
3、發(fā)牌:
將每個人以及底牌設(shè)計為ArrayList,將后3張牌直接存放于底牌,剩余牌通過對3取模依次發(fā)牌。
存放的過程中要求數(shù)字大小與斗地主規(guī)則的大小對應(yīng)。
將代表不同紙牌的數(shù)字分配給不同的玩家與底牌。
4、看牌: 通過Map集合找到對應(yīng)字符展示。
通過查詢紙牌與數(shù)字的對應(yīng)關(guān)系,由數(shù)字轉(zhuǎn)成紙牌字符串再進(jìn)行展示。
/**
*斗地主案例
* @program: practice_masaike
* @author: csl
* @create: 2021-02-23 16:02
**/
/**
*步驟如下
*1.準(zhǔn)備牌
*2.洗牌
*3.發(fā)牌
*4.排序
*5.看牌
**/
public class Poker {
public static void main(String[] args) {
//1.準(zhǔn)備牌
//創(chuàng)建一個Map集合,存儲牌的索引和組裝好的牌
HashMap<Integer,String> poker= new HashMap<>();
//創(chuàng)建一個List集合,存儲牌的的索引
ArrayList<Integer> pokerIndex= new ArrayList<>();
//定義連個集合 存儲牌的花色和牌的序號
List<String> colors = new ArrayList<String>();
List<String> numbers = new ArrayList<String>();
//List<String> colors= Array.asList("♣","♦", "♥", "♠");
//List<String> numbers= List.of("2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3");
/**
* Collections集合的方法
* public static <T> boolean addAll(Collection<T> 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){
//重點(diǎn)注意 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);
/**
* 進(jìn)行發(fā)牌
**/
//需要定義四個集合,存儲玩家牌的索引和底牌的索引
ArrayList<Integer> play01 = new ArrayList<>();
ArrayList<Integer> play02 = new ArrayList<>();
ArrayList<Integer> play03 = new ArrayList<>();
//底牌集合
ArrayList<Integer> diPai = new ArrayList<>();
/**
* 遍歷存儲牌索引的List集合,獲取每一個牌的索引
**/
for(int i =0;i<pokerIndex.size();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.進(jìn)行牌的排序
* 使用Collectiond中的方法sort(List) 默認(rèn)是升序排序
**/
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);
}
/**
* 定義一個看牌的方法,提高代碼的復(fù)用性
* 參數(shù)
* String name:玩家名稱
* HashMap<Integer,String> poker:存儲牌的poker集合
* ArrayList<Integer> pokerIndex:存儲玩家和底牌的List集合
*
* 查表發(fā):
* 遍歷玩家或者底牌集合,獲取牌的索引
* 使用牌的索引,去Map集合中找到對對那個的牌
**/
public static void lookPoker(String name,HashMap<Integer,String> poker,ArrayList<Integer> list){
//輸出玩家的名稱
System.out.print(name+": ");
for(Integer key : list){
//使用牌的索引,去Map集合中找到對對那個的牌
String value=poker.get(key);
System.out.print(value+": ");
}
//打印完每一個玩家的牌后,進(jìn)行換行操作
System.out.println();
}
}
第一次洗牌的結(jié)果

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

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
feign的ribbon超時配置和hystrix的超時配置說明
這篇文章主要介紹了feign的ribbon超時配置和hystrix的超時配置說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09
使用jsoup解析html的table中的文本信息實(shí)例
今天小編就為大家分享一篇使用jsoup解析html的table中的文本信息實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-05-05
Java8新特性之Base64詳解_動力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了Java8新特性之Base64的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06
java中實(shí)體類和JSON對象之間相互轉(zhuǎn)化
Java中關(guān)于Json格式轉(zhuǎn)化Object,Map,Collection類型和String類型之間的轉(zhuǎn)化在我們實(shí)際項(xiàng)目中應(yīng)用的很是普遍和廣泛。最近工作的過程中也是經(jīng)常有,因此,自己封裝了一個類分享給大家。2015-05-05

