欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

模拟斗地主

时间:2023-08-11

模拟斗地主

import java.util.ArrayList;import java.util.Collections;import java.util.Comparator;public class Main {public static void main(String[] args) {ArrayList list = new ArrayList();String [] s1= {"♦","♣","♥","♠"};String [] s2= {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};for (String hua : s1) {for(String shu :s2 ){String z=hua+shu;list.add(z);}}list.add("大王");list.add("小王");Collections.shuffle(list);ArrayList< String > men1=new ArrayList<>(); //玩家一ArrayList< String > men2=new ArrayList<>(); //玩家二ArrayList< String > men3=new ArrayList<>(); //玩家三ArrayList< String > dipai=new ArrayList<>(); //底牌for(int i=0;i

斗地主进阶 ~将玩家手里的牌排序显示:
思路:将牌用编号1~54表示,由HashMap 存编号和对应的牌。将编号排序,再输出。

import java.util.ArrayList;import java.util.Collections;import java.util.Comparator;import java.util.HashMap;public class Main {public static void main(String[] args) {HashMap hashMap = new HashMap<>(); // 存整副牌String[] s1 = { "♦", "♣", "♥", "♠" }; //花色String[] s2 = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" }; //数字ArrayList list = new ArrayList<>(); // 存编号int count = 1;for (String shu : s2) {for (String hua : s1) {String z = hua + shu;hashMap.put(count, z);list.add(count);count++;}}hashMap.put(53, "小王");list.add(53);hashMap.put(54, "大王");list.add(54);Collections.shuffle(list); // 随机打乱ArrayList men1 = new ArrayList(); // 玩家一ArrayList men2 = new ArrayList(); // 玩家二ArrayList men3 = new ArrayList(); // 玩家三ArrayList dipai = new ArrayList(); // 底牌//分牌for (int i = 0; i < list.size(); i++) {if (i < 3) {dipai.add(list.get(i));} else if (i < 20) {men1.add(list.get(i));} else if (i < 37) {men2.add(list.get(i));} else {men3.add(list.get(i));}}// 将编号从小到大排序Comparator comparator = new Comparator() {@Overridepublic int compare(Integer o1, Integer o2) {// TODO 自动生成的方法存根return o1 - o2;}};men1.sort(comparator);men2.sort(comparator);men3.sort(comparator);dipai.sort(comparator);System.out.print("玩家一:");for (Integer one : men1) {System.out.print(hashMap.get(one) + " ");}System.out.print("n玩家二:");for (Integer tow : men2) {System.out.print(hashMap.get(tow) + " ");}System.out.print("n玩家三:");for (Integer three : men3) {System.out.print(hashMap.get(three) + " ");}System.out.print("n底牌 :");for (Integer d : dipai) {System.out.print(hashMap.get(d) + " ");}}}

编译结果:

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。