JAVA实现网页版斗地主_java实现斗地主小案例
本文實例為大家分享了java實現斗地主案例的具體代碼,供大家參考,具體內容如下
斗地主案例
按照斗地主的規則,完成洗牌發牌的動作。
具體規則: 使用54張牌打亂順序,三個玩家參與游戲,三人交替摸牌,每人17張牌,后三張留作底牌
具體操作如下
1、準備牌:
完成數字與紙牌的映射關系:
使用雙列map(hashmap)集合,完成一個數字與字符串紙牌的對應關系(相當于一個字典)。
2、洗牌:
通過數字完成洗牌發牌
3、發牌:
將每個人以及底牌設計為arraylist,將后3張牌直接存放于底牌,剩余牌通過對3取模依次發牌。
存放的過程中要求數字大小與斗地主規則的大小對應。
將代表不同紙牌的數字分配給不同的玩家與底牌。
4、看牌: 通過map集合找到對應字符展示。
通過查詢紙牌與數字的對應關系,由數字轉成紙牌字符串再進行展示。
/**
*斗地主案例
* @program: practice_masaike
* @author: csl
* @create: 2021-02-23 16:02
**/
/**
*步驟如下
*1.準備牌
*2.洗牌
*3.發牌
*4.排序
*5.看牌
**/
public class poker {
public static void main(string[] args) {
//1.準備牌
//創建一個map集合,存儲牌的索引和組裝好的牌
hashmap poker= new hashmap<>();
//創建一個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
//循環嵌套遍歷兩個集合,組合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);
/**
* 進行發牌
**/
//需要定義四個集合,存儲玩家牌的索引和底牌的索引
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) {
//給底牌發牌
dipai.add(in);
}else if(i%3==0){
//給玩家1發牌
play01.add(in);
}else if(i%3==1){
//給玩家1發牌
play02.add(in);
}else if(i%3==2){
//給玩家1發牌
play03.add(in);
}
}
/**
* 4.進行牌的排序
* 使用collectiond中的方法sort(list) 默認是升序排序
**/
collections.sort(play01);
collections.sort(play02);
collections.sort(play03);
collections.sort(dipai);
/**
* 5.看牌
* 調用看牌的方法
**/
lookpoker("張三",poker,play01);
lookpoker("李四",poker,play02);
lookpoker("王五",poker,play03);
lookpoker("底牌",poker,dipai);
}
/**
* 定義一個看牌的方法,提高代碼的復用性
* 參數
* string name:玩家名稱
* hashmap poker:存儲牌的poker集合
* arraylist pokerindex:存儲玩家和底牌的list集合
*
* 查表發:
* 遍歷玩家或者底牌集合,獲取牌的索引
* 使用牌的索引,去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();
}
}
第一次洗牌的結果
第二次洗牌的結果
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持萬仟網。
如您對本文有疑問或者有任何想說的,請點擊進行留言回復,萬千網友為您解惑!
總結
以上是生活随笔為你收集整理的JAVA实现网页版斗地主_java实现斗地主小案例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 小数转换成二进制c语言,只写出了十进制小
- 下一篇: Leetcode::Subsets