编写一个斗地主程序
Java编写一个斗地主,展示不同手牌
Map集合练习:
模拟斗地主:
创建Map集合存储54牌
创建3个玩家和一个底牌的集合
map集合排序随机,通过业务判断,给4个集合进行发牌
打印输出
代码
package com;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class DouDiZhu {
public static void main(String[] args) {
Map<Integer, String> poker = new HashMap<>();
List<Integer> pokerIndex = new ArrayList<>();
int index = 0;
poker.put(index, "♚");
pokerIndex.add(index);
index++;
poker.put(index, "♔");
pokerIndex.add(index);
index++;
String[] colors = {"♠","♥","♣","♦"};
String[] nums = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
for (String c : colors) {
for (String n : nums) {
poker.put(index, c+n);
pokerIndex.add(index);
index++;
}
}
Collections.shuffle(pokerIndex);
List<Integer> player1 = new ArrayList<>();
List<Integer> player2 = new ArrayList<>();
List<Integer> player3 = new ArrayList<>();
List<Integer> dipai = new ArrayList<>();
for (int i = 0; i < pokerIndex.size(); i++) {
Integer paiIndex = pokerIndex.get(i);
if(i>=51){
dipai.add(paiIndex);
}else if(i%3==0){
player1.add(paiIndex);
}else if(i%3==1){
player2.add(paiIndex);
}else if(i%3==2){
player3.add(paiIndex);
}
}
//排序
Collections.sort(player1);
Collections.sort(player2);
Collections.sort(player3);
//看牌
look("底牌",dipai,poker);
look("大乔",player1,poker);
look("诸葛亮",player2,poker);
look("小乔",player3,poker);
}
public static void look(String name,List<Integer> list,Map<Integer, String> map) {
System.out.println(name);
for (Integer i : list) {
String value = map.get(i);
System.out.print(value+" ");
}
System.out.println();
}
}