欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

斗地主模拟Java

程序员文章站 2022-07-10 18:38:13
...

斗地主模拟实现 洗牌–发牌
下面展示一些 内联代码片

import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
import java.util.HashMap;
import java.util.TreeSet;
public static void main(String[] args){
        HashMap<Integer,String> hm = new HashMap<Integer,String>();
        ArrayList<Integer> array = new ArrayList<Integer>();

        String colors[] = {"方块","黑桃","红心","梅花"};
        String numbers[] = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};

        int idx=0;
        /*for(String color : colors){
            for(String number : numbers){
                hm.put(idx,color+number);
                array.add(idx);
                idx++;
            }
        }*/
        for(String number : numbers){
            for(String color : colors){
                hm.put(idx,color+number);
                array.add(idx);
                idx++;
            }
        }
        array.add(idx);
        hm.put(idx,"小王");
        idx++;
        array.add(idx);
        hm.put(idx,"大王");
        Collections.shuffle(array);
        TreeSet<Integer> P1 = new TreeSet<Integer>();
        TreeSet<Integer> P2 = new TreeSet<Integer>();
        TreeSet<Integer> P3 = new TreeSet<Integer>();
        TreeSet<Integer> P4 = new TreeSet<Integer>();
        int num = 1;
        for(int i : array){
            if(num<=17){
                P1.add(i);
            }else if(num<=34){
                P2.add(i);
            }else if(num<=51){
                P3.add(i);
            }else{
                P4.add(i);
            }
            num++;
        }

        Random r = new Random();
        int num2 = r.nextInt(3);
        switch(num2){
            case 0:for(int i : P4){
                P1.add(i);
            }
                /*for(int i : P1){
                    System.out.println(i);
                }*/
                System.out.println("地主 (P1)玩家1:");
                int j = 1;
                for(Integer i : P1){
                    System.out.print(hm.get(i)+"\t");
                    if(j%5==0) System.out.println();
                    j++;
                }
                System.out.println();
                System.out.println("(P2)玩家2:");
                j=1;
                for(Integer i : P2){
                    System.out.print(hm.get(i)+"\t");
                    if(j%5==0) System.out.println();
                    j++;
                }
                System.out.println();
                System.out.println("(P3)玩家3:");
                j=1;
                for(Integer i : P3){
                    System.out.print(hm.get(i)+"\t");
                    if(j%5==0) System.out.println();
                    j++;
                }
                System.out.println();
                System.out.print("地主是: ");
                for(Integer i : P4){
                    System.out.print(hm.get(i)+"\t");
                }
                break;
            case 1:for(int i : P4){
                P2.add(i);
            }
                System.out.println("(P1)玩家1:");
                j=1;
                for(Integer i : P1){
                    System.out.print(hm.get(i)+"\t");
                    if(j%5==0) System.out.println();
                    j++;
                }
                System.out.println();
                System.out.println("地主 (P2)玩家2:");
                j=1;
                for(Integer i : P2){
                    System.out.print(hm.get(i)+"\t");
                    if(j%5==0) System.out.println();
                    j++;
                }
                System.out.println();
                System.out.println("(P3)玩家3:");
                j=1;
                for(Integer i : P3){
                    System.out.print(hm.get(i)+"\t");
                    if(j%5==0) System.out.println();
                    j++;
                }
                System.out.println();
                System.out.print("地主是: ");
                for(Integer i : P4){
                    System.out.print(hm.get(i)+"\t");
                }
                break;
            case 2:for(int i : P4){
                P3.add(i);
            }
                System.out.println("(P1)玩家1:");
                j=1;
                for(Integer i : P1){
                    System.out.print(hm.get(i)+"\t");
                    if(j%5==0) System.out.println();
                    j++;
                }
                System.out.println();
                System.out.println("(P2)玩家2:");
                j=1;
                for(Integer i : P2){
                    System.out.print(hm.get(i)+"\t");
                    if(j%5==0) System.out.println();
                    j++;
                }
                System.out.println();
                System.out.println("地主 (P3)玩家3:");
                j=1;
                for(Integer i : P3){
                    System.out.print(hm.get(i)+"\t");
                    if(j%5==0) System.out.println();
                    j++;
                }
                System.out.println();
                System.out.print("地主是: ");
                for(Integer i : P4){
                    System.out.print(hm.get(i)+"\t");
                }
                break;
        }
    }
相关标签: Java代码实现