JavaScript实现双色球

双色球小游戏规则;

红球一共6组,每组从1-33中抽取一个,六个互相不重复。然后蓝球是从1-16中抽取一个数字,这整个组成的双色球

HTML代码部分如下:

​
<div class="container">
     <div class="option">
          <button type="button" id="btnClick">开始</button>
     </div>
     <div class="content">
          <div class="ball red">8</div>
          <div class="ball red">8</div>
          <div class="ball red">8</div>
          <div class="ball red">8</div>
          <div class="ball red">8</div>
          <div class="ball red">8</div>
          <div class="ball blue">8</div>
      </div>
</div>

​

CSS样式部分代码:

* {
	margin: 0;
	padding: 0;
}
.container {
	width: 700px;
	margin: 0 auto;

}
div.content {
	display: flex;
}
.ball {
	width: 90px;
	margin: 5px;
	height: 90px;
	border-radius: 50%;
	font-size: 36px;
	color: white;
	text-align: center;
	line-height: 90px;
}
.red {
	background-color: red;
}
.blue {
	background-color: blue;
}
.option {
	margin: 20px 0;
	text-align: center;
}
.option button {
	width: 120px;
	height: 40px;
}

JavaScript部分代码:

window.onload = function () {
    let btnClick = document.querySelector('#btnClick');
	let redBalls = document.querySelectorAll('.red');
	let blueBall = document.querySelector('.blue');
	let flag = 0;
	btnClick.onclick = function () {
		if (flag == 0) {
			this.innerText = '停止'
			flag = 1;
			n = setInterval(() => {
				let reds = getRedNumber();
				reds.forEach((value, index) => {
					redBalls[index].innerText = value;
				})
				blueBall.innerText = 1 + parseInt(Math.random() * 16);
			}, 50);
		} else {
			this.innerText = '开始';
			flag = 0;
			clearInterval(n);
		}
	}
}


function getRedNumber() {
	let reds = [];
	reds[0] = 1 + parseInt(Math.random() * 33);
	//用来生成剩余的五个数字
	 while(reds.length<6){
		let tNum = 1 + parseInt(Math.random() * 33);
		//用来判断是否有重复的数字
		let flag = false;
		for (let j = 0; j < reds.length; j++) {
			if (tNum == reds[j]) {
				flag = true;
				break;
			}
		}
		if (!flag) {
			reds.push(tNum);
		}
	}
	reds.sort((a, b) => {
		return a - b;
	})
	return reds;
}

实现效果:

没有实现兑奖部分,需要可以自行实现。

兑奖规则:

一等奖(6+1)中奖概率为:红球33选6乘以蓝球16选1=1/17721088=0.0000056%;

二等奖(6+0)中奖概率为:红球33选6乘以蓝球16选0=15/17721088=0.0000846%;

三等奖(5+1)中奖概率为:红球33选5乘以蓝球16选1=162/17721088=0.000914%;

四等奖(5+0、4+1)中奖概率为:红球33选5乘以蓝球16选0=7695/17721088=0.0434%;

五等奖(4+0、3+1)中奖概率为:红球33选4乘以蓝球16选0=137475/17721088=0.7758%;

六等奖(2+1、1+1、0+1)中奖概率为:红球33选2乘以蓝球16选1=1043640/17721088=5.889%;

以上代码仅供学习参考,如有问题,请自行改正罒ω罒

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值