Java手动实现一个双色球游戏,每天自己买个彩票玩

前言

之前购买彩票时很少中奖,实际上彩票的中奖概率非常低。在多次尝试后,只中过一次。考虑到中奖机会有限,我决定尝试实现一个彩票自动购买程序,类似于机选功能。这样,我可以更加灵活地进行尝试,而不受实际彩票中奖概率的限制。

代码实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package demo.game;

import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSON;

import java.util.*;

/**
* @author jisl on 2023/7/31 15:52
* 双色球彩票
*/

public class DoubleColorBallLottery {

// 彩票期数
private int lotteryNumber = 0;

// 开奖号码
private Set<Integer> winRedNum = new HashSet<>();
private int winBlueNum;

// 用户购买的彩票
private final List<List<Integer>> userLottery = new ArrayList<>();

// 用户中奖的彩票
private final Map<Integer, List<String>> userPrizeLottery = new HashMap<>();

public int[] lotteryAmount = new int[]{0, 5000000, 1250000, 3000, 200, 10, 5};

private static final int MAX_PRIZE_LEVEL = 6;

Random random = new Random();

/**
* 彩票开奖
*
* @author jisl on 2023/7/31 16:06
**/
public void lotteryDrawing() {
winRedNum = buildRedNum();
winBlueNum = buildBlueNum();
System.out.printf("[第%s期双色球]开奖结果:红球:%s,蓝球:%s%n", lotteryNumber, JSON.toJSONString(winRedNum), winBlueNum);
}

private int buildBlueNum() {
return random.nextInt(16) + 1;
}

private HashSet<Integer> buildRedNum() {
final HashSet<Integer> set = new HashSet<>();
while (set.size() < 6) {
final int randomNum = random.nextInt(33) + 1;
set.add(randomNum);
}
return set;
}

/**
* 买彩票
*
* @author jisl on 2023/7/31 16:07
**/
public void buy(int count) {
lotteryNumber = RandomUtil.randomInt(999);
for (int i = 0; i < count; i++) {
final ArrayList<Integer> lottery = new ArrayList<>(buildRedNum());
lottery.add(buildBlueNum());
userLottery.add(lottery);
}
}

public void redemption() {
// 一到六等奖
for (int i = 1; i <= MAX_PRIZE_LEVEL; i++) {
userPrizeLottery.put(i, new ArrayList<>());
}
for (List<Integer> lottery : userLottery) {
int readBallMatches = countMatchingNumbers(lottery);
boolean blueBallMatch = lottery.get(6) == winBlueNum;
int prizeItem = calculatePrize(readBallMatches, blueBallMatch);
if (prizeItem != 0) {
// 中奖了
final List<String> numbers = userPrizeLottery.get(prizeItem);
numbers.add(JSON.toJSONString(lottery));
}
}
System.out.println(String.format("[第%s期双色球]中奖结果:", lotteryNumber));
int userPrizeCount = 0;
int userPrizeAmount = 0;
for (int i = 1; i <= MAX_PRIZE_LEVEL; i++) {
final int userPrizeItemCount = userPrizeLottery.get(i).size();
userPrizeCount += userPrizeItemCount;
userPrizeAmount += lotteryAmount[i] * userPrizeItemCount;
if (i <= 3) {
System.out.println(String.format("%s等奖中奖个数:%s,中奖号码:%s", i, userPrizeItemCount, userPrizeLottery.get(i)));
} else {
System.out.println(String.format("%s等奖中奖个数:%s,中奖号码:-", i, userPrizeItemCount));
}
}
// 共计中奖率:6.71%
System.out.println(String.format("一共买了[%s张]彩票,中奖个数=%s,中奖概率=%s;购买彩票费用=%s,奖金=%s,盈利=%s"
, userLottery.size(), userPrizeCount, 1.0 * userPrizeCount / userLottery.size(), userLottery.size() * 2, userPrizeAmount, userPrizeAmount - userLottery.size() * 2));
}


private int calculatePrize(int readBallMatches, boolean blueBallMatch) {
if (readBallMatches == 6 && blueBallMatch) {
return 1;
} else if (readBallMatches == 6) {
return 2;
} else if (readBallMatches == 5 && blueBallMatch) {
return 3;
} else if (readBallMatches == 5 || (readBallMatches == 4 && blueBallMatch)) {
return 4;
} else if (readBallMatches == 4 || (readBallMatches == 3 && blueBallMatch)) {
return 5;
} else if (blueBallMatch) {
return 6;
}
return 0;
}

private int countMatchingNumbers(List<Integer> lottery) {
int count = 0;
for (int i = 0; i < 6; i++) {
if (winRedNum.contains(lottery.get(i))) {
count += 1;
}
}
return count;
}

public void game(int n) {
buy(n);
lotteryDrawing();
redemption();
}

public static void main(String[] args) {
final DoubleColorBallLottery lottery = new DoubleColorBallLottery();
lottery.game(100000);
}


}

阅读更多

统一支付接口,无限扩展不同支付服务工具包实现。【开箱即用】(可用于生产环境中)

前言

在实际生产环境中,我们经常面临多个支付厂商的选择和变更,这可能导致支付代码的不断修改和维护。为了更好地应对产品需求的变化,我们采用了策略模式,通过统一的支付接口将各个支付服务进行实现。这样一来,支付厂商的变化对业务代码没有任何影响,使得我们能够更灵活地应对支付服务的变化和新增,而无需频繁修改核心业务逻辑。这种设计不仅提高了代码的可维护性,还为未来的扩展和变更提供了便利。

阅读更多

【版本号比较工具-(算法版)】

1.版本号工具

之前在网上看到版本号工具,觉得还行,就一直用这个。代码如下,写的也挺简洁的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public static int compareAppVersion(String version1, String version2) {
if (version1 == null || version2 == null) {
throw new RuntimeException("版本号不能为空");
}
// 注意此处为正则匹配,不能用.
String[] versionArray1 = version1.split("\\.");
String[] versionArray2 = version2.split("\\.");
int idx = 0;
// 取数组最小长度值
int minLength = Math.min(versionArray1.length, versionArray2.length);
int diff = 0;
// 先比较长度,再比较字符
while (idx < minLength
&& (diff = versionArray1[idx].length() - versionArray2[idx].length()) == 0
&& (diff = versionArray1[idx].compareTo(versionArray2[idx])) == 0) {
++idx;
}
// 如果已经分出大小,则直接返回,如果未分出大小,则再比较位数,有子版本的为大
diff = (diff != 0) ? diff : versionArray1.length - versionArray2.length;
return diff;
}
阅读更多