手记

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

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<Integer, String> poker = new HashMap<Integer, String>();// 放扑克
    static ArrayList<Integer> xuhao = new ArrayList<Integer>();// 放排序
    static HashMap<Integer, String> player = new HashMap<Integer, String>();
    static String[] dianshu = { "2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3" };
    static String[] huase = { "", "", "", "" };
    static ArrayList<Integer> player1 = new ArrayList<Integer>();//玩家1的牌
    static ArrayList<Integer> player2 = new ArrayList<Integer>();//玩家2的牌
    static ArrayList<Integer> number = new ArrayList<Integer>();//存放玩家序号

    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 < 4; o++) {
            if (o % 2 == 0) {
                player1.add(xuhao.get(o));
                System.out.println("---玩家:" + player.get(number.get(0)) + "-拿牌");
            } else if (o % 2 == 1) {
                player2.add(xuhao.get(o));
                System.out.println("---玩家:" + player.get(number.get(1)) + "-拿牌");
            }
        }
        System.out.println("---------发牌结束!---------");
    }

    public static void start() {
        System.out.println("---------开始游戏...---------");
        Collections.sort(player1);
        Collections.sort(player2);
        System.out.println(player.get(number.get(0)) + "最大的手牌为:" + poker.get(player1.get(0)));
        System.out.println(player.get(number.get(1)) + "最大的手牌为:" + poker.get(player2.get(0)));
        if (player1.get(0) > player2.get(0)) {
            System.out.println("---------玩家:" + player.get(2) + "获胜!---------");
        } else {
            System.out.println("---------玩家:" + player.get(1) + "获胜!---------");
        }

    }

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

}

运行效果图

2人推荐
随时随地看视频
慕课网APP