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;
}
}
}
}
}
}