ITPub博客

首页 > IT职业 > IT生活 > JAVA入门第三季比大小扑克游戏手记

JAVA入门第三季比大小扑克游戏手记

IT生活 作者:54xx 时间:2021-09-09 09:05:26 0 删除 编辑
package pokergame;

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

public class bidaxiao {
    static boolean flag = true;
    static HashMap poker = new HashMap();// 放扑克
    static ArrayList xuhao = new ArrayList();// 放排序
    static HashMap player = new HashMap();
    static String[] dianshu = { "2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3" };
    static String[] huase = { "", "", "", "" };
    static ArrayList player1 = new ArrayList();//玩家1的牌
    static ArrayList player2 = new ArrayList();//玩家2的牌
    static ArrayList number = new ArrayList();//存放玩家序号

    public static void main(String[] args) {
        int index = 0;
        for (String ds : dianshu) {
            for (String hs : huase) {
                poker.put(index, hs + ds);
                xuhao.add(index);
                index++;
            }
        }//做牌
        Collections.shuffle(xuhao);//打乱牌序
        System.out.println("------------创建玩家------------");
        startgame();//进入游戏
        fapai();//开始发牌
        start();//开始游戏比大小
        lookpoker(player.get(number.get(0)), player1, poker);//看牌
        lookpoker(player.get(number.get(1)), player2, poker);//看牌
    }

    public static void startgame() {

        int i = 1;
        while (flag) {
            try {
                Scanner input = new Scanner(System.in);
                System.out.println("请输入第" + i + "位玩家的ID和姓名:");
                System.out.println("输入ID:");
                int a = input.nextInt();
                number.add(a);
                System.out.println("输入姓名:");
                String name = input.next();
                player.put(a, name);
                i++;
            } catch (InputMismatchException e) {
                System.out.println("请输入整数类型的ID!");
            }
            if (i == 3) {
                flag = false;
            }
        }
        for (Integer Key : player.keySet()) {
            System.out.println("----欢迎玩家:" + player.get(Key));
        }
    }

    public static void fapai() {
        System.out.println("---------开始发牌...---------");
        for (int o = 0; o  player2.get(0)) {
            System.out.println("---------玩家:" + player.get(2) + "获胜!---------");
        } else {
            System.out.println("---------玩家:" + player.get(1) + "获胜!---------");
        }

    }

    public static void lookpoker(String name, ArrayList playernum, HashMap poker) {
        System.out.println("玩家各自的手牌为:   ");
        System.out.print(name + ":");
        for (Integer key : playernum) {
            String value = poker.get(key);
            System.out.print(value + " ");
        }
        System.out.println();
    }

}

运行效果图
图片描述

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/1976/viewspace-2807993/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-04-16

  • 博文量
    226
  • 访问量
    1612