java实现斗地主

java实现的小程序,涉及基础包括双列集合/单列集合的使用,简单for循环,从编码的效率看,python的代码量可以少很多。

下面说说小程序的步骤:

  • 1.组牌,花色加数字或字母,double for loop,单双列集合
  • 2.洗牌,用到shuffle
  • 3.抽牌,对3取模及留底牌
  • 4.看牌,map.get()
package com.study.demo;

import java.util.*;

public class Poker {
    public static void main(String[] args) {
        // 1.assemble 54 poker cards
        HashMap<Integer, String> pokerMap = new HashMap<Integer, String>();
        // 1.1 create 花色 & 数字
        ArrayList<String> colors = new ArrayList<String>();
        ArrayList<String> numbers = new ArrayList<String>();

        // 1.2 save
        Collections.addAll(colors, "方块", "梅花", "红桃", "黑桃");
        Collections.addAll(numbers, "2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3");

        // 存储编号变量
        int count = 1;
        pokerMap.put(count++, "大王");
        pokerMap.put(count++, "小王");
        // 1.4 create poker, save to map
        for (String number:numbers){
            for (String color:colors) {
                String card = color + number;
                pokerMap.put(count++, card);
            }
        }
        // 2.洗牌
        Set<Integer> numberSet = pokerMap.keySet();
        ArrayList<Integer> numberList = new ArrayList<Integer>();
        numberList.addAll(numberSet);

        // shuffle
        Collections.shuffle(numberList);

        // 3.发牌
        ArrayList<Integer> noP1 = new ArrayList<Integer>();
        ArrayList<Integer> noP2 = new ArrayList<Integer>();
        ArrayList<Integer> noP3 = new ArrayList<Integer>();
        ArrayList<Integer> bottom1 = new ArrayList<Integer>();

        // 3.1 number
        for (int i = 0; i < numberList.size(); i++) {
            Integer no = numberList.get(i);
            if (i >= 51) {
                bottom1.add(no);
            }else {
                if (i % 3 == 0){
                    noP1.add(no);
                }else if (i % 3 == 1){
                    noP2.add(no);
                }else {
                    noP3.add(no);
                }
            }
        }

        // 4. show cards
        Collections.sort(noP1);
        Collections.sort(noP2);
        Collections.sort(noP3);
        Collections.sort(bottom1);

        // cards transfer
        ArrayList<String> player1 = new ArrayList<String>();
        ArrayList<String> player2 = new ArrayList<String>();
        ArrayList<String> player3 = new ArrayList<String>();
        ArrayList<String> bottom = new ArrayList<String>();
        // double for loop也可以实现
        for (Integer i:noP1){
            String card = pokerMap.get(i);
            player1.add(card);
        }
        for (Integer i:noP2){
            String card = pokerMap.get(i);
            player2.add(card);
        }
        for (Integer i:noP3){
            String card = pokerMap.get(i);
            player3.add(card);
        }
        for (Integer i:bottom1){
            String card = pokerMap.get(i);
            bottom.add(card);
        }

        // show cards
        System.out.println("令狐冲:" + player1);
        System.out.println("东方不败:" + player2);
        System.out.println("乔峰:" + player3);
        System.out.println("底牌:" + bottom);
    }
}

posted on 2021-04-20 22:25  进击的davis  阅读(314)  评论(0)    收藏  举报

导航