用java写一个抽卡游戏!来体验下黑心老板的感觉!
今天呢我在b站逛的时候,看到了一个视频吸引了我的注意力(这里放出视频链接)
游戏氪金玩家必看!教你怎么躲避大数据算法杀熟,助你抽卡SSR、爆落神装、宝箱大赚
我就心血来潮,决定自己也写一个简易版的抽卡游戏,现在就给大家展示一下! ps:其实不算游戏qwq,就只能算是"集五福"的那种.
卡: 有六张卡
目标:集齐所有的卡!
变量: 1.name 2.总充值 3.现有余额 4.最后上线时间(这个在我懒得做他的分析了) 5.总在线时间 6.背包
这里用到sql数据库 注意:package中用"1"表示"0",因为在sql中011101会被记为11101当然也有其他解决问题的方法,这里就不细说了。
主程序: 1.SQL连接
public class Amain {
public static final String USER = "root";
public static final String PASS = "1234";
public static void main(String args[]) throws Exception{
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (Exception e) {
e.printStackTrace();
}
Connection conn;
var name = "1";
var money = 0;
var moneyin = 0;
var lastonline = "";
var totalonline = 0;
var pack = 0;
try {
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/user?useSSL=false", USER, PASS);
Statement state = conn.createStatement();
} catch (Exception e) {
System.err.println("发生错误");
e.printStackTrace();
}
}
程序要读取你的数据,所以增加
ResultSet rs = state.executeQuery(s);
while (rs.next()) {
String a1 = rs.getString(1);
if (a1.equals(name)) {
money = rs.getInt(2);
moneyin = rs.getInt(3);
Date lastonline1 = rs.getDate(4);
lastonline = "" + lastonline1;
totalonline = rs.getInt(5);
pack = rs.getInt(6);
}
}
System.out.println(name + "," + money + "," + moneyin + "," + lastonline + "," + totalonline + "," + pack);
String main1 = "" + pack;
String result[] = main1.split("");
System.out.println("+--------------+");
System.out.println("您的一号卷有" + (Integer.parseInt(result[0]) - 1) + "张.");
System.out.println("您的二号卷有" + (Integer.parseInt(result[1]) - 1) + "张.");
System.out.println("您的三号卷有" + (Integer.parseInt(result[2]) - 1) + "张.");
System.out.println("您的四号卷有" + (Integer.parseInt(result[3]) - 1) + "张.");
System.out.println("您的五号卷有" + (Integer.parseInt(result[4]) - 1) + "张.");
System.out.println("您的六号卷有" + (Integer.parseInt(result[5]) - 1) + "张.");
System.out.println("+--------------+");
之后呢,再加个循环判断输入语句
while(true) {
String moneyinup = "UPDATE `user`.`new` SET `moneyin` = '" + moneyin + "' WHERE `name` = '" + name + "'";
state.executeUpdate(moneyinup);
String moneyup = "UPDATE `user`.`new` SET `money` = '" + money + "' WHERE `name` = '" + name + "'";
state.executeUpdate(moneyup);
ResultSet rs1 = state.executeQuery(s);
while (rs1.next()) {
String a1 = rs1.getString(1);
if (a1.equals(name)) {
money = rs1.getInt(2);
moneyin = rs1.getInt(3);
Date lastonline1 = rs1.getDate(4);
lastonline = "" + lastonline1;
totalonline = rs1.getInt(5);
pack = rs1.getInt(6);
}
}
System.out.println("输入:");
Scanner scan = new Scanner(System.in);
if (scan.hasNext()) {
String a = scan.next();
}
}
ok,框架已经完成了!这时候我们就来解决卡池的问题吧!
这里建了A,B,C三个池子,与你所料,A池子的爆率最高出现重复的概率也越少。 A池:
class A {
private int pack;
public A(int pack) {
this.pack = pack;
}
public String run() {
var a1 = "" + this.pack;
String result[] = a1.split("");
int number0 = 0;
for (int x = 0; x <result.length; x ++) {
if(result[x].equals("1")) {
number0 ++;
}
}
if (number0 <= 2 && number0 > 0) {
Random rand = new Random();
int a2 = (rand.nextInt(100));
if (a2 > 5 && a2 < 9) {
int a3 = 0;
while(true) {
if(result[a3].equals("1")) {
var num = Integer.parseInt(a1);
num = (int) (num + Math.pow(10,(5 - a3)));
a1 = "" + num;
return "恭喜中奖! " + a1;
} else {
a3 ++;
}
}
} else if (a2 > 10 && a2 < 40) {
int a3 = (rand.nextInt(5));
var num = Integer.parseInt(a1);
num = (int) (num + Math.pow(10,(5 - a3)));
a1 = "" + num;
return "恭喜中奖! " + a1;
}
else {
return "您没有中奖! " + a1;
}
} else if (number0 <= 4 && number0 > 2) {
Random rand = new Random();
int a2 = (rand.nextInt(100));
if (a2 > 5 && a2 < 30) {
int a3 = 0;
while(true) {
if(result[a3].equals("1")) {
var num = Integer.parseInt(a1);
num = (int) (num + Math.pow(10,(5 - a3)));
a1 = "" + num;
return "恭喜中奖! " + a1;
} else {
a3 ++;
}
}
} else if (a2 > 30 && a2 < 60) {
int a3 = (rand.nextInt(5));
var num = Integer.parseInt(a1);
num = (int) (num + Math.pow(10,(5 - a3)));
a1 = "" + num;
return "恭喜中奖! " + a1;
}
else {
return "您没有中奖! " + a1;
}
} else if (number0 <= 6 && number0 > 4) {
Random rand = new Random();
int a2 = (rand.nextInt(100));
if (a2 > 5 && a2 < 70) {
int a3 = (rand.nextInt(5));
var num = Integer.parseInt(a1);
num = (int) (num + Math.pow(10,(5 - a3)));
a1 = "" + num;
return "恭喜中奖! " + a1;
} else {
return "您没有中奖! " + a1;
}
} else if (number0 == 0) {
Random rand = new Random();
int a2 = (rand.nextInt(100));
if (a2 > 5 && a2 < 50) {
int a3 = (rand.nextInt(4));
var num = Integer.parseInt(a1);
num = (int) (num + Math.pow(10,(5 - a3)));
a1 = "" + num;
return "恭喜中奖! " + a1;
} else {
return "您没有中奖! " + a1;
}
}
else {
return "您没有中奖! " + a1;
}
}
}
B和C池:
class B {
private int pack;
public B(int pack) {
this.pack = pack;
}
public String run() {
var a1 = "" + this.pack;
String result[] = a1.split("");
int number0 = 0;
for (int x = 0; x <result.length; x ++) {
if(result[x].equals("1")) {
number0 ++;
}
}
if (number0 <= 2 && number0 > 0) {
Random rand = new Random();
int a2 = (rand.nextInt(200));
if (a2 > 5 && a2 < 8) {
int a3 = 0;
while(true) {
if(result[a3].equals("1")) {
var num = Integer.parseInt(a1);
num = (int) (num + Math.pow(10,(5 - a3)));
a1 = "" + num;
return "恭喜中奖! " + a1;
} else {
a3 ++;
}
}
} else if (a2 > 10 && a2 < 30) {
int a3 = (rand.nextInt(5));
var num = Integer.parseInt(a1);
num = (int) (num + Math.pow(10,(5 - a3)));
a1 = "" + num;
return "恭喜中奖! " + a1;
}
else {
return "您没有中奖! " + a1;
}
} else if (number0 <= 4 && number0 > 2) {
Random rand = new Random();
int a2 = (rand.nextInt(100));
if (a2 > 5 && a2 < 25) {
int a3 = 0;
while(true) {
if(result[a3].equals("1")) {
var num = Integer.parseInt(a1);
num = (int) (num + Math.pow(10,(5 - a3)));
a1 = "" + num;
return "恭喜中奖! " + a1;
} else {
a3 ++;
}
}
} else if (a2 > 30 && a2 < 45) {
int a3 = (rand.nextInt(5));
var num = Integer.parseInt(a1);
num = (int) (num + Math.pow(10,(5 - a3)));
a1 = "" + num;
return "恭喜中奖! " + a1;
}
else {
return "您没有中奖! " + a1;
}
} else if (number0 <= 6 && number0 > 4) {
Random rand = new Random();
int a2 = (rand.nextInt(100));
if (a2 > 5 && a2 < 60) {
int a3 = (rand.nextInt(5));
var num = Integer.parseInt(a1);
num = (int) (num + Math.pow(10,(5 - a3)));
a1 = "" + num;
return "恭喜中奖! " + a1;
} else {
return "您没有中奖! " + a1;
}
} else if (number0 == 0) {
Random rand = new Random();
int a2 = (rand.nextInt(100));
if (a2 > 5 && a2 < 50) {
int a3 = (rand.nextInt(4));
var num = Integer.parseInt(a1);
num = (int) (num + Math.pow(10,(5 - a3)));
a1 = "" + num;
return "恭喜中奖! " + a1;
} else {
return "您没有中奖! " + a1;
}
}
else {
return "您没有中奖! " + a1;
}
}
}
class C {
private int pack;
public C(int pack) {
this.pack = pack;
}
public String run() {
var a1 = "" + this.pack;
String result[] = a1.split("");
int number0 = 0;
for (int x = 0; x <result.length; x ++) {
if(result[x].equals("1")) {
number0 ++;
}
}
if (number0 <= 2 && number0 > 0) {
Random rand = new Random();
int a2 = (rand.nextInt(300));
if (a2 > 5 && a2 < 7) {
int a3 = 0;
while(true) {
if(result[a3].equals("1")) {
var num = Integer.parseInt(a1);
num = (int) (num + Math.pow(10,(5 - a3)));
a1 = "" + num;
return "恭喜中奖! " + a1;
} else {
a3 ++;
}
}
} else if (a2 > 10 && a2 < 55) {
int a3 = (rand.nextInt(5));
var num = Integer.parseInt(a1);
num = (int) (num + Math.pow(10,(5 - a3)));
a1 = "" + num;
return "恭喜中奖! " + a1;
}
else {
return "您没有中奖! " + a1;
}
} else if (number0 <= 4 && number0 > 2) {
Random rand = new Random();
int a2 = (rand.nextInt(100));
if (a2 > 5 && a2 < 20) {
int a3 = 0;
while(true) {
if(result[a3].equals("1")) {
var num = Integer.parseInt(a1);
num = (int) (num + Math.pow(10,(5 - a3)));
a1 = "" + num;
return "恭喜中奖! " + a1;
} else {
a3 ++;
}
}
} else if (a2 > 30 && a2 < 40) {
int a3 = (rand.nextInt(5));
var num = Integer.parseInt(a1);
num = (int) (num + Math.pow(10,(5 - a3)));
a1 = "" + num;
return "恭喜中奖! " + a1;
}
else {
return "您没有中奖! " + a1;
}
} else if (number0 <= 6 && number0 > 4) {
Random rand = new Random();
int a2 = (rand.nextInt(100));
if (a2 > 5 && a2 < 50) {
int a3 = (rand.nextInt(5));
var num = Integer.parseInt(a1);
num = (int) (num + Math.pow(10,(5 - a3)));
a1 = "" + num;
return "恭喜中奖! " + a1;
} else {
return "您没有中奖! " + a1;
}
} else if (number0 == 0) {
Random rand = new Random();
int a2 = (rand.nextInt(100));
if (a2 > 5 && a2 < 50) {
int a3 = (rand.nextInt(4));
var num = Integer.parseInt(a1);
num = (int) (num + Math.pow(10,(5 - a3)));
a1 = "" + num;
return "恭喜中奖! " + a1;
} else {
return "您没有中奖! " + a1;
}
}
else {
return "您没有中奖! " + a1;
}
}
}
之后呢,我们再加个判断数据来安排池子的if程序(此处代码较多)
if (a.equals("抽奖")) {
if (moneyin >= 10) {
moneyin = moneyin - 10;
System.out.println("已扣除10¥!\n目前剩余:" + moneyin + "¥");
if(money == 0) {
if (totalonline > 10 && totalonline <= 30) {
A abc = new A(pack);
String str1 = abc.run();
String result1[] = str1.split(" ");
System.out.println(result1[0]);
String if0 = result1[0];
if (if0.equals("恭喜中奖!")) {
System.out.println("恭喜中奖!");
String sql = "UPDATE `user`.`new` SET `package` = '" + result1[1] + "' WHERE `name` = '" + name + "'";
state.executeUpdate(sql);
}
} else if (totalonline >= 0 && totalonline <= 10) {
B abc = new B(pack);
String str1 = abc.run();
String result1[] = str1.split(" ");
System.out.println(result1[0]);
String if0 = result1[0];
if (if0.equals("恭喜中奖!")) {
System.out.println("恭喜中奖!");
String sql = "UPDATE `user`.`new` SET `package` = '" + result1[1] + "' WHERE `name` = '" + name + "'";
state.executeUpdate(sql);
}
} else {
C abc = new C(pack);
String str1 = abc.run();
String result1[] = str1.split(" ");
System.out.println(result1[0]);
String if0 = result1[0];
if (if0.equals("恭喜中奖!")) {
System.out.println("恭喜中奖!");
String sql = "UPDATE `user`.`new` SET `package` = '" + result1[1] + "' WHERE `name` = '" + name + "'";
state.executeUpdate(sql);
}
}
} else if (money < 50 && money > 0) {
if (moneyin == 0) {
B abc = new B(pack);
String str1 = abc.run();
String result1[] = str1.split(" ");
System.out.println(result1[0]);
String if0 = result1[0];
if (if0.equals("恭喜中奖!")) {
System.out.println("恭喜中奖!");
String sql = "UPDATE `user`.`new` SET `package` = '" + result1[1] + "' WHERE `name` = '" + name + "'";
state.executeUpdate(sql);
}
} else if (moneyin > 0 && moneyin < 50) {
C abc = new C(pack);
String str1 = abc.run();
String result1[] = str1.split(" ");
System.out.println(result1[0]);
String if0 = result1[0];
if (if0.equals("恭喜中奖!")) {
System.out.println("恭喜中奖!");
String sql = "UPDATE `user`.`new` SET `package` = '" + result1[1] + "' WHERE `name` = '" + name + "'";
state.executeUpdate(sql);
}
}
} else if (money < 200 && money >= 50) {
if (moneyin == 0) {
B abc = new B(pack);
String str1 = abc.run();
String result1[] = str1.split(" ");
System.out.println(result1[0]);
String if0 = result1[0];
if (if0.equals("恭喜中奖!")) {
System.out.println("恭喜中奖!");
String sql = "UPDATE `user`.`new` SET `package` = '" + result1[1] + "' WHERE `name` = '" + name + "'";
state.executeUpdate(sql);
}
} else if (moneyin > 0 && moneyin <= 50) {
C abc = new C(pack);
String str1 = abc.run();
String result1[] = str1.split(" ");
System.out.println(result1[0]);
String if0 = result1[0];
if (if0.equals("恭喜中奖!")) {
System.out.println("恭喜中奖!");
String sql = "UPDATE `user`.`new` SET `package` = '" + result1[1] + "' WHERE `name` = '" + name + "'";
state.executeUpdate(sql);
}
} else if (moneyin > 50 && moneyin <= 70) {
A abc = new A(pack);
String str1 = abc.run();
String result1[] = str1.split(" ");
System.out.println(result1[0]);
String if0 = result1[0];
if (if0.equals("恭喜中奖!")) {
System.out.println("恭喜中奖!");
String sql = "UPDATE `user`.`new` SET `package` = '" + result1[1] + "' WHERE `name` = '" + name + "'";
state.executeUpdate(sql);
}
} else if (moneyin > 70 && moneyin <= 200) {
C abc = new C(pack);
String str1 = abc.run();
String result1[] = str1.split(" ");
System.out.println(result1[0]);
String if0 = result1[0];
if (if0.equals("恭喜中奖!")) {
System.out.println("恭喜中奖!");
String sql = "UPDATE `user`.`new` SET `package` = '" + result1[1] + "' WHERE `name` = '" + name + "'";
state.executeUpdate(sql);
}
} else {
B abc = new B(pack);
String str1 = abc.run();
String result1[] = str1.split(" ");
System.out.println(result1[0]);
String if0 = result1[0];
if (if0.equals("恭喜中奖!")) {
System.out.println("恭喜中奖!");
String sql = "UPDATE `user`.`new` SET `package` = '" + result1[1] + "' WHERE `name` = '" + name + "'";
state.executeUpdate(sql);
}
}
} else if (money < 500 && money >= 200) {
if (moneyin > 50 && moneyin <= 70) {
A abc = new A(pack);
String str1 = abc.run();
String result1[] = str1.split(" ");
System.out.println(result1[0]);
String if0 = result1[0];
if (if0.equals("恭喜中奖!")) {
System.out.println("恭喜中奖!");
String sql = "UPDATE `user`.`new` SET `package` = '" + result1[1] + "' WHERE `name` = '" + name + "'";
state.executeUpdate(sql);
}
} else if (moneyin > 300 && moneyin <= 400) {
C abc = new C(pack);
String str1 = abc.run();
String result1[] = str1.split(" ");
System.out.println(result1[0]);
String if0 = result1[0];
if (if0.equals("恭喜中奖!")) {
System.out.println("恭喜中奖!");
String sql = "UPDATE `user`.`new` SET `package` = '" + result1[1] + "' WHERE `name` = '" + name + "'";
state.executeUpdate(sql);
}
} else {
B abc = new B(pack);
String str1 = abc.run();
String result1[] = str1.split(" ");
System.out.println(result1[0]);
String if0 = result1[0];
if (if0.equals("恭喜中奖!")) {
System.out.println("恭喜中奖!");
String sql = "UPDATE `user`.`new` SET `package` = '" + result1[1] + "' WHERE `name` = '" + name + "'";
state.executeUpdate(sql);
}
}
} else if (money > 500) {
if (moneyin > 500 && moneyin <= 700) {
A abc = new A(pack);
String str1 = abc.run();
String result1[] = str1.split(" ");
System.out.println(result1[0]);
String if0 = result1[0];
if (if0.equals("恭喜中奖!")) {
System.out.println("恭喜中奖!");
String sql = "UPDATE `user`.`new` SET `package` = '" + result1[1] + "' WHERE `name` = '" + name + "'";
state.executeUpdate(sql);
}
} else if (moneyin > 900 && moneyin <= 2000) {
C abc = new C(pack);
String str1 = abc.run();
String result1[] = str1.split(" ");
System.out.println(result1[0]);
String if0 = result1[0];
if (if0.equals("恭喜中奖!")) {
System.out.println("恭喜中奖!");
String sql = "UPDATE `user`.`new` SET `package` = '" + result1[1] + "' WHERE `name` = '" + name + "'";
state.executeUpdate(sql);
}
} else {
B abc = new B(pack);
String str1 = abc.run();
String result1[] = str1.split(" ");
System.out.println(result1[0]);
String if0 = result1[0];
if (if0.equals("恭喜中奖!")) {
System.out.println("恭喜中奖!");
String sql = "UPDATE `user`.`new` SET `package` = '" + result1[1] + "' WHERE `name` = '" + name + "'";
state.executeUpdate(sql);
}
}
}
} else {
System.out.println("您的余额不足!");
}
}
ok了,主要功能已经完成了!再加些辅助功能就完美了!
else if (a.equals("查询")) {
String pack2 = "" + pack;
String result2[] = pack2.split("");
System.out.println("+--------------+");
System.out.println("您的一号卷有" + (Integer.parseInt(result2[0]) - 1) + "张.");
System.out.println("您的二号卷有" + (Integer.parseInt(result2[1]) - 1) + "张.");
System.out.println("您的三号卷有" + (Integer.parseInt(result2[2]) - 1) + "张.");
System.out.println("您的四号卷有" + (Integer.parseInt(result2[3]) - 1) + "张.");
System.out.println("您的五号卷有" + (Integer.parseInt(result2[4]) - 1) + "张.");
System.out.println("您的六号卷有" + (Integer.parseInt(result2[5]) - 1) + "张.");
System.out.println("+--------------+");
System.out.println("您的余额:" + moneyin);
} else if (a.equals("退出")) {
System.out.println("正在为您退出中...");
conn.close();
rs.close();
state.close();
System.exit(1);
} else if (a.equals("数据清空")) {
String sql = "UPDATE `user`.`new` SET `package` = '111111' WHERE `name` = '" + name + "'";
state.executeUpdate(sql);
System.out.println("完成!");
} else if (a.contains("充值")) {
String result3[] = a.split("_");
System.out.println(result3[1]);
int b1 = moneyin;
int num = Integer.parseInt(result3[1]);
moneyin = moneyin + num;
money = money + num;
System.out.println("充值完成!");
System.out.println("您之前的余额:" + b1 + "¥");
System.out.println("您现在所拥有的财产:" + moneyin + "¥");
System.out.println("您充值:" + a + "¥!");
}
else {
System.out.println("未知指令!");
}
最后呢再放上一直我自己抽的图: 你们的运气都怎么样呢qwq,快来试试吧! 下载地址点我:良 心 抽 奖
|