简单的Java代码实现斗地主
斗地主综合分析:
1.准备牌:54张牌存储到一个集合中特殊牌:大王,小王其他52张牌:定义一个数组/集合,存储4种花色;定义一个数组/集合,存储13个序号循环嵌套遍历两个数组/集合,组装52张牌2.洗牌
使用集合工具Collections的方法
static void shuffle(List<?> list)
随机打乱集合中元素的顺序3.发牌1人17张牌为底牌,轮流发牌,集合的索引(0-53)%3定义4个集合,存储3个玩家的牌和底牌4.看牌直接打印集合,遍历存储玩家和底牌的集合
代码
import java.util.ArrayList;
import java.util.Collections;public class DouDiZhu {public static void main(String[] args) {ArrayList<String> poker = new ArrayList<>();String[] colors = {"♥","♠","♦","♣"};String[] numbers ={"A","J","Q","K","2","3","4","5","6","7","8","9","10"};poker.add("大王");poker.add("小王");for (String color : colors) {for (String number : numbers) {poker.add(color+number);}}Collections.shuffle(poker);ArrayList<String> player01 = new ArrayList<>();ArrayList<String> player02 = new ArrayList<>();ArrayList<String> player03 = new ArrayList<>();ArrayList<String> dipai = new ArrayList<>();for (int i = 0;i < poker.size();i++) {String p = poker.get(i);if(i >=51) {dipai.add(p);}else if(i % 3 == 0) {player01.add(p);}else if(i % 3 == 1) {player02.add(p);}else if(i % 3 == 2) {player03.add(p);}}System.out.println("张曼玉" + player01);System.out.println("王祖贤" + player02);System.out.println("林青霞" + player03);System.out.println("底牌" + dipai);}
}