/*
* 模拟斗地主洗牌、发牌、看牌*/
package PokerDemo;
import java.util.ArrayList;
import java.util.Collections;
public class Poker {
public static void main(String[] args) {
//定义牌盒
ArrayList arr=new ArrayList();
String[] colors={"♦","♣","♥","♠"};
String[] numbers={"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
//将54张牌存入数组
for(String color:colors){
for(String number: numbers){
arr.add(color+number);
}
}
arr.add("小王");
arr.add("大王");
//洗牌
/* System.out.println(arr);*/
Collections.shuffle(arr);
//创建三个玩家和底牌集合
ArrayList dxp=new ArrayList();
ArrayList cyy=new ArrayList();
ArrayList ztw=new ArrayList();
ArrayList dp=new ArrayList();
//发牌
for(int i=0;i< arr.size();i++){
String poker = arr.get(i);
if(i>= arr.size()-3){
dp.add(poker);
}
else{
if(i%3==0){
dxp.add(poker);
}
else if(i%3==1){
cyy.add(poker);
}
else if(i%3==2){
ztw.add(poker);
}
}
}
//看牌
lookPoker("dxp",dxp);
lookPoker("cyy",cyy);
lookPoker("ztw",ztw);
lookPoker("底牌",dp);
}
//看牌方法
public static void lookPoker(String name,ArrayList arr){
System.out.print(name+"的牌是:");
for(String poker:arr){
System.out.print(poker+" ");
}
System.out.println();
}
}
/* * 思路: * 创建HashMap,键是编号,牌是值 * 创建ArrayList集合,存储编号 * 创建花色和点数数组 * 将花色和对应数组存入HashMap集合,将编号存入ArrayList集合 * 洗牌(洗编号) * 发牌(发编号,为了排序利用TreeSet集合存储) * 定义看牌方法 * 看牌 */
//模拟斗地主洗牌、发牌、看牌,并进行排序
/*
* 思路:
* 创建HashMap,键是编号,牌是值
* 创建ArrayList集合,存储编号
* 创建花色和点数数组
* 将花色和对应数组存入HashMap集合,将编号存入ArrayList集合
* 洗牌(洗编号)
* 发牌(发编号,为了排序利用TreeSet集合存储)
* 定义看牌方法
* 看牌*/
package PokerDemo;
import java.util.*;
public class Poker2 {
public static void main(String[] args) {
//创建HashMap,键是编号,牌是值
HashMap hm=new HashMap();
//创建ArrayList集合,存储编号
ArrayList arr=new ArrayList();
//创建花色和点数数组
String[] colors={"♦","♣","♥","♠"};
String[] numbers={"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
//创建Poker集合存储牌
ArrayList poker=new ArrayList() ;
for(String number:numbers){
for(String color:colors){
poker.add(color+number);
}
}
poker.add("小王");
poker.add("大王");
//* 将花色和对应数组存入HashMap集合,将编号存入arr集合
for(int i=0;i<54;i++){
hm.put(i, poker.get(i));
arr.add(i);
}
//System.out.println(hm);
//* 洗牌(洗编号)
Collections.shuffle(arr);
//* 发牌(发编号,为了排序利用TreeSet集合存储)
TreeSet dxp=new TreeSet();
TreeSet cyy=new TreeSet();
TreeSet ztw=new TreeSet();
TreeSet dp=new TreeSet();
for(int i=0;i< arr.size();i++){
int poke = arr.get(i);
if(i>= arr.size()-3){
dp.add(poke);
}
else{
if(i%3==0){
dxp.add(poke);
}
else if(i%3==1){
cyy.add(poke);
}
else if(i%3==2){
ztw.add(poke);
}
}
}
//看牌
lookPoker("dxp",hm,dxp);
lookPoker("cyy",hm,cyy);
lookPoker("ztw",hm,ztw);
lookPoker("dp",hm,dp);
}
public static void lookPoker(String name,HashMap hm,TreeSet ts){
System.out.print(name+"的牌是:");
for(Integer i:ts){
String s = hm.get(i);
System.out.print(s+" ");
}
System.out.println();
}
}