【Java】斗地主和斗牛游戏

x33g5p2x  于2021-11-22 转载在 Java  
字(7.2k)|赞(0)|评价(0)|浏览(381)

一、前言

练一个斗地主小游戏,只能发看牌

1.斗地主: 

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class PokerPlay2 {
    public static void main(String[] args) {
        ArrayList<String> array=new ArrayList<String>();
        String []colors={"♣","♥","♠","♦"};
        String[] numbers = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};
        for (String color:colors){
            for (String number:numbers){
                array.add(color+number);
            }
        }int count=1;
        array.add("大🃏");
        array.add("小🃏");
        while(true){
            System.out.println("第"+count+++"局");
        Collections.shuffle(array);
        ArrayList<String> poker1=new ArrayList<String>();
        ArrayList<String> poker2=new ArrayList<String>();
        ArrayList<String> poker3=new ArrayList<String>();
        ArrayList<String> poker4=new ArrayList<String>();
        for (int i=0;i<array.size();i++){
            String poker=array.get(i);
            if (i>=array.size()-3){
                poker4.add(poker);
            }else if(i%3==0){
                poker1.add(poker);
            }else if(i%3==1){
                poker2.add(poker);
            }else if(i%3==2){
                poker3.add(poker);
            }
        }
            Scanner sc=new Scanner(System.in);
        String name1=sc.nextLine();
        lookPoker("1", poker1);
        lookPoker("2", poker2);
        lookPoker("3", poker3);
        lookPoker("底牌", poker4);

    }}
    public static void lookPoker(String name,ArrayList<String> arrayList){
        System.out.print(name+"的牌:");
        for (String s:arrayList){
            System.out.print(" "+s);
        }
        System.out.println();

    }
}

1.1运行结果: 

2.斗地主升级版

增加了对牌的排序和地主牌的加入,后续可能会增加玩牌的功能

2.1原理:

斗地主升级版的原理就是,创建HashMap用来后续键找值,然后创建ArrayList

集合(创建其他集合也是可以的)ArrayList集合的作用是用来存储和HashMap

对应的键值,通过两个for进行组合每产生一个就将键值加一,产生不同的键值,

所对应的牌是唯一,  这样为后续排列打下了坚实的前提,然后再创建TreeSet集

合(TreeSet集合可以进行自然排序),然后将ArrayList集合中的元素(其实就是

一些数子,这些数字是HashMap中的键值),分配到三个人上,里面的已经被

TreeSet排序完成,调用方法是通过HashMap将每个人的键值来得到对应的牌。

import java.util.*;
public class PokerPuls {
    public static void main(String[] args) {
        //定义HashMap集合
        HashMap<Integer,String> hm=new HashMap<Integer, String>();
        //定义ArrayList集合用来存储编号
        ArrayList<Integer> array=new ArrayList<Integer>();
        //定义花色和底数数组
        String []colors={"♣","♥","♠","♦"};
        String[] numbers = { "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A","2"};
        int index=0;
        //注意需要将数字for在外头,否则是按花色排序的
        for (String number:numbers){
            for (String color:colors){
                hm.put(index, color+number);
                array.add(index);
                index++;
            }
        }
        hm.put(index, "小🃏");
        array.add(index);
        index++;
        hm.put(index, "大🃏");
        array.add(index);
        int count=1;
       while(true){
           System.out.println("**********第"+count+++"局斗地主**********");
        //洗牌
        Collections.shuffle(array);
        //创建四个牌位分别用来存储三个玩家和三张底牌,因要排序所以用TreeSet集合
        TreeSet<Integer> play1=new TreeSet<Integer>();
        TreeSet<Integer> play2=new TreeSet<Integer>();
        TreeSet<Integer> play3=new TreeSet<Integer>();
        TreeSet<Integer> play4=new TreeSet<Integer>();
        for (int i=0;i<array.size();i++){
            Integer index1 = array.get(i);
            if(i>=array.size()-3){
                play4.add(index1);
            }else if (i%3==0){
                play1.add(index1);
            }else if (i%3==1){
                play2.add(index1);
            }else if (i%3==2){
                    play3.add(index1);
            }
        }
Scanner sc=new Scanner(System.in);
    System.out.print("第一位玩家:");
    String name1=sc.nextLine();
    System.out.print("第二位玩家:");
    String name2=sc.nextLine();
    System.out.print("第三位玩家:");
    String name3=sc.nextLine();
    lookerPoker("1号:"+name1, play1,hm);
    lookerPoker("2号:"+name2, play2,hm);
    lookerPoker("3号:"+name3, play3,hm );
        lookerPoker("底牌", play4,hm);
    int i=0; int num=0;
    while(true){
    System.out.print("几号是地主:");
  num=sc.nextInt();
   switch(num){
       case 1:{i++;
           play1.addAll(play4);break;
       }
       case 2:{i++;
           play2.addAll(play4);break;
       }
       case 3:{i++;
          play3.addAll(play4);break;
       }
       default:{
           System.out.println("输入有误,重新输入");break;
       }
   }
        lookerPoker("1号:"+name1, play1,hm);
        lookerPoker("2号:"+name2, play2,hm);
        lookerPoker("3号:"+name3, play3,hm );
        if (i!=0){
            break;
        }
        System.out.println("-------游戏结束-------");
    } } }
    //定义遍历方法,并通过存储的index1的键来获取对应的值
    public static void lookerPoker(String name,TreeSet<Integer>  ts,HashMap<Integer,String>  hm){
        System.out.print(name+"的牌:");
        for (Integer key:ts){
            String value=hm.get(key);
            System.out.print(value+" ");
        }
        System.out.println();
    }

2.2运行结果:

3.斗牛游戏:

import java.util.*;

public class PokerPlay {
    public static void main(String[] args) {
        //定义一个ArrayList集合用来存放排盒
        //分别用来给玩家和庄家的两副牌
        ArrayList<String> array = new ArrayList<String>();
        ArrayList<String> array1 = new ArrayList<String>();
        //定义花色、点数数组
        String[] color = {"♣", "♦", "♠", "♥"};
        String[] numbers = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};
        //封装组合成牌放入集合中
        for (String s1 : color) {
            for (String s2 : numbers) {
                array.add(s1 + s2);
            }
        }

        //定义庄家的牌盒
        String[] color1 = {"♣", "♦", "♠", "♥"};
        String[] number1 = {"10", "J", "Q", "K",};
        for (String s1 : color1) {
            for (String s2 : number1) {
                array1.add(s1 + s2);
            }
        }
        int count = 1;//定义局数
        while (true) {
            int sum1 = 0, sum2 = 0, sum3 = 0;  int sum4 = 0, sum5 = 0;
            //打乱牌的顺序
            Collections.shuffle(array);
            Collections.shuffle(array1);
            //定义五个牌位
            ArrayList<String> poker1 = new ArrayList<String>();
            ArrayList<String> poker2 = new ArrayList<String>();
            ArrayList<String> poker3 = new ArrayList<String>();
            ArrayList<String> poker4 = new ArrayList<String>();
            ArrayList<String> poker5 = new ArrayList<String>();
           // 为了防止玩家崩溃,随机数给庄家无敌牌或普通牌
            Random r=new Random();
            int 换位=r.nextInt(100);
            //给庄家的无敌牌
            for (int i=0;i<array1.size();i++){
                String s1 = array1.get(i);
                if(sum5<5&&换位<50){
                    poker5.add(s1);
                    sum5++;
                }
            }
            for (int i = 0; i < array.size(); i++) {
                String poker = array.get(i);//得到每张牌
                if (sum1++ < 5) {
                    poker1.add(poker);
                } else if (sum2++ < 5) {
                    poker2.add(poker);
                } else if (sum3++ < 5) {
                    poker3.add(poker);
                } else if (sum4++< 5) {
                    poker4.add(poker);
                }//使庄家的牌正常,可以设置多少一个轮回
                 else if (sum5++<5) {
                    poker5.add(poker);
                }

            }
                    System.out.println("**************第" + (count++) + "局斗牛游戏开始:**************");
                    Scanner sc = new Scanner(System.in);
                    System.out.print("庄家:");
                    String play5 = sc.nextLine();
                    System.out.print("第一位玩家:");
                    String play1 = sc.nextLine();
                    System.out.print("第二位玩家:");
                    String play2 = sc.nextLine();
                    System.out.print("第三位玩家:");
                    String play3 = sc.nextLine();
                    System.out.print("第四位玩家:");
                    String play4 = sc.nextLine();
            System.out.println("-------买定离手--------");
                    lookPoker("庄家"+play5, poker5);
                    lookPoker("玩家"+play1, poker1);
                    lookPoker("玩家"+play2, poker2);
                    lookPoker("玩家"+play3, poker3);
                    lookPoker("玩家"+play4, poker4);
            System.out.println("-------游戏结束--------");
        }
    }
    //定义一个看牌的动作
    public static void lookPoker(String name, ArrayList<String > arrayList){
        //遍历牌
        System.out.print(name + "的牌:");
        for (String poker : arrayList) {
            System.out.print(" " + poker);
        }
        System.out.println();
    }}

其中 在用random,用if是控制庄家的牌

Random r=new Random();
            int 换位=r.nextInt(100);
            //给庄家的无敌牌
            for (int i=0;i<array1.size();i++){
                String s1 = array1.get(i);
                if(sum5<5&&换位<50){
                    poker5.add(s1);
                    sum5++;
                }

3.1运行结果:

相关文章