-
Notifications
You must be signed in to change notification settings - Fork 1
/
Wechat-simulation.html
123 lines (95 loc) · 2.58 KB
/
Wechat-simulation.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title></title>
<link rel="stylesheet" href="">
</head>
<body>
<table id="panel">
</table>
<script>
var boss = {
money:1000,
offer:20, //每包放数额
offerNum:10, //每包10人分
}
var players = [];
var board = {}; //记录板
var sys = {
playerCount:1000, //玩的次数
currentCount:0, //当前次数
}
function initPlayer(){
for(var i=0; i<boss.offerNum; i++){
var player = {
name:'player'+(i+1),
money:1000,
}
players.push(player);
}
players[0].name = "GrounpMaster"; //群主 根据规则尾数两个相同群主免反包
}
//抢红包
function grap(remainCount,remainMoney){
var player = players[players.length - remainCount];
if(remainCount == 1){
counter(player,remainMoney);
remainCount--;
}else{
var min = .01;
var max = remainMoney/remainCount * 2 ;
var getMoney = Math.round((min+ Math.random()*(max-min))*100)/100;
counter(player,getMoney);
remainMoney -= player.getMoney;
remainCount--;
grap(remainCount,remainMoney);
//处理群主尾包情况
freeGrounpMaster();
}
function counter(player,_getMoney){
player.getMoney = _getMoney.toFixed(2);
player.tailNum = player.getMoney.substr(-1);
player.money += player.getMoney-0;
if(player.tailNum == boss.currentTailNum ){ //尾数跟发包者相同
boss.money += boss.offer;
boss.currentWin += boss.offer;
player.money -= boss.offer;
player.currentLost = boss.offer;
}
}
}
//判断条件免除群主的反包
function freeGrounpMaster(){
if(boss.currentWin >= 20 && players[0].currentLost == boss.offer){
players[0].currentLost = 0;
players[0].money += boss.offer;
boss.money -= boss.offer;
}
}
//开启模拟
function lunch(){
initPlayer();
for(var i=0; i<sys.playerCount; i++){
//boss 为红包贴上尾数
boss.currentTailNum = Math.floor(Math.random()*10);
//还原
players.forEach(function(p){p.currentLost = 0;})
boss.currentWin = 0;
boss.money -= boss.offer;
grap(boss.offerNum,boss.offer);
console.log(players)
sys.currentCount++;
}
console.log('boss',boss);
var count = 0;
for(i=0; i<players.length; i++){
count += players[i].money;
}
console.log(count+boss.money)
}
lunch();
</script>
</body>
</html>