1.初级模拟:
需求 :通过程序实现斗地主过程中的洗牌,发牌和看牌;
思路:
- 创建一个牌盒,也就是定义一个集合对象,用ArrayList集合实现
- 往牌盒里面装牌
- 洗牌,也就是把牌打撒,用Collections的shuffle(方法实现
- 发牌,也就是遍历集合,给三个玩家发牌
- 看牌,也就是三个玩家分别遍历自己的牌
package Doudizhu;
import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;
import java.util.ArrayList;
import java.util.Collections;
public class DouDiZhu1 {
public static void main(String[] args) {
//创建牌盒
ArrayList<String> arrayList = new ArrayList<String>();
//装牌牌
String[] flower = {"♣","♦","❤","♠"};
String[] num = {"3","4","5","6","7","8","9","10","J","Q","K"};
for(String s1:flower){
for (String s2:num){
arrayList.add(s1+s2);
}
}
arrayList.add("大王");
arrayList.add("小王");
//System.out.println(arrayList);
//洗牌
Collections.shuffle(arrayList);
//创建玩家
ArrayList<String> meini = new ArrayList<String>();
ArrayList<String> tudou = new ArrayList<String>();
ArrayList<String> longjie = new ArrayList<String>();
ArrayList<String> dizhupai = new ArrayList<String>();
//发牌
for(int i=0;i<arrayList.size();i++){
String poker = arrayList.get(i);
if(i>=arrayList.size()-3){
dizhupai.add(poker);
}else if (i%3==0){
meini.add(poker);
}else if(i%3==1){
tudou.add(poker);
}else if(i%3==2){
longjie.add(poker);
}
}
kanpai("地主牌",dizhupai);
kanpai("妮妮的牌",meini);
kanpai("土豆的牌",tudou);
kanpai("龙杰的牌",longjie);
}
//看牌
public static void kanpai(String name,ArrayList array){
System.out.println(name +":"+ array);
}
}
2.高级模拟
对拿到手的牌进行排序
思路:
- 创建HashMap,键是编号,值是牌
- 创建ArrayList,存储编号
- 创建花色数组和点数数组
- 从0开始往HashMap里面存储编号,并存储对应的牌。同时往ArrayList里面存储编号
- 洗牌(洗的是编号),用Collections的shuffle()方法实现
- 发牌(发的也是编号,为了保证编号是排序的,创建TreeSet集合接收)
- 定义方法看牌(遍历TreeSet集合,获取编号,到HashMap集合找对应的牌)
- 调用看牌方法
package Doudizhu;
import java.util.*;
public class Doudizhu2 {
public static void main(String[] args) {
//创建HashMap 建是编号,值是牌
Map<Integer,String> map = new HashMap<Integer, String>();
//创建arrayList存储编号
ArrayList<Integer> arrayList = new ArrayList<Integer>();
//装牌牌
String[] flower = {"♣","♦","❤","♠"};
String[] num = {"3","4","5","6","7","8","9","10","J","Q","K"};
int index= 0;
for(String number:num){
for (String color:flower){
map.put(index,color+number);
arrayList.add(index);
index++;
}
}
map.put(index,"小王");
arrayList.add(index);
index++;
map.put(index,"大王");
arrayList.add(index);
System.out.println(map);
// System.out.println(arrayList);
//洗牌 (洗的是编号)
Collections.shuffle(arrayList);
//创建玩家
TreeSet<Integer> meini = new TreeSet<Integer>();
TreeSet<Integer> tudou = new TreeSet<Integer>();
TreeSet<Integer> longjie = new TreeSet<Integer>();
TreeSet<Integer> dizhupai = new TreeSet<Integer>();
//发牌
for(int i=0;i<arrayList.size();i++){
if(i>=arrayList.size()-3){
dizhupai.add(arrayList.get(i));
}else if (i%3==0){
meini.add(arrayList.get(i));
}else if(i%3==1){
tudou.add(arrayList.get(i));
}else if(i%3==2){
longjie.add(arrayList.get(i));
}
}
kanpai("地主牌",dizhupai, (HashMap<Integer, String>) map);
kanpai("妮妮的牌",meini, (HashMap<Integer, String>) map);
kanpai("土豆的牌",tudou, (HashMap<Integer, String>) map);
kanpai("龙杰的牌",longjie, (HashMap<Integer, String>) map);
}
//看牌 (遍历TreeSet集合,获取编号,得到HashMap集合找对应的牌)
public static void kanpai(String name,TreeSet<Integer> ts,HashMap<Integer,String> hm){
System.out.print(name +":");
for(Integer key:ts){
String s = hm.get(key);
System.out.print(s);
}
System.out.println();
}
}