记录非洲生活学习
卡牌数据获取(用了jxl)
public void setBlueCard() {
//创建workbook
Workbook workbook= null;
try {
workbook = Workbook.getWorkbook(new File("G:/JavaStudy2/cardData/blueCard.xls"));
} catch (IOException e) {
e.printStackTrace();
} catch (BiffException e) {
e.printStackTrace();
}
//获取第一个工作表sheet
Sheet sheet=workbook.getSheet(0);
//获取数据
for (int i=0;i<sheet.getRows();i++){
for (int j=0;j<sheet.getColumns();j++){
Cell cell=sheet.getCell(j,i);
blueCard.add(cell.getContents()+"");
}
}
workbook.close();
}
public List<String> getBlueCard() {
return blueCard;
}
保底设置和抽卡结果判断(太粗糙了
public class Raffle {
private int count;
private int countPurple;
private int countGold;
private double purplePercent;
private double goldPercent;
Raffle(){
count = 1;
countGold = 1;
countPurple = 1;
purplePercent = 0.1;
goldPercent = 0.06;
}
private double getRandom(){
return Math.random();
}
//抽卡结果判断
private String getAnswer(){
double chance = getRandom();
boolean isGold = getGold(chance);
boolean isPurple = getPurple(chance);
if(isGold){
countGold = 1;
goldPercent = 0.06;
count++;
countPurple = countPurple < 10?(countPurple+1):10;
return "gold";
}else if(isPurple){
countGold++;
count++;
countPurple = 1;
return "purple";
}else{
countPurple++;
countGold++;
count++;
return "blue";
}
}
private boolean getGold(double chance){
//保底机制
if(countGold >= 76) goldPercent += 0.08;
return chance <= goldPercent;
}
private boolean getPurple(double chance){
//保底机制
if(countPurple == 10)return true;
return chance <= purplePercent;
}
抽卡流程
private List<String> blue;
private List<String> purple;
private List<String> gold;
private int blueLength;
private int purpleLength;
private int goldLength;
PickCard(){
blue = new Card().getBlueCard();
blueLength = blue.size();
purple = new Card().getPurpleCard();
purpleLength = purple.size();
gold = new Card().getGoldCard();
goldLength = gold.size();
}
private double getRandom(){
return Math.random();
}
public String getCard(String ans){
if(ans.equals("gold")){
int a = (int)(getRandom()*goldLength);
return gold.get(a);
}else if(ans.equals("purple")){
int a = (int)(getRandom()*purpleLength);
return purple.get(a);
}else{
int a = (int)(getRandom()*blueLength);
return blue.get(a);
}
}
最后调用
public class TEST {
Raffle raffle = new Raffle();
PickCard pick = new PickCard();
private void tenTimesPick(){
int count = 10;
while (count-- > 0){
String color = raffle.pickCard();
System.out.println("[" + color + "]" + pick.getCard(color));
}
}
private void singlePick(){
String color = raffle.pickCard();
System.out.println("[" + color + "]" + pick.getCard(color));
}
}
效果(咋还是非酋艹
加油优化