栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java实现斗地主案例

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java实现斗地主案例

本文实例为大家分享了Java实现斗地主的具体代码,供大家参考,具体内容如下

import java.util.ArrayList;
import java.util.Collections;

public class DemoPoker {
 public static void main(String[] args) {
 
 String[] numbers = {"2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3"};
 String[] colors = {"♥", "♠", "♣", "♦"};
 ArrayList pokerBox = new ArrayList<>();
 for (String color : colors) {
  for (String number : numbers) {
  pokerBox.add(color + number);
  }
 }
 pokerBox.add("大王");
 pokerBox.add("小王");

 
 Collections.shuffle(pokerBox);
 for (int i = 0; i < pokerBox.size(); i++) {
  System.out.print(pokerBox.get(i)+"t");
  if (i==26) {
  System.out.println();
  }
 }
 System.out.println();

 
 //玩家一
 ArrayList player01 = new ArrayList<>();
 //玩家二
 ArrayList player02 = new ArrayList<>();
 //玩家三
 ArrayList player03 = new ArrayList<>();
 //底牌
 ArrayList diPai = new ArrayList<>();
 for (int i = 0; i < pokerBox.size(); i++) {
  String faces = pokerBox.get(i);
  if (i>=51) {
  diPai.add(faces);
  } else if (i%3==0) {
  player01.add(faces);
  } else if (i%3==1) {
  player02.add(faces);
  } else if (i%3==2) {
  player03.add(faces);
  }
 }

 
 System.out.println("张无忌"+player01);
 System.out.println("张翠山"+player02);
 System.out.println("殷素素"+player03);
 System.out.println("底牌"+diPai);
 }
}

带排序版的

package com.demo_2.poker;

import java.util.Collections;
import java.util.HashMap;
import java.util.linkedList;
import java.util.List;

public class Poker {
 
 public static void main(String[] args) {
 
 //numbers:存储普通牌 2、A、K...3从大到小
 List numbers = List.of("2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3");
 //colors:存储四种花色 ♥、♠、♦、♣
 List colors = List.of("♥", "♠", "♦", "♣");
 //创建一个Map集合存储索引和组装好的牌
 HashMap pokerBox = new HashMap<>();
 //创建一个List集合储存牌的索引
 linkedList pokerIndex = new linkedList<>();
 //先把大王、小王和下标分别放进Map的键和值里面,再向linkedList里面存储下标,下标增加1
 int index = 0;
 pokerBox.put(index, "大王");
 pokerIndex.add(index);
 index++;
 pokerBox.put(index, "小王");
 pokerIndex.add(index);
 index++;
 //组装牌:遍历两个List集合,使用Map接口中的put()方法给pokerBox添加键和值,并给linkedList传下标
 for (String number : numbers) {
  for (String color : colors) {
  pokerBox.put(index, color + number);
  pokerIndex.add(index);
  index++;
  }
 }
 
 Collections.shuffle(pokerIndex);

 
 linkedList player01 = new linkedList<>();
 linkedList player02 = new linkedList<>();
 linkedList player03 = new linkedList<>();
 linkedList diPai = new linkedList<>();

 for (int i = 0; i < pokerIndex.size(); i++) {
  Integer in = pokerIndex.get(i);
  if (i >= 51) {
  diPai.add(in);
  } else if (i % 3 == 0) {
  player01.add(in);
  } else if (i % 3 == 1) {
  player02.add(in);
  } else if (i % 3 == 2) {
  player03.add(in);
  }
 }
 //给玩家的牌排序,使用Collocations接口中的sort()方法排序
 Collections.sort(player01);
 Collections.sort(player02);
 Collections.sort(player03);
 Collections.sort(diPai);
 
 print("令狐冲",player01,pokerBox);
 print("诸葛瑾",player02,pokerBox);
 print("司马懿",player03,pokerBox);
 print("底牌",diPai,pokerBox);
 }
 
 public static void print(String name,linkedList list,HashMap map){
 System.out.print(name+":");
 for (Integer key : list) {
  System.out.print(map.get(key)+" ");
 }
 System.out.println();
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/134369.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号