- 案例:模拟斗地主
需求:通过程序实现斗地主过程中的洗牌、发牌和看牌。
分析:
(1)创建一个牌盘,即定义一个集合对象,用ArrayList集合实现;
(2)在牌盘里装牌;
(3)洗牌:即把牌打散,用Collections的shuffle()方法实现。
(4)发牌:即遍历集合,给三个玩家发牌;
(5)看牌:即三个玩家分别遍历自己的牌。
完整代码:
package com.xuexi;
import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils;
import java.util.ArrayList;
import java.util.Collections;
public class Demo3 {
public static void main(String[] args) {
//创建一个牌盘,即定义一个集合对象,用ArrayList集合实现;
ArrayList<String> arrayList = new ArrayList<String>();
//在牌盘里装牌
/*
♦2,♦3,♦4,···♦A,
♣2,···
♠2,···
♥2,···
小王
大王
*/
//定义花色数组
String[] colors = {"♦","♣","♠","♥"};
//定义点数数组
String[] numbers = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
for (String color : colors){
for (String number : numbers){
arrayList.add(color+number);
}
}
arrayList.add("小王");
arrayList.add("大王");
//洗牌:即把牌打散,用Collections的shuffle()方法实现。
Collections.shuffle(arrayList);
System.out.println(arrayList);
//发牌:即遍历集合,给三个玩家发牌;
ArrayList<String> TomArray = new ArrayList<String>();
ArrayList<String> BobArray = new ArrayList<String>();
ArrayList<String> AmyArray = new ArrayList<String>();
ArrayList<String> dpArray = new ArrayList<String>();
for (int i=0; i<arrayList.size(); i++){
String poker = arrayList.get(i);
if (i >= arrayList.size()-3){
dpArray.add(poker);
}else if (i % 3 ==0){
TomArray.add(poker);
}else if (i % 3 ==1){
BobArray.add(poker);
}else if (i % 3 ==2){
AmyArray.add(poker);
}
}
//看牌:即三个玩家分别遍历自己的牌。
lookpoker("Tom",TomArray);
lookpoker("Bob",BobArray);
lookpoker("Amy",AmyArray);
lookpoker("dp",dpArray);
}
public static void lookpoker(String name,ArrayList<String> arrayList){
System.out.print(name + "的牌是:");
for (String poker : arrayList){
System.out.print(poker + " ");
}
System.out.println();
}
}
运行结果:
[♠6, ♠4, ♠3, ♣K, ♥K, ♥Q, 小王, ♠5, ♦5, ♣Q, ♦J, ♦10, ♦7, ♠7, ♦2, ♥8, ♣3, ♠8, ♦6, ♥J, ♣J, ♣9, ♦K, ♠10, ♥6, ♣A, ♥7, ♥2, ♠2, ♦A, ♣8, ♦8, ♥A, ♠9, ♦3, ♥5, ♣4, ♥4, ♥9, ♣7, ♠K, ♦Q, ♠Q, ♦4, ♥3, ♣5, ♥10, ♣2, ♠J, ♣10, ♠A, ♣6, ♦9, 大王]
Tom的牌是:♠6 ♣K 小王 ♣Q ♦7 ♥8 ♦6 ♣9 ♥6 ♥2 ♣8 ♠9 ♣4 ♣7 ♠Q ♣5 ♠J
Bob的牌是:♠4 ♥K ♠5 ♦J ♠7 ♣3 ♥J ♦K ♣A ♠2 ♦8 ♦3 ♥4 ♠K ♦4 ♥10 ♣10
Amy的牌是:♠3 ♥Q ♦5 ♦10 ♦2 ♠8 ♣J ♠10 ♥7 ♦A ♥A ♥5 ♥9 ♦Q ♥3 ♣2 ♠A
dp的牌是:♣6 ♦9 大王
- 案例:模拟斗地主升级版
需求:通过程序实现斗地主过程中的洗牌、发牌和看牌。
要求:对牌进行排序。
分析:
(1)创建HashMap,键是编号,值是牌;
(2)创建ArrayList,存储编号;
(3)创建花色数组和点数数组;
(4)从0开始往HashMap里面存储编号,并存储对应的牌,同时往ArrayList里面存储编号;
(5)洗牌(洗的是编号),用Collections的shuffle()方法实现;
(6)发牌(发的也是编号,为了保证编号是排序的,创建TreeSet集合接收);
(7)定义方法看牌(遍历TreeSet集合,获取编号,到HashMap集合找对应的牌);
(8)调用看牌方法。
完整代码:
package com.xuexi;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.TreeSet;
public class Demo4 {
public static void main(String[] args) {
//创建HashMap,键是编号,值是牌
HashMap<Integer,String> hm = new HashMap<Integer, String>();
//创建ArrayList,存储编号;
ArrayList<Integer> arrayList = new ArrayList<Integer>();
//创建花色数组和点数数组
String [] colors = {"♦","♣","♠","♥"};
String[] numbers = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
//从0开始往HashMap里面存储编号,并存储对应的牌,同时往ArrayList里面存储编号
int index = 0;
//先数字后花色,级数字排序在前
for (String number : numbers){
for (String color : colors){
hm.put(index,color+number);
arrayList.add(index);
index++;
}
}
hm.put(index,"小王");
arrayList.add(index);
index++;
hm.put(index,"大王");
arrayList.add(index);
//洗牌(洗的是编号),用Collections的shuffle()方法实现;
Collections.shuffle(arrayList);
//发牌(发的也是编号,为了保证编号是排序的,创建TreeSet集合接收)
TreeSet<Integer> TomSet = new TreeSet<Integer>();
TreeSet<Integer> BobSet = new TreeSet<Integer>();
TreeSet<Integer> AmySet = new TreeSet<Integer>();
TreeSet<Integer> dpSet = new TreeSet<Integer>();
for (int i=0; i<arrayList.size(); i++){
Integer x = arrayList.get(i);
if (i>=arrayList.size()-3){
dpSet.add(x);
}else if (i % 3==0){
TomSet.add(x);
}else if (i % 3 == 1){
BobSet.add(x);
}else if (i % 3 ==2){
AmySet.add(x);
}
}
//调用看牌方法
lookPoker("Tom",TomSet,hm);
lookPoker("Bob",BobSet,hm);
lookPoker("Amy",AmySet,hm);
lookPoker("dp",dpSet,hm);
}
//定义方法看牌(遍历TreeSet集合,获取编号,到HashMap集合找对应的牌)
public static void lookPoker(String name, TreeSet<Integer> ts, HashMap<Integer,String> hm){
System.out.print(name+"的牌是:");
for (Integer key :ts){
String poker = hm.get(key);
System.out.print(poker+" ");
}
System.out.println();
}
}
运行结果:
Tom的牌是:♠2 ♦6 ♣6 ♠6 ♣7 ♦8 ♥8 ♥9 ♠10 ♣J ♦Q ♥Q ♣K ♠K ♥K ♣A 小王
Bob的牌是:♦2 ♥2 ♦3 ♠3 ♥3 ♣4 ♥4 ♦5 ♣5 ♠5 ♥6 ♦7 ♠8 ♥10 ♦J ♠Q 大王
Amy的牌是:♣2 ♣3 ♠4 ♥5 ♠7 ♣8 ♦9 ♠9 ♦10 ♣10 ♠J ♥J ♣Q ♦K ♦A ♠A ♥A
dp的牌是:♦4 ♥7 ♣9