Java Debug追踪,斗地主案例的简单实现

1、Debug追踪

Debug调试程序:
可以让代码逐行执行,查看代码执行的过程,调试程序中出现的bug。
使用方式:
在行号的右边,鼠标左键单击,添加断点(初学可以添加到每个方法的第一行,熟练之后哪里有bug添加到哪里)
右键,选择Debug执行程序,程序就会停留在添加的第一个断点处
执行程序:
F8:逐行执行程序
F7:进入到方法中
shift + F8:跳出方法
F9:跳到下一个断点,如果没有下一个断点就结束程序
Ctrl + F2:退出Debug模式
Console:切换到控制台

2、斗地主案例的简单实现

/*
    斗地主综合案例
    1、准备牌
    2、洗牌
    3、发牌
    4、排序
    5、看牌
 */
import java.util.*;

public class DouDiZhu {
    
    
    public static void main(String[] args) {
    
    
        //1、准备牌
        //创建一个Map集合,存储牌的索引和组装好的牌
        HashMap<Integer, String> poker = new HashMap<>();
        //创建list集合存储牌的索引
        ArrayList<Integer> pokerIndex = new ArrayList<>();
        //定义两个集合存储花色和牌的序号
        List<String> colors = new ArrayList<>();
        colors.add("♥");
        colors.add("♠");
        colors.add("♣");
        colors.add("◆");
        List<String> numbers = Arrays.asList("2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3");
        //把大王和小王存储到集合中
        //定义一个牌的索引
        int index = 0;
        poker.put(index,"大王");
        pokerIndex.add(index);
        index++;
        poker.put(index,"小王");
        pokerIndex.add(index);
        index++;
        //循环嵌套遍历两个集合组装牌
        for (String number : numbers) {
    
    
            for (String color : colors) {
    
    
                poker.put(index, color + number);
                pokerIndex.add(index);
                index++;
            }
        }
        //System.out.println(poker);
        //System.out.println(pokerIndex);

        /*
        2、洗牌
        使用Collections中的方法shuffle(list)
         */
        Collections.shuffle(pokerIndex);
        //System.out.println(pokerIndex);

        /*
        3、发牌
        定义四个集合存储玩家牌的索引和底牌的索引
         */
        ArrayList<Integer> player01 = new ArrayList<>();
        ArrayList<Integer> player02 = new ArrayList<>();
        ArrayList<Integer> player03 = new ArrayList<>();
        ArrayList<Integer> Rest = new ArrayList<>();
        //遍历存储牌索引的list集合,获取每一个牌的索引
        for (int i = 0; i < pokerIndex.size(); i++) {
    
    
            Integer in = pokerIndex.get(i);
            //先判断底牌
            if (i >= 51) {
    
    
                Rest.add(in);
            } else if(i % 3 == 0) {
    
    
                player01.add(in);
            } else if(i % 3 == 1) {
    
    
                player02.add(in);
            } else if(i % 3 == 2) {
    
    
                player03.add(in);
            }
        }

        /*
        4、排序
        使用Collections.sort(list)
        默认是升序排序
         */
        Collections.sort(player01);
        Collections.sort(player02);
        Collections.sort(player03);
        Collections.sort(Rest);

        //5、看牌,调用方法
        watchPoker("刘德华", poker, player01);
        watchPoker("周润发", poker, player02);
        watchPoker("周星驰", poker, player03);
        watchPoker("底牌", poker, Rest);
    }

    /*
        定义一个看牌的方法,提高代码的复用性
        参数:
            String name;玩家名称
            HashMap<Integer, String> poker:存储牌poker的集合
            ArrayList<Integer> list:存储玩家和底牌的list集合
        查表法:
            遍历玩家或者底牌集合,获取牌的索引
            使用牌的索引,去Map集合中,找到对应的牌
     */
    public static void watchPoker(String name,HashMap<Integer, String> poker, ArrayList<Integer> list) {
    
    
        //输出玩家的名称,不换行
        System.out.print(name + ":  ");
        //遍历玩家或者底牌集合,获取牌的索引
        for (Integer key : list) {
    
    
            //使用牌的索引,去Map集合找对应的牌
            String value = poker.get(key);
            System.out.print(value + " ");
        }
        System.out.println(); //打印完每个玩家的牌,换行
    }
}

猜你喜欢

转载自blog.csdn.net/Gaoju12138/article/details/109478842