java集合实现斗地主发牌项目
package com.tjcu;import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class FightTheLandlord {public static void main(String[] args) {List<String> poker = new ArrayList<String>();poker.add("大王");poker.add("小王");String[] color = {"♤", "♡", "♣", "♢"};String[] font = {"A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3", "2"};
for (String c : color) {
for (String f : font) {poker.add(c + f);}}Collections.shuffle(poker);List<String> p1 = new ArrayList<>();List<String> p2 = new ArrayList<>();List<String> p3 = new ArrayList<>();for (int i = 0; i < poker.size() - 3; i++) {if (i % 3 == 0) {p1.add(poker.get(i));} else if (i % 3 == 1) {p2.add(poker.get(i));} else {p3.add(poker.get(i));}}Scanner scanner = new Scanner(System.in);System.out.println("抢地主的过程: ");System.out.println("1号玩家是否抢地主(0-3):");int p1Result = scanner.nextInt();if (p1Result == 3) {p1.add(poker.get(51));p1.add(poker.get(52));p1.add(poker.get(53));} else {System.out.println("2号玩家是否抢地主(0-3):");int p2Result = scanner.nextInt();if (p1Result == 3) {p2.add(poker.get(51));p2.add(poker.get(52));p2.add(poker.get(53));} else {System.out.println("2号玩家是否抢地主(0-3):");int p3Result = scanner.nextInt();if (p3Result == 3 && p3Result > p2Result && p3Result > p1Result) {p3.add(poker.get(51));p3.add(poker.get(52));p3.add(poker.get(53));} else if (p2Result > p1Result) {p2.add(poker.get(51));p2.add(poker.get(52));p2.add(poker.get(53));}}}System.out.println("1号玩家的牌: ");for (String s : p1) {System.out.print(s + ",");}
System.out.println();System.out.println("-------------------------");System.out.println("2号玩家的牌: ");for (String s : p2) {System.out.print(s + ",");}
System.out.println();System.out.println("---------------------------");System.out.println("3号玩家的牌: ");for (String s : p3) {System.out.print(s + ",");}}
}