简单斗地主纸牌游戏的编码
该代码只是在服务端显示的棋牌代码
package cn.com.play.demo;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
public class PlayDiZhu {
public static void main(String[] args) {
//创建Map集合 键是编号 值是牌
HashMap<Integer, String> pooker = new HashMap<Integer,String>();
//创建ArrayList集合 储存编号
ArrayList<Integer> pookerNumber = new ArrayList<Integer>();
//定义13个点数的数组
String[] numbers={"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
//定义 梅花 方块 红桃 黑桃 并用 数组保存
String[] colors={"♠","♥","♣","♦"};
//定义整数变量,作为键的出现
int index=2;
//遍历数组,花色+点数的组合 , 存储到Map集合
for(String number: numbers){
for(String color:colors){
pooker.put(index, number+color);
pookerNumber.add(index);
index++;
}
}
//存储大王,和小王
pooker.put(0, "大王");
pookerNumber.add(0);
pooker.put(1, "小王");
pookerNumber.add(1);
//洗牌, 将牌的编号打乱
Collections.shuffle(pookerNumber);
//发牌功能, 将牌编号发给玩家集合, 底牌集合
ArrayList<Integer> player1 = new ArrayList<Integer>();
ArrayList<Integer> player2 = new ArrayList<Integer>();
ArrayList<Integer> player3 = new ArrayList<Integer>();
ArrayList<Integer> bottom = new ArrayList<Integer>();
//发牌采用的是集合索引 %3
for(int i=0;i<pookerNumber.size();i++){
//先将底牌做好
if(i<3){
//存底牌集合中
bottom.add(pookerNumber.get(i));
}else if(i%3==0){
player1.add(pookerNumber.get(i));
}else if(i%3==1){
player2.add(pookerNumber.get(i));
}else if(i%3==2){
player3.add(pookerNumber.get(i));
}
}
//对玩家手中的编号排序
Collections.sort(player1);
Collections.sort(player2);
Collections.sort(player3);
//看牌, 将玩家手中的编号,到Map集合中查找,根据键找值
look("zhangyi1",player1,pooker);
look("zhangyi2",player2,pooker);
look("zhangyi3",player3,pooker);
look("底牌",bottom,pooker);
}
public static void look(String name,ArrayList<Integer> player,HashMap<Integer,String> pooker){
//遍历ArrayList集合,获得元素,作为键,到集合Map中找值
System.out.print(name+" ");
for(Integer key:player){
String value = pooker.get(key);
System.out.print(value+" ");
}
System.out.println();
}
}
//输出结果为
/*
zhangyi1 小王 2♥ 2♣ 3♠ 4♣ 5♠ 6♠ 6♦ 8♥ 9♠ 9♦ 9♣ 10♦ J♠ Q♣ K♠ K♦
zhangyi2 大王 A♦ A♣ 2♦ 4♠ 4♥ 4♦ 5♦ 5♣ 7♦ 8♦ 10♠ J♥ J♣ Q♥ K♥ K♣
zhangyi3 A♥ 3♥ 3♦ 3♣ 5♥ 6♥ 6♣ 7♠ 7♥ 8♠ 8♣ 9♥ 10♥ 10♣ J♦ Q♠ Q♦
底牌 A♠ 2♠ 7♣
*/