随机抽卡片

package com.example.demo.util;

import com.example.demo.model.Card;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

/*
随机抽卡片
 */
public class CardRandom {
    final static String[] cardNames = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};

    //正常数组
    static List<Card> cardList = new ArrayList<>();

    //垃圾箱
    static List<Card> rubbishList = new ArrayList<>();

    static int length = cardNames.length;

    //重置
    final static String RESET = "reset";

    //获取
    final static String GET = "get";

    //结束
    final static String STOP = "stop";

    //垃圾箱
    final static String RUBBISH = "rubbish";

    /*
    初始化
     */
    static void init() {
        for (int i = 0; i < length; i++) {
            cardList.add(new Card(i, cardNames[i]));
        }
    }

    /*
    重置
     */
    static void resetList() {
        cardList.clear();
        length = cardNames.length;
        for (int i = 0; i < length; i++) {
            cardList.add(new Card(i, cardNames[i]));
        }
    }

    /*
    随机获取卡片
     */
    public static int getCard() {
        System.out.println("现在列表长度是:" + length);
        Random random = new Random();
        return random.nextInt(length);
    }

    /*
    获取卡片后将被移除
     */
    public static void removeCard(int index) {
        //放入垃圾箱
        rubbishList.add(cardList.get(index));
        cardList.remove(index);
        length = cardList.size();
        System.out.println("移除后列表长度是:" + length);
    }

    public static void main(String[] args) {
        init();
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入指令==>{GET:获取 ,RESET:重置 ,STOP:结束}");
        while (true) {
            if (scanner.hasNext()) {
                String value = scanner.next();
                System.out.println("输入指令为:" + value);
                if (length > 0) {
                    if (GET.equalsIgnoreCase(value)) {
                        int index = getCard();
                        System.out.println(cardList.get(index));
                        removeCard(index);
                        if (length == 0) {
                            System.out.println("卡牌抽取完毕,即将重置");
                            resetList();
                        }
                    }
                    if (RESET.equalsIgnoreCase(value)) {
                        resetList();
                        System.out.println("手动初始化卡组");
                    }
                    if (RUBBISH.equalsIgnoreCase(value)) {
                        System.out.println("已抽取卡片:" + rubbishList);
                    }
                    if (STOP.equalsIgnoreCase(value)) {
                        break;
                    }
                }
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值