java实现斗地主最简代码实例-kb88凯时官网登录

时间:2020-05-10
阅读:
免费资源网,https://freexyz.cn/

案例说明:使用java实现简单的斗地主洗牌发牌的操作;

具体规则:

共有54张牌,顺序打乱;

三个玩家参与游戏,三人交替摸牌,每人17张牌,最后留三张为底牌(地主牌)。

具体代码实现步骤:

扑克牌准备操作

import java.util.arraylist;
import java.util.collections;
import java.util.list;
public class pokergame {
  public static void main(string[] args) {
    //定义扑克牌的花色以及数字的数组
    string[] flower = {"♥️","♠️","♣️","♦️"};
    string[] numbers = {"a","2","3","4","5","6","7","8","9","10","j","q","k"};
    
    //创建牌组集合
    list poker = new arraylist();
    //组合扑克牌样式
    //拿出每一个花色,然后跟每一个数字进行结合,储存在牌组中
    for (int i = 0; i < flower.length; i  ) {
      for (int j = 0; j < numbers.length; j  ) {
        poker.add(flower[i].concat(numbers[j]));
      }
    }
    //添加大小王到牌组集合中
    poker.add("大王");
    poker.add("小王");
    //打印输出所有扑克牌测试
    system.out.println(poker);
    //定义三个玩家和地主牌的集合
    list player1 = new arraylist();
    list player2 = new arraylist();
    list player3 = new arraylist();
    list dizhupai = new arraylist();
    //洗牌操作,使用shuffle方法
    collections.shuffle(poker);
    system.out.println(poker); //测试牌有没有被打乱
    //发牌操作
    for (int k = 0; k 50) {
        dizhupai.add(poker.get(k));
      } else if (k % 3 == 0) {
        player1.add(poker.get(k));
      } else if (k % 3 == 1) {
        player2.add(poker.get(k));
      } else if (k % 3 == 2) {
        player3.add(poker.get(k));
      }
    }
    //最终看牌操作
    system.out.println("玩家一的牌" player1);
    system.out.println("玩家二的牌" player2);
    system.out.println("玩家三的牌" player3);
    system.out.println("地主牌" dizhupai);
  }
}

结果展示

玩家一的牌:[♥️9, ♠️a, ♠️5, ♥️a, ♥️2, ♥️10, ♠️7, ♣️j, ♥️3, ♠️2, ♣️5, ♦️2, ♠️6, ♥️7, ♦️k, ♣️9, 大王]
玩家二的牌:[♦️9, ♠️4, ♣️4, ♣️10, ♥️j, ♦️4, ♥️8, ♥️5, ♦️a, ♣️a, ♦️7, ♦️3, ♦️q, ♣️k, ♥️6, ♣️3, ♠️q]
玩家三的牌:[♣️q, ♣️2, ♣️8, ♣️7, ♥️q, ♦️10, ♠️8, ♣️6, ♥️4, ♠️10, 小王, ♥️k, ♠️3, ♦️8, ♦️6, ♠️k, ♠️j]
地主牌:[♠️9, ♦️5, ♦️j]

实例补充

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 pooker = new hashmap();
 //创建list集合,存储编号
 arraylist pookernumber = new arraylist();
 //定义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 player1 = new arraylist();
 arraylist player2 = new arraylist();
 arraylist player3 = new arraylist();
 arraylist buttom = new arraylist();
 //发牌,采用的是集合的索引%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 player,hashmap pooker) {
 //遍历arraylist集合,获取元素,作为键,到集合map中找值
 system.out.print(name " ");
 for(integer key : player) {
 string value = pooker.get(key);
 system.out.print(value " ");
 }
 system.out.println();
 }
}

包身工 大王 ♣2 ♥2 ♦2 ♦a ♦k ♣j ♥10 ♠9 ♣9 ♥9 ♦9 ♣5 ♦5 ♦4 ♠3 ♦3
清洁工 小王 ♠k ♥k ♠q ♣q ♦q ♦j ♠10 ♦10 ♥8 ♠7 ♥7 ♦7 ♥5 ♠4 ♣3 ♥3
洗碗工 ♠2 ♥a ♣k ♥q ♠j ♥j ♣10 ♠8 ♦8 ♣7 ♠6 ♣6 ♥6 ♦6 ♠5 ♣4 ♥4
底牌 ♠a ♣a ♣8

免费资源网,https://freexyz.cn/
返回顶部
顶部
网站地图