本文实例为大家分享了Java实现斗地主小游戏的具体代码,供大家参考,具体内容如下
原理图:
斗地主过程:
* 1、组合牌
* 2、洗牌
* 3、发牌
* 4、看牌
代码实现:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
package itcast.demo6;
import java.util.ArrayList; import java.util.Collections; import java.util.HashMap;
public class DouDiZhu { public static void main(String[] args) { //1、组合牌 //创建Map集合,键:编号 值:牌 HashMap<Integer,String> pooker = new HashMap<Integer,String>(); //创建List集合,存储编号 ArrayList<Integer> pookerNumber = new ArrayList<Integer>(); //定义13个点数的数组 String[] numbers = { "2" , "A" , "K" , "Q" , "J" , "10" , "9" , "8" , "7" , "6" , "5" , "4" , "3" }; //定义4个花色组合 String[] colors = { "♠" , "♣" , "♥" , "♦" };
//定义一个整数变量,作为Map的键 int index = 2 ; //遍历数组,用花色+点数的组合,存储到Map集合中 for (String number : numbers) { for (String color : colors) { pooker.put(index, color + number); pookerNumber.add(index); index++; } } //System.out.println(pooker); //System.out.println(pookerNumber);
//单独存储大王和小王 pooker.put( 0 , "大王" ); pookerNumber.add( 0 ); pooker.put( 1 , "小王" ); pookerNumber.add( 1 );
//洗牌,将牌的编号打乱 Collections.shuffle(pookerNumber); //System.out.println(pookerNumber);
//发牌,将牌编号,发给3个玩家集合+1个底牌集合 ArrayList<Integer> player1 = new ArrayList<Integer>(); ArrayList<Integer> player2 = new ArrayList<Integer>(); ArrayList<Integer> player3 = new ArrayList<Integer>(); ArrayList<Integer> buttom = new ArrayList<Integer>(); //发牌,采用的是集合的索引%3 for ( int i = 0 ; i < pookerNumber.size();i++) { //现将底牌做好 if (i< 3 ) { //存到底牌去 buttom.add(pookerNumber.get(i)); //对索引%3判断 } else if (i % 3 == 0 ) { //索引上的编号,发给玩家1 player1.add(pookerNumber.get(i)); } else if (i % 3 == 1 ) { //发给玩家2 player2.add(pookerNumber.get(i)); } else if (i % 3 == 2 ) { //发给玩家3 player3.add(pookerNumber.get(i)); } } //对玩家手中的编号进行排序 Collections.sort(player1); Collections.sort(player2); Collections.sort(player3);
//看牌,就是将玩家手中的编号,到Map集合中查找,根据键找值 //定义实现方法 look( "包身工 " ,player1,pooker); look( "清洁工 " ,player2,pooker); look( "洗碗工 " ,player3,pooker); look( "底牌 " ,buttom,pooker); }
public static void look(String name,ArrayList<Integer> player,HashMap<Integer,String> pooker) { //遍历ArrayList集合,获取元素,作为键,到集合Map中找值 System.out.print(name+ " " ); for (Integer key : player) { String value = pooker.get(key); System.out.print(value+ " " ); } System.out.println(); } } |
运行结果:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
原文链接:https://blog.csdn.net/weixin_40521823/article/details/84144861