Java实现斗地主案例
作者:-_Coco_ 发布时间:2023-06-01 06:43:59
标签:java,斗地主
本文实例为大家分享了Java实现斗地主的具体代码,供大家参考,具体内容如下
import java.util.ArrayList;
import java.util.Collections;
public class DemoPoker {
public static void main(String[] args) {
/**
*一、准备牌
普通牌:2 A K...3
花色:♥ ♠ ♣ ♦
王牌:大王 小王
创建一个集合,把牌组装之后存进去
*/
String[] numbers = {"2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3"};
String[] colors = {"♥", "♠", "♣", "♦"};
ArrayList<String> pokerBox = new ArrayList<>();
for (String color : colors) {
for (String number : numbers) {
pokerBox.add(color + number);
}
}
pokerBox.add("大王");
pokerBox.add("小王");
/**
* 二、洗牌
static void shuffle(List<?> list) 使用默认的随机源随机置换指定的列表。
此处为了输出结果工整所以没有直接输出集合
*/
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();
/**
* 三、发牌
遍历集合,用索引%3发牌,余0给玩家1,余1给玩家2,余2给玩家3
索引0-50是玩家的牌,51-53是底牌
*/
//玩家一
ArrayList<String> player01 = new ArrayList<>();
//玩家二
ArrayList<String> player02 = new ArrayList<>();
//玩家三
ArrayList<String> player03 = new ArrayList<>();
//底牌
ArrayList<String> 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) {
/**
* 第一步:备牌
使用List接口中的of()方法添加并分别创建numbers和colors集合
*/
//numbers:存储普通牌 2、A、K...3从大到小
List<String> numbers = List.of("2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3");
//colors:存储四种花色 ♥、♠、♦、♣
List<String> colors = List.of("♥", "♠", "♦", "♣");
//创建一个Map集合存储索引和组装好的牌
HashMap<Integer, String> pokerBox = new HashMap<>();
//创建一个List集合储存牌的索引
LinkedList<Integer> 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++;
}
}
/**
* 第二步:洗牌
使用Collocations类中的shuffler方法,传递参数pokerIndex
*/
Collections.shuffle(pokerIndex);
/**
* 第三步:发牌
创建四个List集合,分别存储三位玩家和底牌
使用for循环遍历pokerIndex,i%3结果为0的给玩家1,1的给玩家2,2的给玩家3
*/
LinkedList<Integer> player01 = new LinkedList<>();
LinkedList<Integer> player02 = new LinkedList<>();
LinkedList<Integer> player03 = new LinkedList<>();
LinkedList<Integer> 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);
/**
*第四步:看牌
遍历排过序的List集合作为Map集合的键值获取对应的值
为提高代码复用性定义一个方法代替
*/
print("令狐冲",player01,pokerBox);
print("诸葛瑾",player02,pokerBox);
print("司马懿",player03,pokerBox);
print("底牌",diPai,pokerBox);
}
/**
*看牌的方法:
参数:
String name
LinkedList<Integer> list
HashMap<Integer, String> map
*/
public static void print(String name,LinkedList<Integer> list,HashMap<Integer, String> map){
System.out.print(name+":");
for (Integer key : list) {
System.out.print(map.get(key)+" ");
}
System.out.println();
}
}
来源:https://blog.csdn.net/wwwwssw_/article/details/104622499


猜你喜欢
- java读取文件里面部分汉字内容乱码读取一个txt文件,到代码中打印出来,发票有部分汉字的内容是乱码的。我开始的方式是这样的, 如下,这是完
- 下面是配置Android开发ADB环境变量的操作步骤。工具/原料win7系统电脑+Android SDK方法/步骤1.首先右击计算机——属性
- @NonNull导致无法序列化的问题以上这个代码在接参的时候报了一个缺少无参构造函数无法序列化的错误将.class反编译可以看到编译后的源码
- 快速普及1、mybatis是什么 mybatis是一个支持普通SQL查询,存储过
- 说到关注功能,可能很多小伙伴要说了。谁不会写但是没有合理的架构,大家写出来的代码很可能是一大堆的复制粘贴。比如十几个页面,都有这个关注按钮。
- webflux介绍Spring Boot 2.0spring.io 官网有句醒目的话是:BUILD ANYTHING WITH SPRING
- Feign远程调用Multipartfile参数今天在写业务代码的时候遇到的问题, 前端请求A服务,能正确把参数给到A服务<参数里面包
- 目录问题为每个request设置超时值Http Handler给Request加上超时处理抛出正确的异常使用Handler总结HttpCli
- 本文实例讲述了WPF中不规则窗体与WindowsFormsHost控件兼容问题的解决方法。分享给大家供大家参考。具体方法如下:这里首先说明一
- 由于又开了新机器所以又要重新布置Jenkins从老项目拷贝过来,发现Job Import Plugin 这个插件更新了,和以前的有些出入所以
- Web基础和HTTP协议┌─────────┐┌─────────┐
- from jnius import autoclass>>> Stack = autoclass('java.ut
- Java7中文件IO发生了很大的变化,专门引入了很多新的类:import java.nio.file.DirectoryStream;imp
- 这篇博客将梳理一下.NET中4个Timer类,及其用法。1. System.Threading.Timerpublic Timer(Time
- 本文实例讲述了Android图片处理的方法。分享给大家供大家参考,具体如下:package cn.szbw.util;import Andr
- 题目一 解法class Solution { public int findLengthOfLCIS(i
- C++实现接两个链表实例代码有以ha为头结点的链表,元素个数为m;以hb为头结点的链表,元素个数为n。现在需要你把这两个链表连接
- 软件生存周期中,涉及代码运行的环节有编码、测试和维护阶段,而一套成熟的代码,在此三个阶段,数据库、日志路径、日志级别、线程池大小等配置一般会
- 1.介绍我们知道,我们要使一个类型支持foreach循环,就需要这个类型满足下面条件之一:该类型实例如果实现了下列接口中的其中之一:Syst
- 问题:写了一个新的dao接口,进行单元测试时提示:Initialization of bean failed; nested excepti