按照斗地主的规则,完成洗牌发牌看牌的动作。最终结果预计为:
具体规则:
1.组装54张扑克牌
2.将54张扑克牌顺序打乱
3.三个玩家参与游戏,三人交替摸牌,每人17张,最后三张做底牌
4.查看三人手中各自的牌(按牌的大小排序)、底牌
实现代码步骤:
1.首先,要修改java文件编码,由GBK修改为UTF-8,因为默认的字符编码GBK没有梅花、方片、黑桃、红桃(♠♥♦♣)等特殊字符。
2.使用双列Map(HashMap)集合,完成一个数字与字符串纸牌的对应关系(相当于一个字典)。
//准备花色
ArrayList<String> color = new ArrayList<String>();
color.add("♠");
color.add("♥");
color.add("♦");
color.add("♣");
//准备数字
ArrayList<String> number = new ArrayList<String>();
Collections.addAll(number,"3","4","5","6","7","8","9","10","J","Q","K","A","2");
//定义一个map集合:用来将数字与每一张牌进行对应
HashMap<Integer, String> map = new HashMap<Integer, String>();
int index = 0;
for (String thisNumber : number) {
for (String thisColor : color) {
map.put(index++, thisColor+thisNumber);
}
}
//加入大小王
map.put(index++, "小王");
map.put(index++, "大王");
3.洗牌,通过数字完成洗牌发牌
//一副54张的牌 ArrayList里边为0-53的数的新牌
ArrayList<Integer> cards = new ArrayList<Integer>();
for (int i = 0; i <= 53; i++) {
cards.add(i);
}
//洗牌
Collections.shuffle(cards);
4.发牌,将每个人以及底牌设计为ArrayList<String>,将最后三张牌直接存放于底牌,剩余牌通过对3取模依次发牌。存放的过程要求数字大小与斗地主规则大小对应。
//创建三个玩家和底牌
ArrayList<Integer> iPlayer = new ArrayList<Integer>();
ArrayList<Integer> iPlayer2 = new ArrayList<Integer>();
ArrayList<Integer> iPlayer3 = new ArrayList<Integer>();
ArrayList<Integer> itCards = new ArrayList<Integer>();
//遍历这副洗好的牌,遍历过程中,将牌发到三个玩家和底牌中
for (int i = 0; i < cards.size(); i++) {
if(i>=51) {
iCards.add(cards.get(i));
} else {
if(i%3==0) {
iPlayer.add(cards.get(i));
}else if(i%3==1) {
iPlayer2.add(cards.get(i));
}else {
iPlayer3.add(cards.get(i));
}
}
}
5.看牌,通过Map集合找到对应的字符展示,通过查询纸牌与数字对应关系,由数字转成纸牌字符串再进行展示。
//对每个人手中的牌排序
Collections.sort(iPlayer);
Collections.sort(iPlayer2);
Collections.sort(iPlayer3);
//对应数字形式的每个人手中的牌,定义字符串形式的牌
ArrayList<String> sPlayer = new ArrayList<String>();
ArrayList<String> sPlayer2 = new ArrayList<String>();
ArrayList<String> sPlayer3 = new ArrayList<String>();
ArrayList<String> sCards = new ArrayList<String>();
for (Integer key : iPlayer) {
sPlayer.add(map.get(key));
}
for (Integer key : iPlayer2) {
sPlayer2.add(map.get(key));
}
for (Integer key : iPlayer3) {
sPlayer3.add(map.get(key));
}
for (Integer key : iCards) {
sCards.add(map.get(key));
}
//看牌
System.out.println(sPlayer);
System.out.println(sPlayer2);
System.out.println(sPlayer3);
System.out.println(sCards);
}
}
完整代码为:
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
/*
* 斗地主洗牌发牌排序
*/
public class Poker {
public static void main(String[] args) {
//准备花色
ArrayList<String> color = new ArrayList<String>();
color.add("♠");
color.add("♥");
color.add("♦");
color.add("♣");
//准备数字
ArrayList<String> number = new ArrayList<String>();
Collections.addAll(number,"3","4","5","6","7","8","9","10","J","Q","K","A","2");
//定义一个map集合:用来将数字与每一张牌进行对应
HashMap<Integer, String> map = new HashMap<Integer, String>();
int index = 0;
for (String thisNumber : number) {
for (String thisColor : color) {
map.put(index++, thisColor+thisNumber);
}
}
//加入大小王
map.put(index++, "小王");
map.put(index++, "大王");
//一副54张的牌 ArrayList里边为0-53的数的新牌
ArrayList<Integer> cards = new ArrayList<Integer>();
for (int i = 0; i <= 53; i++) {
cards.add(i);
}
//洗牌
Collections.shuffle(cards);
//创建三个玩家和底牌
ArrayList<Integer> iPlayer = new ArrayList<Integer>();
ArrayList<Integer> iPlayer2 = new ArrayList<Integer>();
ArrayList<Integer> iPlayer3 = new ArrayList<Integer>();
ArrayList<Integer> itCards = new ArrayList<Integer>();
//遍历这副洗好的牌,遍历过程中,将牌发到三个玩家和底牌中
for (int i = 0; i < cards.size(); i++) {
if(i>=51) {
itCards.add(cards.get(i));
} else {
if(i%3==0) {
iPlayer.add(cards.get(i));
}else if(i%3==1) {
iPlayer2.add(cards.get(i));
}else {
iPlayer3.add(cards.get(i));
}
}
}
//对每个人手中的牌排序
Collections.sort(iPlayer);
Collections.sort(iPlayer2);
Collections.sort(iPlayer3);
//对应数字形式的每个人手中的牌,定义字符串形式的牌
ArrayList<String> sPlayer = new ArrayList<String>();
ArrayList<String> sPlayer2 = new ArrayList<String>();
ArrayList<String> sPlayer3 = new ArrayList<String>();
ArrayList<String> sCards = new ArrayList<String>();
for (Integer key : iPlayer) {
sPlayer.add(map.get(key));
}
for (Integer key : iPlayer2) {
sPlayer2.add(map.get(key));
}
for (Integer key : iPlayer3) {
sPlayer3.add(map.get(key));
}
for (Integer key : itCards) {
sCards.add(map.get(key));
}
//看牌
System.out.println("玩家一"+sPlayer);
System.out.println("玩家二"+sPlayer2);
System.out.println("玩家三"+sPlayer3);
System.out.println("底牌"+sCards);
}
}
运行结果为: